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" |
| 28 | |
| 29 | |
| 30 | # |
| 31 | # functions |
| 32 | # |
| 33 | |
| 34 | help() |
| 35 | { |
| 36 | echo " |
| 37 | NAME: |
| 38 | ${script_name} - wrapper for ansible-playbook command |
| 39 | |
| 40 | DESCRIPTION: |
| 41 | Run ansible playbook (or other command if it is there) inside a docker |
| 42 | container or a chroot environment. |
| 43 | |
| 44 | By default the chroot is used because it has less dependencies and no |
| 45 | service needs to be run (provided that chroot command is installed). |
| 46 | |
| 47 | Docker support is kept for compatibility reasons. |
| 48 | |
| 49 | To run ansible docker image you must set environment variable: |
| 50 | ANSIBLE_DOCKER_IMAGE |
| 51 | |
| 52 | So this wrapper can know by which name you have built the included |
| 53 | Dockerfile and also to trigger this different behaviour. |
| 54 | |
| 55 | For example: |
| 56 | ANSIBLE_DOCKER_IMAGE=ansible |
| 57 | |
| 58 | USAGE: |
| 59 | ./${script_name} |
| 60 | This help |
| 61 | |
| 62 | ./${script_name} <args> |
| 63 | Run ansible-playbook command inside a chroot |
| 64 | |
| 65 | ANSIBLE_DOCKER_IMAGE=<docker-image> ./${script_name} <args> |
| 66 | Run ansible-playbook command inside a docker container |
| 67 | |
| 68 | REQUIREMENTS: |
| 69 | For the optimal usage your system should support overlay mount. Which |
| 70 | should be available on any recent kernel at least couple of years back. |
| 71 | |
| 72 | Another requirement is the 'unshare' utility which is part of 'util-linux' |
| 73 | package and also is part of system for couple of years already. |
| 74 | |
| 75 | The last is 'chroot' command itself and that is also part of system |
| 76 | basically everywhere. |
| 77 | " |
| 78 | } |
| 79 | |
| 80 | |
| 81 | # |
| 82 | # run playbook |
| 83 | # |
| 84 | |
| 85 | # if no arg then print help and exit |
| 86 | if [ -z "$1" ] ; then |
| 87 | help |
| 88 | exit 0 |
| 89 | fi |
| 90 | |
| 91 | # we must be root |
| 92 | if [ "$(id -u)" -ne 0 ] ; then |
| 93 | echo ERROR: "I need root privileges and you are not root: $(id -nu)" >&2 |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # if env var is set then run in docker |
| 98 | if [ -n "$ANSIBLE_DOCKER_IMAGE" ] ; then |
| 99 | exec docker run --rm \ |
| 100 | -v "${HOME}"/.ssh:/root/.ssh:rw \ |
| 101 | -v "$ANSIBLE_DIR:/ansible:ro" \ |
| 102 | -v "$ANSIBLE_DIR/application:/ansible/application:rw" \ |
| 103 | -v "$ANSIBLE_DIR/certs/:/certs:rw" \ |
| 104 | -it "${ANSIBLE_DOCKER_IMAGE}" "$@" |
| 105 | fi |
| 106 | |
| 107 | # if not already there then unpack chroot |
| 108 | if ! [ -d "$ANSIBLE_CHROOT" ] ; then |
| 109 | if ! [ -f "$ANSIBLE_DIR"/docker/ansible_chroot.tgz ] ; then |
| 110 | echo ERROR: "Missing chroot archive: ${ANSIBLE_DIR}/ansible_chroot.tgz" >&2 |
| 111 | exit 1 |
| 112 | fi |
| 113 | |
| 114 | echo INFO: "Unpacking chroot tar into: ${ANSIBLE_CHROOT}" >&2 |
| 115 | if ! tar -C "$ANSIBLE_DIR" -xzf "$ANSIBLE_DIR"/docker/ansible_chroot.tgz ; then |
| 116 | echo ERROR: "Unpacking failed - ABORT" >&2 |
| 117 | exit 1 |
| 118 | fi |
| 119 | fi |
| 120 | |
| 121 | # run chroot |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 122 | "$ANSIBLE_DIR"/docker/run_chroot.sh \ |
| 123 | --mount rw:"${HOME}/.ssh":/root/.ssh \ |
| 124 | --mount ro:"$ANSIBLE_DIR":/ansible \ |
| 125 | --mount rw:"$ANSIBLE_DIR"/application:/ansible/application \ |
| 126 | --mount rw:"$ANSIBLE_DIR"/certs:/certs \ |
Petr Ospalý | fb01a65 | 2019-01-07 13:28:57 +0100 | [diff] [blame] | 127 | --mount ro:/etc/resolv.conf:/etc/resolv.conf \ |
| 128 | --mount ro:/etc/hosts:/etc/hosts \ |
Petr Ospalý | 22c7039 | 2018-12-19 15:07:22 +0100 | [diff] [blame] | 129 | --workdir /ansible \ |
| 130 | execute "$ANSIBLE_CHROOT" ansible-playbook "$@" |
| 131 | |
| 132 | exit 0 |