Skip to content

Commit b3abb47

Browse files
doosuuDennis Meister
andauthored
feature: Fast-lane upgrade of CLI to latest version (#31)
* feature: Fast-lane upgrade of CLI to latest version * Add integration test Signed-off-by: Dennis Meister <[email protected]> --------- Signed-off-by: Dennis Meister <[email protected]> Co-authored-by: Dennis Meister <[email protected]>
1 parent 65a932a commit b3abb47

File tree

11 files changed

+183
-4
lines changed

11 files changed

+183
-4
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ jobs:
103103
repository: eclipse-velocitas/vehicle-app-${{ matrix.language }}-template
104104
path: test/${{ matrix.language }}/repo
105105

106+
- name: Install requirements
107+
run: pip install -r test/requirements.txt
108+
106109
- name: Prepare .velocitas.json for integration test
107110
run: |
108111
COMMIT_REF=$GITHUB_SHA
@@ -138,6 +141,11 @@ jobs:
138141
cd test/${{ matrix.language }}/repo
139142
velocitas sync
140143
144+
- name: Test for CLI Auto-Upgrade
145+
run: |
146+
cd test/${{ matrix.language }}/repo
147+
pytest -s ../../integration/test_poststart.py
148+
141149
- name: Reset .velocitas.json to latest template state
142150
run: |
143151
cd test/${{ matrix.language }}/repo
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
# Copyright (c) 2023 Robert Bosch GmbH
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Apache License, Version 2.0 which is available at
6+
# https://www.apache.org/licenses/LICENSE-2.0.
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
# SPDX-License-Identifier: Apache-2.0
15+
16+
echo "#######################################################"
17+
echo "### Auto-Upgrade CLI ###"
18+
echo "#######################################################"
19+
20+
ROOT_DIRECTORY=$( realpath "$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/../.." )
21+
DESIRED_VERSION=$(cat $ROOT_DIRECTORY/.velocitas.json | jq .cliVersion | tr -d '"')
22+
23+
# Get installed CLI version
24+
INSTALLED_VERSION=v$(velocitas --version | sed -E 's/velocitas-cli\/(\w+.\w+.\w+).*/\1/')
25+
26+
if [ "$DESIRED_VERSION" = "$INSTALLED_VERSION" ]; then
27+
echo "> Already up to date!"
28+
exit 0
29+
else
30+
echo "> Checking upgrade to $DESIRED_VERSION"
31+
fi
32+
33+
AUTHORIZATION_HEADER=""
34+
if [ "${GITHUB_API_TOKEN}" != "" ]; then
35+
AUTHORIZATION_HEADER="-H \"Authorization: Bearer ${GITHUB_API_TOKEN}\""
36+
fi
37+
38+
if [ "$DESIRED_VERSION" = "latest" ]; then
39+
CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/latest
40+
else
41+
CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/tags/${DESIRED_VERSION}
42+
fi
43+
44+
CLI_RELEASES=$(curl -s -L \
45+
-H "Accept: application/vnd.github+json" \
46+
-H "X-GitHub-Api-Version: 2022-11-28" \
47+
${AUTHORIZATION_HEADER} \
48+
${CLI_RELEASES_URL})
49+
50+
res=$?
51+
if test "$res" != "0"; then
52+
echo "the curl command failed with exit code: $res"
53+
exit 0
54+
fi
55+
56+
DESIRED_VERSION_TAG=$(echo ${CLI_RELEASES} | jq -r .name)
57+
58+
if [ "$DESIRED_VERSION_TAG" = "null" ] || [ "$DESIRED_VERSION_TAG" = "" ]; then
59+
echo "> Can't find desired Velocitas CLI version: $DESIRED_VERSION. Skipping Auto-Upgrade."
60+
exit 0
61+
fi
62+
63+
if [ "$DESIRED_VERSION_TAG" != "$INSTALLED_VERSION" ]; then
64+
echo "> Upgrading CLI..."
65+
if [[ $(arch) == "aarch64" ]]; then
66+
CLI_ASSET_NAME=velocitas-linux-arm64
67+
else
68+
CLI_ASSET_NAME=velocitas-linux-x64
69+
fi
70+
CLI_INSTALL_PATH=/usr/bin/velocitas
71+
CLI_DOWNLOAD_URL="https://github.com/eclipse-velocitas/cli/releases/download/${DESIRED_VERSION_TAG}/${CLI_ASSET_NAME}"
72+
73+
echo "> Downloading Velocitas CLI from ${CLI_DOWNLOAD_URL}"
74+
sudo curl -s -L ${CLI_DOWNLOAD_URL} -o "${CLI_INSTALL_PATH}"
75+
sudo chmod +x "${CLI_INSTALL_PATH}"
76+
else
77+
echo "> Up to date!"
78+
fi
79+
80+
echo "> Using CLI: $(velocitas --version)"

setup/src/cpp/common/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@
135135
// Use 'forwardPorts' to make a list of ports inside the container available locally.
136136
// "forwardPorts": [],
137137
// Use 'onCreateCommand' to run commands when the container is created.
138-
"onCreateCommand": "bash .devcontainer/scripts/postCreateCommand.sh",
138+
"onCreateCommand": "bash .devcontainer/scripts/onCreateCommand.sh",
139+
"postStartCommand": "bash .devcontainer/scripts/postStartCommand.sh",
139140
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
140141
"remoteUser": "vscode"
141142
}
File renamed without changes.
File renamed without changes.

setup/src/python/common/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
]
9090
}
9191
},
92-
"onCreateCommand": "bash .devcontainer/scripts/postCreateCommand.sh",
92+
"onCreateCommand": "bash .devcontainer/scripts/onCreateCommand.sh",
93+
"postStartCommand": "bash .devcontainer/scripts/postStartCommand.sh",
9394
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
9495
"remoteUser": "vscode"
9596
}
File renamed without changes.

test/cpp/.velocitas.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"githubRepoId": "eclipse-velocitas/vehicle-app-cpp-template",
1313
"appExecutionPlatforms": "linux/amd64",
1414
"generatedModelPath": "./app/vehicle_model"
15-
}
15+
},
16+
"cliVersion": "v0.5.5"
1617
}

test/integration/test_poststart.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright (c) 2023 Robert Bosch GmbH
2+
#
3+
# This program and the accompanying materials are made available under the
4+
# terms of the Apache License, Version 2.0 which is available at
5+
# https://www.apache.org/licenses/LICENSE-2.0.
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
# SPDX-License-Identifier: Apache-2.0
14+
15+
import json
16+
import os
17+
import platform
18+
from re import Pattern, compile, search
19+
from subprocess import PIPE, Popen, check_output
20+
21+
older_cli_version = "v0.5.4"
22+
23+
24+
def get_cli_asset_name() -> str:
25+
platform_arch = platform.machine()
26+
cli_arch = "x64"
27+
if platform_arch == "aarch64":
28+
cli_arch = "arm64"
29+
return f"velocitas-linux-{cli_arch}"
30+
31+
32+
def download_older_cli_version():
33+
cli_asset_name = get_cli_asset_name()
34+
cli_install_path = "/usr/bin/velocitas"
35+
cli_download_url = (
36+
"https://github.com/eclipse-velocitas/cli/"
37+
f"releases/download/{older_cli_version}/{cli_asset_name}"
38+
)
39+
40+
print(
41+
f"Downloading CLI version {older_cli_version}"
42+
"from {cli_download_url} to {cli_install_path}"
43+
)
44+
download_cli = f"sudo curl -s -L {cli_download_url} -o {cli_install_path}"
45+
chmod = f"sudo chmod +x {cli_install_path}"
46+
47+
cli_download_process = Popen(download_cli, stdout=PIPE, shell=True)
48+
cli_download_process.wait()
49+
Popen(chmod, stdout=PIPE, shell=True)
50+
51+
52+
def get_cli_version():
53+
velocitas_version_output = check_output(["velocitas", "--version"]).decode()
54+
search_velocitas_version_regex: Pattern[str] = compile(
55+
r"velocitas-cli\/(\w+.\w+.\w+).*"
56+
)
57+
cli_version = search(
58+
search_velocitas_version_regex, velocitas_version_output
59+
).group(1)
60+
return f"v{cli_version}"
61+
62+
63+
def test_post_start_auto_upgrade_cli():
64+
download_older_cli_version()
65+
# This should be the right check
66+
# But since we do not have a proper version output
67+
# on previous CLI releases we need to have a workaround
68+
# assert get_cli_version() == older_cli_version
69+
assert get_cli_version() == "v0.0.0"
70+
71+
post_create_script_path = os.path.join(
72+
os.getcwd(),
73+
".devcontainer",
74+
"scripts",
75+
"postStartCommand.sh",
76+
)
77+
78+
post_create_script_process = Popen(
79+
f"{post_create_script_path}", stdout=PIPE, shell=True
80+
)
81+
82+
post_create_script_process.wait()
83+
84+
velocitas_json = open(os.path.join(os.getcwd(), ".velocitas.json"))
85+
data = json.load(velocitas_json)
86+
assert data["cliVersion"] == get_cli_version()

test/python/.velocitas.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"appManifestPath": "app/AppManifest.json",
1212
"githubRepoId": "eclipse-velocitas/vehicle-app-python-template",
1313
"generatedModelPath": "./gen/vehicle_model"
14-
}
14+
},
15+
"cliVersion": "v0.5.5"
1516
}

0 commit comments

Comments
 (0)