Skip to content

Commit ae63f45

Browse files
committed
feat: add more docker scripts
With this commit the support for project development with Docker becomes a first class citizen.
1 parent dd86b02 commit ae63f45

File tree

4 files changed

+227
-73
lines changed

4 files changed

+227
-73
lines changed

scripts/docker/install_docker.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#! /usr/bin/env bash
2+
3+
# Below steps were copied from https://docs.docker.com/engine/install/ubuntu/#install-using-the-convenience-script
4+
# Following repository installation method
5+
sudo apt update
6+
sudo apt install apt-transport-https ca-certificates curl software-properties-common
7+
8+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
9+
echo \
10+
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
11+
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
12+
13+
sudo apt-get update
14+
sudo apt install docker-ce docker-ce-cli containerd.io
15+
16+
# Create a docker group
17+
sudo groupadd docker
18+
sudo usermod -aG docker "${USER}"
19+
newgrp docker
20+
su -s "${USER}"
21+
22+
# Start docker at boot
23+
sudo systemctl enable docker.service
24+
sudo systemctl enable containerd.service
25+
26+
echo "Docker installation completed successfully."
27+
echo "Please log out or reboot your system to apply the changes."

scripts/docker/run_container.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/env bash
2+
# Usage scripts/enter_docker_environment.sh [image_type]
3+
#
4+
# This script enters the docker environment suitable for project development.
5+
#
6+
# If no argument is given, the script will use the `dev` docker image.
7+
# If image_type argument is given, it will be used to determine the docker
8+
# image type.
9+
# If the first argument is "zsh", the script will use the `zsh` docker image.
10+
11+
# Get absolute path of the script location
12+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
13+
WEST_DIR=$(realpath "${SCRIPT_DIR}/../../..")
14+
15+
if [ -z "${1}" ]; then
16+
TYPE="dev"
17+
else
18+
TYPE="${1}"
19+
fi
20+
21+
docker run -it --rm \
22+
--privileged \
23+
--volume "${WEST_DIR}":/home/user/workdir \
24+
--volume /dev:/dev \
25+
--workdir /home/user/workdir/project \
26+
--device-cgroup-rule='c 166:* rmw' \
27+
ghcr.io/irnas/ncs-zephyr-v3.0.2-"${TYPE}":latest
28+
29+
# Flags:
30+
# --privileged,
31+
# --volume /dev:/dev and
32+
# --device-cgroup-rule
33+
#
34+
# are needed to have access to the USB devices in the container.
35+
# Number 166 corresponds to the major group number of tty devices.
36+
# If this is not the case on your system, you can find the group number by
37+
# running:
38+
# ls -al /dev/tty*
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env bash
2+
# Usage: ./update_docker_image_name.sh <options>
3+
#
4+
# Description:
5+
#
6+
# Search through all repository files for hardcoded Docker image names and
7+
# update either the Docker image tag, the NCS version, or both.
8+
#
9+
# Notes:
10+
#
11+
# At least one of the two options must be provided. The script will update all
12+
# matching references in repository files accordingly.
13+
#
14+
# Options:
15+
#
16+
# --image-tag <IMAGE_TAG> The new image tag to replace the existing
17+
# one in Docker image names. Must be either a
18+
# valid semantic version in the format
19+
# vMAJOR.MINOR.PATCH or the string "latest".
20+
#
21+
# --ncs-revision <NCS_VERSION> The new NCS version to replace the existing
22+
# one in Docker image names. Must be a valid
23+
# semantic version number in the format
24+
# vMAJOR.MINOR.PATCH.
25+
#
26+
# -h, --help Show this help message and exit.
27+
#
28+
29+
# Starting part of the docker image that the script searches for.
30+
# If the image name changes, this variable must be updated.
31+
DOCKER_IMAGE="ghcr.io/irnas/ncs-zephyr"
32+
33+
NCS_REVISION=""
34+
IMAGE_TAG=""
35+
36+
print_help() {
37+
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' <"$0"
38+
}
39+
40+
val_image_tag() {
41+
# Validate the argument against the regex and "latest" literal
42+
if [[ ! $IMAGE_TAG =~ $SEMVER_REGEX && ! $IMAGE_TAG =~ "latest" ]]; then
43+
echo -e "${RED}Invalid image tag: ${IMAGE_TAG}${NC}"
44+
echo
45+
print_help
46+
exit 1
47+
fi
48+
}
49+
50+
val_ncs_revision() {
51+
# Validate the argument against the regex
52+
if [[ ! $NCS_REVISION =~ $SEMVER_REGEX ]]; then
53+
echo -e "${RED}Invalid NCS revision: ${NCS_REVISION}${NC}"
54+
echo
55+
print_help
56+
exit 1
57+
fi
58+
}
59+
60+
# Parse arguments
61+
while [[ $# -gt 0 ]]; do
62+
case "$1" in
63+
--ncs-revision)
64+
NCS_REVISION="$2"
65+
val_ncs_revision
66+
shift 2
67+
;;
68+
--image-tag)
69+
IMAGE_TAG="$2"
70+
val_image_tag
71+
shift 2
72+
;;
73+
--help | -h)
74+
print_help
75+
exit 0
76+
;;
77+
*)
78+
echo "Unknown option: $1"
79+
print_help
80+
exit 1
81+
;;
82+
esac
83+
done
84+
85+
SEMVER_REGEX="v[0-9]+\.[0-9]+\.[0-9]+"
86+
87+
# Color codes
88+
RED='\033[0;31m'
89+
NC='\033[0m' # No Color
90+
91+
# Check if at least one variable is set
92+
if [[ -z $NCS_REVISION && -z $IMAGE_TAG ]]; then
93+
echo -e "${RED}Error: At least one argument must be provided.${NC}"
94+
echo
95+
print_help
96+
exit 1
97+
fi
98+
99+
# Get absolute path of the script location
100+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
101+
PROJ_DIR=$(realpath "${SCRIPT_DIR}/../..")
102+
103+
# shellcheck disable=SC2164
104+
cd "${PROJ_DIR}"
105+
106+
# Find files containing the search pattern, only search in tracked files to be
107+
# faster.
108+
files=$(git ls-files | xargs -I {} grep -rl "$DOCKER_IMAGE" {})
109+
110+
# Process each file
111+
for file in $files; do
112+
if [[ -n $IMAGE_TAG ]]; then
113+
# Replace Docker image tag
114+
#
115+
# 1. (${DOCKER_IMAGE}-[^:]+:) - 1st capture group that matches by the
116+
# DOCKER_IMAGE up to the first colon.
117+
# 2. (${SEMVER_REGEX}|latest) - 2nd capture group that matches either
118+
# the semantic version number or literal "latest".
119+
# 3. (.*) - 3rd capture group, matches everything after the 2nd group.
120+
# Needed so that the text after docker image name is not modified.
121+
#
122+
# The replacement pattern just surrounds the NEW_VERSION with the
123+
# first and second capture groups.
124+
#
125+
# For example, if "v2.0.0" is passed as the argument, then the:
126+
#
127+
# "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v1.0.0"
128+
#
129+
# is replaced by
130+
#
131+
# "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v2.0.0"
132+
#
133+
sed -i -E \
134+
"s#(${DOCKER_IMAGE}[^:]+:)(${SEMVER_REGEX}|latest)(.*)#\1${IMAGE_TAG}\3#g" \
135+
"$file"
136+
fi
137+
138+
if [[ -n $NCS_REVISION ]]; then
139+
# Replace NCS revisions
140+
#
141+
# 1. (${DOCKER_IMAGE}-) - 1st capture group that matches by the
142+
# DOCKER_IMAGE up to the first hyphen.
143+
# 2. ${SEMVER_REGEX} - Match the semantic version number.
144+
# 3. (.*) - 2nd capture group, matches everything after the 2nd group.
145+
# Needed so that the text after docker image name is not modified.
146+
#
147+
# The replacement pattern just surrounds the NEW_VERSION with the
148+
# first and second capture groups.
149+
#
150+
# For example, if "v2.0.0" is passed as the argument, then the:
151+
#
152+
# "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v1.0.0"
153+
#
154+
# is replaced by
155+
#
156+
# "image": "ghcr.io/irnas/some_image-v2.0.0-dev:v1.0.0"
157+
sed -i -E \
158+
"s|(${DOCKER_IMAGE}-)${SEMVER_REGEX}(-.*)|\1${NCS_REVISION}\2|g" \
159+
"$file"
160+
fi
161+
162+
done

scripts/update_docker_versions.sh

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)