Skip to content

Commit 9ac5d30

Browse files
Introduced docker machine support
Fix test cases, class and readme Removed travis customisation
1 parent ddd99ac commit 9ac5d30

File tree

6 files changed

+237
-2
lines changed

6 files changed

+237
-2
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [Networks](#networks)
1818
* [Volumes](#volumes)
1919
* [Compose](#compose)
20+
* [Machine](#machine)
2021
* [Swarm mode](#swarm-mode)
2122
* [Tasks](#tasks)
2223
* [Docker services](#docker-services)
@@ -684,6 +685,25 @@ docker_stack { 'test':
684685

685686
To remove the stack, set `ensure => absent`.
686687

688+
### Machine
689+
690+
Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands. You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.
691+
692+
For more information on machines see the [Docker Machines](https://docs.docker.com/machine/) documentation.
693+
694+
This module only takes responsability for installing the Docker Machine utility.
695+
696+
To install Docker Machine, add the following code to the manifest file:
697+
698+
```puppet
699+
class {'docker::machine':
700+
ensure => present,
701+
version => '1.16.1',
702+
}
703+
```
704+
705+
Set the `version` parameter to any version you need to install.
706+
687707
### Swarm mode
688708

689709
To natively manage a cluster of Docker Engines known as a swarm, Docker Engine 1.12 includes a swarm mode.
@@ -969,6 +989,7 @@ docker::plugin {'foo/fooplugin:latest'
969989
* docker
970990
* docker::compose
971991
* docker::images
992+
* docker::machine
972993
* docker::networks
973994
* docker::params
974995
* docker::plugins

manifests/machine.pp

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# == Class: docker::machine
2+
#
3+
# Class to install Docker Machine using the recommended curl command.
4+
#
5+
# === Parameters
6+
#
7+
# [*ensure*]
8+
# Whether to install or remove Docker Machine
9+
# Valid values are absent present
10+
# Defaults to present
11+
#
12+
# [*version*]
13+
# The version of Docker Machine to install.
14+
# Defaults to the value set in $docker::params::machine_version
15+
#
16+
# [*install_path*]
17+
# The path where to install Docker Machine.
18+
# Defaults to the value set in $docker::params::machine_install_path
19+
#
20+
# [*proxy*]
21+
# Proxy to use for downloading Docker Machine.
22+
#
23+
class docker::machine(
24+
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
25+
Optional[String] $version = $docker::params::machine_version,
26+
Optional[String] $install_path = $docker::params::machine_install_path,
27+
Optional[String] $proxy = undef
28+
) inherits docker::params {
29+
30+
if $proxy != undef {
31+
validate_re($proxy, '^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$')
32+
}
33+
34+
if $::osfamily == 'windows' {
35+
$file_extension = '.exe'
36+
$file_owner = 'Administrator'
37+
} else {
38+
$file_extension = ''
39+
$file_owner = 'root'
40+
}
41+
42+
$docker_machine_location = "${install_path}/docker-machine${file_extension}"
43+
$docker_machine_location_versioned = "${install_path}/docker-machine-${version}${file_extension}"
44+
45+
if $ensure == 'present' {
46+
$docker_machine_url = "https://github.com/docker/machine/releases/download/v${version}/docker-machine-${::kernel}-x86_64${file_extension}"
47+
48+
if $proxy != undef {
49+
$proxy_opt = "--proxy ${proxy}"
50+
} else {
51+
$proxy_opt = ''
52+
}
53+
54+
if $::osfamily == 'windows' {
55+
# lint:ignore:140chars
56+
$docker_download_command = "if (Invoke-WebRequest ${docker_machine_url} ${proxy_opt} -UseBasicParsing -OutFile \"${docker_machine_location_versioned}\") { exit 0 } else { exit 1}"
57+
# lint:endignore
58+
59+
exec { "Install Docker Machine ${version}":
60+
command => template('docker/windows/download_docker_machine.ps1.erb'),
61+
provider => powershell,
62+
creates => $docker_machine_location_versioned,
63+
}
64+
65+
file { $docker_machine_location:
66+
ensure => 'link',
67+
target => $docker_machine_location_versioned,
68+
require => Exec["Install Docker Machine ${version}"]
69+
}
70+
} else {
71+
ensure_packages(['curl'])
72+
exec { "Install Docker Machine ${version}":
73+
path => '/usr/bin/',
74+
cwd => '/tmp',
75+
command => "curl -s -S -L ${proxy_opt} ${docker_machine_url} -o ${docker_machine_location_versioned}",
76+
creates => $docker_machine_location_versioned,
77+
require => Package['curl'],
78+
}
79+
80+
file { $docker_machine_location_versioned:
81+
owner => $file_owner,
82+
mode => '0755',
83+
require => Exec["Install Docker Machine ${version}"]
84+
}
85+
86+
file { $docker_machine_location:
87+
ensure => 'link',
88+
target => $docker_machine_location_versioned,
89+
require => File[$docker_machine_location_versioned]
90+
}
91+
}
92+
} else {
93+
file { [
94+
$docker_machine_location_versioned,
95+
$docker_machine_location
96+
]:
97+
ensure => absent,
98+
}
99+
}
100+
}

manifests/params.pp

+3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
$tls_key = "${::docker_program_data_path}/docker/certs.d/server-key.pem"
3131
$compose_version = '1.21.2'
3232
$compose_install_path = "${::docker_program_files_path}/Docker"
33+
$machine_install_path = "${::docker_program_files_path}/Docker"
3334
} else {
3435
$tls_cacert = '/etc/docker/tls/ca.pem'
3536
$tls_cert = '/etc/docker/tls/cert.pem'
3637
$tls_key = '/etc/docker/tls/key.pem'
3738
$compose_version = '1.9.0'
3839
$compose_install_path = '/usr/local/bin'
40+
$machine_install_path = '/usr/local/bin'
3941
}
42+
$machine_version = '0.16.1'
4043
$ip_forward = true
4144
$iptables = true
4245
$ipv6 = false

spec/classes/compose_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
end
7070

7171
context 'when proxy contains username and password' do
72-
let(:params) { {:proxy => 'http://user:[email protected]:3128/',
73-
:version => '1.7.0'} }
72+
let(:params) { {:proxy => 'http://user:[email protected]:3128/',
73+
:version => '1.7.0'} }
7474
it { is_expected.to compile }
7575
it { is_expected.to contain_exec('Install Docker Compose 1.7.0').with_command(
7676
'curl -s -S -L --proxy http://user:[email protected]:3128/ https://github.com/docker/compose/releases/download/1.7.0/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose-1.7.0')

spec/classes/machine_spec.rb

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'spec_helper'
2+
3+
describe 'docker::machine', :type => :class do
4+
let(:facts) do
5+
{
6+
:kernel => 'Linux',
7+
:osfamily => 'Debian',
8+
:operatingsystem => 'Ubuntu',
9+
:lsbdistid => 'Ubuntu',
10+
:lsbdistcodename => 'maverick',
11+
:kernelrelease => '3.8.0-29-generic',
12+
:operatingsystemrelease => '10.04',
13+
:operatingsystemmajrelease => '10',
14+
}
15+
end
16+
17+
it { is_expected.to compile }
18+
19+
context 'with defaults for all parameters' do
20+
it { should compile.with_all_deps }
21+
it { should contain_exec('Install Docker Machine 0.16.1').with(
22+
'path' => '/usr/bin/',
23+
'cwd' => '/tmp',
24+
'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',
25+
'creates' => '/usr/local/bin/docker-machine-0.16.1',
26+
'require' => 'Package[curl]'
27+
)}
28+
it { should contain_file('/usr/local/bin/docker-machine-0.16.1').with(
29+
'owner' => 'root',
30+
'mode' => '0755',
31+
'require' => 'Exec[Install Docker Machine 0.16.1]'
32+
)}
33+
it { should contain_file('/usr/local/bin/docker-machine').with(
34+
'ensure' => 'link',
35+
'target' => '/usr/local/bin/docker-machine-0.16.1',
36+
'require' => 'File[/usr/local/bin/docker-machine-0.16.1]'
37+
)}
38+
end
39+
40+
context 'with ensure => absent' do
41+
let (:params) { { :ensure => 'absent' } }
42+
it { should contain_file('/usr/local/bin/docker-machine-0.16.1').with_ensure('absent') }
43+
it { should contain_file('/usr/local/bin/docker-machine').with_ensure('absent') }
44+
end
45+
46+
context 'when no proxy is provided' do
47+
let(:params) { {:version => '0.16.0'} }
48+
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command(
49+
'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')
50+
}
51+
end
52+
53+
context 'when proxy is provided' do
54+
let(:params) { {:proxy => 'http://proxy.example.org:3128/',
55+
:version => '0.16.0'} }
56+
it { is_expected.to compile }
57+
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command(
58+
'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')
59+
}
60+
end
61+
62+
context 'when proxy is not a http proxy' do
63+
let(:params) { {:proxy => 'this is not a URL'} }
64+
it do
65+
expect {
66+
is_expected.to compile
67+
}.to raise_error(/does not match/)
68+
end
69+
end
70+
71+
context 'when proxy contains username and password' do
72+
let(:params) { {:proxy => 'http://user:[email protected]:3128/',
73+
:version => '0.16.0'} }
74+
it { is_expected.to compile }
75+
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command(
76+
'curl -s -S -L --proxy http://user:[email protected]: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')
77+
}
78+
end
79+
80+
context 'when proxy IP is provided' do
81+
let(:params) { {:proxy => 'http://10.10.10.10:3128/',
82+
:version => '0.16.0'} }
83+
it { is_expected.to compile }
84+
it { is_expected.to contain_exec('Install Docker Machine 0.16.0').with_command(
85+
'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')
86+
}
87+
end
88+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
try {
2+
$WebClient = New-Object System.Net.WebClient
3+
<%if @proxy %>
4+
$uri = New-Object Uri("<%= @proxy %>")
5+
if ($uri.UserInfo -eq $null) {
6+
$WebProxy = New-Object System.Net.WebProxy("<%= @proxy %>",$true)
7+
$WebClient.Proxy = $WebProxy
8+
}
9+
else {
10+
$user,$password = $uri.UserInfo -split (':')
11+
$proxyAddress = $uri.Scheme + "://" + $uri.Host + ":" + $uri.Port + $uri.PathAndQuery
12+
$WebProxy = New-Object System.Net.WebProxy($uri,$true)
13+
$WebClient.Proxy = $WebProxy
14+
$WebClient.Proxy.Credentials = New-Object System.Net.NetworkCredential($user, $password)
15+
}
16+
<% end %>
17+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
18+
$WebClient.DownloadFile("<%= @docker_machine_url %>","<%= @docker_machine_location_versioned %>")
19+
}
20+
catch {
21+
exit 1
22+
}
23+
exit 0

0 commit comments

Comments
 (0)