Skip to content

Commit

Permalink
Add devcontainer scripts on create and post start (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
doosuu authored Apr 12, 2024
1 parent d46d53d commit 1dd4e2d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 8 deletions.
1 change: 0 additions & 1 deletion components/cores/uapp-java/.project-creation/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"files": [
".pre-commit-config.yaml",
".project-creation/templates/.devcontainer/devcontainer.json",
".project-creation/templates/.vscode/launch.json"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def is_file(path: str):
def test_files_in_place():
os.chdir(os.environ["VELOCITAS_APP_ROOT"])

# devcontainer
is_dir(".devcontainer")
is_file(".devcontainer/devcontainer.json")

# vscode project
is_dir(".vscode")
is_file(".vscode/launch.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"ghcr.io/devcontainers-contrib/features/pre-commit:2": {
"version": "3.5.0"
},
"ghcr.io/devcontainers/features/common-utils:2.3.2": {},
"ghcr.io/devcontainers/features/common-utils:2.3.2": {}
},
// 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": "",
"onCreateCommand": "bash .devcontainer/scripts/onCreateCommand.sh",
"postStartCommand": "bash .devcontainer/scripts/postStartCommand.sh",
// Configure tool-specific properties.
"customizations": {
"vscode": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0

# exit when any command fails
set -e

# restart Docker connection if in Codespaces
# Workaround according to https://github.com/devcontainers/features/issues/671#issuecomment-1701754897
if [ "${CODESPACES}" = "true" ]; then
sudo pkill dockerd && sudo pkill containerd
/usr/local/share/docker-init.sh
fi

echo "#######################################################"
echo "### Run VADF Lifecycle Management ###"
echo "#######################################################"
# needed to get rid of old leftovers
sudo rm -rf ~/.velocitas
velocitas init
velocitas sync

sudo chmod +x .devcontainer/scripts/*.sh
sudo chown -R $(whoami) $HOME

if [ "${CODESPACES}" = "true" ]; then
echo "#######################################################"
echo "### Setup Access to Codespaces ###"
echo "#######################################################"

# Remove the default credential helper
sudo sed -i -E 's/helper =.*//' /etc/gitconfig

# Add one that just uses secrets available in the Codespace
git config --global credential.helper '!f() { sleep 1; echo "username=${GITHUB_USER}"; echo "password=${MY_GH_TOKEN}"; }; f'
fi

git config --global --add safe.directory "*"

echo "#######################################################"
echo "### Install Dependencies ###"
echo "#######################################################"
velocitas exec build-system install

echo "#######################################################"
echo "### VADF package status ###"
echo "#######################################################"
velocitas upgrade --dry-run

# Don't let container creation fail if lifecycle management fails
echo "Done!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0

echo "#######################################################"
echo "### Auto-Upgrade CLI ###"
echo "#######################################################"

ROOT_DIRECTORY=$( realpath "$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/../.." )
DESIRED_VERSION=$(cat $ROOT_DIRECTORY/.velocitas.json | jq .cliVersion | tr -d '"')

# Get installed CLI version
INSTALLED_VERSION=v$(velocitas --version | sed -E 's/velocitas-cli\/(\w+.\w+.\w+).*/\1/')

if [ "$DESIRED_VERSION" = "$INSTALLED_VERSION" ]; then
echo "> Already up to date!"
exit 0
else
echo "> Checking upgrade to $DESIRED_VERSION"
fi

AUTHORIZATION_HEADER=""
if [ "${GITHUB_API_TOKEN}" != "" ]; then
AUTHORIZATION_HEADER="-H \"Authorization: Bearer ${GITHUB_API_TOKEN}\""
fi

if [ "$DESIRED_VERSION" = "latest" ]; then
CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/latest
else
CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/tags/${DESIRED_VERSION}
fi

CLI_RELEASES=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${AUTHORIZATION_HEADER} \
${CLI_RELEASES_URL})

res=$?
if test "$res" != "0"; then
echo "the curl command failed with exit code: $res"
exit 0
fi

DESIRED_VERSION_TAG=$(echo ${CLI_RELEASES} | jq -r .name)

if [ "$DESIRED_VERSION_TAG" = "null" ] || [ "$DESIRED_VERSION_TAG" = "" ]; then
echo "> Can't find desired Velocitas CLI version: $DESIRED_VERSION. Skipping Auto-Upgrade."
exit 0
fi

if [ "$DESIRED_VERSION_TAG" != "$INSTALLED_VERSION" ]; then
echo "> Upgrading CLI..."
if [[ $(arch) == "aarch64" ]]; then
CLI_ASSET_NAME=velocitas-linux-arm64
else
CLI_ASSET_NAME=velocitas-linux-x64
fi
CLI_INSTALL_PATH=/usr/bin/velocitas
CLI_DOWNLOAD_URL="https://github.com/eclipse-velocitas/cli/releases/download/${DESIRED_VERSION_TAG}/${CLI_ASSET_NAME}"

echo "> Downloading Velocitas CLI from ${CLI_DOWNLOAD_URL}"
sudo curl -s -L ${CLI_DOWNLOAD_URL} -o "${CLI_INSTALL_PATH}"
sudo chmod +x "${CLI_INSTALL_PATH}"
else
echo "> Up to date!"
fi

echo "> Using CLI: $(velocitas --version)"
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"compatibleCores": [
"uprotocol-core-java"
],
"files": [
{
"src": "components/extensions/devcontainer-setup-java/src",
"dst": "."
}
],
"mandatory": true
}
]
Expand Down

0 comments on commit 1dd4e2d

Please sign in to comment.