Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | |
| 5 | # Copyright 2018 © Samsung Electronics Co., Ltd. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | |
| 19 | # COPYRIGHT NOTICE ENDS HERE |
| 20 | |
| 21 | |
| 22 | set -e |
| 23 | |
| 24 | script_path=$(readlink -f "$0") |
| 25 | script_name=$(basename "$script_path") |
| 26 | ANSIBLE_DIR=$(dirname "$script_path") |
| 27 | ANSIBLE_CHROOT="${ANSIBLE_DIR}/ansible_chroot" |
Petr Ospalý | 5b197fb | 2019-01-15 10:49:30 +0100 | [diff] [blame] | 28 | ANSIBLE_LOG_PATH="/ansible/log/ansible-$(date +%Y.%m.%d-%H%M%S).log" |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 29 | |
| 30 | |
| 31 | # |
| 32 | # functions |
| 33 | # |
| 34 | |
| 35 | help() |
| 36 | { |
| 37 | echo " |
| 38 | NAME: |
| 39 | ${script_name} - wrapper for ansible-playbook command |
| 40 | |
| 41 | DESCRIPTION: |
| 42 | Run ansible playbook (or other command if it is there) inside a docker |
| 43 | container or a chroot environment. |
| 44 | |
| 45 | By default the chroot is used because it has less dependencies and no |
| 46 | service needs to be run (provided that chroot command is installed). |
| 47 | |
| 48 | Docker support is kept for compatibility reasons. |
| 49 | |
| 50 | To run ansible docker image you must set environment variable: |
| 51 | ANSIBLE_DOCKER_IMAGE |
| 52 | |
| 53 | So this wrapper can know by which name you have built the included |
| 54 | Dockerfile and also to trigger this different behaviour. |
| 55 | |
| 56 | For example: |
| 57 | ANSIBLE_DOCKER_IMAGE=ansible |
| 58 | |
| 59 | USAGE: |
| 60 | ./${script_name} |
| 61 | This help |
| 62 | |
| 63 | ./${script_name} <args> |
| 64 | Run ansible-playbook command inside a chroot |
| 65 | |
| 66 | ANSIBLE_DOCKER_IMAGE=<docker-image> ./${script_name} <args> |
| 67 | Run ansible-playbook command inside a docker container |
| 68 | |
| 69 | REQUIREMENTS: |
| 70 | For the optimal usage your system should support overlay mount. Which |
| 71 | should be available on any recent kernel at least couple of years back. |
| 72 | |
| 73 | Another requirement is the 'unshare' utility which is part of 'util-linux' |
| 74 | package and also is part of system for couple of years already. |
| 75 | |
| 76 | The last is 'chroot' command itself and that is also part of system |
| 77 | basically everywhere. |
| 78 | " |
| 79 | } |
| 80 | |
| 81 | |
| 82 | # |
| 83 | # run playbook |
| 84 | # |
| 85 | |
Petr Ospalý | 5b197fb | 2019-01-15 10:49:30 +0100 | [diff] [blame] | 86 | export ANSIBLE_LOG_PATH |
| 87 | |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 88 | # if no arg then print help and exit |
| 89 | if [ -z "$1" ] ; then |
| 90 | help |
| 91 | exit 0 |
| 92 | fi |
| 93 | |
| 94 | # we must be root |
| 95 | if [ "$(id -u)" -ne 0 ] ; then |
| 96 | echo ERROR: "I need root privileges and you are not root: $(id -nu)" >&2 |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | # if env var is set then run in docker |
| 101 | if [ -n "$ANSIBLE_DOCKER_IMAGE" ] ; then |
| 102 | exec docker run --rm \ |
| 103 | -v "${HOME}"/.ssh:/root/.ssh:rw \ |
| 104 | -v "$ANSIBLE_DIR:/ansible:ro" \ |
| 105 | -v "$ANSIBLE_DIR/application:/ansible/application:rw" \ |
Samuli Silvius | e9fca5e | 2019-03-03 13:34:16 +0200 | [diff] [blame] | 106 | -v "$ANSIBLE_DIR/certs/:/ansible/certs:rw" \ |
Petr Ospalý | 5b197fb | 2019-01-15 10:49:30 +0100 | [diff] [blame] | 107 | -v "$ANSIBLE_DIR/log/:/ansible/log:rw" \ |
| 108 | -e ANSIBLE_LOG_PATH \ |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 109 | -it "${ANSIBLE_DOCKER_IMAGE}" "$@" |
| 110 | fi |
| 111 | |
| 112 | # if not already there then unpack chroot |
| 113 | if ! [ -d "$ANSIBLE_CHROOT" ] ; then |
| 114 | if ! [ -f "$ANSIBLE_DIR"/docker/ansible_chroot.tgz ] ; then |
| 115 | echo ERROR: "Missing chroot archive: ${ANSIBLE_DIR}/ansible_chroot.tgz" >&2 |
| 116 | exit 1 |
| 117 | fi |
| 118 | |
| 119 | echo INFO: "Unpacking chroot tar into: ${ANSIBLE_CHROOT}" >&2 |
| 120 | if ! tar -C "$ANSIBLE_DIR" -xzf "$ANSIBLE_DIR"/docker/ansible_chroot.tgz ; then |
| 121 | echo ERROR: "Unpacking failed - ABORT" >&2 |
| 122 | exit 1 |
| 123 | fi |
| 124 | fi |
| 125 | |
| 126 | # run chroot |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 127 | "$ANSIBLE_DIR"/docker/run_chroot.sh \ |
| 128 | --mount rw:"${HOME}/.ssh":/root/.ssh \ |
| 129 | --mount ro:"$ANSIBLE_DIR":/ansible \ |
| 130 | --mount rw:"$ANSIBLE_DIR"/application:/ansible/application \ |
Petr Ospalý | 5b197fb | 2019-01-15 10:49:30 +0100 | [diff] [blame] | 131 | --mount rw:"$ANSIBLE_DIR"/log:/ansible/log \ |
Samuli Silvius | e9fca5e | 2019-03-03 13:34:16 +0200 | [diff] [blame] | 132 | --mount rw:"$ANSIBLE_DIR"/certs:/ansible/certs \ |
Petr Ospalý | fb01a65 | 2019-01-07 13:28:57 +0100 | [diff] [blame] | 133 | --mount ro:/etc/resolv.conf:/etc/resolv.conf \ |
| 134 | --mount ro:/etc/hosts:/etc/hosts \ |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 135 | --workdir /ansible \ |
| 136 | execute "$ANSIBLE_CHROOT" ansible-playbook "$@" |
| 137 | |
| 138 | exit 0 |