diff --git a/CHANGELOG.md b/CHANGELOG.md index 55b4545..45c457f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased](../../releases/tag/X.Y.Z) +### Added + +- Add support to install Docker Ansible module dependencies ([#48](../../issues/48)) +- Add support to install packages after install via PiP or OS package manager ([#49](../../issues/49)) + ## [1.7.2](../../releases/tag/1.7.2) - 2018-09-27 ### Fixed diff --git a/README.md b/README.md index 8f9020b..9546065 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,19 @@ docker_latest_version: true docker_pkg_name: docker-ce # If below variable is set to true it will remove older Docker installation before Docker CE. docker_remove_pre_ce: false +# Ensures 'docker_container' Ansible module dependencies are installed +docker_container_deps: false +# Ensures 'docker_service' Ansible module dependencies are installed +docker_service_deps: false +# Ensures 'docker_stack' Ansible module dependencies are installed +docker_stack_deps: false +# Ensure PiP is upgraded before further use. +# IMPORTANT! Be carful to set this because it might cause dependecy problems +docker_pip_upgrade: false +# Additional PiP packages to install after Docker is configured and started +docker_additional_packages_pip: [] +# Additional OS packages to install after Docker is configured and started +docker_additional_packages_os: [] ``` ## Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index 3297d46..c27a90d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,4 @@ --- -# defaults file for ansible-role-docker-ce - # Daemon configuration (https://docs.docker.com/engine/reference/commandline/dockerd/) # Example: # docker_daemon_config: @@ -32,3 +30,16 @@ docker_latest_version: true docker_pkg_name: docker-ce # If below variable is set to true it will remove older Docker installation before Docker CE. docker_remove_pre_ce: false +# Ensures 'docker_container' Ansible module dependencies are installed +docker_container_deps: false +# Ensures 'docker_service' Ansible module dependencies are installed +docker_service_deps: false +# Ensures 'docker_stack' Ansible module dependencies are installed +docker_stack_deps: false +# Ensure PiP is upgraded before further use. +# IMPORTANT! Be carful to set this because it might cause dependecy problems +docker_pip_upgrade: false +# Additional PiP packages to install after Docker is configured and started +docker_additional_packages_pip: [] +# Additional OS packages to install after Docker is configured and started +docker_additional_packages_os: [] \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index bd6e3ef..d9768af 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -67,3 +67,7 @@ - name: Configure Docker include_tasks: configure-docker.yml tags: ["configure"] + +- name: Postinstall tasks + include_tasks: postinstall.yml + tags: ["install"] \ No newline at end of file diff --git a/tasks/postinstall.yml b/tasks/postinstall.yml new file mode 100644 index 0000000..f8debff --- /dev/null +++ b/tasks/postinstall.yml @@ -0,0 +1,61 @@ +--- +- name: Reset internal variables for additional packages to be installed + set_fact: + _docker_additional_packages_os: [] + _docker_additional_packages_pip: [] + +- name: Set facts to ensure 'docker_container' module works + set_fact: + _docker_additional_packages_pip: "{{ _docker_additional_packages_pip + docker_predefined_packages_pip[_docker_os_dist]['sdk'] }}" + when: + - docker_container_deps + +- name: Set facts to ensure 'docker_service' module works + set_fact: + _docker_additional_packages_pip: "{{ _docker_additional_packages_pip + docker_predefined_packages_pip[_docker_os_dist]['compose'] }}" + when: + - docker_service_deps + +- name: Set facts to ensure 'docker_stack' module works + set_fact: + _docker_additional_packages_pip: "{{ _docker_additional_packages_pip + docker_predefined_packages_pip[_docker_os_dist]['stack'] }}" + when: + - docker_stack_deps + +- name: Set facts with additional package to be installed + set_fact: + _docker_additional_packages_pip: "{{ _docker_additional_packages_pip + docker_additional_packages_pip }}" + _docker_additional_packages_os: "{{ _docker_additional_packages_os + docker_additional_packages_os }}" + +- name: Set facts to ensure PiP is installed if required + set_fact: + _docker_additional_packages_os: "{{ _docker_additional_packages_os + ['python-pip', 'python-virtualenv'] }}" + when: + - _docker_additional_packages_pip | length > 0 + +- name: Install additional packages (OS package manager) + become: true + package: + name: "{{ item }}" + state: present + with_items: + - "{{ _docker_additional_packages_os }}" + when: _docker_additional_packages_os | length > 0 + +- name: Upgrade PiP + become: true + pip: + name: pip + state: forcereinstall + when: docker_pip_upgrade + +- name: Install additional packages (PiP) + become: true + pip: + name: "{{ item }}" + state: present + with_items: + - "{{ _docker_additional_packages_pip }}" + when: _docker_additional_packages_pip | length > 0 + environment: + PYTHONWARNINGS: ignore \ No newline at end of file diff --git a/tests/Vagrantfile b/tests/Vagrantfile index 50a1938..8b832ac 100644 --- a/tests/Vagrantfile +++ b/tests/Vagrantfile @@ -5,8 +5,16 @@ require 'yaml' current_dir = File.dirname(File.expand_path(__FILE__)) configs = YAML.load_file("#{current_dir}/vagrant_config.yml") -config_key = ENV['CONFIG_KEY'] || configs['configs']['use'] -vagrant_config = configs['configs'][config_key] +vagrant_config={} +if ENV.has_key?('CI') + config_key = "CI" + vagrant_config['box'] = ENV['VAGRANT_BOX'] + vagrant_config['prep_yml'] = ENV['VAGRANT_PREP_YML'] + vagrant_config['test_yml'] = ENV['VAGRANT_TEST_YML'] +else + config_key = ENV['CONFIG_KEY'] || configs['configs']['use'] + vagrant_config = configs['configs'][config_key] +end # Plugins: # vagrant plugin install vagrant-vbguest @@ -29,6 +37,8 @@ Vagrant.configure("2") do |config| # Print config in use config.vm.provision "shell", inline: "echo '" + config_key + "' > /var/tmp/provisioning-config" + config.vm.provision "shell", + inline: "echo '--> Using configuration key: " + config_key + "'" # Prepare Ansible roles directory config.vm.provision "shell", diff --git a/tests/ci-test.sh b/tests/ci-test.sh index b64c242..1c504d8 100644 --- a/tests/ci-test.sh +++ b/tests/ci-test.sh @@ -1,22 +1,4 @@ #!/usr/bin/env bash -YAML_INC_FILE=yamlparser.sh.inc -if [[ ! -f "$YAML_INC_FILE" ]]; then - echo "Fetching YAML parsing script..." - wget -O $YAML_INC_FILE -q https://raw.githubusercontent.com/jasperes/bash-yaml/master/yaml.sh -fi -. $YAML_INC_FILE - -UBUNTU_ON_WIN=$(uname -a | grep Microsoft) -if [[ $? -eq 0 ]]; then - echo "Ubuntu on Windows - assuming Vagrant is installed in Windows." - VAGRANT_CMD=vagrant.exe - # To share CONFIG_KEY to Windows environment - export CONFIG_KEY= - export WSLENV=CONFIG_KEY/w -else - VAGRANT_CMD=vagrant -fi - BLDRED='\033[1;31m' BLDGRN='\033[1;32m' BLDBLU='\033[1;34m' @@ -45,6 +27,29 @@ RedText() { printf "%b\n" "${BLDRED}$1${TXTRST}" } +Info() { + printf "%b\n" "${BLDBLU}[INFO]${TXTRST} $1" +} + +SetupYamlParser() { + YAML_INC_FILE=yamlparser.sh.inc + if [[ ! -f "$YAML_INC_FILE" ]]; then + Info "Fetching YAML parsing script..." + wget -O $YAML_INC_FILE -q https://raw.githubusercontent.com/jasperes/bash-yaml/master/yaml.sh + fi + . $YAML_INC_FILE +} + +DetectWSL() { + UBUNTU_ON_WIN=$(uname -a | grep Microsoft) + if [[ $? -eq 0 ]]; then + Info "Ubuntu on Windows - assuming Vagrant is installed in Windows." + VAGRANT_CMD=vagrant.exe + else + VAGRANT_CMD=vagrant + fi +} + VagrantExists() { if ! vagrant_loc="$(type -p $VAGRANT_CMD)" || [[ -z $vagrant_loc ]]; then echo "1" @@ -89,10 +94,9 @@ VagrantBoxAdd() { } DownloadBoxes() { - echo "Downloading boxes..." - boxes=$(parse_yaml vagrant_config.yml | grep _box | cut -d= -f2 | sed 's/[\(\"\)]//g' | sed "s/'//g" | sort | uniq) + Info "Downloading boxes..." exitCode=0 - for box in $boxes; do + for box in ${boxes[*]}; do if [[ "$box" != *"$LIMIT"* ]]; then Skip "Download $box" continue @@ -109,36 +113,50 @@ DownloadBoxes() { } ExecuteTests() { - echo "Starting tests..." - configs=$(parse_yaml vagrant_config.yml | grep _box | awk '{split($0,a,"_box"); $1=a[1]; split($1,b,"configs_"); $2=b[2]; print $2}') - exitCode=0 - for config in $configs; do - CONFIG_KEY=$config - export CONFIG_KEY - if [[ "$CONFIG_KEY" != *"$LIMIT"* ]]; then - Skip "$CONFIG_KEY" - continue - fi - VagrantUp - exitCode=$? - VagrantDestroy - if [[ $exitCode == "0" ]]; then - Pass "Test $CONFIG_KEY" - else - Fail "Test $CONFIG_KEY" - break - fi + Info "Starting tests..." + #configs=$(parse_yaml vagrant_config.yml | grep _box | awk '{split($0,a,"_box"); $1=a[1]; split($1,b,"configs_"); $2=b[2]; print $2}') + configs= + for index in $(seq 0 `expr ${#tests__name[@]} - 1`); do + for box in ${boxes[*]}; do + test_name=${tests__name[$index]} + WSLENV=CI:VAGRANT_BOX:VAGRANT_PREP_YML:VAGRANT_TEST_YML + CI=1 + VAGRANT_BOX=$box + VAGRANT_PREP_YML=${tests__prep_yml[$index]} + VAGRANT_TEST_YML=${tests__test_yml[$index]} + if [[ "$test_name" != *"$LIMIT"* ]]; then + Skip "Test: $test_name" + continue + fi + Info "Test: ${tests__name[$index]} [$box]" + export WSLENV CI VAGRANT_BOX VAGRANT_PREP_YML VAGRANT_TEST_YML + VagrantUp + exitCode=$? + VagrantDestroy + if [[ $exitCode == "0" ]]; then + Pass "Test: $test_name" + else + Fail "Test: $test_name" + break + fi + done done - echo "Ended with exit code $exitCode" + exitCode=0 + Info "Ended with exit code $exitCode" return $exitCode } +SetupYamlParser +DetectWSL + LIMIT=$1 +create_variables vagrant_config.yml + if [[ "$SKIP_DOWNLOAD" == "" ]]; then DownloadBoxes downloadResult=$? - echo "Download complete!" + Info "Download complete!" if [[ "$DOWNLOAD_ONLY" != "" ]]; then exit $downloadResult fi diff --git a/tests/test_postinstall.yml b/tests/test_postinstall.yml new file mode 100644 index 0000000..ad03fa4 --- /dev/null +++ b/tests/test_postinstall.yml @@ -0,0 +1,24 @@ +--- +- hosts: test-host + vars: + docker_container_deps: true + docker_service_deps: true + docker_stack_deps: true + roles: + - haxorof.docker-ce + post_tasks: + - name: Test hello container + become: yes + docker_container: + name: hello + image: hello-world + + - name: Test hello service + become: yes + docker_service: + project_name: hello + definition: + version: '3' + services: + hello: + image: "hello-world" diff --git a/tests/vagrant_config.yml b/tests/vagrant_config.yml index 3fce204..8d3a034 100644 --- a/tests/vagrant_config.yml +++ b/tests/vagrant_config.yml @@ -1,3 +1,43 @@ +boxes: + - geerlingguy/centos7 + - geerlingguy/ubuntu1404 + - geerlingguy/ubuntu1604 + - geerlingguy/ubuntu1804 + - geerlingguy/debian8 + - geerlingguy/debian9 +tests: + - + name: No configuration + prep_yml: prepare.yml + test_yml: test_defaults.yml + - + name: Simple configuration + prep_yml: prepare.yml + test_yml: test_config.yml + - + name: Configuration via file + prep_yml: prepare.yml + test_yml: test_config_file.yml + - + name: Advanced configuration + prep_yml: prepare.yml + test_yml: test_config_adv.yml + - + name: Storage configuration + prep_yml: prepare_storage.yml + test_yml: test_config_storage.yml + - + name: Postinstall + prep_yml: prepare.yml + test_yml: test_postinstall.yml + - + name: Old version of Docker + prep_yml: prepare.yml + test_yml: test_old_version.yml + - + name: Issue 42 + prep_yml: prepare.yml + test_yml: test_issue_42.yml configs: use: 'defaults_centos' # CentOS 7 @@ -11,6 +51,31 @@ configs: box: 'geerlingguy/centos7' prep_yml: prepare.yml test_yml: test_defaults.yml + # Postinstall tests + postinstall_centos: + box: 'geerlingguy/centos7' + prep_yml: prepare.yml + test_yml: test_postinstall.yml + postinstall_ubuntu_trusty: + box: 'geerlingguy/ubuntu1404' + prep_yml: prepare.yml + test_yml: test_postinstall.yml + postinstall_ubuntu_xenial: + box: 'geerlingguy/ubuntu1604' + prep_yml: prepare.yml + test_yml: test_postinstall.yml + postinstall_ubuntu_bionic: + box: 'geerlingguy/ubuntu1804' + prep_yml: prepare.yml + test_yml: test_postinstall.yml + postinstall_debian_jessie: + box: 'geerlingguy/debian8' + prep_yml: prepare.yml + test_yml: test_postinstall.yml + postinstall_debian_stretch: + box: 'geerlingguy/debian9' + prep_yml: prepare.yml + test_yml: test_postinstall.yml # Config tests config_centos: box: 'geerlingguy/centos7' diff --git a/vars/main.yml b/vars/main.yml index 98d76b2..925f921 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -77,3 +77,47 @@ docker_old_packages: - docker - docker-engine - docker.io + +docker_predefined_packages_os: [] + +docker_predefined_packages_pip: + CentOS: + sdk: + - docker + compose: + - docker-compose + stack: + - jsondiff + - pyyaml + RedHat: + sdk: + - docker + compose: + - docker-compose + stack: + - jsondiff + - pyyaml + Fedora: + sdk: + - docker + compose: + - docker-compose + stack: + - jsondiff + - pyyaml + Ubuntu: + sdk: + - docker + compose: + - docker-compose + stack: + - jsondiff + - pyyaml + Debian: + sdk: + - docker + compose: + - docker-compose + stack: + - jsondiff + - pyyaml