blob: 3bc56b3c9c62a445ed0525482472a92c1cc1bbe1 [file] [log] [blame]
Petr Ospalý22c70392018-12-19 15:07:22 +01001#!/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
22set -e
23
24script_path=$(readlink -f "$0")
25script_name=$(basename "$script_path")
26ANSIBLE_DIR=$(dirname "$script_path")
27ANSIBLE_CHROOT="${ANSIBLE_DIR}/ansible_chroot"
Petr Ospalý5b197fb2019-01-15 10:49:30 +010028ANSIBLE_LOG_PATH="/ansible/log/ansible-$(date +%Y.%m.%d-%H%M%S).log"
Petr Ospalý22c70392018-12-19 15:07:22 +010029
30
31#
32# functions
33#
34
35help()
36{
37 echo "
38NAME:
39 ${script_name} - wrapper for ansible-playbook command
40
41DESCRIPTION:
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
59USAGE:
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
69REQUIREMENTS:
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ý5b197fb2019-01-15 10:49:30 +010086export ANSIBLE_LOG_PATH
87
Petr Ospalý22c70392018-12-19 15:07:22 +010088# if no arg then print help and exit
89if [ -z "$1" ] ; then
90 help
91 exit 0
92fi
93
94# we must be root
95if [ "$(id -u)" -ne 0 ] ; then
96 echo ERROR: "I need root privileges and you are not root: $(id -nu)" >&2
97 exit 1
98fi
99
100# if env var is set then run in docker
101if [ -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 Silviuse9fca5e2019-03-03 13:34:16 +0200106 -v "$ANSIBLE_DIR/certs/:/ansible/certs:rw" \
Petr Ospalý5b197fb2019-01-15 10:49:30 +0100107 -v "$ANSIBLE_DIR/log/:/ansible/log:rw" \
108 -e ANSIBLE_LOG_PATH \
Petr Ospalý22c70392018-12-19 15:07:22 +0100109 -it "${ANSIBLE_DOCKER_IMAGE}" "$@"
110fi
111
112# if not already there then unpack chroot
113if ! [ -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
124fi
125
126# run chroot
Petr Ospalý22c70392018-12-19 15:07:22 +0100127"$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ý5b197fb2019-01-15 10:49:30 +0100131 --mount rw:"$ANSIBLE_DIR"/log:/ansible/log \
Samuli Silviuse9fca5e2019-03-03 13:34:16 +0200132 --mount rw:"$ANSIBLE_DIR"/certs:/ansible/certs \
Petr Ospalýfb01a652019-01-07 13:28:57 +0100133 --mount ro:/etc/resolv.conf:/etc/resolv.conf \
134 --mount ro:/etc/hosts:/etc/hosts \
Petr Ospalý22c70392018-12-19 15:07:22 +0100135 --workdir /ansible \
136 execute "$ANSIBLE_CHROOT" ansible-playbook "$@"
137
138exit 0