Skip to content

Commit

Permalink
initial commit for Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaibuzgau committed May 4, 2018
1 parent 534626b commit 4b9dac3
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ fixtures:
stdlib: 'puppetlabs-stdlib'
apt: 'puppetlabs-apt'
translate: 'puppetlabs-translate'
powershell: 'puppetlabs-powershell'
reboot: 'puppetlabs-reboot'
symlinks:
docker: "#{source_dir}"
docker: "#{source_dir}"
6 changes: 5 additions & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# == Class: docker::config
#
class docker::config {
docker::system_user { $docker::docker_users: }
if $::osfamily != 'windows' {
docker::system_user { $docker::docker_users: }
} else {
docker::windows_account { $docker::docker_users: }
}
}
80 changes: 59 additions & 21 deletions manifests/image.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,32 @@
include docker::params
$docker_command = $docker::params::docker_command

if $::osfamily == 'windows' {
$update_docker_image_template = 'docker/windows/update_docker_image.ps1.erb'
$update_docker_image_path = 'c:/Windows/Temp/update_docker_image.ps1'
$exec_environment = undef
$exec_timeout = 3000
$update_docker_image_owner = undef
$exec_path = ['c:/Windows/Temp/', 'C:/Program Files/Docker/']
$exec_provider = 'powershell'
} else {
$update_docker_image_template = 'docker/update_docker_image.sh.erb'
$update_docker_image_path = '/usr/local/bin/update_docker_image.sh'
$update_docker_image_owner = 'root'
$exec_environment = 'HOME=/root'
$exec_path = ['/bin', '/usr/bin']
$exec_timeout = 0
$exec_provider = undef
}

# Wrapper used to ensure images are up to date
ensure_resource('file', '/usr/local/bin/update_docker_image.sh',
ensure_resource('file', $update_docker_image_path,
{
ensure => $docker::params::ensure,
owner => 'root',
group => 'root',
owner => $update_docker_image_owner,
group => $update_docker_image_owner,
mode => '0555',
content => template('docker/update_docker_image.sh.erb'),
content => template($update_docker_image_template),
}
)

Expand Down Expand Up @@ -76,51 +94,71 @@
if $image_tag {
$image_arg = "${image}:${image_tag}"
$image_remove = "${docker_command} rmi ${image_force}${image}:${image_tag}"
$image_find = "${docker_command} images | egrep '^(docker.io/)?${image} ' | awk '{ print \$2 }' | grep ^${image_tag}$"
$image_find = "${docker_command} images -q ${image}:${image_tag}"
} elsif $image_digest {
$image_arg = "${image}@${image_digest}"
$image_remove = "${docker_command} rmi ${image_force}${image}:${image_digest}"
$image_find = "${docker_command} images --digests | egrep '^(docker.io/)?${image} ' | awk '{ print \$3 }' | grep ^${image_digest}$"
$image_find = "${docker_command} images -q ${image}@${image_digest}"

} else {
$image_arg = $image
$image_remove = "${docker_command} rmi ${image_force}${image}"
$image_find = "${docker_command} images | cut -d ' ' -f 1 | egrep '^(docker\\.io/)?${image}$'"
$image_find = "${docker_command} images -q ${image}"
}
if $::osfamily == 'windows' {
$_image_find = "\$image_id=${image_find}; If (!\$image_id) { Exit 1 }"
} else {
$_image_find = $image_find
}

if ($docker_dir) and ($docker_file) {
$image_install = "${docker_command} build -t ${image_arg} -f ${docker_file} ${docker_dir}"
} elsif $docker_dir {
$image_install = "${docker_command} build -t ${image_arg} ${docker_dir}"
} elsif $docker_file {
$image_install = "${docker_command} build -t ${image_arg} - < ${docker_file}"
if $::osfamily == windows {
$image_install = "Get-Content ${docker_file} | ${docker_command} build -t ${image_arg} -"
} else {
$image_install = "${docker_command} build -t ${image_arg} - < ${docker_file}"
}
} elsif $docker_tar {
$image_install = "${docker_command} load -i ${docker_tar}"
} else {
$image_install = "/usr/local/bin/update_docker_image.sh ${image_arg}"
if $::osfamily == 'windows' {
$image_install = "& ${update_docker_image_path} ${image_arg}"
} else {
$image_install = "${update_docker_image_path} ${image_arg}"
}
}

if $ensure == 'absent' {
exec { $image_remove:
path => ['/bin', '/usr/bin'],
onlyif => $image_find,
timeout => 0,
path => $exec_path,
onlyif => $_image_find,
provider => $exec_provider,
timeout => $exec_timeout,
logoutput => true,
}
} elsif $ensure == 'latest' or $image_tag == 'latest' {
exec { "echo 'Update of ${image_arg} complete'":
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
environment => $exec_environment,
path => $exec_path,
timeout => $exec_timeout,
onlyif => $image_install,
require => File['/usr/local/bin/update_docker_image.sh'],
require => File[$update_docker_image_path],
provider => $exec_provider,
logoutput => true,
}
} elsif $ensure == 'present' {
exec { $image_install:
unless => $image_find,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
unless => $_image_find,
environment => $exec_environment,
path => $exec_path,
timeout => $exec_timeout,
returns => ['0', '2'],
require => File['/usr/local/bin/update_docker_image.sh'],
require => File[$update_docker_image_path],
provider => $exec_provider,
logoutput => true,
}
}

Expand Down
18 changes: 16 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@
# Sets the prefered container registry mirror.
# Default: undef
#
# [*nuget_package_provider_version*]
# The version of the NuGet Package provider
# Default: undef
#
# [*docker_msft_provider_version*]
# The version of the Microsoft Docker Provider Module
# Default: undef

class docker(
Optional[String] $version = $docker::params::version,
String $ensure = $docker::params::ensure,
Expand Down Expand Up @@ -474,12 +482,15 @@
Optional[Boolean] $service_hasstatus = $docker::params::service_hasstatus,
Optional[Boolean] $service_hasrestart = $docker::params::service_hasrestart,
Optional[String] $registry_mirror = $docker::params::registry_mirror,
# Windows specific parameters
Optional[String] $docker_msft_provider_version = $docker::params::docker_msft_provider_version,
Optional[String] $nuget_package_provider_version = $docker::params::nuget_package_provider_version,
) inherits docker::params {


if $::osfamily {
assert_type(Pattern[/^(Debian|RedHat)$/], $::osfamily) |$a, $b| {
fail translate(('This module only works on Debian or Red Hat based systems.'))
assert_type(Pattern[/^(Debian|RedHat|windows)$/], $::osfamily) |$a, $b| {
fail translate(('This module only works on Debian, Red Hat or Windows based systems.'))
}
}

Expand Down Expand Up @@ -563,6 +574,9 @@
$package_location = "https://download.docker.com/linux/centos/${::operatingsystemmajrelease}/${::architecture}/${docker_ce_channel}"
$package_key_source = $docker_ce_key_source
$package_key_check_source = true
}
'windows': {
fail translate(('This module only work for Docker Enterprise Edition on Windows'))
}
default: {}
}
Expand Down
54 changes: 44 additions & 10 deletions manifests/install.pp
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# == Class: docker
#
# Module to install an up-to-date version of Docker from a package repository.
# This module works only on Debian and Red Hat based distributions.
# This module works only on Debian Red Hat and Windows based distributions.
#
class docker::install {
# === Parameters
# [*version*]
# The package version to install, used to set the package name.
#
# [*nuget_package_provider_version*]
# The version of the NuGet Package provider
# Default: undef
#
# [*docker_msft_provider_version*]
# The version of the Microsoft Docker Provider Module
# Default: undef

class docker::install (
$version = $docker::version,
$nuget_package_provider_version = $docker::nuget_package_provider_version,
$docker_msft_provider_version = $docker::docker_msft_provider_version
) {
$docker_start_command = $docker::docker_start_command
if $::osfamily {
assert_type(Pattern[/^(Debian|RedHat)$/], $::osfamily) |$a, $b| {
fail translate(('This module only works on Debian or RedHat.'))
assert_type(Pattern[/^(Debian|RedHat|windows)$/], $::osfamily) |$a, $b| {
fail translate(('This module only works on Debian, RedHat or Windows.'))
}
}
if $docker::version and $docker::ensure != 'absent' {
Expand All @@ -17,7 +33,6 @@
}

if $docker::manage_package {

if empty($docker::repo_opt) {
$docker_hash = {}
} else {
Expand All @@ -36,7 +51,6 @@
$pk_provider = undef
}
}

case $docker::package_source {
/docker-engine/ : {
ensure_resource('package', 'docker', merge($docker_hash, {
Expand All @@ -54,14 +68,34 @@
name => $docker::docker_ce_package_name,
}))
}
#TODO: Windows with download URL
default : {}
}


} else {
ensure_resource('package', 'docker', merge($docker_hash, {
ensure => $ensure,
name => $docker::docker_package_name,
}))
if $::osfamily != 'windows' {
ensure_resource('package', 'docker', merge($docker_hash, {
ensure => $ensure,
name => $docker::docker_package_name,
}))
} else {
$install_script_path = 'c:/Windows/Temp/install_powershell_provider.ps1'
file{ $install_script_path:
ensure => present,
force => true,
content => template('docker/windows/install_powershell_provider.ps1.erb'),
}
exec { 'install-docker-package':
command => "& ${install_script_path}",
provider => powershell,
logoutput => true,
}
exec { 'service-restart-on-failure':
command => 'cmd /c "SC failure Docker reset= 432000 actions= restart/30000/restart/60000/restart/60000"',
path => 'c:/Windows/System32/'
}
}
}
}
}
10 changes: 10 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
$compose_version = '1.9.0'
$compose_install_path = '/usr/local/bin'
$os_lc = downcase($::operatingsystem)
$docker_msft_provider_version = undef
$nuget_package_provider_version = undef

case $::osfamily {
'Debian' : {
Expand Down Expand Up @@ -207,6 +209,14 @@
$repo_opt = undef
}
}
'windows' : {
$msft_nuget_package_provider_version = $nuget_package_provider_version
$msft_provider_version = $docker_msft_provider_version
$msft_package_version = $version
$service_config_template = 'docker/windows/config/daemon.json.erb'
$service_config = 'C:/ProgramData/docker/config/daemon.json'
$docker_group = 'docker'
}
default: {
$docker_group = $docker_group_default
$socket_group = $socket_group_default
Expand Down
17 changes: 15 additions & 2 deletions manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
$registry_mirror = $docker::registry_mirror,
) {

unless $::osfamily =~ /(Debian|RedHat)/ {
fail translate(('The docker::service class needs a Debian or Redhat based system.'))
unless $::osfamily =~ /(Debian|RedHat|windows)/ {
fail translate(('The docker::service class needs a Debian, Redhat or Windows based system.'))
}

$dns_array = any2array($dns)
Expand Down Expand Up @@ -150,6 +150,11 @@
notify => $_manage_service,
}
}
if $::osfamily == 'windows' {
file { ['C:/ProgramData/docker/', 'C:/ProgramData/docker/config/']:
ensure => directory,
}
}

case $service_provider {
'systemd': {
Expand Down Expand Up @@ -202,6 +207,14 @@
}

if $manage_service {
if $::osfamily == 'windows' {
reboot { 'pending_reboot':
when => 'pending',
onlyif => 'component_based_servicing',
timeout => 10,
}
}

if ! defined(Service['docker']) {
service { 'docker':
ensure => $service_state,
Expand Down
9 changes: 9 additions & 0 deletions manifests/windows_account.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# == Define: docker::windows_account
#
# Define the Windows account that owns the docker services
#


define docker::windows_account () {
notice('TODO:Not implemented')
}
10 changes: 9 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"dependencies": [
{"name":"puppetlabs/stdlib","version_requirement":">= 4.24.0"},
{"name":"puppetlabs/apt","version_requirement":">= 4.4.1"},
{"name":"puppetlabs/translate","version_requirement":">= 0.0.1 < 1.1.0"}
{"name":"puppetlabs/translate","version_requirement":">= 0.0.1 < 1.1.0"},
{"name":"puppetlabs/powershell","version_requirement":">= 2.1.4"},
{"name":"puppetlabs/reboot","version_requirement":">= 2.0.0"}
],
"data_provider": null,
"operatingsystem_support": [
Expand Down Expand Up @@ -39,6 +41,12 @@
"8.0",
"9.0"
]
},
{
"operatingsystem": "Windows",
"operatingsystemrelease": [
"Server 2016"
]
}
],
"requirements": [
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@
it do
expect {
should contain_package('docker')
}.to raise_error(Puppet::Error, /This module only works on Debian or Red Hat based systems./)
}.to raise_error(Puppet::Error, /This module only works on Debian, Red Hat or Windows based systems./)
end
end

Expand Down
Loading

0 comments on commit 4b9dac3

Please sign in to comment.