Skip to content

Commit

Permalink
Feat: Avd install for codespaces (#3476)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankudinov authored Jan 16, 2024
1 parent 182516f commit fc39ade
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
8 changes: 0 additions & 8 deletions .devcontainer/Dockerfile

This file was deleted.

42 changes: 25 additions & 17 deletions containers/dev/.devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#!/bin/bash

AVD_DEV_REQ_FILE="${HOME}/.ansible/collections/ansible_collections/arista/avd/requirements-dev.txt"
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

# Install ansible if not yet installed and avd collection is locally mounted.
ansible --version &> /dev/null || if [ -f $AVD_DEV_REQ_FILE ]; then
sudo chown -R ${USERNAME} ${HOME}/.ansible
ANSIBLE_CORE_VERSION=$(cat ${AVD_DEV_REQ_FILE}| grep ansible-core)
pip3 install "${ANSIBLE_CORE_VERSION}"
ansible-galaxy collection install -r ${HOME}/.ansible/collections/ansible_collections/arista/avd/collections.yml
pip3 install -r ${HOME}/.ansible/collections/ansible_collections/arista/avd/requirements.txt
pip3 install -r ${HOME}/.ansible/collections/ansible_collections/arista/avd/requirements-dev.txt
# 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)
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
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 based on any AVD git branch (or its fork).
# The ANSIBLE_INSTALL_LOCATION_FORMAT must be "git+https://github.com/${AVD_GITHUB_REPO}.git#/ansible_collections/arista/avd/,${AVD_BRANCH_NAME}".
# AVD_GITHUB_REPO and AVD_BRANCH_NAME must be defined for ANSIBLE_INSTALL_LOCATION_FORMAT to be crafted successfully.
ansible --version &> /dev/null || if ! [ -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)
# install ansible core and requirements
if ! [ -z "${ANSIBLE_CORE_VERSION}" ]; then
pip3 install "${ANSIBLE_CORE_VERSION}"
ansible-galaxy collection install git+https://github.com/${AVD_GITHUB_REPO}.git#/ansible_collections/arista/avd/,${AVD_BRANCH_NAME}
pip3 install -r ${HOME}/.ansible/collections/ansible_collections/arista/avd/requirements.txt
pip3 install -r ${HOME}/.ansible/collections/ansible_collections/arista/avd/requirements-dev.txt
if ! [ -z "${AVD_INSTALL_PATH}" ]; then
ansible-galaxy collection install ${AVD_INSTALL_PATH}
fi
ansible-galaxy collection install -r ${AVD_COLLECTION_PATH}/collections.yml
pip3 install -r ${AVD_COLLECTION_PATH}/requirements.txt -r ${AVD_COLLECTION_PATH}/requirements-dev.txt
fi

# if ansible installation failed for whatever reason - raise an error
ansible --version &> /dev/null || (echo "ERROR: Failed to install Ansible and collections." >&2; exit 1)

# Execute command from docker cli if any.
Expand Down

0 comments on commit fc39ade

Please sign in to comment.