Skip to content

Commit

Permalink
Fix: Remove devcontainer mounts to address error 16 with molecule (#3541
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ankudinov authored Feb 8, 2024
1 parent aea8072 commit 4dd92cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
10 changes: 4 additions & 6 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{
"name": "AVD Development",
"mounts": [
"source=${localWorkspaceFolder}/ansible_collections/arista/avd,target=/home/avd/.ansible/collections/ansible_collections/arista/avd,type=bind"
],
"image": "ghcr.io/aristanetworks/avd/dev:python3.11",
// The AVD collection can also be installed from any git branch.
// Comment mount section if this is the case.
// Uncomment the section below if it's required
//
// "containerEnv": {
// "AVD_GITHUB_REPO": "aristanetworks/avd",
// "AVD_BRANCH_NAME": "devel"
// },
// Run entrypoint script manually as it's ignored by dev container CLI otherwise.
// The dev entrypoint is used to install Ansible collections and requirements, as they are not included with the dev version.
// "true" is required to exit "onCreateCommand" without entering ZSH.
"onCreateCommand": "/bin/entrypoint.sh true"
// "true" is required to exit "postStartCommand" without entering ZSH.
// the script must be executed ONLY if Ansible is not installed
"postStartCommand": "/bin/entrypoint.sh true"
}
62 changes: 35 additions & 27 deletions containers/dev/.devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,44 @@ 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

# 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
# only install collections if ansible binary is missing
if ! [ -z "$(command -v ansible)" ]; then

# 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
# 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 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
fi

# if ansible installation failed for whatever reason - raise an error
if ! [ -z "$(command -v ansible)" ]; then
echo "ERROR: Failed to install Ansible and collections." >&2
exit 1
fi
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)
fi

# Execute command from docker cli if any.
if [ ${@+True} ]; then
Expand Down

0 comments on commit 4dd92cb

Please sign in to comment.