From 2b643752c4ed7c70a9aec06ed4f01d79a6f68ef3 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 00:19:43 +0200 Subject: [PATCH 01/10] updated avd base with collections changed vscode friendly entrypoint --- avd2.2.0_cvp3.0.0_debian/Dockerfile | 84 ++++++++++++++++++++++++++ avd2.2.0_cvp3.0.0_debian/entrypoint.sh | 41 +++++++++++++ avd2.2.0_cvp3.0.0_debian/gitconfig | 39 ++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 avd2.2.0_cvp3.0.0_debian/Dockerfile create mode 100644 avd2.2.0_cvp3.0.0_debian/entrypoint.sh create mode 100755 avd2.2.0_cvp3.0.0_debian/gitconfig diff --git a/avd2.2.0_cvp3.0.0_debian/Dockerfile b/avd2.2.0_cvp3.0.0_debian/Dockerfile new file mode 100644 index 0000000..5c791aa --- /dev/null +++ b/avd2.2.0_cvp3.0.0_debian/Dockerfile @@ -0,0 +1,84 @@ +FROM python:3.9.5-slim + +# install tools permanently +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + make \ + wget \ + curl \ + less \ + git \ + zsh \ + vim \ + sudo \ + sshpass \ + git-extras \ + openssh-client \ + && rm -rf /var/lib/apt/lists/* \ + && rm -Rf /usr/share/doc && rm -Rf /usr/share/man \ + && apt-get clean + +# install docker in docker and docker-compose +RUN curl -fsSL https://get.docker.com | sh \ + && curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose \ + && chmod +x /usr/local/bin/docker-compose + +# add AVD user +RUN useradd -md /home/avd -s /bin/zsh -u 1000 avd \ + && echo 'avd ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \ + # add docker and sudo to avd group + && usermod -aG docker avd \ + && usermod -aG sudo avd +USER avd +ENV HOME=/home/avd +ENV PATH=$PATH:/home/avd/.local/bin + +WORKDIR /home/avd + +# install zsh +RUN wget --quiet https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh || true \ + && echo 'PROMPT="%{$fg[red]%}A$fg[green]%}V$fg[blue]%}D 🐳 %(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)"' >> /home/avd/.zshrc \ + && echo 'plugins=(ansible common-aliases safe-paste git jsontools history git-extras)' >> ${HOME}/.zshrc \ + && echo 'eval `ssh-agent -s`' >> $HOME/.zshrc \ + && echo 'export TERM=xterm-256color' >> $HOME/.zshrc \ + && echo "export LC_ALL=C.UTF-8" >> $HOME/.zshrc \ + && echo "export LANG=C.UTF-8" >> $HOME/.zshrc \ + && echo 'export PATH=$PATH:/home/avd/.local/bin' >> $HOME/.zshrc + +# ^ ^ ^ +# | | | The section above usually remains unchanged between releases + +# | | | The section below will be updated for every release +# V V V + +# add entrypoint script +COPY ./entrypoint.sh /bin/entrypoint.sh +RUN sudo chmod +x /bin/entrypoint.sh +# use ENTRYPOINT instead of CMD to ensure that entryscript is always executed +ENTRYPOINT [ "/bin/entrypoint.sh" ] + +# add AVD gitconfig to be used if container is not called as VScode devcontainer +COPY ./gitconfig /home/avd/gitconfig-avd-base-template + +# change this for every release +ENV _AVD_VERSION="v2.2.0" +ENV _CVP_VERSION="v3.0.0" + +# labels to be changed for every release +LABEL maintainer="Arista Ansible Team " +LABEL com.example.version="2.0.0" +LABEL vendor1="Arista" +LABEL com.example.release-date="2021-06-28" +LABEL com.example.version.is-production="False" + +# clone AVD and CVP collections +RUN _CURL=$(which curl) \ + && _GIT=$(which git) \ + && _REPO_AVD="https://github.com/aristanetworks/ansible-avd.git" \ + && _REPO_CVP="https://github.com/aristanetworks/ansible-cvp.git" \ + && ${_GIT} clone --depth 1 --branch ${_AVD_VERSION} --single-branch ${_REPO_AVD} /home/avd/ansible-avd \ + && ${_GIT} clone --depth 1 --branch ${_CVP_VERSION} --single-branch ${_REPO_CVP} /home/avd/ansible-cvp \ + && pip3 install --user --no-cache-dir -r /home/avd/ansible-avd/ansible_collections/arista/avd/requirements.txt \ + && pip3 install --user --no-cache-dir -r /home/avd/ansible-avd/ansible_collections/arista/avd/requirements-dev.txt \ + && pip3 install --user --no-cache-dir -r /home/avd/ansible-cvp/ansible_collections/arista/cvp/requirements.txt \ + && pip3 install --user --no-cache-dir -r /home/avd/ansible-cvp/ansible_collections/arista/cvp/requirements-dev.txt diff --git a/avd2.2.0_cvp3.0.0_debian/entrypoint.sh b/avd2.2.0_cvp3.0.0_debian/entrypoint.sh new file mode 100644 index 0000000..03a8f03 --- /dev/null +++ b/avd2.2.0_cvp3.0.0_debian/entrypoint.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +DOCKER_SOCKET=/var/run/docker.sock +DOCKER_GROUP=docker +USER=avd +HOME_AVD=/home/avd + +GITCFGFILE=${HOME_AVD}/.gitconfig + +# if container is called as devcontainer from VScode, .gitconfig must be present +if [ -f ${GITCFGFILE} ]; then + rm -f ${HOME_AVD}/gitconfig-avd-base-template +# if no .gitconfig created by VScode, copy AVD base template and edit +else + mv ${HOME_AVD}/gitconfig-avd-base-template ${HOME_AVD}/.gitconfig + # Update gitconfig with username and email + if [ -n "${AVD_GIT_USER}" ]; then + echo "Update gitconfig with ${AVD_GIT_USER}" + sed -i "s/USERNAME/${AVD_GIT_USER}/g" ${HOME_AVD}/.gitconfig + else + echo "Update gitconfig with default username" + sed -i "s/USERNAME/AVD Base USER/g" ${HOME_AVD}/.gitconfig + fi + if [ -n "${AVD_GIT_EMAIL}" ]; then + echo "Update gitconfig with ${AVD_GIT_EMAIL}" + sed -i "s/USER_EMAIL/${AVD_GIT_EMAIL}/g" ${HOME_AVD}/.gitconfig + else + echo "Update gitconfig with default email" + sed -i "s/USER_EMAIL/avd-base@arista.com/g" ${HOME_AVD}/.gitconfig + fi +fi + +if [ -S ${DOCKER_SOCKET} ]; then + sudo chmod 666 /var/run/docker.sock &>/dev/null +fi + +if [ ${@+True} ]; then + exec "$@" +else + exec zsh +fi diff --git a/avd2.2.0_cvp3.0.0_debian/gitconfig b/avd2.2.0_cvp3.0.0_debian/gitconfig new file mode 100755 index 0000000..f025933 --- /dev/null +++ b/avd2.2.0_cvp3.0.0_debian/gitconfig @@ -0,0 +1,39 @@ +[user] + name = USERNAME + email = USER_EMAIL +[alias] + # Generic command + rollback = reset --hard + recommit = commit --amend --no-edit + commit-unsafe = commit --no-verify + undo = reset HEAD~1 --mixed + uncommit = reset --soft HEAD~1 + + # Misc command + refresh = !git pull --rebase --prune $@ + conflicts = !git ls-files -u | cut -f 2 | sort -u + push-current = push -u origin HEAD + + # Logging & History + last = log --stat -1 HEAD + graph = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit + logs = log --oneline --decorate --color + + # In Fork model + forigin = fetch origin + fupstream = fetch upstream + fetch-all = !sh -c 'for remote in $(git remote);do git fetch remote;done' + +[core] + excludesfile = ~/.gitignore_global +[commit] + template = ~/.stCommitMsg +[pull] + rebase = true +[filter "lfs"] + clean = git-lfs clean -- %f + smudge = git-lfs smudge -- %f + process = git-lfs filter-process + required = true +[pager] + branch = false \ No newline at end of file From 4fc5fe20f80fedc1821e669f6aa2f4fa074b07f2 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 09:40:32 +0200 Subject: [PATCH 02/10] add entrypoint comments remove list of images to simplify maintenance --- README.md | 26 +------------------------- avd2.2.0_cvp3.0.0_debian/entrypoint.sh | 2 ++ 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4e1dd0f..524f7e4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ![](https://img.shields.io/badge/Arista-CVP%20Automation-blue) ![](https://img.shields.io/badge/Arista-EOS%20Automation-blue) ![GitHub](https://img.shields.io/github/license/arista-netdevops-community/docker-avd-base) ![Docker Pulls](https://img.shields.io/docker/pulls/avdteam/base) ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/avdteam/base/latest) ![Docker Image Version (tag latest semver)](https://img.shields.io/docker/v/avdteam/base/latest) + # AVD Base Image Image with all python requirements installed to then run [__Arista Validated Design__](https://github.com/aristanetworks/ansible-avd) collection with a minimal configuration overhead. It can be used to support local development using following workflow @@ -10,9 +11,6 @@ __Docker image:__ [`avdteam/base`](https://hub.docker.com/repository/docker/avdt __Table of content__ - [AVD Base Image](#avd-base-image) - [Description](#description) - - [Available Tags](#available-tags) - - [Stable version](#stable-version) - - [Deprecated](#deprecated) - [Available variables](#available-variables) - [How to leverage image](#how-to-leverage-image) - [Arista Validated Design](#arista-validated-design) @@ -26,26 +24,6 @@ __Table of content__ ## Description -### Available Tags - -- [`3.6`](3.6/Dockerfile)* -- [`3.7`](3.7/Dockerfile) -- [`3.8`](3.8/Dockerfile) / (latest) - -#### Stable version - -- `3.6-v` -- `3.7-v` -- `3.8-v` - -#### Deprecated - -- [`centos-7`](centos-7/Dockerfile) (deprecated) -- [`centos-8`](centos-8/Dockerfile) (deprecated) -`` - -Current image used in AVD development: `avdteam/base:3.6` - ### Available variables These variables are used in `CMD` to customize container content using [`-e` option of docker](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) cli: @@ -59,8 +37,6 @@ These variables are used in `CMD` to customize container content using [`-e` opt - `AVD_GIT_EMAIL`: Email to configure in .gitconfig file. - Can be set with `AVD_GIT_EMAIL=$(git config --get user.email)` - - To see how to customize your container with these options, you can refer to [How to install ansible and Python requirements page](docs/run-options.md) ## How to leverage image diff --git a/avd2.2.0_cvp3.0.0_debian/entrypoint.sh b/avd2.2.0_cvp3.0.0_debian/entrypoint.sh index 03a8f03..9af996b 100644 --- a/avd2.2.0_cvp3.0.0_debian/entrypoint.sh +++ b/avd2.2.0_cvp3.0.0_debian/entrypoint.sh @@ -34,8 +34,10 @@ if [ -S ${DOCKER_SOCKET} ]; then sudo chmod 666 /var/run/docker.sock &>/dev/null fi +# execute command from docker cli if any if [ ${@+True} ]; then exec "$@" +# otherwise just enter zsh else exec zsh fi From 50a8d951713c054eb9fa9942064603e0f2282368 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 09:42:47 +0200 Subject: [PATCH 03/10] update readme to remove variables that are not used in entrypoint script any more --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 524f7e4..b8e96b9 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,6 @@ __Table of content__ These variables are used in `CMD` to customize container content using [`-e` option of docker](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) cli: -- `AVD_REQUIREMENTS`: Path to a `requirements.txt` to install during container startup. -- `AVD_ANSIBLE`: Ansible version to install in container when booting up -- `AVD_UID`: set __uid__ for avd user in container. -- `AVD_GID`: set __gid__ for avd user in container. - `AVD_GIT_USER`: Username to configure in .gitconfig file. - Can be set with `AVD_GIT_USER=$(git config --get user.name)` - `AVD_GIT_EMAIL`: Email to configure in .gitconfig file. From 6f1cbf9f70adece674173c5b1b91457f8596724f Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 09:47:47 +0200 Subject: [PATCH 04/10] fix tab vs spaces in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b8e96b9..92cd2e1 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,8 @@ You can also configure your shell with an alias to make it easy to start contain ```shell # Configure alias in bashrc alias avd-shell='docker run -it --rm \ - -v ${PWD}/:/projects \ - avdteam/base:latest zsh' + -v ${PWD}/:/projects \ + avdteam/base:latest zsh' # Run alias command $ avd-shell From 4fa2a9c287e39abbffce277ae96f050f3d9deb74 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 09:50:56 +0200 Subject: [PATCH 05/10] removing install req section from docs as they are part of the image now --- docs/image-info.md | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/docs/image-info.md b/docs/image-info.md index 720890f..1d650cb 100644 --- a/docs/image-info.md +++ b/docs/image-info.md @@ -12,34 +12,6 @@ __Docker image:__ [`avdteam/base`](https://hub.docker.com/repository/docker/avdt - Requirements from [arista.cvp](https://github.com/aristanetworks/ansible-cvp) - Requirements from [arista.avd](https://github.com/aristanetworks/ansible-avd) -### Installed during container startup - -- [`CMD`](./../_template/entrypoint.sh) configured to support ENV configuration. - -CMD script example: - -```shell -#!/bin/bash - -# Install specific requirement file -if [ ! -z "${AVD_REQUIREMENTS}" ]; then - if [ -f ${AVD_REQUIREMENTS} ]; then - echo "Install new requirements from ${AVD_REQUIREMENTS}" - pip3 install --user -r ${AVD_REQUIREMENTS} - else - echo "Requirement file not found, skipping..." - fi -fi - -# Install specific ANSIBLE version -if [ ! -z "${AVD_ANSIBLE}" ]; then - echo "Install ansible with version ${AVD_ANSIBLE}" - pip3 install --user ansible==${AVD_ANSIBLE} -fi - -exec /bin/zsh -``` - ## Build local images You can use this repository to build your own version to test lib upgrade or a new flavor. it creates an image based on: From e0f767dddd3607cf774ad0088e89439e3e477323 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 10:01:24 +0200 Subject: [PATCH 06/10] remove custom ansible version ref from docs --- docs/run-options.md | 49 --------------------------------------------- 1 file changed, 49 deletions(-) diff --git a/docs/run-options.md b/docs/run-options.md index 5d310bc..0a7a5f8 100644 --- a/docs/run-options.md +++ b/docs/run-options.md @@ -32,52 +32,3 @@ Python 3.8.3 ➜ /projects git:(devel) ✗ ansible --version ansible 2.9.6 ``` - -## Run image for testing with custom ansible version - -### Run container with a specific version of ansible using __Makefile__ command - -```shell -$ make FLAVOR=3.8 ANSIBLE_VERSION=2.9.9 run - -Requirement file not found, skipping... -Install ansible with version 2.9.9 -WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. -Collecting ansible==2.9.9 -[...] -Successfully installed ansible-2.9.9 - -Agent pid 52 -➜ /projects git:(devel) ✗ python --version -Python 3.8.3 -➜ /projects git:(devel) ✗ python3 --version -Python 3.8.3 -➜ /projects git:(devel) ✗ ansible --version -ansible 2.9.9 -``` - -> Only available in current repository - -### Run container with a specific version of ansible using native __docker__ command - -```shell -$ docker run --rm -it -v ${HOME}/arista-ansible/docker-avd-base/:/projects \ - -e AVD_REQUIREMENTS=NONE \ - -e AVD_ANSIBLE=2.9.9 \ - -v /etc/hosts:/etc/hosts avdteam/base:3.8 - -Requirement file not found, skipping... -Install ansible with version 2.9.9 -WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. -Collecting ansible==2.9.9 -[...] -Successfully installed ansible-2.9.9 - -Agent pid 52 -➜ /projects git:(devel) ✗ python --version -Python 3.8.3 -➜ /projects git:(devel) ✗ python3 --version -Python 3.8.3 -➜ /projects git:(devel) ✗ ansible --version -ansible 2.9.9 -``` \ No newline at end of file From 56556c816c828eba20393c2946d2cb2ae3e89836 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 10:03:10 +0200 Subject: [PATCH 07/10] no need to install avd env as it's part of container --- docs/avd-vscode-docker.md | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/docs/avd-vscode-docker.md b/docs/avd-vscode-docker.md index 16f877f..b53a193 100644 --- a/docs/avd-vscode-docker.md +++ b/docs/avd-vscode-docker.md @@ -7,7 +7,6 @@ This how-to explains how to leverage __avdteam/base__ image as shell under [VSco __Table of content__ - [How to use AVD image with VSCODE](#how-to-use-avd-image-with-vscode) - [About](#about) - - [Configure AVD environment](#configure-avd-environment) - [Devcontainer with docker image](#devcontainer-with-docker-image) - [Requirements](#requirements) - [Configure devcontainer](#configure-devcontainer) @@ -17,38 +16,6 @@ __Table of content__ - [Configure devcontainer](#configure-devcontainer-1) - [Open content in container](#open-content-in-container-1) -## Configure AVD environment - -Before running all code with a container, we have to download current AVD ecosystem with the following command: - -- On Linux or Macos: - -```shell -$ curl -fsSL https://get.avd.sh | sh -``` - -- On Windows: - -```shell -PS C:\Users\User> Invoke-WebRequest -OutFile install.sh -Uri \ -https://raw.githubusercontent.com/arista-netdevops-community/avd-install/master/install.sh - -PS C:\Users\User> bash install.sh -``` - -This script git clone AVD and CVP collection as well as example repository to get started with example. - -```shell -$ cd ansible-arista - -$ ls -al -total 24 --rw-rw-r-- 1 tom tom 2517 Jul 20 09:09 Makefile -drwxrwxr-x 8 tom tom 4096 Jul 20 09:09 ansible-avd -drwxrwxr-x 8 tom tom 4096 Jul 20 09:09 ansible-avd-cloudvision-demo -drwxrwxr-x 9 tom tom 4096 Jul 20 09:09 ansible-cvp -``` - ## Devcontainer with docker image ### Requirements From 6fbccaebf6d1aa15eb2e5cd3674245cd1aa2d7d1 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 10:08:46 +0200 Subject: [PATCH 08/10] remove "terminal.integrated.shell.linux" as it's deprecated --- docs/avd-vscode-docker.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/avd-vscode-docker.md b/docs/avd-vscode-docker.md index b53a193..0d7b055 100644 --- a/docs/avd-vscode-docker.md +++ b/docs/avd-vscode-docker.md @@ -49,7 +49,6 @@ Copy following content to `devcontainer.json`: "image": "avdteam/base:3.6", "settings": { - "terminal.integrated.shell.linux": "/bin/zsh", "python.linting.enabled": true, "python.linting.pylintEnabled": true, "python.linting.flake8Path": "/root/.local/bin/flake8", From bb804ca2ddb52211914412f96858c65928ff92a3 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 10:16:47 +0200 Subject: [PATCH 09/10] update devcontainer.json and mention customisation with postCreateCommand and bind mount --- docs/avd-vscode-docker.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/avd-vscode-docker.md b/docs/avd-vscode-docker.md index 0d7b055..95de853 100644 --- a/docs/avd-vscode-docker.md +++ b/docs/avd-vscode-docker.md @@ -77,13 +77,21 @@ Copy following content to `devcontainer.json`: "ms-python.vscode-pylance", "tht13.python" ], + "containerEnv": { "ANSIBLE_CONFIG": "./ansible.cfg" - } + }, + + "remoteUser": "avd" } ``` +If any custom container modifications are required: + +1. Use "postCreateCommand" to install any missing requirements or custom Ansible version. +2. Use bind mount to mount custom ansible-avd and ansible-cvp collections instead of default `/home/avd/ansible-avd` or `/home/avd/ansible-cvp` + ### Open content in container After you configured `.devcontainer/devcontainer.json` correctly, you can open VScode and start local container with following actions: From 93d1859c0eef2ec61dabbde233bc81f9b0b0f531 Mon Sep 17 00:00:00 2001 From: ankudinov Date: Thu, 1 Jul 2021 11:04:18 +0200 Subject: [PATCH 10/10] add projects dir to dockerfile --- avd2.2.0_cvp3.0.0_debian/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/avd2.2.0_cvp3.0.0_debian/Dockerfile b/avd2.2.0_cvp3.0.0_debian/Dockerfile index 5c791aa..d887837 100644 --- a/avd2.2.0_cvp3.0.0_debian/Dockerfile +++ b/avd2.2.0_cvp3.0.0_debian/Dockerfile @@ -82,3 +82,6 @@ RUN _CURL=$(which curl) \ && pip3 install --user --no-cache-dir -r /home/avd/ansible-avd/ansible_collections/arista/avd/requirements-dev.txt \ && pip3 install --user --no-cache-dir -r /home/avd/ansible-cvp/ansible_collections/arista/cvp/requirements.txt \ && pip3 install --user --no-cache-dir -r /home/avd/ansible-cvp/ansible_collections/arista/cvp/requirements-dev.txt + +# if not running as VScode devcontainer, start in projects +WORKDIR /home/avd/projects