DSCClassResources/DockerService/DockerService.psm1

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCDscExamplesPresent', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCDscTestsPresent', '')]
param ()

enum Ensure
{
    Present
    Absent
}

[DscResource()]
class DockerService
{
    [DscProperty(Mandatory)]
    [Ensure] $Ensure

    [DscProperty(Key)]
    [String] $Path

    [DscProperty(NotConfigurable)]
    [bool] $ServiceInstalled

    [DockerService] Get ()
    {
        $DockerDPath = $this.ResolveDockerDPath($this.Path)
        $DockerService = Get-Service Docker -ErrorAction SilentlyContinue
        $_ServiceInstalled = if ($null -ne $DockerService)
        {
            $true
        }
        else
        {
            $false
        }
        $ReturnObj = [DockerService]::new()
        $ReturnObj.Ensure = $this.Ensure
        $ReturnObj.Path = $DockerDPath
        $ReturnObj.ServiceInstalled = $_ServiceInstalled
        return $ReturnObj
    }

    [bool] Test ()
    {
        $Service = Get-Service -Name Docker -ErrorAction SilentlyContinue
        if ($this.Ensure -eq [Ensure]::Present)
        {
            if ($null -ne $Service)
            {
                return $true
            }
            else
            {
                return $false 
            }
        }
        else
        {
            if ($null -eq $Service)
            {
                return $true
            }
            else
            {
                return $false
            }
        }
    }

    [void] Set ()
    {
        $DockerDPath = $this.ResolveDockerDPath($this.Path)
        if ($DockerDPath -eq [String]::Empty)
        {
            throw 'Dockerd.exe was not found at path.'
        }
        
        if (($this.Ensure -eq [Ensure]::Absent) -and (Get-Service -Name docker -ErrorAction SilentlyContinue))
        {
            Write-Verbose -Message "Docker Service is running. Stopping now."
            Stop-Service -Name docker
        }
        $this.DockerDReg($DockerDPath,$this.Ensure)
    }

    [String] ResolveDockerDPath ([String] $Path)
    {
        $DockerDPath = [String]::Empty
        if ((Split-Path -Path $Path -Leaf) -eq 'dockerd.exe')
        {
            $DockerDPath = $Path
        }
        else
        {
            $DockerDPath = Join-Path -Path $Path -ChildPath 'dockerd.exe'
        }

        if (-not (Test-Path -Path $DockerDPath))
        {
            throw 'Dockerd.exe was not found'
        }
        return $DockerDPath
    }
    [void] DockerDReg ([String] $Path, [Ensure] $Ensure)
    {
        if ($Ensure -eq [Ensure]::Present)
        {
            Write-Verbose -Message 'Registering Docker Service'
            & $Path --register-service
        }
        else
        {
            Write-Verbose -Message 'Removing Docker Service'
            & $Path --unregister-service
        }
    }
}