-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced docker machine support (#466)
Fix test cases, class and readme Removed travis customisation
- Loading branch information
1 parent
d4dac10
commit 043395e
Showing
6 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# == Class: docker::machine | ||
# | ||
# Class to install Docker Machine using the recommended curl command. | ||
# | ||
# === Parameters | ||
# | ||
# [*ensure*] | ||
# Whether to install or remove Docker Machine | ||
# Valid values are absent present | ||
# Defaults to present | ||
# | ||
# [*version*] | ||
# The version of Docker Machine to install. | ||
# Defaults to the value set in $docker::params::machine_version | ||
# | ||
# [*install_path*] | ||
# The path where to install Docker Machine. | ||
# Defaults to the value set in $docker::params::machine_install_path | ||
# | ||
# [*proxy*] | ||
# Proxy to use for downloading Docker Machine. | ||
# | ||
class docker::machine( | ||
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present', | ||
Optional[String] $version = $docker::params::machine_version, | ||
Optional[String] $install_path = $docker::params::machine_install_path, | ||
Optional[String] $proxy = undef | ||
) inherits docker::params { | ||
|
||
if $proxy != undef { | ||
validate_re($proxy, '^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$') | ||
} | ||
|
||
if $::osfamily == 'windows' { | ||
$file_extension = '.exe' | ||
$file_owner = 'Administrator' | ||
} else { | ||
$file_extension = '' | ||
$file_owner = 'root' | ||
} | ||
|
||
$docker_machine_location = "${install_path}/docker-machine${file_extension}" | ||
$docker_machine_location_versioned = "${install_path}/docker-machine-${version}${file_extension}" | ||
|
||
if $ensure == 'present' { | ||
$docker_machine_url = "https://github.com/docker/machine/releases/download/v${version}/docker-machine-${::kernel}-x86_64${file_extension}" | ||
|
||
if $proxy != undef { | ||
$proxy_opt = "--proxy ${proxy}" | ||
} else { | ||
$proxy_opt = '' | ||
} | ||
|
||
if $::osfamily == 'windows' { | ||
# lint:ignore:140chars | ||
$docker_download_command = "if (Invoke-WebRequest ${docker_machine_url} ${proxy_opt} -UseBasicParsing -OutFile \"${docker_machine_location_versioned}\") { exit 0 } else { exit 1}" | ||
# lint:endignore | ||
|
||
exec { "Install Docker Machine ${version}": | ||
command => template('docker/windows/download_docker_machine.ps1.erb'), | ||
provider => powershell, | ||
creates => $docker_machine_location_versioned, | ||
} | ||
|
||
file { $docker_machine_location: | ||
ensure => 'link', | ||
target => $docker_machine_location_versioned, | ||
require => Exec["Install Docker Machine ${version}"] | ||
} | ||
} else { | ||
ensure_packages(['curl']) | ||
exec { "Install Docker Machine ${version}": | ||
path => '/usr/bin/', | ||
cwd => '/tmp', | ||
command => "curl -s -S -L ${proxy_opt} ${docker_machine_url} -o ${docker_machine_location_versioned}", | ||
creates => $docker_machine_location_versioned, | ||
require => Package['curl'], | ||
} | ||
|
||
file { $docker_machine_location_versioned: | ||
owner => $file_owner, | ||
mode => '0755', | ||
require => Exec["Install Docker Machine ${version}"] | ||
} | ||
|
||
file { $docker_machine_location: | ||
ensure => 'link', | ||
target => $docker_machine_location_versioned, | ||
require => File[$docker_machine_location_versioned] | ||
} | ||
} | ||
} else { | ||
file { [ | ||
$docker_machine_location_versioned, | ||
$docker_machine_location | ||
]: | ||
ensure => absent, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'spec_helper' | ||
|
||
describe 'docker::machine', :type => :class do | ||
let(:facts) do | ||
{ | ||
:kernel => 'Linux', | ||
:osfamily => 'Debian', | ||
:operatingsystem => 'Ubuntu', | ||
:lsbdistid => 'Ubuntu', | ||
:lsbdistcodename => 'maverick', | ||
:kernelrelease => '3.8.0-29-generic', | ||
:operatingsystemrelease => '10.04', | ||
:operatingsystemmajrelease => '10', | ||
} | ||
end | ||
|
||
it { is_expected.to compile } | ||
|
||
context 'with defaults for all parameters' do | ||
it { should compile.with_all_deps } | ||
it { should contain_exec('Install Docker Machine 0.16.1').with( | ||
'path' => '/usr/bin/', | ||
'cwd' => '/tmp', | ||
'command' => 'curl -s -S -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-Linux-x86_64 -o /usr/local/bin/docker-machine-0.16.1', | ||
'creates' => '/usr/local/bin/docker-machine-0.16.1', | ||
'require' => 'Package[curl]' | ||
)} | ||
it { should contain_file('/usr/local/bin/docker-machine-0.16.1').with( | ||
'owner' => 'root', | ||
'mode' => '0755', | ||
'require' => 'Exec[Install Docker Machine 0.16.1]' | ||
)} | ||
it { should contain_file('/usr/local/bin/docker-machine').with( | ||
'ensure' => 'link', | ||
'target' => '/usr/local/bin/docker-machine-0.16.1', | ||
'require' => 'File[/usr/local/bin/docker-machine-0.16.1]' | ||
)} | ||
end | ||
|
||
context 'with ensure => absent' do | ||
let (:params) { { :ensure => 'absent' } } | ||
it { should contain_file('/usr/local/bin/docker-machine-0.16.1').with_ensure('absent') } | ||
it { should contain_file('/usr/local/bin/docker-machine').with_ensure('absent') } | ||
end | ||
|
||
context 'when no proxy is provided' do | ||
let(:params) { {:version => '0.16.0'} } | ||
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command( | ||
'curl -s -S -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-Linux-x86_64 -o /usr/local/bin/docker-machine-0.16.0') | ||
} | ||
end | ||
|
||
context 'when proxy is provided' do | ||
let(:params) { {:proxy => 'http://proxy.example.org:3128/', | ||
:version => '0.16.0'} } | ||
it { is_expected.to compile } | ||
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command( | ||
'curl -s -S -L --proxy http://proxy.example.org:3128/ https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-Linux-x86_64 -o /usr/local/bin/docker-machine-0.16.0') | ||
} | ||
end | ||
|
||
context 'when proxy is not a http proxy' do | ||
let(:params) { {:proxy => 'this is not a URL'} } | ||
it do | ||
expect { | ||
is_expected.to compile | ||
}.to raise_error(/does not match/) | ||
end | ||
end | ||
|
||
context 'when proxy contains username and password' do | ||
let(:params) { {:proxy => 'http://user:password@proxy.example.org:3128/', | ||
:version => '0.16.0'} } | ||
it { is_expected.to compile } | ||
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command( | ||
'curl -s -S -L --proxy http://user:password@proxy.example.org:3128/ https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-Linux-x86_64 -o /usr/local/bin/docker-machine-0.16.0') | ||
} | ||
end | ||
|
||
context 'when proxy IP is provided' do | ||
let(:params) { {:proxy => 'http://10.10.10.10:3128/', | ||
:version => '0.16.0'} } | ||
it { is_expected.to compile } | ||
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command( | ||
'curl -s -S -L --proxy http://10.10.10.10:3128/ https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-Linux-x86_64 -o /usr/local/bin/docker-machine-0.16.0') | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
try { | ||
$WebClient = New-Object System.Net.WebClient | ||
<%if @proxy %> | ||
$uri = New-Object Uri("<%= @proxy %>") | ||
if ($uri.UserInfo -eq $null) { | ||
$WebProxy = New-Object System.Net.WebProxy("<%= @proxy %>",$true) | ||
$WebClient.Proxy = $WebProxy | ||
} | ||
else { | ||
$user,$password = $uri.UserInfo -split (':') | ||
$proxyAddress = $uri.Scheme + "://" + $uri.Host + ":" + $uri.Port + $uri.PathAndQuery | ||
$WebProxy = New-Object System.Net.WebProxy($uri,$true) | ||
$WebClient.Proxy = $WebProxy | ||
$WebClient.Proxy.Credentials = New-Object System.Net.NetworkCredential($user, $password) | ||
} | ||
<% end %> | ||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | ||
$WebClient.DownloadFile("<%= @docker_machine_url %>","<%= @docker_machine_location_versioned %>") | ||
} | ||
catch { | ||
exit 1 | ||
} | ||
exit 0 |