Skip to content

Commit 5dd5505

Browse files
committed
separate deploy and start command
Signed-off-by: Simone Tollardo <[email protected]>
1 parent cac4f51 commit 5dd5505

File tree

8 files changed

+254
-19
lines changed

8 files changed

+254
-19
lines changed

localinstall/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ Get your own KernelCI instance up and running in no time.
88
Configure and setup credentials in config files located in `config` folder.
99

1010
## Run
11-
You can start your KernelCI deployment by simply executing:
11+
You can deploy your KernelCI deployment by simply executing:
1212
```bash
13-
./kci-deploy.sh run
13+
./kci-deploy.sh deploy
1414
```
15-
and
15+
16+
You can stop your local deployment by executing:
1617
```bash
1718
./kci-deploy.sh stop
1819
```
19-
to terminate it.
20+
and
21+
```bash
22+
./kci-deploy.sh start
23+
```
24+
to start it again.

localinstall/kci-deploy.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ ACTION=""
88
CONTAINER_ARGS=()
99

1010
function print_help() {
11-
echo "Usage: $0 [--build] (run|stop) [args...]"
11+
echo "Usage: $0 [--build] (deploy|start|stop) [args...]"
1212
echo
1313
echo "Options:"
1414
echo " --build Force rebuild of the Deployer image (optional)"
15-
echo " run Run kernelci deployment (default if no action specified)"
16-
echo " stop Stop and remove kernelci deployment"
15+
echo " deploy Configure and start the kernelci deployment"
16+
echo " start Start the already configured kernelci deployment (default if no action specified)"
17+
echo " stop Stop the kernelci deployment"
1718
echo " -h, --help Show this help message"
1819
echo
19-
echo "Arguments after 'run' or 'stop' are passed to the container entrypoint"
20+
echo "Arguments after 'deploy', 'start' or 'stop' are passed to the container entrypoint"
2021
exit 0
2122
}
2223

@@ -27,9 +28,9 @@ while [[ $# -gt 0 ]]; do
2728
BUILD_IMAGE=true
2829
shift
2930
;;
30-
run|stop)
31+
deploy|start|stop)
3132
if [[ -n "$ACTION" ]]; then
32-
echo "Error: Cannot use both 'run' and 'stop'"
33+
echo "Error: Cannot use more than one command among 'deploy', 'start' or 'stop'"
3334
exit 1
3435
fi
3536
ACTION=$1
@@ -49,7 +50,7 @@ done
4950

5051
# Default
5152
if [[ -z "$ACTION" ]]; then
52-
ACTION="run"
53+
ACTION="start"
5354
fi
5455

5556
USER_ID=$(id -u)

localinstall/scripts/2-prepare_api.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
. ./config/main.cfg
4+
5+
set -e
6+
7+
# i am groot?
8+
if [ $(id -u) -ne 0 ]; then
9+
SUDO=sudo
10+
else
11+
SUDO=
12+
fi
13+
14+
cp config/.env-api kernelci/kernelci-api/.env
15+
cp config/api-configs.yaml kernelci/kernelci-core/config/core/
16+
cp config/kernelci-cli.toml kernelci/kernelci-core/kernelci.toml
17+
18+
sed -i "s/#SECRET_KEY=/SECRET_KEY=${API_SECRET_KEY}/" kernelci/kernelci-api/.env
19+
20+
cd kernelci/kernelci-api
21+
mkdir -p docker/redis/data
22+
${SUDO} chmod -R 0777 docker/storage/data
23+
${SUDO} chmod -R 0777 docker/redis/data
24+
# enable ssh and storage nginx
25+
mkdir -p ../../config/out
26+
sed -i 's/^# / /' docker-compose.yaml
27+
if [ -f ../../config/out/ssh.key ]; then
28+
echo "ssh.key already exists"
29+
else
30+
# generate non-interactively ssh key to ssh.key
31+
ssh-keygen -t rsa -b 4096 -N "" -f ../../config/out/ssh.key
32+
fi
33+
# get public key and add to docker/ssh/user-data/authorized_keys
34+
cat ../../config/out/ssh.key.pub > docker/ssh/user-data/authorized_keys

localinstall/scripts/3-start_api.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
. ./config/main.cfg
4+
5+
set -e
6+
7+
# i am groot?
8+
if [ $(id -u) -ne 0 ]; then
9+
SUDO=sudo
10+
else
11+
SUDO=
12+
fi
13+
14+
cd kernelci/kernelci-api
15+
16+
# down, just in case old containers are running
17+
docker compose down
18+
docker compose up -d
19+
echo "Waiting for API to be up"
20+
sleep 1
21+
# loop until the API is up, try 5 times
22+
i=0
23+
while [ $i -lt 5 ]; do
24+
ANSWER=$(curl http://localhost:8001/latest/)
25+
# must be {"message":"KernelCI API"}
26+
if [ "$ANSWER" != "{\"message\":\"KernelCI API\"}" ]; then
27+
echo "API is not up"
28+
i=$((i+1))
29+
sleep 5
30+
else
31+
echo "API is up"
32+
break
33+
fi
34+
done
35+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
. ./config/main.cfg
4+
5+
set -e
6+
7+
# i am groot?
8+
if [ $(id -u) -ne 0 ]; then
9+
SUDO=sudo
10+
else
11+
SUDO=
12+
fi
13+
14+
cd kernelci/kernelci-api
15+
16+
# check for expect
17+
if [ -z "$(which expect)" ]; then
18+
echo "expect is not installed, please install it"
19+
exit 1
20+
fi
21+
22+
# INFO, if you have issues with stale/old data, check for
23+
# docker volume kernelci-api_mongodata and delete it
24+
expect ../../helpers/scripts_setup_admin_user.exp "${YOUR_EMAIL}" "${ADMIN_PASSWORD}"
25+
26+
cd ../kernelci-core
27+
echo "Issuing token for admin user"
28+
expect ../../helpers/kci_user_token_admin.exp "${ADMIN_PASSWORD}" > ../../config/out/admin-token.txt
29+
ADMIN_TOKEN=$(cat ../../config/out/admin-token.txt | tr -d '\r\n')
30+
31+
echo "[kci.secrets]
32+
api.\"docker-host\".token = \"$ADMIN_TOKEN\"
33+
" >> kernelci.toml
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/bin/bash
2+
3+
. ./config/main.cfg
4+
5+
set -e
6+
7+
## This is hacky way of inserting things that probably will outlive trivial patch after changes
8+
# find line number with storage:
9+
function append_storage() {
10+
line=$(grep -n "^storage:$" kernelci/kernelci-pipeline/config/pipeline.yaml | cut -d: -f1)
11+
head -n $line kernelci/kernelci-pipeline/config/pipeline.yaml > tmp.yaml
12+
# insert after line with storage: the following lines
13+
echo "
14+
personal:
15+
storage_type: backend
16+
base_url: http://localhost:8080/user1/
17+
api_url: http://localhost:8080/" >> tmp.yaml
18+
# insert the rest of the file
19+
tail -n +$((line+1)) kernelci/kernelci-pipeline/config/pipeline.yaml >> tmp.yaml
20+
mv tmp.yaml kernelci/kernelci-pipeline/config/pipeline.yaml
21+
}
22+
23+
# TODO: Check if this is already done
24+
#append_storage
25+
26+
# We can build on docker only
27+
sed -i 's/name: k8s-all/name: docker/g' kernelci/kernelci-pipeline/config/pipeline.yaml
28+
29+
sed -i 's/env_file: .env/env_file: .env\/.env/g' kernelci/kernelci-pipeline/config/pipeline.yaml
30+
31+
#- - 'data/ssh/:/home/kernelci/data/ssh'
32+
#- - 'data/output/:/home/kernelci/data/output'
33+
#+ - '/root/kernelci-pipeline/data/ssh/:/home/kernelci/data/ssh'
34+
#+ - '/root/kernelci-pipeline/data/output/:/home/kernelci/data/output'
35+
cd kernelci/kernelci-pipeline
36+
PIPELINE_PWD=$(pwd)
37+
# replace lab_type: kubernetes to lab_type: docker
38+
# This is BAD hack, but it works for now
39+
sed -i 's/lab_type: kubernetes/lab_type: docker/g' config/pipeline.yaml
40+
41+
# replace data/output by $PIPELINE_PWD/data/output
42+
# might be two variants (default and staging)
43+
# - '/data/kernelci-deploy-checkout/kernelci-pipeline/data/ssh/:/home/kernelci/data/ssh'
44+
# - '/data/kernelci-deploy-checkout/kernelci-pipeline/data/output/:/home/kernelci/data/output'
45+
# AND
46+
# - 'data/ssh/:/home/kernelci/data/ssh'
47+
# - 'data/output/:/home/kernelci/data/output'
48+
sed -i "s|- 'data/output/|- '$PIPELINE_PWD/data/output/|g" config/pipeline.yaml
49+
sed -i "s|- 'data/ssh/|- '$PIPELINE_PWD/data/ssh/|g" config/pipeline.yaml
50+
# OR
51+
sed -i "s|- '/data/kernelci-deploy-checkout/kernelci-pipeline/data/ssh/|- '$PIPELINE_PWD/data/ssh/|g" config/pipeline.yaml
52+
sed -i "s|- '/data/kernelci-deploy-checkout/kernelci-pipeline/data/output/|- '$PIPELINE_PWD/data/output/|g" config/pipeline.yaml
53+
54+
chmod 777 data/ssh
55+
cp ../../config/out/ssh.key data/ssh/id_rsa_tarball
56+
chmod 600 data/ssh/id_rsa_tarball
57+
chown -R $(id -u):$(id -g) data/output
58+
cd ../..
59+
60+
#replace kernelci/staging- by local/staging-
61+
#TODO: Make PR to pipeline with ENV var for image prefix
62+
sed -i 's/kernelci\/staging-/local\/staging-/g' kernelci/kernelci-pipeline/docker-compose.yaml
63+
64+
# same for yaml files in config
65+
sed -i 's/kernelci\/staging-/local\/staging-/g' kernelci/kernelci-pipeline/config/pipeline.yaml
66+
67+
# check if kernelci/kernelci-pipeline/config/kernelci.toml
68+
# has [trigger] and then force = 1
69+
# this will force builds on each restart
70+
if ! grep -q "force = 1" kernelci/kernelci-pipeline/config/kernelci.toml; then
71+
sed -i '/\[trigger\]/a force = 1' kernelci/kernelci-pipeline/config/kernelci.toml
72+
fi
73+
74+
# remove from pipeline yaml all build_configs:
75+
sed -i '/build_configs:/,$d' kernelci/kernelci-pipeline/config/pipeline.yaml
76+
# add
77+
cat <<EOF >> kernelci/kernelci-pipeline/config/pipeline.yaml
78+
build_configs:
79+
kernelci_staging-stable:
80+
tree: kernelci
81+
branch: 'staging-stable'
82+
EOF
83+
84+
#create .env
85+
#KCI_STORAGE_CREDENTIALS=L0CALT0KEN
86+
#KCI_API_TOKEN=
87+
#API_TOKEN=
88+
API_TOKEN=$(cat config/out/admin-token.txt)
89+
echo "KCI_STORAGE_CREDENTIALS=/home/kernelci/data/ssh/id_rsa_tarball" > .env
90+
echo "KCI_API_TOKEN=${API_TOKEN}" >> .env
91+
echo "API_TOKEN=${API_TOKEN}" >> .env
92+
cp .env kernelci/kernelci-pipeline/.docker-env
93+
mv .env kernelci/kernelci-pipeline/.env
94+
95+
# Add JWT section with the secret key to kernelci.toml for pipeline callback
96+
sed -i 's/#\[jwt\]$/[jwt]/' kernelci/kernelci-pipeline/config/kernelci.toml
97+
sed -i 's/#secret = "SomeSecretString"/secret = "'"${PIPELINE_SECRET_KEY}"'"/' kernelci/kernelci-pipeline/config/kernelci.toml
98+
# Generate kci-dev token
99+
pip install pyjwt
100+
TOKEN=$(kernelci/kernelci-pipeline/tools/jwt_generator.py --toml kernelci/kernelci-pipeline/config/kernelci.toml \
101+
--email ${YOUR_EMAIL} --permissions checkout,testretry,patchset | grep "JWT token:" | cut -d' ' -f3)
102+
echo $TOKEN > config/out/kci-dev-token.txt
103+
echo "kci-dev token saved to config/out/kci-dev-token.txt"

localinstall/scripts/run.sh

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,51 @@ set -e
55
ACTION=$1
66

77
function print_help() {
8-
echo "Usage: $0 (run|stop)"
8+
echo "Usage: $0 (deploy|start|stop)"
99
echo
10-
echo " run Execute deployment sequence"
11-
echo " stop Stop and remove deployment"
10+
echo " deploy Configure and start deployment"
11+
echo " start Start deployment"
12+
echo " stop Stop deployment"
1213
exit 1
1314
}
1415

16+
function check_deploy() {
17+
if [ ! -f kernelci/.done ]; then
18+
echo "Error: Deployment not completed. Please run 'deploy' first."
19+
exit 1
20+
fi
21+
}
22+
1523
if [[ -z "$ACTION" ]]; then
16-
echo "Error: Missing required action (run or stop)"
24+
echo "Error: Missing required action (deploy, start or stop)"
1725
print_help
1826
fi
1927

2028
case "$ACTION" in
21-
run)
29+
deploy)
30+
# Check if kernelci directory exists
31+
if [ -f kernelci/.done ]; then
32+
echo "Stopping previous deployment..."
33+
$0 stop
34+
fi
35+
sudo rm -rf kernelci
2236
echo "Starting deployment sequence, this may take a while..."
2337
./scripts/1-rebuild_all.sh
24-
./scripts/2-install_api.sh
25-
./scripts/3-install_pipeline.sh
26-
./scripts/4-start_cycle.sh
38+
./scripts/2-prepare_api.sh
39+
./scripts/3-start_api.sh
40+
./scripts/4-set_api_admin.sh
41+
./scripts/5-prepare_pipeline.sh
42+
./scripts/6-start_pipeline.sh
43+
touch kernelci/.done
44+
;;
45+
start)
46+
check_deploy
47+
echo "Starting deployment"
48+
./scripts/3-start_api.sh
49+
./scripts/6-start_pipeline.sh
2750
;;
2851
stop)
52+
check_deploy
2953
echo "Stopping deployment"
3054
cd kernelci/kernelci-api
3155
docker compose down

0 commit comments

Comments
 (0)