Modules/StackDeploy.psm1


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
        }
    }
}