Skip to content

Commit

Permalink
Squashed 'lib/killrvideo-docker-common/' changes from c40d0fa..0c67c94
Browse files Browse the repository at this point in the history
0c67c94 Add powershell script for creating a .env file
0bc2e46 Add back support for docker toolbox setups in Windows
f14f9cb Rework shell script to mirror powershell
b78ef09 Remove leading slash for shell call
721c3bd Rename shell script for consistency
28f7b43 Fix broken powershell script

git-subtree-dir: lib/killrvideo-docker-common
git-subtree-split: 0c67c9442eb3873625ff39407e5a2c8a6b07e24c
  • Loading branch information
LukeTillman committed Aug 13, 2016
1 parent 2be9718 commit 9ee998a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
killrvideo.env
.env
68 changes: 68 additions & 0 deletions create-environment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<#
.DESCRIPTION
Gets the environment variables needed to run the Killrvideo docker-compose commands, then writes
them to a .env file in the working directory or the directory specified by the -Path switch.
.PARAMETER Path
The path to create the environment file. Defaults to the current working directory.
.PARAMETER FileName
The name of the environment file. Defaults to ".env".
.PARAMETER ProjectName
The COMPOSE_PROJECT_NAME value to use. Defaults to "killrvideo".
.PARAMETER Force
Switch to force creation of the file (i.e. overwrite it) if it already exists.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
[string]
$Path = $((Resolve-Path .\).Path),

[Parameter(Mandatory=$false)]
[string]
$FileName = '.env',

[Parameter(Mandatory=$false)]
[string]
$ProjectName = 'killrvideo',

[Parameter(Mandatory=$false)]
[switch]
$Force
)

$ErrorActionPreference = 'stop'

# Make sure we have an absolute path
if ([System.IO.Path]::IsPathRooted($Path) -eq $false) {
$cwd = (Resolve-Path .\).Path
$Path = Join-Path $cwd $Path
}

# Path to the file and does it exist
$envFilePath = Join-Path $Path $FileName
if ((Test-Path $envFilePath) -and ($Force -eq $false)) {
Write-Host "Environment file $(Resolve-Path $envFilePath) already exists, will not overwrite"
Exit
}

# Base environment variables
$dockerEnv = @("COMPOSE_PROJECT_NAME=$ProjectName")

# Get path to the get-environment script and run it, adding each value to the env array
$scriptPath = Split-Path -parent $PSCommandPath
$getEnvCommand = Resolve-Path "$scriptPath\get-environment.ps1"
& "$getEnvCommand" |% { $dockerEnv += $_ }

# Make sure the path exists for the file
if ((Test-Path $Path) -eq $false) {
New-Item -Path $Path -Type Directory | Out-Null
}

# Write the file (have to use the .NET API here because we need UTF-8 WITHOUT the BOM)
$Utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllLines($envFilePath, $dockerEnv, $Utf8NoBom)
Write-Host "Environment file written to $(Resolve-Path $envFilePath)"
197 changes: 45 additions & 152 deletions get-environment.ps1
Original file line number Diff line number Diff line change
@@ -1,170 +1,63 @@
<#
.DESCRIPTION
Gets the environment variables needed to run the Killrvideo docker-compose commands.
Gets the environment variables needed to run the Killrvideo docker-compose commands and outputs
them to stdout.
#>
[CmdletBinding()]
Param ()

# Custom type representing the type of docker installation
Add-Type -TypeDefinition "public enum DockerInstallType { Windows, Toolbox }"

function Get-DockerType {
<#
.DESCRIPTION
Determines what type of docker installation is present (Docker for Windows or Docker Toolbox)
.OUTPUTS
A DockerInstallType enum value indicating the docker environment
#>
[CmdletBinding()]
Param()

Write-Host 'Determining docker installation type'
# Figure out if we're Docker for Windows or Docker Toolbox setup
Write-Host 'Determining docker installation type'

# Docker toolbox sets an install path environment variable so check for it
$dt = [DockerInstallType]::Windows
if ($Env:DOCKER_TOOLBOX_INSTALL_PATH) {
$dt = [DockerInstallType]::Toolbox
}

Write-Verbose " => Using docker type $dt"
$dt
# Docker toolbox sets an install path environment variable so check for it
$isToolbox = $false
if ($Env:DOCKER_TOOLBOX_INSTALL_PATH) {
$isToolbox = $true
}

function Get-DockerVirtualMachineIp {
<#
.DESCRIPTION
Find the IP address of the docker virtual machine
.PARAMETER DockerType
The type of docker installation.
.OUTPUTS
The IP Address of the docker virtual machine
#>
[CmdletBinding()]
Param (
[parameter(Mandatory=$true)]
[DockerInstallType]
$DockerType
)

Write-Host 'Finding docker Virtual Machine IP'

if ($DockerType -eq [DockerInstallType]::Windows) {
# In the DfW beta, the VM will be reachable via a host named 'docker'
$DOCKER_WINDOWS_HOST_NAME = 'docker'
$dnsResults = Resolve-DnsName $DOCKER_WINDOWS_HOST_NAME -ErrorAction SilentlyContinue
if ($dnsResults) {
Write-Verbose " => Able to resolve hostname '$DOCKER_WINDOWS_HOST_NAME' at $($dnsResults.IPAddress)"
$dnsResults.IPAddress
return
}
throw "Unable to resolve host '$DOCKER_WINDOWS_HOST_NAME'. Is Docker for Windows started?"
} else {
# Make sure the docker VM is started
Write-Host 'Ensuring Docker virtual machine is started'
Invoke-Expression "docker-machine start 2>&1" | Write-Host

# When using Docker Toolbox, we should be able to get the IP from docker-machine
$dockerHost = Invoke-Expression 'docker-machine ip 2>&1'
if ($LastExitCode -eq 0) {
Write-Verbose " => Docker machine returned $dockerHost"
$dockerHost
return
}

Write-Host 'Could not resolve the Docker IP with docker-machine ip command'
throw $dockerHost
}
}
Write-Verbose " => Is Docker Toolbox: $isToolbox"

function Get-NetworkAddress {
<#
.DESCRIPTION
Get a network address from an IP address and subnet mask
.PARAMETER Address
The IP address
.PARAMETER SubnetMask
The subnet mask
.OUTPUTS
The IP address of the network.
#>
[CmdletBinding()]
Param (
[parameter(Mandatory=$true)]
[System.Net.IPAddress]
$Address,

[parameter(Mandatory=$false)]
[System.Net.IPAddress]
$SubnetMask
)

# Set default subnet mask if not provided
if ($SubnetMask -eq $null) {
$SubnetMask = [System.Net.IPAddress]::Parse("255.255.255.0")
}

# Get as bytes
$ipBytes = $Address.GetAddressBytes()
$subnetBytes = $SubnetMask.GetAddressBytes()

# Create array for network bytes and use bitwise and to apply subnet mask
$networkBytes = @()
for ($i = 0; $i -le 3; $i++) {
$networkBytes += $ipBytes[$i] -band $subnetBytes[$i]
# Do things differently for Toolbox vs Docker for Windows
if ($isToolbox) {
# See if the docker VM is running
& docker-machine status default | Tee-Object -Variable dockerMachineStatus | Out-Null
if ($dockerMachineStatus -ne 'Running') {
& docker-machine start default | Out-Null
}
# Return as IPAddress (constructor takes a single array as argument)
New-Object System.Net.IPAddress -ArgumentList @(,$networkBytes)

# Add environment to this shell
& docker-machine env | Invoke-Expression
}

function Get-HostIp {
<#
.DESCRIPTION
Get the Host's IP address that's on the same network as the provided vmIp
.PARAMETER VirtualMachineIPAddress
The virtual machine's IP address
.OUTPUTS
The IP address on the host that's on the same network.
#>
[CmdletBinding()]
Param (
[parameter(Mandatory=$true)]
[string]
$VirtualMachineIPAddress
)

Write-Host 'Finding host IP on same network as docker Virtual Machine'

$addresses = Get-NetIPAddress | ? AddressFamily -eq IPv4 | Select IPAddress, SubnetMask
foreach($address in $addresses) {
$vmNetworkAddress = Get-NetworkAddress -Address $VirtualMachineIPAddress -SubnetMask $address.SubnetMask
$networkAddress = Get-NetworkAddress -Address $address.IPAddress -SubnetMask $address.SubnetMask

if ($networkAddress.Equals($vmNetworkAddress)) {
Write-Verbose " => Found host IP $($address.IPAddress)"
$address.IPAddress
return
}
}

throw "Could not find a host IP address on same network as $VirtualMachineIPAddress"
# Determine the Docker VM's IP address
Write-Host 'Getting Docker VM IP'
if ($isToolbox) {
# Just use the command that comes with docker-machine
& docker-machine ip | Tee-Object -Variable dockerIp | Out-Null
} else {
# The VM's IP should be the IP address for eth0 when running a container in host networking mode
$dockerIpCmd = "ip -4 addr show scope global dev eth0 | grep inet | awk `'{print `$2}`' | cut -d / -f 1"
& docker run --rm --net=host busybox bin/sh -c $dockerIpCmd | Tee-Object -Variable dockerIp | Out-Null
}
Write-Verbose " => Got Docker IP: $dockerIp"

# Figure out the network setup
$dockerType = Get-DockerType
$dockerVmIp = Get-DockerVirtualMachineIp -DockerType $dockerType
$hostIp = Get-HostIp $dockerVmIp
$isToolbox = $dockerType -eq [DockerInstallType]::Toolbox
# Determine the VM host's IP address
Write-Host 'Getting corresponding local machine IP'
if ($isToolbox) {
# The host only CIDR address will contain the host's IP (along with a suffix like /24)
& docker-machine inspect --format '{{ .Driver.HostOnlyCIDR }}' default |
Tee-Object -Variable hostCidr |
Out-Null
$hostIp = $hostCidr -replace "\/\d{2}", ""
} else {
# The host's IP should be the default route for eth0 when running a container in host networking mode
$hostIpCmd = "ip -4 route list dev eth0 0/0 | cut -d `' `' -f 3"
& docker run --rm --net=host busybox bin/sh -c $hostIpCmd | Tee-Object -Variable hostIp | Out-Null
}
Write-Verbose " => Got Host IP: $hostIp"

# Write environment variable key value pairs to output
# Write environment variable pairs to stdout (so this can be piped to a file)
Write-Output "KILLRVIDEO_DOCKER_TOOLBOX=$($isToolbox.ToString().ToLower())"
Write-Output "KILLRVIDEO_HOST_IP=$hostIp"
Write-Output "KILLRVIDEO_DOCKER_IP=$dockerVmIp"
Write-Output "KILLRVIDEO_DOCKER_IP=$dockerIp"

23 changes: 23 additions & 0 deletions get-environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

set -e # Bail if something fails

# TODO: Determine if a Docker Toolbox setup
IS_TOOLBOX=false

if [ "$IS_TOOLBOX" = true ]; then
echo "TODO: Docker machine environment setup"
fi

# Get the docker VM's IP address
DOCKER_IP_CMD="ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1"
DOCKER_IP=$(docker run --rm --net=host busybox bin/sh -c "$DOCKER_IP_CMD")

# Get the docker VM Host's IP address
HOST_IP_CMD="ip -4 route list dev eth0 0/0 | cut -d ' ' -f 3"
HOST_IP=$(docker run --rm --net=host busybox bin/sh -c "$HOST_IP_CMD")

# Write values to stdout
echo "KILLRVIDEO_DOCKER_TOOLBOX=$IS_TOOLBOX"
echo "KILLRVIDEO_HOST_IP=$HOST_IP"
echo "KILLRVIDEO_DOCKER_IP=$DOCKER_IP"
39 changes: 0 additions & 39 deletions getenvironment.sh

This file was deleted.

0 comments on commit 9ee998a

Please sign in to comment.