Skip to content

Commit

Permalink
Merge pull request #179 from hanhsuan/add_agent_dockerfile
Browse files Browse the repository at this point in the history
Add Dockerfile to create an OCI image of agent
  • Loading branch information
plars authored Jan 11, 2024
2 parents 1ffc4a3 + 9e10be3 commit b86ef6d
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
85 changes: 85 additions & 0 deletions agent/extra/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
FROM ubuntu:22.04

# normal settings
ENV TZ=Asia/Taipei \
LANG=C.UTF-8 \
CONFIG_DIR=/tmp/config \
INFLUX_HOST=localhost \
INFLUX_PORT=8086 \
INFLUX_USER=testflinger-agent \
INFLUX_PW=testflinger-agent

# device-connectors default config
ENV DC_CONFIG=device-connectors.yaml \
DC_DUT_IP=127.0.0.1 \
DC_DUT_SID=aabbccdd \
DC_NODE_ID= \
DC_NODE_NAME=core-taipei-testflinger \
DC_AGENT_NAME=staging-tfagent-cid-REPLACE_TO_HOSTNAME \
DC_MASS_USER=bot \
DC_TIMEOUT=120

# testflinger-agent default config
ENV AGENT_CONFIG=agent.yaml \
AGENT_ID=${DC_AGENT_NAME} \
TESTFLINGER_SERVER=https://testflinger.canonical.com \
AGENT_TIMEOUT=43200 \
AGENT_OUTPUT_TIMEOUT=9000 \
AGENT_EXEC_DIR=/testflinger/run/ \
AGENT_LOG_DIR=/testflinger/log/ \
AGENT_RESULT_DIR=/testflinger/result/ \
AGENT_LOG_LEVEL=DEBUG \
AGENT_JOB_QUEUE=staging-job-cid-REPLACE_TO_HOSTNAME \
AGENT_SET_CMD=/bin/true \
AGENT_PROV_CMD=/bin/true \
AGENT_TEST_CMD="PYTHONIOENCODING=utf-8 PYTHONUNBUFFERED=1 testflinger-device-connector maas2 runtest -c ${CONFIG_DIR}/${DC_CONFIG} testflinger.json" \
AGENT_CLEAN_CMD=/bin/true


# python
ARG PYTHON=python3

# preinstall packages
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get -y install \
software-properties-common \
gpg \
ca-certificates \
curl \
git \
git-lfs \
netbase \
less \
openssh-client \
patch \
tzdata \
python3-pip \
python3-dev \
ssh-import-id \
jq \
sshpass \
build-essential && \
apt-get clean && \
rm -rf /tmp/* /var/cache/* /usr/share/doc/* /usr/share/man/* /var/lib/apt/lists/*

# For security reason, the code below should run with non-root.
# However, we have to install checkbox by apt command
# from testflinger-agent itself to get more flexiblity.
# Please use rootless mode to install Docker, nerdctl or podman.

# install testflinger-agent testflinger-cli and device-connectors
RUN git clone https://github.com/canonical/testflinger.git && \
cd testflinger && \
${PYTHON} -m pip install pip -U && \
${PYTHON} -m pip install -I agent/ && \
${PYTHON} -m pip install -I device-connectors/ && \
${PYTHON} -m pip install -I cli/ && \
rm -rf *

# create agent working dir
RUN mkdir -p ${AGENT_EXEC_DIR} ${AGENT_LOG_DIR} ${AGENT_RESULT_DIR} ${CONFIG_DIR}

COPY --chmod=0111 agent.sh ./

CMD ["./agent.sh"]
52 changes: 52 additions & 0 deletions agent/extra/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
**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.**

# Description
This Dockerfile is used to build an OCI image includes testflinger-agent, testflinger-cli, testfliger-device-connector inside.
Testflinger-agent is the only application that running at beginning.

# How to build
```shell
docker build -t [where is the images registry]/[image name]:[tag] .
```

# Variables
There are some environment variables that could be used to change the setting:
|Variable|description|default|
|--|--|--|
|CONFIG\_DIR|where the configuration files are|/tmp/config|
|INFLUX\_HOST|for testflinger-agent|127.0.0.1|
|INFLUX\_PORT|for testflinger-agent|8086|
|INFLUX\_USER|for testflinger-agent|testflinger-agent|
|INFLUX\_PW|for testflinger-agent|testflinger-agent|
|DC\_CONFIG|configuration file of device-connector|device-connectors.yaml|
|DC\_DUT\_IP|DUT IP that device-connector will operate|127.0.0.1|
|DC\_DUT\_SID|secure id of DUT|aabbccdd|
|DC\_NODE\_ID|where the device-connector running is||
|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)|
|DC\_MASS\_USER|mass user|bot|
|DC\_TIMEOUT|timeout of device-connector|120|
|AGENT\_CONFIG|configuration file of testflinger-agent|agent.yaml|
|AGENT\_ID|name of agent|same with `DC_AGENT_NAME`|
|TESTFLINGER\_SERVER|where the testflinger server is|https://testflinger.canonical.com|
|AGENT\_TIMEOUT|timeout of testflinger-agent|43200|
|AGENT\_OUTPUT\_TIMEOUT| output timeout of testflinger-agent|9000|
|AGENT\_EXEC\_DIR|where the testflinger-agent to store running info is|/testflinger/run/|
|AGENT\_LOG\_DIR|where the testflinger-agent to store log info is|/testflinger/log/|
|AGENT\_RESULT\_DIR|where the testflinger-agent to store result info is|/testflinger/result/|
|AGENT\_LOG\_LEVEL|log level of testflinger-agent|DEBUG|
|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)|
|AGENT\_SET\_CMD|set command of testflinger-agent|/bin/true|
|AGENT\_PROV\_CMD|provision command of testflinger-agent|/bin/true|
|AGENT\_CLEAN\_CMD|cleanup command of testflinger-agent|/bin/true|
|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|

# How to start this container
```shell
docker run -d --rm -e INFLUX_HOST=[where it is] --hostname [CID] [images name]
```

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.

# How to put configuration file into this container
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.

57 changes: 57 additions & 0 deletions agent/extra/docker/agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# if no testflinger-agent configuration create one
if [ ! -f "${CONFIG_DIR}/${AGENT_CONFIG}" ]; then
cat <<EOF >"${CONFIG_DIR}/${AGENT_CONFIG}"
agent_id: ${AGENT_ID}
server_address: ${TESTFLINGER_SERVER}
global_timeout: ${AGENT_TIMEOUT}
output_timeout: ${AGENT_OUTPUT_TIMEOUT}
execution_basedir: ${AGENT_EXEC_DIR}
logging_basedir: ${AGENT_LOG_DIR}
results_basedir: ${AGENT_RESULT_DIR}
logging_level: ${AGENT_LOG_LEVEL}
job_queues:
- ${AGENT_JOB_QUEUE}
setup_command: ${AGENT_SET_CMD}
provision_command: ${AGENT_PROV_CMD}
test_command: ${AGENT_TEST_CMD}
cleanup_command: ${AGENT_CLEAN_CMD}
EOF
fi

# if no device-connectors configuration create one
if [ ! -f "${CONFIG_DIR}/${DC_CONFIG}" ]; then
cat <<EOF >"${CONFIG_DIR}/${DC_CONFIG}"
device_ip: ${DC_DUT_IP}
secure_id: ${DC_DUT_SID}
node_id: ${DC_NODE_ID}
node_name: ${DC_NODE_NAME}
agent_name: ${DC_AGENT_NAME}
maas_user: ${DC_MASS_USER}
timeout_min: ${DC_TIMEOUT}
env:
HEXR_DEVICE_SECURE_ID: ${DC_DUT_SID}
DEVICE_IP: ${DC_DUT_IP}
EOF
fi

# show usage
testflinger-agent -h
testflinger-cli -h
testflinger-device-connector -h

# replace placeholder "REPLACE_TO_HOSTNAME" to ${HOSTNAME}
sed -i s/REPLACE_TO_HOSTNAME/"${HOSTNAME}"/g "${CONFIG_DIR}/${AGENT_CONFIG}"
sed -i s/REPLACE_TO_HOSTNAME/"${HOSTNAME}"/g "${CONFIG_DIR}/${DC_CONFIG}"

# show agent information
echo "$(grep agent_id ${CONFIG_DIR}/${AGENT_CONFIG})"
echo "$(grep -A 1 job_queues ${CONFIG_DIR}/${AGENT_CONFIG})"

# start testflinger-agent
if [ -z "${NO_AGENT_LOG}" ] ;then
testflinger-agent -c "${CONFIG_DIR}/${AGENT_CONFIG}"
else
testflinger-agent -c "${CONFIG_DIR}/${AGENT_CONFIG}" > /dev/null 2>&1
fi

0 comments on commit b86ef6d

Please sign in to comment.