Skip to content

Commit

Permalink
Updates to devcontainer (#369)
Browse files Browse the repository at this point in the history
* updated devcontainer

New devcontainer has EDA updates plus since we no longer
are using Docker as our molecule backend, this replaces the
need to have Docker in Docker.

* add wheel
  • Loading branch information
carlosmmatos authored Jul 21, 2023
1 parent 84a9b02 commit 572f184
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 438 deletions.
44 changes: 11 additions & 33 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
# Note: You can use any Debian/Ubuntu based image you want.
FROM willhallonline/ansible:2.12.2-ubuntu-20.04
FROM mcr.microsoft.com/devcontainers/base:bullseye

# [Option] Install zsh
ARG INSTALL_ZSH="true"
# [Option] Upgrade OS packages to their latest versions
ARG UPGRADE_PACKAGES="false"
# [Option] Enable non-root Docker access in container
ARG ENABLE_NONROOT_DOCKER="true"
# [Option] Use the OSS Moby Engine instead of the licensed Docker Engine
ARG USE_MOBY="true"
# [Option] Engine/CLI Version
ARG DOCKER_VERSION="latest"

# Enable new "BUILDKIT" mode for Docker CLI
ENV DOCKER_BUILDKIT=1

# Install needed packages and setup non-root user. Use a separate RUN statement to add your
# own dependencies. A user of "automatic" attempts to reuse an user ID if one already exists.
ARG USERNAME=automatic
ARG USER_UID=1000
ARG USER_GID=$USER_UID
COPY library-scripts/*.sh /tmp/library-scripts/
COPY requirements.txt /tmp/requirements.txt
RUN apt-get update \
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
# Use Docker script from script library to set things up
&& /bin/bash /tmp/library-scripts/docker-in-docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "${USERNAME}" "${USE_MOBY}" "${DOCKER_VERSION}" \
&& apt-get install -y libssl-dev libffi-dev python3-dev python3-pip \
# Clean up
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/

RUN pip3 install -r /tmp/requirements.txt
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
&& apt-get install -y libssl-dev libffi-dev python3-dev python3-pip openjdk-17-jdk vim \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

VOLUME [ "/var/lib/docker" ]
# Install Ansible + Dependencies
RUN python3 -m pip install -r /tmp/requirements.txt

# Setting the ENTRYPOINT to docker-init.sh will start up the Docker Engine
# inside the container "overrideCommand": false is set in devcontainer.json.
# The script will also execute CMD if you need to alter startup behaviors.
ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
CMD [ "sleep", "infinity" ]

# [Optional] Uncomment this section to install additional OS packages.
Expand Down
83 changes: 50 additions & 33 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
# CS Ansible Docker in Docker
# Ansible Development Container

*Create child containers _inside_ a container, independent from the host's docker instance. Installs Docker extension in the container along with needed CLIs.*
This is a development container for working with Ansible. It provides an environment for running Ansible playbooks and managing infrastructure.

## Description
## Getting Started

This container provides a solid foundation for developing and testing Ansible content using the DinD approach to facilitate molecule testing via Docker. More information relating to DinD in regards to VScode devcontainers can be found [here](https://github.com/microsoft/vscode-dev-containers/tree/main/containers/docker-in-docker).
To get started, follow these steps:

The main purpose of this container is to provide the necessary bits needed to develop and test Ansible content. What's included:
* Container is based on: `willhallonline/ansible:2.12.2-ubuntu-20.04`
> You can substitute the version of Ansible if needed. You can view more tags [here](https://github.com/willhallonline/docker-ansible#immutable-images).
* The following mount provides a location for the Ansible Collection:
* `/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon`
* The following VScode extensions are installed:
* redhat.ansible
* shd101wyy.markdown-preview-enhanced
* ms-python.python
* redhat.vscode-yaml
* ms-azuretools.vscode-docker
* Oh-my-zsh is installed and enabled by default
> View the [devcontainer.json](./devcontainer.json) file for more details
1. Install [Visual Studio Code](https://code.visualstudio.com/)
2. Install the [Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension for Visual Studio Code.
3. Clone this repository to your local machine.
4. Open the repository in Visual Studio Code.
5. When prompted, click on the green "Open in Container" button in the lower right corner of Visual Studio Code.

### Usage and Caveats
## Container Configuration

When opening up a folder with a `.devcontainer` directory, VScode should prompt you if you would like to open the workspace in the remote container. Otherwise you can also do the following:
The container is based on the `mcr.microsoft.com/devcontainers/base:bullseye` image. It has the following configuration:

* press <kbd>F1</kbd> or <kbd>shift+command+p</kbd> and run **Remote-Containers: Reopen Folder in Container** or **Remote-Containers: Rebuild Container** to start using the definition.
- Installs zsh (if enabled in devcontainer.json)
- Upgrades OS packages to their latest versions (if enabled in devcontainer.json)
- Enables non-root Docker access in the container (if enabled in devcontainer.json)
- Uses the OSS Moby Engine instead of the licensed Docker Engine (if enabled in devcontainer.json)
- Uses the specified Docker version (if specified in devcontainer.json)
- Installs the required packages and sets up the non-root user
- Installs Ansible and its dependencies from the `requirements.txt` file
- Mounts the local workspace folder to `/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon` in the container

>When openinig up for the first time, it might take a few minutes for the container to be built.
## Shell Customization

##### Caveats
* After the container is built, some extensions and settings might not take effect right away. Restart the remote-connection:
* Click the bottom left <kbd>Dev Container: Ansible DinD</kbd> and select `Reopen Folder Locally`
* The reopen again in the remote-container.
The container is configured to use zsh as the default shell. It includes the following customizations:

* Molecule testing only supports using the Docker provider. If you need to test against Windows systems, you will need to use your localhost + vagrant.
* If you need to run tests with `ansible-test` suite:
1. Navigate to `/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon`
2. Execute the following for a sanity test:
```bash
$ ansible-test sanity -v
```
> Do not use --docker as it will not work in this configuration.
- Files with the extension `.yml` in the `defaults`, `group_vars`, `host_vars`, `vars`, `tasks`, `handlers`, `meta`, `roles`, and `playbooks` folders are associated with the `jinja-yaml` language mode.
- Files named `hosts` or `inventory` in the `ansible` folder are associated with the `ini` language mode.
- The default profile for the integrated terminal is set to `zsh` for Linux.

## Extensions

The following extensions are installed in the container:

- shd101wyy.markdown-preview-enhanced
- ms-python.python
- redhat.vscode-yaml
- redhat.ansible
- GitHub.copilot
- bierner.github-markdown-preview
- GitHub.vscode-pull-request-github

## Port Forwarding

Port forwarding is not enabled by default in the container. If you need to forward ports, you can add them to the `forwardPorts` section in the devcontainer.json file.

## Post-Create Command

You can run additional commands after the container is created by adding them to the `postCreateCommand` section in the devcontainer.json file.

Currently, the following commands are run after the container is created:

```bash
pre-commit install --install-hooks
```
92 changes: 52 additions & 40 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,66 @@
{
"name": "Ansible DinD",
"dockerFile": "Dockerfile",
"runArgs": [
"--init",
"--privileged"
],
"name": "Ansible Development Container",
"build": {
"dockerfile": "Dockerfile",
"args": {
"INSTALL_ZSH": "true",
"UPGRADE_PACKAGES": "false",
"INSTALL_AZURE_CLI": "false",
"INSTALL_DOCKER": "false",
"NODE_VERSION": "none"
}
},
"runArgs": ["--init"],
"mounts": [
// [Optional] Anisble Collections: Uncomment if you want to mount your local .ansible/collections folder.
"source=${localWorkspaceFolder},target=/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon,type=bind,consistency=cached",
"target=/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon/.devcontainer,type=volume",
"source=dind-var-lib-docker,target=/var/lib/docker,type=volume"
"source=${localWorkspaceFolder},target=/usr/share/ansible/collections/ansible_collections/crowdstrike/falcon,type=bind,consistency=cached"
],
"overrideCommand": false,
// Set *default* container specific settings.json values on container create.
"settings": {
"files.associations": {
// Ansible
"**/defaults/**/*.yml": "jinja-yaml",
"**/group_vars/**/*": "jinja-yaml",
"**/host_vars/**/*": "jinja-yaml",
"**/vars/**/*.yml": "jinja-yaml",
"**/tasks/**/*.yml": "ansible",
"**/handlers/*.yml": "ansible",
"**/meta/*.yml": "ansible",
"**/roles/**/*.yml": "ansible",
"**/playbooks/**/*.yml": "ansible",
"**/ansible/**/hosts": "ini",
"**/ansible/**/inventory": "ini",
"ansible.cfg": "ini"

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"shd101wyy.markdown-preview-enhanced",
"ms-python.python",
"redhat.vscode-yaml",
"redhat.ansible",
"GitHub.copilot",
"bierner.github-markdown-preview",
"GitHub.vscode-pull-request-github"
]
},
"terminal.integrated.defaultProfile.linux": "zsh",
"[jinja-yaml]": {
"editor.tabSize": 2
},
"[python]": {
"editor.tabSize": 4
"settings": {
"files.associations": {
// Ansible
"**/defaults/**/*.yml": "jinja-yaml",
"**/group_vars/**/*": "jinja-yaml",
"**/host_vars/**/*": "jinja-yaml",
"**/vars/**/*.yml": "jinja-yaml",
"**/tasks/**/*.yml": "ansible",
"**/handlers/*.yml": "ansible",
"**/meta/*.yml": "ansible",
"**/roles/**/*.yml": "ansible",
"**/playbooks/**/*.yml": "ansible",
"**/ansible/**/hosts": "ini",
"**/ansible/**/inventory": "ini",
"ansible.cfg": "ini"
},
"terminal.integrated.defaultProfile.linux": "zsh",
"[jinja-yaml]": {
"editor.tabSize": 2
},
"[python]": {
"editor.tabSize": 4
}
}
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"shd101wyy.markdown-preview-enhanced",
"ms-python.python",
"redhat.vscode-yaml",
"ms-azuretools.vscode-docker",
"redhat.ansible"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pre-commit install --install-hooks",
"postCreateCommand": "ansible-galaxy collection install ansible.windows",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
10 changes: 7 additions & 3 deletions .devcontainer/library-scripts/common-debian.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if [ "${PACKAGES_ALREADY_INSTALLED}" != "true" ]; then
libkrb5-3 \
libgssapi-krb5-2 \
libicu[0-9][0-9] \
liblttng-ust0 \
liblttng-ust[0-9] \
libstdc++6 \
zlib1g \
locales \
Expand Down Expand Up @@ -205,7 +205,7 @@ else
fi
fi

# Add add sudo support for non-root user
# Add sudo support for non-root user
if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
chmod 0440 /etc/sudoers.d/$USERNAME
Expand Down Expand Up @@ -258,6 +258,9 @@ if [ -z "$(git config --get core.editor)" ] && [ -z "${GIT_EDITOR}" ]; then
fi
fi
# Ansible EDA
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
EOF
)"

Expand Down Expand Up @@ -287,7 +290,7 @@ cat << 'EOF' > /usr/local/bin/systemctl
#!/bin/sh
set -e
if [ -d "/run/systemd/system" ]; then
exec /bin/systemctl/systemctl "$@"
exec /bin/systemctl "$@"
else
echo '\n"systemd" is not running in this container due to its overhead.\nUse the "service" command to start services instead. e.g.: \n\nservice --status-all'
fi
Expand Down Expand Up @@ -389,6 +392,7 @@ if [ "${INSTALL_ZSH}" = "true" ]; then
-c receive.fsck.zeroPaddedFilemode=ignore \
"https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1
echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
echo -e "# Ansible EDA\nexport JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ${user_rc_file}
sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="codespaces"/g' ${user_rc_file}

mkdir -p ${oh_my_install_dir}/custom/themes
Expand Down
Loading

0 comments on commit 572f184

Please sign in to comment.