Skip to content

Commit b86ef6d

Browse files
authored
Merge pull request #179 from hanhsuan/add_agent_dockerfile
Add Dockerfile to create an OCI image of agent
2 parents 1ffc4a3 + 9e10be3 commit b86ef6d

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

agent/extra/docker/Dockerfile

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
FROM ubuntu:22.04
2+
3+
# normal settings
4+
ENV TZ=Asia/Taipei \
5+
LANG=C.UTF-8 \
6+
CONFIG_DIR=/tmp/config \
7+
INFLUX_HOST=localhost \
8+
INFLUX_PORT=8086 \
9+
INFLUX_USER=testflinger-agent \
10+
INFLUX_PW=testflinger-agent
11+
12+
# device-connectors default config
13+
ENV DC_CONFIG=device-connectors.yaml \
14+
DC_DUT_IP=127.0.0.1 \
15+
DC_DUT_SID=aabbccdd \
16+
DC_NODE_ID= \
17+
DC_NODE_NAME=core-taipei-testflinger \
18+
DC_AGENT_NAME=staging-tfagent-cid-REPLACE_TO_HOSTNAME \
19+
DC_MASS_USER=bot \
20+
DC_TIMEOUT=120
21+
22+
# testflinger-agent default config
23+
ENV AGENT_CONFIG=agent.yaml \
24+
AGENT_ID=${DC_AGENT_NAME} \
25+
TESTFLINGER_SERVER=https://testflinger.canonical.com \
26+
AGENT_TIMEOUT=43200 \
27+
AGENT_OUTPUT_TIMEOUT=9000 \
28+
AGENT_EXEC_DIR=/testflinger/run/ \
29+
AGENT_LOG_DIR=/testflinger/log/ \
30+
AGENT_RESULT_DIR=/testflinger/result/ \
31+
AGENT_LOG_LEVEL=DEBUG \
32+
AGENT_JOB_QUEUE=staging-job-cid-REPLACE_TO_HOSTNAME \
33+
AGENT_SET_CMD=/bin/true \
34+
AGENT_PROV_CMD=/bin/true \
35+
AGENT_TEST_CMD="PYTHONIOENCODING=utf-8 PYTHONUNBUFFERED=1 testflinger-device-connector maas2 runtest -c ${CONFIG_DIR}/${DC_CONFIG} testflinger.json" \
36+
AGENT_CLEAN_CMD=/bin/true
37+
38+
39+
# python
40+
ARG PYTHON=python3
41+
42+
# preinstall packages
43+
RUN apt-get update && \
44+
DEBIAN_FRONTEND=noninteractive \
45+
apt-get -y install \
46+
software-properties-common \
47+
gpg \
48+
ca-certificates \
49+
curl \
50+
git \
51+
git-lfs \
52+
netbase \
53+
less \
54+
openssh-client \
55+
patch \
56+
tzdata \
57+
python3-pip \
58+
python3-dev \
59+
ssh-import-id \
60+
jq \
61+
sshpass \
62+
build-essential && \
63+
apt-get clean && \
64+
rm -rf /tmp/* /var/cache/* /usr/share/doc/* /usr/share/man/* /var/lib/apt/lists/*
65+
66+
# For security reason, the code below should run with non-root.
67+
# However, we have to install checkbox by apt command
68+
# from testflinger-agent itself to get more flexiblity.
69+
# Please use rootless mode to install Docker, nerdctl or podman.
70+
71+
# install testflinger-agent testflinger-cli and device-connectors
72+
RUN git clone https://github.com/canonical/testflinger.git && \
73+
cd testflinger && \
74+
${PYTHON} -m pip install pip -U && \
75+
${PYTHON} -m pip install -I agent/ && \
76+
${PYTHON} -m pip install -I device-connectors/ && \
77+
${PYTHON} -m pip install -I cli/ && \
78+
rm -rf *
79+
80+
# create agent working dir
81+
RUN mkdir -p ${AGENT_EXEC_DIR} ${AGENT_LOG_DIR} ${AGENT_RESULT_DIR} ${CONFIG_DIR}
82+
83+
COPY --chmod=0111 agent.sh ./
84+
85+
CMD ["./agent.sh"]

agent/extra/docker/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
**This image is for a specific use case and that most device connectors will not function properly this way, so it's not recommended for use in most cases.**
2+
3+
# Description
4+
This Dockerfile is used to build an OCI image includes testflinger-agent, testflinger-cli, testfliger-device-connector inside.
5+
Testflinger-agent is the only application that running at beginning.
6+
7+
# How to build
8+
```shell
9+
docker build -t [where is the images registry]/[image name]:[tag] .
10+
```
11+
12+
# Variables
13+
There are some environment variables that could be used to change the setting:
14+
|Variable|description|default|
15+
|--|--|--|
16+
|CONFIG\_DIR|where the configuration files are|/tmp/config|
17+
|INFLUX\_HOST|for testflinger-agent|127.0.0.1|
18+
|INFLUX\_PORT|for testflinger-agent|8086|
19+
|INFLUX\_USER|for testflinger-agent|testflinger-agent|
20+
|INFLUX\_PW|for testflinger-agent|testflinger-agent|
21+
|DC\_CONFIG|configuration file of device-connector|device-connectors.yaml|
22+
|DC\_DUT\_IP|DUT IP that device-connector will operate|127.0.0.1|
23+
|DC\_DUT\_SID|secure id of DUT|aabbccdd|
24+
|DC\_NODE\_ID|where the device-connector running is||
25+
|DC\_NODE\_NAME|where the device-connector running is|staging-tfagent-cid-REPLACE\_TO\_HOSTNAME(This defualt value will replace `REPLACE_TO_HOSTNAME` to hostname. Therefore, you could use CID as the hostname while starting container)|
26+
|DC\_MASS\_USER|mass user|bot|
27+
|DC\_TIMEOUT|timeout of device-connector|120|
28+
|AGENT\_CONFIG|configuration file of testflinger-agent|agent.yaml|
29+
|AGENT\_ID|name of agent|same with `DC_AGENT_NAME`|
30+
|TESTFLINGER\_SERVER|where the testflinger server is|https://testflinger.canonical.com|
31+
|AGENT\_TIMEOUT|timeout of testflinger-agent|43200|
32+
|AGENT\_OUTPUT\_TIMEOUT| output timeout of testflinger-agent|9000|
33+
|AGENT\_EXEC\_DIR|where the testflinger-agent to store running info is|/testflinger/run/|
34+
|AGENT\_LOG\_DIR|where the testflinger-agent to store log info is|/testflinger/log/|
35+
|AGENT\_RESULT\_DIR|where the testflinger-agent to store result info is|/testflinger/result/|
36+
|AGENT\_LOG\_LEVEL|log level of testflinger-agent|DEBUG|
37+
|AGENT\_JOB\_QUEUE|the job queue the testflinger-agent listened|staging-job-cid-REPLACE\_TO\_HOSTNAME(This defualt value will replace `REPLACE_TO_HOSTNAME` to hostname. Therefore, you could use CID as the hostname while starting container)|
38+
|AGENT\_SET\_CMD|set command of testflinger-agent|/bin/true|
39+
|AGENT\_PROV\_CMD|provision command of testflinger-agent|/bin/true|
40+
|AGENT\_CLEAN\_CMD|cleanup command of testflinger-agent|/bin/true|
41+
|AGENT\_TEST\_CMD|test command of testflinger-agent|PYTHONIOENCODING=utf-8 PYTHONUNBUFFERED=1 testflinger-device-connector maas2 runtest -c ${CONFIG\_DIR}/${DC\_CONFIG} testflinger.json|
42+
43+
# How to start this container
44+
```shell
45+
docker run -d --rm -e INFLUX_HOST=[where it is] --hostname [CID] [images name]
46+
```
47+
48+
The user in this conatiner is `root`, you don't have to add `sudo` before the command while root-privilege being needed. For security reason, this container should be ran by `rootless` container runtime.
49+
50+
# How to put configuration file into this container
51+
The start script will check the configuration files are under `CONFIG_DIR` or not, you could mount volume that contain the configuration files to use your own setting without using too much environment varialbs.
52+

agent/extra/docker/agent.sh

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# if no testflinger-agent configuration create one
4+
if [ ! -f "${CONFIG_DIR}/${AGENT_CONFIG}" ]; then
5+
cat <<EOF >"${CONFIG_DIR}/${AGENT_CONFIG}"
6+
agent_id: ${AGENT_ID}
7+
server_address: ${TESTFLINGER_SERVER}
8+
global_timeout: ${AGENT_TIMEOUT}
9+
output_timeout: ${AGENT_OUTPUT_TIMEOUT}
10+
execution_basedir: ${AGENT_EXEC_DIR}
11+
logging_basedir: ${AGENT_LOG_DIR}
12+
results_basedir: ${AGENT_RESULT_DIR}
13+
logging_level: ${AGENT_LOG_LEVEL}
14+
job_queues:
15+
- ${AGENT_JOB_QUEUE}
16+
setup_command: ${AGENT_SET_CMD}
17+
provision_command: ${AGENT_PROV_CMD}
18+
test_command: ${AGENT_TEST_CMD}
19+
cleanup_command: ${AGENT_CLEAN_CMD}
20+
EOF
21+
fi
22+
23+
# if no device-connectors configuration create one
24+
if [ ! -f "${CONFIG_DIR}/${DC_CONFIG}" ]; then
25+
cat <<EOF >"${CONFIG_DIR}/${DC_CONFIG}"
26+
device_ip: ${DC_DUT_IP}
27+
secure_id: ${DC_DUT_SID}
28+
node_id: ${DC_NODE_ID}
29+
node_name: ${DC_NODE_NAME}
30+
agent_name: ${DC_AGENT_NAME}
31+
maas_user: ${DC_MASS_USER}
32+
timeout_min: ${DC_TIMEOUT}
33+
env:
34+
HEXR_DEVICE_SECURE_ID: ${DC_DUT_SID}
35+
DEVICE_IP: ${DC_DUT_IP}
36+
EOF
37+
fi
38+
39+
# show usage
40+
testflinger-agent -h
41+
testflinger-cli -h
42+
testflinger-device-connector -h
43+
44+
# replace placeholder "REPLACE_TO_HOSTNAME" to ${HOSTNAME}
45+
sed -i s/REPLACE_TO_HOSTNAME/"${HOSTNAME}"/g "${CONFIG_DIR}/${AGENT_CONFIG}"
46+
sed -i s/REPLACE_TO_HOSTNAME/"${HOSTNAME}"/g "${CONFIG_DIR}/${DC_CONFIG}"
47+
48+
# show agent information
49+
echo "$(grep agent_id ${CONFIG_DIR}/${AGENT_CONFIG})"
50+
echo "$(grep -A 1 job_queues ${CONFIG_DIR}/${AGENT_CONFIG})"
51+
52+
# start testflinger-agent
53+
if [ -z "${NO_AGENT_LOG}" ] ;then
54+
testflinger-agent -c "${CONFIG_DIR}/${AGENT_CONFIG}"
55+
else
56+
testflinger-agent -c "${CONFIG_DIR}/${AGENT_CONFIG}" > /dev/null 2>&1
57+
fi

0 commit comments

Comments
 (0)