Modules/StackDeploy.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

Import-Module (Join-Path (Split-Path $PSScriptRoot -Parent) "Helpers\GeneralHelper.psm1") -Force

function Invoke-StackDeploy
{
    <#
    .SYNOPSIS
        docker stack deploy
    .DESCRIPTION
        Executes docker stack deploy, remotely or locally
    .PARAMETER Node
        Node on which to run, if running remotely (must be an eligible swarm manager)
    .PARAMETER StackName
        Name to give the deployed stack
    .PARAMETER Credential
        PSCredential to used when running remotely
    .PARAMETER StackDefinition
        Definition to deploy if deploying a StackDefinition object
    .PARAMETER DefinitionPath
        Path to definition if deploying a stackDefinition file
    .PARAMETER Local
        Set if running locally
    #>


    [CmdletBinding(DefaultParameterSetName='Local')]
    Param(

        [Parameter(
            Mandatory=$True,
            Position=0,
            ParameterSetName='Remote'
        )]
        [ValidateScript({ Test-Connection $_ })]
        [String]$Node,

        [Parameter(
            Mandatory=$True,
            Position=1
        )]
        [Parameter(ParameterSetName='Remote')]
        [Parameter(ParameterSetName='Local')]
        [String]$StackName,

        [Parameter(
            Mandatory=$True,
            Position=2,
            ParameterSetName='Remote'
        )]
        [PSCredential]$Credential,

        [Parameter(
            Mandatory=$False,
            Position=3,
            ValueFromPipeline=$True
        )]
        [Parameter(ParameterSetName='Remote')]
        [Parameter(ParameterSetName='Local')]
        [ValidateScript({Test-ObjectValidYaml $_})]
        [Alias('sd', 'definition', 'def')]
        [PsCustomObject]$StackDefinition,

        [Parameter(
            Mandatory=$False,
            Position=4
        )]
        [Parameter(ParameterSetName='Remote')]
        [Parameter(ParameterSetName='Local')]
        [ValidateScript({ Test-Path $_ })]
        [ValidateScript({ try { ConvertFrom-YamlFile $_; return $True } catch { return $False } })]
        [Alias('dp', 'defpath', 'dpath', 'path')]
        [string]$DefinitionPath,

        [Parameter(
            Mandatory=$False,
            Position=5
        )]
        [Parameter(ParameterSetName='Local')]
        [switch]$Local
    )

    process {

        if ($Local)
        {
            $Session = $null
        }
        else
        {
            try 
            {
                $Session = New-PSSession -ComputerName $Node -Credential $cred
            }
            catch 
            {
                Throw "Session could not be created on Node $Node with the provided credentials."    
            }
        }

        if (![String]::IsNullOrEmpty($DefinitionPath)) {
            $Content = Get-Content $DefinitionPath -Raw
        }
        elseif ($Null -ne $StackDefinition) {
            $Content = ConvertTo-Yaml $StackDefinition
        }
        else {
            Throw "Stack definition not found."
        }

        $block = {

            $Content   = $args[0]
            $StackName = $args[1]

            $Guid = New-Guid
            $Null = New-Item "$Guid.yml" -Itemtype "File"
            $Content | Set-Content "./$Guid.yml" -Force
            $DefinitionPath = "$Guid.yml"

            if (docker stack ls | Select-String $StackName) {
                $Null = docker stack rm $StackName
            }

            #does nothing if already part of swarm, this is fine #TODO
            $Null = docker swarm init

            Invoke-Expression -Command "docker stack deploy --compose-file $DefinitionPath $StackName"
            Remove-Item $DefinitionPath -Force
        }

        if ($Session -eq $null)
        {
            Invoke-Command $block -ArgumentList $Content, $StackName
        }
        else 
        {
            Invoke-Command $block -Session $Session -ArgumentList $Content, $StackName
        }
    }
}