Classes/Providers/DockerParcelProvider.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class DockerParcelProvider : ParcelProvider
{
    DockerParcelProvider() : base('Docker', $false, 'docker') {}

    [bool] TestProviderInstalled([hashtable]$_context)
    {
        $cmd = Get-Command -Name 'docker' -ErrorAction Ignore
        return ($null -ne $cmd)
    }

    #TODO: come back here when we can have "GetProviderInstallPackages"
    # which can return a number of packages to install.
    # this will allow us to install more complicated packages, and see what is being installed.
    # also can add a flag on providers of "install: false", which will disable self-install/check

    [string] GetPackageInstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "docker pull $($_package.Name):$($this.GetVersionArgument($_package))"
    }

    [string] GetPackageUninstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "docker rmi --force $($_package.Name)"
    }

    [bool] TestPackageInstalled([ParcelPackage]$_package)
    {
        # always pull down the latest
        if ($_package.IsLatest) {
            return $false
        }

        # get current images
        return ($null -ne $this.FindDockerImages($_package))
    }

    [bool] TestPackageUninstalled([ParcelPackage]$_package)
    {
        return ($null -eq $this.FindDockerImages($_package))
    }

    [string] GetPackageLatestVersion([ParcelPackage]$_package)
    {
        return 'latest'
    }

    [string] GetVersionArgument([ParcelPackage]$_package)
    {
        if ($_package.IsLatest) {
            return 'latest'
        }

        return $_package.Version
    }

    [object] FindDockerImages([ParcelPackage]$_package)
    {
        $_images = Invoke-Expression -Command "docker images --format '{{json .}}'" -ErrorAction Stop
        return ($_images | ConvertFrom-Json) | Where-Object { ($_.Repository -ieq $_package.Name) -and ($_.Tag -ieq $_package.Version) }
    }
}