Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit c773aac

Browse files
authored
Merge pull request #472 from microsoft/clantz/ansible-common-script
Update azure-ansible to use common scripts
2 parents 2353106 + d3fb398 commit c773aac

File tree

10 files changed

+366
-67
lines changed

10 files changed

+366
-67
lines changed
Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,54 @@
1-
# Pick any base image, but if you select node, skip installing node. 😊
2-
FROM debian:9
1+
# You can pick any Debian/Ubuntu-based image. 😊
2+
FROM mcr.microsoft.com/vscode/devcontainers/base:buster
33

4-
# This Dockerfile adds a non-root user with sudo access. Update the “remoteUser” property in
5-
# devcontainer.json to use it. More info: https://aka.ms/vscode-remote/containers/non-root-user.
4+
COPY library-scripts/*.sh /tmp/library-scripts/
5+
6+
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
7+
ARG INSTALL_ZSH="true"
8+
ARG UPGRADE_PACKAGES="false"
69
ARG USERNAME=vscode
710
ARG USER_UID=1000
811
ARG USER_GID=$USER_UID
12+
RUN apt-get update \
13+
&& export DEBIAN_FRONTEND=noninteractive \
14+
&& bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
15+
&& apt-get install -y libssl-dev libffi-dev python3-dev python3-pip \
16+
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
917

10-
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
18+
# Optional installs. Use a separate RUN statement to add your own dependencies.
19+
ARG INSTALL_AZURE_CLI="true"
20+
ARG INSTALL_DOCKER="true"
21+
ARG INSTALL_NODE="true"
22+
ARG NODE_VERSION="lts/*"
23+
ENV NVM_DIR=/usr/local/share/nvm
24+
ENV NVM_SYMLINK_CURRENT=true \
25+
PATH=${NVM_DIR}/current/bin:${PATH}
1126
RUN apt-get update \
1227
&& export DEBIAN_FRONTEND=noninteractive \
13-
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
14-
#
15-
# Verify git, required tools installed
16-
&& apt-get install -y \
17-
git \
18-
openssh-client \
19-
less \
20-
iproute2 \
21-
curl \
22-
procps \
23-
unzip \
24-
apt-transport-https \
25-
ca-certificates \
26-
gnupg-agent \
27-
software-properties-common \
28-
lsb-release 2>&1 \
29-
#
30-
# [Optional] Install Node.js for Azure Cloud Shell support
31-
# Change the "lts/*" in the two lines below to pick a different version
32-
&& curl -so- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 2>&1 \
33-
&& /bin/bash -c "source $HOME/.nvm/nvm.sh \
34-
&& nvm install lts/* \
35-
&& nvm alias default lts/*" 2>&1 \
36-
#
37-
# [Optional] For local testing instead of cloud shell
38-
# Install Docker CE CLI.
39-
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | apt-key add - 2>/dev/null \
40-
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" \
41-
&& apt-get update \
42-
&& apt-get install -y docker-ce-cli \
43-
#
44-
# [Optional] For local testing instead of cloud shell
45-
# Install the Azure CLI
46-
&& echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/azure-cli.list \
47-
&& curl -sL https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 2>/dev/null \
48-
&& apt-get update \
49-
&& apt-get install -y azure-cli \
50-
#
51-
# Install Ansible
52-
&& apt-get install -y libssl-dev libffi-dev python-dev python-pip \
53-
&& pip install ansible[azure] \
54-
#
55-
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
56-
&& groupadd --gid $USER_GID $USERNAME \
57-
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
58-
# [Optional] Add sudo support for the non-root user
59-
&& apt-get install -y sudo \
60-
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
61-
&& chmod 0440 /etc/sudoers.d/$USERNAME \
62-
#
63-
# Clean up
64-
&& apt-get autoremove -y \
65-
&& apt-get clean -y \
66-
&& rm -rf /var/lib/apt/lists/*
28+
# Install Azure CLI
29+
&& if [ "${INSTALL_AZURE_CLI}" = "true" ]; then \
30+
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/azure-cli.list \
31+
&& curl -sL https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 2>/dev/null \
32+
&& apt-get update \
33+
&& apt-get install -y azure-cli; \
34+
fi \
35+
# Install Node.js
36+
&& if [ "${INSTALL_AZURE_CLI}" = "true" ]; then bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "${NODE_VERSION}" "${USERNAME}"; fi \
37+
# Install Docker CLI
38+
&& if [ "${INSTALL_DOCKER}" = "true" ]; then \
39+
bash /tmp/library-scripts/docker-debian.sh "true" "/var/run/docker-host.sock" "/var/run/docker.sock" "${USERNAME}"; \
40+
else \
41+
echo '#!/bin/bash\n"$@"' > /usr/local/share/docker-init.sh \
42+
&& chmod +x /usr/local/share/docker-init.sh; \
43+
fi \
44+
&& rm -rf /var/lib/apt/lists/* /tmp/library-scripts
45+
46+
# Install Ansible
47+
RUN pip3 install ansible[azure]
48+
49+
ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
50+
CMD [ "sleep", "infinity" ]
6751

6852
# [Optional] Uncomment this section to install additional OS packages.
6953
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
7054
# && apt-get -y install --no-install-recommends <your-package-list-here>
71-
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
{
22
"name": "Azure Ansible",
3-
"dockerFile": "Dockerfile",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"args": {
6+
"INSTALL_AZURE_CLI": "true",
7+
"INSTALL_DOCKER": "true",
8+
"INSTALL_NODE": "true"
9+
}
10+
},
411
"mounts": [
512
// [Optional] Anisble Collections: Uncomment if you want to mount your local .ansible/collections folder.
613
// "source=${localEnv:HOME}${localEnv:USERPROFILE}/.ansible/collections,target=/root/.ansible/collections,type=bind,consistency=cached",
7-
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
14+
"source=/var/run/docker.sock,target=/var/run/docker-host.sock,type=bind"
815
],
16+
917
// Set *default* container specific settings.json values on container create.
1018
"settings": {
1119
"terminal.integrated.shell.linux": "/bin/bash"
1220
},
21+
1322
// Add the IDs of extensions you want installed when the container is created.
1423
"extensions": [
1524
"vscoss.vscode-ansible",
@@ -19,10 +28,10 @@
1928
]
2029
// Use 'forwardPorts' to make a list of ports inside the container available locally.
2130
// "forwardPorts": [],
31+
2232
// Use 'postCreateCommand' to run commands after the container is created.
2333
// "postCreateCommand": "ansible --version",
24-
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
25-
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
34+
2635
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
2736
// "remoteUser": "vscode"
2837
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Warning: Folder contents may be replaced
2+
3+
The contents of this folder will be automatically replaced with a file of the same name in the repository's [script-library folder](https://github.com/microsoft/vscode-dev-containers/tree/master/script-library) whenever the repository is packaged.
4+
5+
To retain your edits, move the file to a different location. You may also delete the files if they are not needed.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
# Syntax: ./common-debian.sh <install zsh flag> <username> <user UID> <user GID> <upgrade packages flag>
8+
9+
set -e
10+
11+
INSTALL_ZSH=${1:-"true"}
12+
USERNAME=${2:-"$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"}
13+
USER_UID=${3:-1000}
14+
USER_GID=${4:-1000}
15+
UPGRADE_PACKAGES=${5:-"true"}
16+
17+
if [ "$(id -u)" -ne 0 ]; then
18+
echo 'Script must be run a root. Use sudo or set "USER root" before running the script.'
19+
exit 1
20+
fi
21+
22+
# Treat a user name of "none" as root
23+
if [ "${USERNAME}" = "none" ] || [ "${USERNAME}" = "root" ]; then
24+
USERNAME=root
25+
USER_UID=0
26+
USER_GID=0
27+
fi
28+
29+
# Ensure apt is in non-interactive to avoid prompts
30+
export DEBIAN_FRONTEND=noninteractive
31+
32+
# Install apt-utils to avoid debconf warning
33+
apt-get -y install --no-install-recommends apt-utils 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )
34+
35+
# Get to latest versions of all packages
36+
if [ "${UPGRADE_PACKAGES}" = "true" ]; then
37+
apt-get -y upgrade --no-install-recommends
38+
fi
39+
40+
# Install common developer tools and dependencies
41+
apt-get -y install --no-install-recommends \
42+
git \
43+
openssh-client \
44+
less \
45+
iproute2 \
46+
procps \
47+
curl \
48+
wget \
49+
unzip \
50+
nano \
51+
jq \
52+
lsb-release \
53+
ca-certificates \
54+
apt-transport-https \
55+
dialog \
56+
gnupg2 \
57+
libc6 \
58+
libgcc1 \
59+
libgssapi-krb5-2 \
60+
libicu[0-9][0-9] \
61+
liblttng-ust0 \
62+
libstdc++6 \
63+
zlib1g \
64+
locales
65+
66+
# Ensure at least the en_US.UTF-8 UTF-8 locale is available.
67+
# Common need for both applications and things like the agnoster ZSH theme.
68+
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
69+
locale-gen
70+
71+
# Install libssl1.1 if available
72+
if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
73+
apt-get -y install --no-install-recommends libssl1.1
74+
fi
75+
76+
# Install appropriate version of libssl1.0.x if available
77+
LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
78+
if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
79+
if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
80+
# Debian 9
81+
apt-get -y install --no-install-recommends libssl1.0.2
82+
elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
83+
# Ubuntu 18.04, 16.04, earlier
84+
apt-get -y install --no-install-recommends libssl1.0.0
85+
fi
86+
fi
87+
88+
# Create or update a non-root user to match UID/GID - see https://aka.ms/vscode-remote/containers/non-root-user.
89+
if id -u $USERNAME > /dev/null 2>&1; then
90+
# User exists, update if needed
91+
if [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
92+
groupmod --gid $USER_GID $USERNAME
93+
usermod --gid $USER_GID $USERNAME
94+
fi
95+
if [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
96+
usermod --uid $USER_UID $USERNAME
97+
fi
98+
else
99+
# Create user
100+
groupadd --gid $USER_GID $USERNAME
101+
useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME
102+
fi
103+
104+
# Add add sudo support for non-root user
105+
apt-get install -y sudo
106+
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
107+
chmod 0440 /etc/sudoers.d/$USERNAME
108+
109+
# Ensure ~/.local/bin is in the PATH for root and non-root users for bash. (zsh is later)
110+
echo "export PATH=\$PATH:\$HOME/.local/bin" | tee -a /root/.bashrc >> /home/$USERNAME/.bashrc
111+
chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc
112+
113+
# Optionally install and configure zsh
114+
if [ "$INSTALL_ZSH" = "true" ] && [ ! -d "/root/.oh-my-zsh" ]; then
115+
apt-get install -y zsh
116+
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
117+
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
118+
cp -R /root/.oh-my-zsh /home/$USERNAME
119+
cp /root/.zshrc /home/$USERNAME
120+
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
121+
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
122+
fi
123+

0 commit comments

Comments
 (0)