From 802441a2403d7e2b770cffc974929f150c14b900 Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Thu, 21 Dec 2023 14:11:25 +0800 Subject: [PATCH 1/2] Add Dockerfile to create an image include 1. testflinger-agent 2. testflinger-cli 3. device-connector and could change the set by environment variables while starting. --- agent/docker/Dockerfile | 85 +++++++++++++++++++++++++++++++++++++++++ agent/docker/README.md | 49 ++++++++++++++++++++++++ agent/docker/agent.sh | 57 +++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 agent/docker/Dockerfile create mode 100644 agent/docker/README.md create mode 100755 agent/docker/agent.sh diff --git a/agent/docker/Dockerfile b/agent/docker/Dockerfile new file mode 100644 index 00000000..a9b68650 --- /dev/null +++ b/agent/docker/Dockerfile @@ -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"] diff --git a/agent/docker/README.md b/agent/docker/README.md new file mode 100644 index 00000000..f91eecdd --- /dev/null +++ b/agent/docker/README.md @@ -0,0 +1,49 @@ +# 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. + diff --git a/agent/docker/agent.sh b/agent/docker/agent.sh new file mode 100755 index 00000000..d5bc8f83 --- /dev/null +++ b/agent/docker/agent.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# if no testflinger-agent configuration create one +if [ ! -f "${CONFIG_DIR}/${AGENT_CONFIG}" ]; then +cat <"${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 <"${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 From 9e10be3f0e603125d1f9ddd0070a6f603a02ce7d Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Thu, 11 Jan 2024 14:48:06 +0800 Subject: [PATCH 2/2] 1. Move files to extra/docker 2. Notice user this is for a specific use case in README --- agent/{ => extra}/docker/Dockerfile | 0 agent/{ => extra}/docker/README.md | 3 +++ agent/{ => extra}/docker/agent.sh | 0 3 files changed, 3 insertions(+) rename agent/{ => extra}/docker/Dockerfile (100%) rename agent/{ => extra}/docker/README.md (95%) rename agent/{ => extra}/docker/agent.sh (100%) diff --git a/agent/docker/Dockerfile b/agent/extra/docker/Dockerfile similarity index 100% rename from agent/docker/Dockerfile rename to agent/extra/docker/Dockerfile diff --git a/agent/docker/README.md b/agent/extra/docker/README.md similarity index 95% rename from agent/docker/README.md rename to agent/extra/docker/README.md index f91eecdd..639fb51e 100644 --- a/agent/docker/README.md +++ b/agent/extra/docker/README.md @@ -1,6 +1,9 @@ +**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] . diff --git a/agent/docker/agent.sh b/agent/extra/docker/agent.sh similarity index 100% rename from agent/docker/agent.sh rename to agent/extra/docker/agent.sh