Skip to content

Commit

Permalink
Feat(containers): switch to pyavd and editable install for container …
Browse files Browse the repository at this point in the history
…build (#4087)
  • Loading branch information
ankudinov authored Jun 10, 2024
1 parent 64a618a commit 18d2df7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/container_build_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
matrix:
container_name: [base]
python_version: ["3.9", "3.10", "3.11"]
python_version: ["3.10", "3.11", "3.12"]
include:
- python_version: "3.11"
container_tags: latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/container_build_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
matrix:
container_name: [dev]
python_version: ["3.9", "3.10", "3.11"]
python_version: ["3.10", "3.11", "3.12"]
include:
- python_version: "3.11"
container_tags: latest
Expand Down
16 changes: 6 additions & 10 deletions .github/workflows/container_build_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,13 @@ jobs:
exit 1
fi
- name: Find ansible-core version 🔥
id: find-ansible-core-version
run: |
ANSIBLE_CORE_VERSION=$(cat ansible_collections/arista/avd/requirements-dev.txt| grep ansible-core)
echo "ansible_core_version=$ANSIBLE_CORE_VERSION" >> $GITHUB_OUTPUT
- name: Find ansible install location
id: find-ansible-install-location
- name: Find ansible and pyavd install location
id: find-install-locations
run: |
PYAVD_INSTALL_LOCATION="git+https://github.com/${{ github.repository }}.git@${{ github.ref_name }}#subdirectory=python-avd"
ANSIBLE_INSTALL_LOCATION="git+https://github.com/${{ github.repository }}.git#/ansible_collections/arista/avd/,${{ github.ref_name }}"
echo "ansible_install_location=$ANSIBLE_INSTALL_LOCATION" >> $GITHUB_OUTPUT
echo "pyavd_install_location=$PYAVD_INSTALL_LOCATION" >> $GITHUB_OUTPUT
# This check is temporarily deactivated, will be rewised in later PRs
#
Expand Down Expand Up @@ -176,8 +172,8 @@ jobs:
env:
FROM_IMAGE: ${{ steps.generate-from-parameters.outputs.from_image }}
FROM_VARIANT: ${{ steps.generate-from-parameters.outputs.from_variant }}
ANSIBLE_INSTALL_LOCATION: ${{ steps.find-ansible-install-location.outputs.ansible_install_location }}
ANSIBLE_CORE_VERSION: ${{ steps.find-ansible-core-version.outputs.ansible_core_version }}
ANSIBLE_INSTALL_LOCATION: ${{ steps.find-install-locations.outputs.ansible_install_location }}
PYAVD_INSTALL_LOCATION: ${{ steps.find-install-locations.outputs.pyavd_install_location }}
USERNAME: ${{ inputs.username }}
UID: ${{ inputs.user_id }}
GID: ${{ inputs.group_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/container_build_universal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
container_name: [universal]
python_version: ["3.9", "3.10", "3.11"]
python_version: ["3.10", "3.11", "3.12"]
include:
- python_version: "3.11"
container_tags: latest
Expand Down
32 changes: 8 additions & 24 deletions containers/dev/.devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
#!/bin/bash

USERNAME=$(whoami)
AVD_COLLECTION_PATH="${HOME}/.ansible/collections/ansible_collections/arista/avd"
CONTAINER_WORKSPACE=$(git rev-parse --show-toplevel)
CONTAINER_WSF_AVD_PATH=${CONTAINER_WORKSPACE}/ansible_collections/arista/avd

# only install collections if ansible binary is missing
if [ -z "$(command -v ansible)" ]; then

# if collection is already mounted, it will be used to install the requirements
# there is no need to install AVD collection for this case as it's already mounted to the correct location
if [ -f ${AVD_COLLECTION_PATH}/requirements.txt ] && [ -f ${AVD_COLLECTION_PATH}/requirements-dev.txt ] ; then
sudo chown -R ${USERNAME} ${HOME}/.ansible # make sure mounted path is owned by container user and not root
ANSIBLE_CORE_VERSION=$(cat ${AVD_COLLECTION_PATH}/requirements-dev.txt | grep ansible-core)
# if env variables are set - use git
elif ! [ -z "${AVD_GITHUB_REPO}" ] && ! [ -z "${AVD_BRANCH_NAME}" ]; then
ANSIBLE_CORE_VERSION=$(curl -s https://raw.githubusercontent.com/${AVD_GITHUB_REPO}/${AVD_BRANCH_NAME}/ansible_collections/arista/avd/requirements-dev.txt | grep ansible-core)
if ! [ -z "${AVD_GITHUB_REPO}" ] && ! [ -z "${AVD_BRANCH_NAME}" ]; then
AVD_INSTALL_PATH="git+https://github.com/${AVD_GITHUB_REPO}.git#/ansible_collections/arista/avd/,${AVD_BRANCH_NAME}"
# In some cases AVD can not be correctly mounted, for ex. when running dev container as Codespace
# In that case if collection is available in the container workspace, it will be installed from there
PYAVD_INSTALL_LOCATION="git+https://github.com/${AVD_GITHUB_REPO}.git@${AVD_BRANCH_NAME}#subdirectory=python-avd"
pip install "pyavd[ansible] @ ${PYAVD_INSTALL_LOCATION}"
ansible-galaxy collection install --force ${AVD_INSTALL_PATH}
# otherwise install requirements and collection from container workspace
elif [ -f ${CONTAINER_WSF_AVD_PATH}/requirements.txt ] && [ -f ${CONTAINER_WSF_AVD_PATH}/requirements-dev.txt ] ; then
ANSIBLE_CORE_VERSION=$(cat ${CONTAINER_WSF_AVD_PATH}/requirements-dev.txt | grep ansible-core)
AVD_INSTALL_PATH="${CONTAINER_WSF_AVD_PATH}/"
fi

# install ansible core and requirements
if ! [ -z "${ANSIBLE_CORE_VERSION}" ]; then
pip3 install "${ANSIBLE_CORE_VERSION}"
if ! [ -z "${AVD_INSTALL_PATH}" ]; then
ansible-galaxy collection install --force ${AVD_INSTALL_PATH}
else
# if collection was mounted and not installed - add requirements
ansible-galaxy collection install -r ${AVD_COLLECTION_PATH}/collections.yml
fi
pip3 install -r ${AVD_COLLECTION_PATH}/requirements.txt -r ${AVD_COLLECTION_PATH}/requirements-dev.txt
# use editable install for requirements
pip install -r ${CONTAINER_WSF_AVD_PATH}/requirements-dev.txt -r ${CONTAINER_WSF_AVD_PATH}/requirements.txt
ansible-galaxy collection install --force ${CONTAINER_WSF_AVD_PATH}
fi

# if ansible installation failed for whatever reason - raise an error
Expand Down
5 changes: 2 additions & 3 deletions containers/universal/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ ARG FROM_VARIANT

FROM ${FROM_IMAGE}:${FROM_VARIANT}

ARG ANSIBLE_CORE_VERSION
ARG PYAVD_INSTALL_LOCATION
ARG ANSIBLE_INSTALL_LOCATION
ARG USERNAME

# Switch user to given USERNAME otherwise Ansible will be installed as root.
USER ${USERNAME}
ENV PATH=$PATH:/home/${USERNAME}/.local/bin
# Install Ansible AVD collection.
RUN pip3 install "${ANSIBLE_CORE_VERSION}" \
RUN pip install "pyavd[ansible] @ ${PYAVD_INSTALL_LOCATION}" \
&& ansible-galaxy collection install ${ANSIBLE_INSTALL_LOCATION} \
&& pip3 install -r /home/${USERNAME}/.ansible/collections/ansible_collections/arista/avd/requirements.txt \
# install community.general to support callback plugins in ansible.cfg, etc.
&& ansible-galaxy collection install community.general
1 change: 1 addition & 0 deletions containers/universal/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"FROM_IMAGE": "${localEnv:FROM_IMAGE}",
"FROM_VARIANT": "${localEnv:FROM_VARIANT}",
"ANSIBLE_INSTALL_LOCATION": "${localEnv:ANSIBLE_INSTALL_LOCATION}",
"PYAVD_INSTALL_LOCATION": "${localEnv:PYAVD_INSTALL_LOCATION}",
"ANSIBLE_CORE_VERSION": "${localEnv:ANSIBLE_CORE_VERSION}",
"USERNAME": "${localEnv:USERNAME}"
}
Expand Down

0 comments on commit 18d2df7

Please sign in to comment.