forked from cytopia/ansible-debian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
159 lines (150 loc) · 6.32 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# vim:set ft=dockerfile:
FROM debian:testing
LABEL maintainer="cytopia <cytopia@everythingcli.org>, kalos <kalos@nerdrug.org>"
RUN set -eux \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
python-apt \
python-dev \
python-jmespath \
python-pip \
python-setuptools \
sudo \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge -y --autoremove
RUN set -eux \
&& pip install wheel \
&& pip install ansible
# Add user with password-less sudo
RUN set -eux \
&& useradd -m -s /bin/bash kalos \
&& echo "kalos ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/kalos
# Copy files
COPY ./ /home/kalos/ansible
RUN set -eux \
&& chown -R kalos:kalos /home/kalos/ansible
# Switch to user
USER kalos
# Change working directory
WORKDIR /home/kalos/ansible
# Systemd cannot be checked inside Docker, so replace it with a dummy role
RUN set -eux \
&& mkdir roles/dummy \
&& sed -i'' 's/systemd-meta/dummy/g' playbook.yml
# Randomize roles to install each time the container is build (each travis run)
RUN set -eux \
&& ROLES_INSTALL="$( for d in $(/bin/ls roles/); do if [ -d roles/${d} ]; then echo $d; fi done | grep -vE '*-meta$' | sort -R )" \
&& ROLES_REMOVE="$( for d in $(/bin/ls roles/); do if [ -d roles/${d} ]; then echo $d; fi done | grep -vE '*-meta$' | sort -R )" \
\
&& ( \
echo "#!/bin/sh -eux"; \
echo; \
echo "# Ansible verbosity"; \
echo "if ! set | grep '^verbose=' >/dev/null 2>&1; then"; \
echo " verbose=\"\""; \
echo "else"; \
echo " if [ \"\${verbose}\" = \"1\" ]; then"; \
echo " verbose=\"-v\""; \
echo " elif [ \"\${verbose}\" = \"2\" ]; then"; \
echo " verbose=\"-vv\""; \
echo " elif [ \"\${verbose}\" = \"3\" ]; then"; \
echo " verbose=\"-vvv\""; \
echo " else"; \
echo " verbose=\"\""; \
echo " fi"; \
echo "fi"; \
echo; \
echo "# Ansible tagged role (only run a specific tag)"; \
echo "if ! set | grep '^tag=' >/dev/null 2>&1; then"; \
echo " tag=\"\""; \
echo "fi"; \
echo; \
echo "# Ansible random order (run roles in random order)"; \
echo "if ! set | grep '^random=' >/dev/null 2>&1; then"; \
echo " random=\"0\""; \
echo "fi"; \
echo; \
echo; \
# ---------- [1] Only run a specific tag ----------
echo "if [ \"\${tag}\" != \"\" ]; then"; \
echo " role=\"\$( echo \"\${tag}\" | sed 's/-/_/g' )\""; \
echo " # [install] (only tag)"; \
echo " ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff -t bootstrap-system-apt-repo"; \
echo " ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff -t \${tag}"; \
echo; \
echo "else"; \
echo; \
# ---------- [2] Install in random order ----------
echo " # Random"; \
echo " if [ \"\${random}\" = \"1\" ]; then"; \
echo " # [INSTALL] Bootstrap roles"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t bootstrap-system --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t bootstrap-system --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t bootstrap-python --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t bootstrap-python --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo; \
echo " # [INSTALL] Pre-defined roles (randomized)"; \
for r in ${ROLES_INSTALL}; do \
echo " if ! ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
done; \
echo; \
echo " # [INSTALL] Custom apt packages"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t apt --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t apt --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo; \
echo " # [INSTALL] Default applications"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t xdg --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t xdg --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo; \
# ---------- [3] Install in normal playbook order ----------
echo " # In order"; \
echo " else"; \
echo; \
echo " # [INSTALL] Normal playbook"; \
echo " if ! ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo " fi"; \
echo; \
echo " apt list --installed > install1.txt"; \
echo; \
\
# ---------- Installation (full 2nd round) ----------
echo " # Full install 2nd round"; \
echo " if ! ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml --limit \${MY_HOST} \${verbose} --diff"; \
echo " fi"; \
echo; \
echo " apt list --installed > install2.txt"; \
echo; \
# ---------- Validate diff ----------
echo " # Validate"; \
echo " diff install1.txt install2.txt"; \
echo; \
# ---------- Uninstallation (twice) ----------
echo " # [REMOVE] Pre-defined roles (randomized)"; \
for r in ${ROLES_REMOVE}; do \
del="$(echo $r | sed 's/-/_/g')=remove"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} -e ${del} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} -e ${del} --diff"; \
echo " fi"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} -e ${del} --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t ${r} --limit \${MY_HOST} \${verbose} -e ${del} --diff"; \
echo " fi"; \
done; \
echo; \
echo " # [REMOVE] Custom apt packages"; \
echo " if ! ansible-playbook -i inventory playbook.yml -t apt --limit \${MY_HOST} \${verbose} -e apt_state=absent --diff; then"; \
echo " ansible-playbook -i inventory playbook.yml -t apt --limit \${MY_HOST} \${verbose} -e apt_state=absent --diff"; \
echo " fi"; \
echo "fi"; \
\
) > run-tests.sh \
&& chmod +x run-tests.sh
ENTRYPOINT ["./run-tests.sh"]