-
Notifications
You must be signed in to change notification settings - Fork 147
/
Dockerfile
411 lines (395 loc) · 16.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
###########################################################
# base tools and dependencies
###########################################################
FROM ubuntu:22.04 AS base
SHELL ["/bin/bash", "-c"]
# Arguments set during docker-compose build -b --build from .env file
ARG versionVault \
versionKubectl \
versionKubelogin \
versionDockerCompose \
versionPowershell \
versionPacker \
versionGolang \
versionTerraformDocs \
versionAnsible \
versionTerrascan \
versionTfupdate \
extensionsAzureCli \
SSH_PASSWD \
TARGETARCH \
TARGETOS
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=${USER_UID}
ENV SSH_PASSWD=${SSH_PASSWD} \
USERNAME=${USERNAME} \
versionVault=${versionVault} \
versionGolang=${versionGolang} \
versionKubectl=${versionKubectl} \
versionKubelogin=${versionKubelogin} \
versionDockerCompose=${versionDockerCompose} \
versionTerraformDocs=${versionTerraformDocs} \
versionPacker=${versionPacker} \
versionPowershell=${versionPowershell} \
versionAnsible=${versionAnsible} \
extensionsAzureCli=${extensionsAzureCli} \
versionTerrascan=${versionTerrascan} \
versionTfupdate=${versionTfupdate} \
PATH="${PATH}:/opt/mssql-tools/bin:/home/vscode/.local/lib/shellspec/bin:/home/vscode/go/bin:/usr/local/go/bin" \
TF_DATA_DIR="/home/${USERNAME}/.terraform.cache" \
TF_PLUGIN_CACHE_DIR="/tf/cache" \
TF_REGISTRY_DISCOVERY_RETRY=5 \
TF_REGISTRY_CLIENT_TIMEOUT=15 \
ARM_USE_MSGRAPH=true \
LANG=en_US.UTF-8 \
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8 \
DEBIAN_FRONTEND=noninteractive \
DEBCONF_NONINTERACTIVE_SEEN=true
WORKDIR /tf/rover
COPY ./scripts/.kubectl_aliases .
COPY ./scripts/zsh-autosuggestions.zsh .
# installation common tools
RUN apt-get update && \
apt-get install -y --no-install-recommends \
apt-transport-https \
apt-utils \
bsdmainutils \
ca-certificates \
curl \
fonts-powerline \
gcc \
gettext \
git \
gpg \
gpg-agent \
jq \
less \
locales \
make \
# Networking tools
dnsutils net-tools iputils-ping traceroute \
python3-dev \
python3-pip \
rsync \
# openvpn client and ipsec tools to generate certificates
openvpn network-manager-openvpn strongswan strongswan-pki libstrongswan-extra-plugins libtss2-tcti-tabrmd0 openssh-client \
#
software-properties-common \
gosu \
sudo \
unzip \
vim \
wget \
zsh \
zip && \
#
# Create USERNAME
#
echo "Creating ${USERNAME} user..." && \
groupadd docker && \
useradd --uid $USER_UID -m -G docker ${USERNAME} && \
#
# Set the locale
locale-gen en_US.UTF-8 && \
#
# ############### APT Repositories ###################
#
# Add Microsoft key
#
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/microsoft.gpg && \
#
# Add Microsoft repository
#
gosu root apt-add-repository https://packages.microsoft.com/ubuntu/22.04/prod && \
#
# Add Docker repository
#
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor > /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg && \
echo "deb [arch=${TARGETARCH}] https://download.docker.com/linux/ubuntu focal stable" > /etc/apt/sources.list.d/docker.list && \
#
# Kubernetes repo
#
curl -fsSL https://pkgs.k8s.io/core:/stable:/v${versionKubectl}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v${versionKubectl}/deb/ /" | gosu root tee /etc/apt/sources.list.d/kubernetes.list && \
#
# Github shell
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | gosu root dd of=/etc/apt/trusted.gpg.d/githubcli-archive-keyring.gpg && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null &&\
#
apt-get update && \
apt-get install -y --no-install-recommends \
docker-ce-cli \
kubectl \
gh && \
#
# Install Docker Compose - required to rebuild the rover and dynamic terminal in VSCode
#
echo "Installing docker compose ${versionDockerCompose}..." && \
mkdir -p /usr/libexec/docker/cli-plugins/ && \
if [ ${TARGETARCH} == "amd64" ]; then \
curl -L -o /usr/libexec/docker/cli-plugins/docker-compose https://github.com/docker/compose/releases/download/v${versionDockerCompose}/docker-compose-${TARGETOS}-x86_64 ; \
else \
curl -L -o /usr/libexec/docker/cli-plugins/docker-compose https://github.com/docker/compose/releases/download/v${versionDockerCompose}/docker-compose-${TARGETOS}-aarch64 ; \
fi \
&& chmod +x /usr/libexec/docker/cli-plugins/docker-compose && \
#
# Install Helm
#
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash && \
#
# Install tflint
#
echo "Installing latest tflint ..." && \
curl -sSL -o /tmp/tflint.zip https://github.com/terraform-linters/tflint/releases/latest/download/tflint_${TARGETOS}_${TARGETARCH}.zip && \
unzip -d /usr/bin /tmp/tflint.zip && \
chmod +x /usr/bin/tflint && \
#
# Install terrascan
#
echo "Installing terrascan v${versionTerrascan} ..." && \
if [ ${TARGETARCH} == "amd64" ]; then \
curl -sSL -o terrascan.tar.gz https://github.com/tenable/terrascan/releases/download/v${versionTerrascan}/terrascan_${versionTerrascan}_Linux_x86_64.tar.gz ; \
else \
curl -sSL -o terrascan.tar.gz https://github.com/tenable/terrascan/releases/download/v${versionTerrascan}/terrascan_${versionTerrascan}_Linux_${TARGETARCH}.tar.gz ; \
fi \
&& tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && \
install terrascan /usr/local/bin && rm terrascan && \
#
# Install tfupdate
#
echo "Installing tfupdate v${versionTfupdate} ..." && \
if [ ${TARGETARCH} == "amd64" ]; then \
curl -sSL -o tfupdate.tar.gz https://github.com/minamijoyo/tfupdate/releases/download/v${versionTfupdate}/tfupdate_${versionTfupdate}_linux_amd64.tar.gz ; \
else \
curl -sSL -o tfupdate.tar.gz https://github.com/minamijoyo/tfupdate/releases/download/v${versionTfupdate}/tfupdate_${versionTfupdate}_linux_${TARGETARCH}.tar.gz ; \
fi \
&& tar -xf tfupdate.tar.gz tfupdate && rm tfupdate.tar.gz && \
install tfupdate /usr/local/bin && rm tfupdate && \
#
# Install tfsec
#
echo "Installing latest tfsec ..." && \
curl -sSL -o /bin/tfsec https://github.com/tfsec/tfsec/releases/latest/download/tfsec-${TARGETOS}-${TARGETARCH} && \
chmod +x /bin/tfsec && \
#
# Install terraform docs
#
echo "Installing terraform docs ${versionTerraformDocs}..." && \
curl -sSL -o /tmp/terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v${versionTerraformDocs}/terraform-docs-v${versionTerraformDocs}-${TARGETOS}-${TARGETARCH}.tar.gz && \
tar -zxf /tmp/terraform-docs.tar.gz --directory=/usr/bin && \
chmod +x /usr/bin/terraform-docs && \
#
# Install bash completions for git
#
echo "Installing bash completions for git" && \
mkdir -p /etc/bash_completion.d/ && \
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o /etc/bash_completion.d/git-completion.bash && \
#
# Install PowerShell via alternate method as apt not working for arm64
# https://docs.microsoft.com/en-us/powershell/scripting/install/install-other-linux?view=powershell-7.2#binary-archives
#
echo "Installing PowerShell ${versionPowershell}..." && \
if [ ${TARGETARCH} == "amd64" ]; then curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v${versionPowershell}/powershell-${versionPowershell}-${TARGETOS}-x64.tar.gz ; \
else curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v${versionPowershell}/powershell-${versionPowershell}-${TARGETOS}-${TARGETARCH}.tar.gz ; \
fi \
&& mkdir -p /opt/microsoft/powershell/7 && \
tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7 && \
chmod +x /opt/microsoft/powershell/7/pwsh && \
ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh && \
# echo "Installing PowerShell modules..." && \
# pwsh -Command Install-Module -name Az.DesktopVirtualization -Force && \
# pwsh -Command Install-Module -name Az.Resources -Force && \
#
# kubectl node shell
#
curl -L0 -o /usr/local/bin/kubectl-node_shell https://github.com/kvaps/kubectl-node-shell/raw/master/kubectl-node_shell && \
chmod +x /usr/local/bin/kubectl-node_shell && \
#
# Hashicorp Packer
#
echo "Installing Packer ${versionPacker}..." && \
curl -sSL -o /tmp/packer.zip https://releases.hashicorp.com/packer/${versionPacker}/packer_${versionPacker}_${TARGETOS}_${TARGETARCH}.zip 2>&1 && \
unzip -d /usr/bin /tmp/packer.zip && \
chmod +x /usr/bin/packer && \
rm /tmp/packer.zip && \
#
# Kubelogin
#
echo "Installing Kubelogin ${versionKubelogin}..." && \
curl -sSL -o /tmp/kubelogin.zip https://github.com/Azure/kubelogin/releases/download/v${versionKubelogin}/kubelogin-${TARGETOS}-${TARGETARCH}.zip 2>&1 && \
unzip -d /usr/ /tmp/kubelogin.zip && \
if [ ${TARGETARCH} == "amd64" ]; then \
chmod +x /usr/bin/linux_amd64/kubelogin ; \
else \
chmod +x /usr/bin/linux_arm64/kubelogin ; \
fi && \
# Hashicorp Vault
#
echo "Installing Vault ${versionVault}..." && \
curl -sSL -o /tmp/vault.zip https://releases.hashicorp.com/vault/${versionVault}/vault_${versionVault}_${TARGETOS}_${TARGETARCH}.zip 2>&1 && \
unzip -o -d /usr/bin /tmp/vault.zip && \
chmod +x /usr/bin/vault && \
setcap cap_ipc_lock=-ep /usr/bin/vault && \
rm /tmp/vault.zip && \
#
# ################# Install PIP clients ###################
#
# Install pre-commit
#
echo "Installing latest pre-commit ..." && \
pip3 install pre-commit && \
#
# Install yq
#
echo "Installing latest yq ..." && \
pip3 install yq && \
#
# Install Azure-cli
#
echo "Installing latest Azure CLI ..." && \
pip3 install azure-cli && \
az extension add --name ${extensionsAzureCli} --system && \
az extension add --name containerapp --system && \
az config set extension.use_dynamic_install=yes_without_prompt && \
#
# Install checkov
#
echo "Installing latest Checkov ..." && \
pip3 install checkov && \
#
# Install pywinrm
#
echo "Installing latest pywinrm ..." && \
pip3 install pywinrm && \
#
#
# Install Ansible
#
echo "Installing Ansible ${versionAnsible} ..." && \
pip3 install ansible-core==${versionAnsible} && \
#
#
# ################ Install apt packages ##################
# For amd64 only - as no arm64 version packages available per: https://packages.microsoft.com/ubuntu/20.04/prod/pool/main/m/mssql-tools/
if [ ${TARGETARCH} == "amd64" ]; then \
echo ACCEPT_EULA=Y apt-get install -y --no-install-recommends unixodbc mssql-tools; \
else \
echo "mssql-tools skipped as not running on amr64"; \
fi \
#
&& echo "Installing latest shellspec..." && \
curl -fsSL https://git.io/shellspec | sh -s -- --yes && \
#
# Golang
#
echo "Installing Golang ${versionGolang}..." && \
curl -sSL -o /tmp/golang.tar.gz https://go.dev/dl/go${versionGolang}.${TARGETOS}-${TARGETARCH}.tar.gz 2>&1 && \
tar -C /usr/local -xzf /tmp/golang.tar.gz && \
export PATH=$PATH:/usr/local/go/bin && \
go version && \
#
echo "Installing latest Tflint Ruleset for Azure" && \
curl -sSL -o /tmp/tflint-ruleset-azurerm.zip https://github.com/terraform-linters/tflint-ruleset-azurerm/releases/latest/download/tflint-ruleset-azurerm_${TARGETOS}_${TARGETARCH}.zip 2>&1 && \
mkdir -p /home/${USERNAME}/.tflint.d/plugins && \
mkdir -p /home/${USERNAME}/.tflint.d/config && \
echo "plugin \"azurerm\" {" > /home/${USERNAME}/.tflint.d/config/.tflint.hcl && \
echo " enabled = true" >> /home/${USERNAME}/.tflint.d/config/.tflint.hcl && \
echo "}" >> /home/${USERNAME}/.tflint.d/config/.tflint.hcl && \
unzip -d /home/${USERNAME}/.tflint.d/plugins /tmp/tflint-ruleset-azurerm.zip && \
#
# Change ownership on the plugin cache directory
mkdir /tf/cache && \
chown -R ${USERNAME}:${USERNAME} ${TF_PLUGIN_CACHE_DIR} && \
#
# Create USERNAME home folder structure
#
mkdir -p /tf/caf \
/tf/rover \
/tf/logs \
/home/${USERNAME}/.ansible \
/home/${USERNAME}/.azure \
/home/${USERNAME}/.gnupg \
/home/${USERNAME}/.packer.d \
/home/${USERNAME}/.ssh \
/home/${USERNAME}/.ssh-localhost \
/home/${USERNAME}/.terraform.logs \
/home/${USERNAME}/.terraform.cache \
/home/${USERNAME}/.terraform.cache/tfstates \
/home/${USERNAME}/.vscode-server \
/home/${USERNAME}/.vscode-server-insiders && \
chown -R ${USER_UID}:${USER_GID} /home/${USERNAME} /tf/rover /tf/caf /tf/logs && \
chmod 777 -R /home/${USERNAME} /tf/caf /tf/rover && \
chmod 700 /home/${USERNAME}/.ssh && \
echo ${USERNAME} ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/${USERNAME} && \
chmod 0440 /etc/sudoers.d/${USERNAME} && \
# for non-root user
mkdir /commandhistory && \
touch /commandhistory/.bash_history && \
chown -R ${USERNAME} /commandhistory && \
echo "set -o history" >> "/home/${USERNAME}/.bashrc" && \
echo "export HISTCONTROL=ignoredups:erasedups" >> "/home/${USERNAME}/.bashrc" && \
echo "PROMPT_COMMAND=\"${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r\"" >> "/home/${USERNAME}/.bashrc" && \
echo "[ -f /tf/rover/.kubectl_aliases ] && source /tf/rover/.kubectl_aliases" >> "/home/${USERNAME}/.bashrc" && \
echo "alias watch=\"watch \"" >> "/home/${USERNAME}/.bashrc" && \
#
# Clean-up
#
apt-get remove -y \
gcc \
python3-dev \
apt-utils && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /tmp/* && \
rm -rf /var/lib/apt/lists/* && \
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
#
# Switch to non-root ${USERNAME} context
#
USER ${USERNAME}
COPY .devcontainer/.zshrc /home/${USERNAME}/
COPY ./scripts/sshd_config /home/${USERNAME}/.ssh/sshd_config
RUN echo "Setting up OMZ environment" && \
#
# Install Oh My Zsh
#
curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash -s -- --unattended && \
chmod 700 -R /home/${USERNAME}/.oh-my-zsh && \
echo "DISABLE_UNTRACKED_FILES_DIRTY=\"true\"" >> /home/${USERNAME}/.zshrc && \
echo "alias rover=/tf/rover/rover.sh" >> /home/${USERNAME}/.bashrc && \
echo "alias rover=/tf/rover/rover.sh" >> /home/${USERNAME}/.zshrc && \
echo "alias t=/usr/bin/terraform" >> /home/${USERNAME}/.bashrc && \
echo "alias t=/usr/bin/terraform" >> /home/${USERNAME}/.zshrc && \
echo "alias k=/usr/bin/kubectl" >> /home/${USERNAME}/.zshrc && \
echo "alias k=/usr/bin/kubectl" >> /home/${USERNAME}/.bashrc && \
echo "cd /tf/caf || true" >> /home/${USERNAME}/.bashrc && \
echo "cd /tf/caf || true" >> /home/${USERNAME}/.zshrc && \
echo "[ -f /tf/rover/.kubectl_aliases ] && source /tf/rover/.kubectl_aliases" >> /home/${USERNAME}/.zshrc && \
echo "source /tf/rover/zsh-autosuggestions.zsh" >> /home/${USERNAME}/.zshrc && \
echo "alias watch=\"watch \"" >> /home/${USERNAME}/.zshrc
FROM base
ARG versionTerraform \
USERNAME=vscode \
versionRover
ENV versionRover=${versionRover} \
versionTerraform=${versionTerraform}
#
# Install Terraform
#
# Keeping this method to support alpha build installations
RUN echo "Set rover version to ${versionRover}..." && echo "Installing Terraform ${versionTerraform}..." && \
curl -sSL -o /tmp/terraform.zip "https://releases.hashicorp.com/terraform/${versionTerraform}/terraform_${versionTerraform}_${TARGETOS}_${TARGETARCH}.zip" 2>&1 && \
sudo unzip -o -d /usr/bin /tmp/terraform.zip && \
sudo chmod +x /usr/bin/terraform && \
mkdir -p "/home/${USERNAME}/.terraform.cache/plugin-cache" && \
rm /tmp/terraform.zip && \
#
echo "Set rover version to ${versionRover}..." && \
echo "${versionRover}" > /tf/rover/version.txt
RUN az config set core.login_experience_v2=false
COPY ./scripts/rover.sh ./scripts/tfstate.sh ./scripts/functions.sh ./scripts/remote.sh ./scripts/parse_command.sh ./scripts/banner.sh ./scripts/clone.sh ./scripts/walkthrough.sh ./scripts/sshd.sh ./scripts/backend.hcl.tf ./scripts/backend.azurerm.tf ./scripts/ci.sh ./scripts/cd.sh ./scripts/task.sh ./scripts/symphony_yaml.sh ./scripts/test_runner.sh ./
COPY ./scripts/ci_tasks/* ./ci_tasks/
COPY ./scripts/lib/* ./lib/
COPY ./scripts/tfcloud/* ./tfcloud/