diff --git a/.travis.yml b/.travis.yml index 994e9f8..721fb48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,11 +23,11 @@ install: - eval "$(chef shell-init bash)" - openssl aes-256-cbc -K $encrypted_f6ac716f1c72_key -iv $encrypted_f6ac716f1c72_iv -in codenamephp.pem.enc -out codenamephp.pem -d -script: +script: - chef exec foodcritic . - chef exec rubocop - chef exec rspec - - travis_retry chef exec kitchen test + - chef exec kitchen test before_deploy: - chef exec knife cookbook metadata from file metadata.rb diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..855460a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,59 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Rubocop", + "type": "shell", + "command": "rubocop -a", + "group": "test" + }, + { + "label": "Foodcritic", + "type": "shell", + "command": "foodcritic .", + "group": "test" + }, + { + "label": "RSpec", + "type": "shell", + "command": "rspec", + "group": "test" + }, + { + "label": "Kitchen Test", + "type": "shell", + "command": "kitchen test" + }, + { + "label": "Lint", + "dependsOn": [ + "Rubocop", + "Foodcritic" + ], + "problemMatcher": [] + }, + { + "label": "Unit", + "dependsOn": [ + "RSpec" + ] + }, + { + "label": "Integration", + "dependsOn": [ + "Kitchen Test" + ] + }, + { + "label": "All", + "dependsOn": [ + "Lint", + "Unit", + "Integration" + ], + "problemMatcher": [] + } + ] +} \ No newline at end of file diff --git a/Berksfile b/Berksfile index c772391..0f3e772 100644 --- a/Berksfile +++ b/Berksfile @@ -4,4 +4,4 @@ source 'https://supermarket.chef.io' metadata -# cookbook 'test', path: './test/fixtures/cookbooks/test', group: :test +cookbook 'test', path: './test/fixtures/cookbooks/test', group: :test diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 0000000..8f3a034 --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1,5 @@ +# 3.0 + +The recipes have been removed. Create a wrapper cookbook and use the resource there as needed. The service recipe was replaced by +the `codenamephp_docker_service` resource that does exactly the same as the recipe did. For docker-compose there is the +`codenamephp_docker_compose` resource \ No newline at end of file diff --git a/README.md b/README.md index f9d9284..81a98ee 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,46 @@ This is just a wrapper cookbook for [docker][docker-github] Add the cookbook to your Berksfile: ```ruby -cookbook 'codenamephp_docker', :github 'codenamephp/codenamephp_docker' +cookbook 'codenamephp_docker' ``` -Add the cookbook to your runlist, e.g. in a role: - -```json -{ - "name": "default", - "chef_type": "role", - "json_class": "Chef::Role", - "run_list": [ - "recipe[codenamephp_docker]" - ] -} +Use the resources as needed in a wrapper cookbook. You can check out some examples either in this readme or in the test/fixtures/test cookbook. + +## Resources +### Service +Uses the `docker_service` resource to install docker and create and start the service. + +#### Actions +- `install`: Installs docker and creates and starts the service + +#### Examples +With minimal properties: +```ruby +# Install +codenamephp_docker_service 'Install docker' +``` + +### Compose +Installs docker-compose as a docker image using the run script wrapper from docker-compose github. The bash completion is also installed. + +#### Actions +- `install`: Installs docker-compose and bash completion + +#### Properties +- `version`: The desired version as string, defaults to `'latest'` + +#### Examples +With minimal properties: +```ruby +# Install +codenamephp_docker_service 'Install docker' +``` + +With custom version: +```ruby +codenamephp_docker_compose 'Install docker-compose' do + version '1.26.1' +end ``` [docker-github]: https://github.com/chef-cookbooks/docker \ No newline at end of file diff --git a/kitchen.dokken.yml b/kitchen.dokken.yml index ab58697..5e666d9 100644 --- a/kitchen.dokken.yml +++ b/kitchen.dokken.yml @@ -2,7 +2,7 @@ driver: name: dokken chef_version: current privileged: true # because Docker and SystemD/Upstart - + transport: name: dokken @@ -10,7 +10,7 @@ provisioner: name: dokken deprecations_as_errors: true chef_license: accept - + platforms: - name: debian-10 driver: diff --git a/kitchen.yml b/kitchen.yml index d1aa0f6..fdfd8da 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -14,8 +14,8 @@ platforms: suites: - name: default run_list: - - recipe[codenamephp_docker::default] + - recipe[test] verifier: inspec_tests: - - test/smoke/default + - test/integration/default attributes: diff --git a/metadata.rb b/metadata.rb index 21e57d0..ce18d9e 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,8 +5,8 @@ maintainer_email 'bastian@codename-php.de' license 'Apache-2.0' description 'Installs/Configures codenamephp_docker' -version '2.0.0' -chef_version '>= 13.0' if respond_to?(:chef_version) +version '3.0.0' +chef_version '>= 13.0' issues_url 'https://github.com/codenamephp/chef.cookbook.docker/issues' source_url 'https://github.com/codenamephp/chef.cookbook.docker' diff --git a/recipes/compose.rb b/recipes/compose.rb deleted file mode 100644 index 94798c2..0000000 --- a/recipes/compose.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -# -# Cookbook:: codenamephp_docker -# Recipe:: compose -# -# Copyright:: 2017, The Authors, All Rights Reserved. - -directory 'create bash directory' do - path '/etc/bash_completion.d' - owner 'root' - group 'root' - mode '0755' - recursive true - action :create -end - -remote_file 'download bash completion' do - source lazy { - "https://raw.githubusercontent.com/docker/compose/#{Mixlib::ShellOut.new("docker-compose --version | awk 'NR==1{print $NF}'").run_command.stdout.strip}/contrib/completion/bash/docker-compose" - } - path '/etc/bash_completion.d/docker-compose' - owner 'root' - group 'root' - mode '0755' - action :create -end diff --git a/recipes/default.rb b/recipes/default.rb deleted file mode 100644 index 3f41d14..0000000 --- a/recipes/default.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -# -# Cookbook:: codenamephp_docker -# Recipe:: default -# -# Copyright:: 2017, The Authors, All Rights Reserved. - -include_recipe '::service' diff --git a/recipes/service.rb b/recipes/service.rb deleted file mode 100644 index 0da58eb..0000000 --- a/recipes/service.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -# -# Cookbook:: codenamephp_docker -# Recipe:: service -# -# Copyright:: 2017, The Authors, All Rights Reserved. - -docker_service 'default' do - action %i[create start] -end diff --git a/resources/compose.rb b/resources/compose.rb new file mode 100644 index 0000000..bcfef6b --- /dev/null +++ b/resources/compose.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +property :version, String, default: 'latest', description: 'The version of docker-compose to install, defaults to "latest"' + +action :install do + remote_file 'download docker-compose' do + source compose_url + path '/usr/local/bin/docker-compose' + owner 'root' + group 'root' + mode '0755' + action :create + end + + directory 'create bash completion directory' do + path '/etc/bash_completion.d' + owner 'root' + group 'root' + mode '0755' + recursive true + action :create + end + + remote_file 'download bash completion' do + source lazy { compose_bash_completion_url } + path '/etc/bash_completion.d/docker-compose' + owner 'root' + group 'root' + mode '0755' + action :create + end +end + +action_class do + def compose_url + distro = Mixlib::ShellOut.new('uname -s').run_command.stdout.strip + arch = Mixlib::ShellOut.new('uname -m').run_command.stdout.strip + file = "docker-compose-#{distro}-#{arch}" + + new_resource.version == 'latest' ? "https://github.com/docker/compose/releases/latest/download/#{file}" : "https://github.com/docker/compose/releases/download/#{new_resource.version}/#{file}" + end + + def compose_bash_completion_url + version = Mixlib::ShellOut.new("docker-compose -v | grep version | awk -F'[ ,]+' '{print $3}'").run_command.stdout.strip + "https://raw.githubusercontent.com/docker/compose/#{version}/contrib/completion/bash/docker-compose" + end +end diff --git a/resources/service.rb b/resources/service.rb new file mode 100644 index 0000000..5783ab3 --- /dev/null +++ b/resources/service.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +action :install do + docker_service 'default' do + action %i[create start] + end +end diff --git a/spec/unit/recipes/compose_spec.rb b/spec/unit/recipes/compose_spec.rb deleted file mode 100644 index a9c83de..0000000 --- a/spec/unit/recipes/compose_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -# -# Cookbook:: codenamephp_docker -# Spec:: default -# -# Copyright:: 2017, The Authors, All Rights Reserved. - -require 'spec_helper' - -describe 'codenamephp_docker::compose' do - context 'When all attributes are default, on an Ubuntu 16.04' do - let(:chef_run) do - runner = ChefSpec::SoloRunner.new - runner.converge(described_recipe) - end - - before(:each) do - allow_any_instance_of(Mixlib::ShellOut).to receive_message_chain(:run_command, :stdout).and_return('someversion') - end - - it 'converges successfully' do - expect { chef_run }.to_not raise_error - end - - it 'creates the bash autocomplete directory if not exists ' do - expect(chef_run).to create_directory('create bash directory').with( - path: '/etc/bash_completion.d', - owner: 'root', - group: 'root', - mode: '0755', - recursive: true - ) - end - - it 'downloads the autocompletion for the actual version into the bash folder' do - expect(chef_run).to create_remote_file('download bash completion').with( - source: 'https://raw.githubusercontent.com/docker/compose/someversion/contrib/completion/bash/docker-compose', - path: '/etc/bash_completion.d/docker-compose', - owner: 'root', - group: 'root', - mode: '0755' - ) - end - end -end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb deleted file mode 100644 index c984b04..0000000 --- a/spec/unit/recipes/default_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -# -# Cookbook:: codenamephp_docker -# Spec:: default -# -# Copyright:: 2017, The Authors, All Rights Reserved. - -require 'spec_helper' - -describe 'codenamephp_docker::default' do - context 'When all attributes are default' do - let(:chef_run) do - runner = ChefSpec::SoloRunner.new - runner.converge(described_recipe) - end - - it 'converges successfully' do - expect { chef_run }.to_not raise_error - end - - it 'includes the `service` recipe' do - expect(chef_run).to include_recipe('codenamephp_docker::service') - end - end -end diff --git a/spec/unit/resources/compose_spec.rb b/spec/unit/resources/compose_spec.rb new file mode 100644 index 0000000..11e54c2 --- /dev/null +++ b/spec/unit/resources/compose_spec.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +# +# Cookbook:: codenamephp_docker +# Spec:: default +# +# Copyright:: 2017, The Authors, All Rights Reserved. + +require 'spec_helper' + +describe 'codenamephp_docker::compose' do + platform 'debian' # https://github.com/chefspec/chefspec/issues/953 + + step_into :codenamephp_docker_compose + + context 'When all attributes are default' do + recipe do + codenamephp_docker_compose 'Install docker-compose' + end + + before(:each) do + allow(Mixlib::ShellOut).to receive(:new).and_call_original + allow(Mixlib::ShellOut).to receive(:new).with('uname -s').and_return(double(run_command: double(stdout: 'somedist'))) + allow(Mixlib::ShellOut).to receive(:new).with('uname -m').and_return(double(run_command: double(stdout: 'somearch'))) + allow(Mixlib::ShellOut).to receive(:new).with("docker-compose -v | grep version | awk -F'[ ,]+' '{print $3}'").and_return(double(run_command: double(stdout: 'someversion'))) + end + + it 'converges successfully' do + expect { chef_run }.to_not raise_error + end + + it 'downloads docker-compose' do + expect(chef_run).to create_remote_file('download docker-compose').with( + source: 'https://github.com/docker/compose/releases/latest/download/docker-compose-somedist-somearch', + path: '/usr/local/bin/docker-compose', + owner: 'root', + group: 'root', + mode: '0755' + ) + end + + it 'creates the bash autocomplete directory if not exists ' do + expect(chef_run).to create_directory('create bash completion directory').with( + path: '/etc/bash_completion.d', + owner: 'root', + group: 'root', + mode: '0755', + recursive: true + ) + end + + it 'downloads the autocompletion for the actual version into the bash folder' do + expect(chef_run).to create_remote_file('download bash completion').with( + source: 'https://raw.githubusercontent.com/docker/compose/someversion/contrib/completion/bash/docker-compose', + path: '/etc/bash_completion.d/docker-compose', + owner: 'root', + group: 'root', + mode: '0755' + ) + end + end + + context 'With custom version' do + recipe do + codenamephp_docker_compose 'Install docker-compose' do + version 'someversion' + end + end + + before(:each) do + allow(Mixlib::ShellOut).to receive(:new).and_call_original + allow(Mixlib::ShellOut).to receive(:new).with('uname -s').and_return(double(run_command: double(stdout: 'somedist'))) + allow(Mixlib::ShellOut).to receive(:new).with('uname -m').and_return(double(run_command: double(stdout: 'somearch'))) + allow(Mixlib::ShellOut).to receive(:new).with("docker-compose -v | grep version | awk -F'[ ,]+' '{print $3}'").and_return(double(run_command: double(stdout: 'someversion'))) + end + + it 'converges successfully' do + expect { chef_run }.to_not raise_error + end + + it 'downloads docker-compose' do + expect(chef_run).to create_remote_file('download docker-compose').with( + source: 'https://github.com/docker/compose/releases/download/someversion/docker-compose-somedist-somearch', + path: '/usr/local/bin/docker-compose', + owner: 'root', + group: 'root', + mode: '0755' + ) + end + end +end diff --git a/spec/unit/recipes/service_spec.rb b/spec/unit/resources/service_spec.rb similarity index 57% rename from spec/unit/recipes/service_spec.rb rename to spec/unit/resources/service_spec.rb index b97b750..e059c29 100644 --- a/spec/unit/recipes/service_spec.rb +++ b/spec/unit/resources/service_spec.rb @@ -2,24 +2,27 @@ # # Cookbook:: codenamephp_docker -# Spec:: default +# Spec:: codenamephp_docker_service # # Copyright:: 2017, The Authors, All Rights Reserved. require 'spec_helper' -describe 'codenamephp_docker::service' do +describe 'codenamephp_docker_service' do + platform 'debian' # https://github.com/chefspec/chefspec/issues/953 + + step_into :codenamephp_docker_service + context 'When all attributes are default' do - let(:chef_run) do - runner = ChefSpec::SoloRunner.new - runner.converge(described_recipe) + recipe do + codenamephp_docker_service 'Install docker' end it 'converges successfully' do expect { chef_run }.to_not raise_error end - it 'docker_service[default]' do + it 'creates and starts docker service using resource' do expect(chef_run).to create_docker_service('default') expect(chef_run).to start_docker_service('default') end diff --git a/test/fixtures/cookbooks/test/attributes/default.rb b/test/fixtures/cookbooks/test/attributes/default.rb new file mode 100644 index 0000000..df12860 --- /dev/null +++ b/test/fixtures/cookbooks/test/attributes/default.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +default['resolver']['nameservers'] = ['127.0.0.1', '8.8.8.8', '8.8.4.4'] diff --git a/test/fixtures/cookbooks/test/metadata.rb b/test/fixtures/cookbooks/test/metadata.rb new file mode 100644 index 0000000..7bae43a --- /dev/null +++ b/test/fixtures/cookbooks/test/metadata.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +name 'test' +maintainer 'Bastian Schwarz' +maintainer_email 'bastian@codename-php.de' +license 'Apache-2.0' +description 'Testcookbook for resources' +version '1.0.0' + +supports 'debian', '~>10' + +depends 'codenamephp_docker' +depends 'resolver' diff --git a/test/fixtures/cookbooks/test/recipes/default.rb b/test/fixtures/cookbooks/test/recipes/default.rb new file mode 100644 index 0000000..5734e3c --- /dev/null +++ b/test/fixtures/cookbooks/test/recipes/default.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +include_recipe 'resolver' +codenamephp_docker_service 'Install docker' + +codenamephp_docker_compose 'Install docker-compose' diff --git a/test/integration/default/compose_test.rb b/test/integration/default/compose_test.rb new file mode 100644 index 0000000..14164f8 --- /dev/null +++ b/test/integration/default/compose_test.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +describe command('docker-compose -v') do + its('stdout') { should match(/docker-compose version/) } +end diff --git a/test/integration/default/service_test.rb b/test/integration/default/service_test.rb new file mode 100644 index 0000000..373473b --- /dev/null +++ b/test/integration/default/service_test.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Inspec test for recipe codenamephp_docker::service + +# The Inspec reference, with examples and extensive documentation, can be +# found at http://inspec.io/docs/reference/resources/ + +describe service('docker') do + it { should be_installed } + it { should be_enabled } + it { should be_running } +end diff --git a/test/smoke/default/compose_test.rb b/test/smoke/default/compose_test.rb deleted file mode 100644 index 61a0b36..0000000 --- a/test/smoke/default/compose_test.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -# Inspec test for recipe codenamephp_docker::compose - -# The Inspec reference, with examples and extensive documentation, can be -# found at http://inspec.io/docs/reference/resources/ - -control 'docker-compose-1.0' do - title 'Install Docker Compose' - desc 'Install Docker Compose' - - # describe command('sudo docker-compose -v') do - # its('stdout') { should match(/docker-compose version/) } - # end -end diff --git a/test/smoke/default/service_test.rb b/test/smoke/default/service_test.rb deleted file mode 100644 index b33b1f6..0000000 --- a/test/smoke/default/service_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -# Inspec test for recipe codenamephp_docker::service - -# The Inspec reference, with examples and extensive documentation, can be -# found at http://inspec.io/docs/reference/resources/ - -control 'docker-1.0' do - title 'Install Docker' - desc 'Install Docker and make sure the docker service is running' - - describe service('docker') do - it { should be_installed } - it { should be_enabled } - it { should be_running } - end -end