Drude.psm1

<# Copyright (C) Alex Danilenko. All rights reserved. #>

requires -version 3.0

$drudeMessages = @{
    ComposeFileNotFound = "Sorry, but docker-compose.yml file is not found in current directory."
    ComposeFileFound = "docker-compose.yml file found in current directory."
}

Function Check-ForDockerComposeFile(){
    $file_found = Test-Path ".\docker-compose.yml";
    
    if($file_found -eq $false){
        Write-Host -ForegroundColor Red -Object $drudeMessages.ComposeFileNotFound
    }

    return $file_found
}

# ================================================================================================== #

<#
.Synopsis
   Start container from docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   Executes "docker-compose -d"
.EXAMPLE
   Start-Drude
.EXAMPLE
   dsh-up
#>

Function Start-Drude(){
    [cmdletbinding()]
    [Alias("dsh-up")]
    param (
      [Parameter(Position=0)][string]$cliContainer = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Green -Object "Starting all containers..."

        docker-compose up -d

        Write-Host -ForegroundColor Green -Object "Reseting permissions on /var/www in $cliContainer container..."
        docker exec -u root $(docker-compose ps -q $cliContainer) bash -c "chown -R docker:users /var/www"
    }
}

# ================================================================================================== #

<#
.Synopsis
   Stops containers described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   Executes "docker-compose stop"
.EXAMPLE
   Stop-Drude
#>

Function Stop-Drude(){
    [cmdletbinding()]
    [Alias("dsh-down","dsh-stop")]
    param ()

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Green -Object "Stopping all containers..."
        docker-compose stop
    }
}

# ================================================================================================== #

<#
.Synopsis
   Restarts containers described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   Executes "docker-compose stop; docker-compose up -d"
.EXAMPLE
   Restart-Drude
#>

Function Restart-Drude(){
    [cmdletbinding()]
    [Alias("dsh-restart")]
    param ()

    Stop-Drude
    Start-Drude
}

<#
.Synopsis
   Prints status of containers described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   Executes "docker-compose ps"
.EXAMPLE
   Get-DrudeStatus
.EXAMPLE
   Get-DrudeStatus cli
#>

Function Get-DrudeStatus(){
    [cmdletbinding()]
    [Alias("dsh-status", "dsh-ps")]
    param (
        [Parameter(Position=0)][string]$container = ""
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker-compose ps $container
    }
}

<#
.Synopsis
   Initiates interactive bash shell session with cli container described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
.EXAMPLE
   Invoke-DrudeBash
.EXAMPLE
   dsh-bash
.EXAMPLE
   Invoke-DrudeBash web
.EXAMPLE
   dsh-bash web
#>

Function Invoke-DrudeBash(){
    [cmdletbinding()]
    [Alias("dsh-bash")]
    param (
        [Parameter(Position=0)][string]$container = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker exec -it $(docker-compose ps -q $container) bash
    }
}

# ================================================================================================== #

<#
.Synopsis
   Executes command in needed container's interactive bash shell.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
.EXAMPLE
   Invoke-DrudeBashCommand "cat /etc/hosts"
.EXAMPLE
   dsh-exec "cat /etc/hosts"
.EXAMPLE
   Invoke-DrudeBashCommand "cat /etc/hosts" cli
.EXAMPLE
   dsh-exec "cat /etc/hosts" cli
#>

Function Invoke-DrudeBashCommand(){
    [cmdletbinding()]
    [Alias("dsh-exec")]
    param (
        [Parameter(Position=0,Mandatory=$true)][string]$command = "ls -la",
        [Parameter(Position=1)][string]$container = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker exec -it $(docker-compose ps -q $container) bash -c "$command"
    }
}

<#
.Synopsis
   Executes drush command for needed site in needed docroot folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
.EXAMPLE
   Invoke-DrudeDrushCommand "cc all" default
.EXAMPLE
   dsh-drush "cc all" default
#>

Function Invoke-DrudeDrushCommand(){
    [cmdletbinding()]
    [Alias("dsh-drush")]
    param (
          [Parameter(Position=0)][string]$command = "status",
          [Parameter(Position=1)][string]$site = "default",
          [Parameter(Position=2)][string]$docroot = "/var/www/docroot",
          [Parameter(Position=3)][string]$cliContainer = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan   -Object "Executing"
        Write-Host -ForegroundColor Yellow -Object "$drush $command" 
        Write-Host -ForegroundColor Cyan   -Object "in $docroot/sites/$site folder of $cliContainer container..."
        docker exec -it $(docker-compose ps -q $cliContainer) bash -c "cd $docroot && cd sites/$site && drush $command"
    }
}

<#
.Synopsis
   Prints logs for all or needed container described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
.EXAMPLE
   Get-DrudeLogs
.EXAMPLE
   dsh-logs
.EXAMPLE
   Get-DrudeLogs cli
.EXAMPLE
   dsh-logs cli
#>

Function Get-DrudeLogs(){
    [cmdletbinding()]
    [Alias("dsh-logs")]
    param (
        [Parameter(Position=0)][string]$container = ""
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker-compose logs -f $container
    }
}

<#
.Synopsis
   Drops all containers described in docker-compose.yml in current folder.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   WARNING! This action may result to lose your data like databases.
.EXAMPLE
   Clear-Drude
.EXAMPLE
   dsh-destroy
#>

Function Clear-Drude(){
    [cmdletbinding()]
    [Alias("dsh-destroy")]
    param (
      [string]$arguments = "--remove-orphans"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
        Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
            "Deletes all containers and their contents."

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
            "Keeps all as it is."

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0) 

        switch ($result){
          0 {
            docker-compose down $arguments
          }
        }
    }
}

<#
.Synopsis
   Drops all containers described in docker-compose.yml in current folder and starts containers from scratch.
.DESCRIPTION
   Should be executed in folder with docker-compose.yml.
   WARNING! This action may result to lose your data like databases.
.EXAMPLE
   Reset-Drude
.EXAMPLE
   dsh-reset
#>


Function Reset-Drude(){
    [cmdletbinding()]
    [Alias("dsh-reset")]
    param (
      [string]$arguments = "--remove-orphans"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
        Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
            "Deletes all containers and their contents."

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
            "Keeps all as it is."

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0) 

        switch ($result){
          0 {
            docker-compose down $arguments
            Start-Drude
          }
        }
    }
}