From 3fdf8dd01c8052e4d310ca92e29dcc0daf64b495 Mon Sep 17 00:00:00 2001 From: Stepan Mazurov Date: Fri, 4 Oct 2019 16:19:42 -0600 Subject: [PATCH 1/4] add docker && foreground execution --- Dockerfile | 16 ++++++++++++++++ README.rst | 16 ++++++++++++++++ snmpagent_unity/cli.py | 1 + snmpagent_unity/commands/__init__.py | 2 +- snmpagent_unity/commands/service.py | 25 +++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..94388f2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.7-alpine3.10 + +RUN apk add --quiet --no-cache \ + gcc g++ make libffi-dev openssl-dev + +COPY . /app + +WORKDIR /app + +RUN python setup.py install + +RUN snmpagent-unity create-community --name public + +ENTRYPOINT [ "snmpagent-unity" ] + +CMD ["run", "--conf_file", "/app/unity.ini"] \ No newline at end of file diff --git a/README.rst b/README.rst index d27eda5..a8a501e 100644 --- a/README.rst +++ b/README.rst @@ -124,6 +124,22 @@ Restart a SNMP agent. An error will occur if not running daemon found. snmpagent-unity stop +Docker & Foreground service +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When running in docker, it has to be run in foreground, cli allows this + +.. code-block:: console + + snmpagent-unity run --conf_file + +Start a SNMP agent in the foreground with the specified configuration +file ````. + +.. code-block:: console + + docker run -v /path/to/config.conf:/app/config.conf -p 11161:11161/udp fctninc/snmp-unity-agent:latest run --conf_file /app/config.conf + SNMP user management ^^^^^^^^^^^^^^^^^^^^ - Create a SNMP v2 community diff --git a/snmpagent_unity/cli.py b/snmpagent_unity/cli.py index 17e8f7c..ffe9c35 100644 --- a/snmpagent_unity/cli.py +++ b/snmpagent_unity/cli.py @@ -20,6 +20,7 @@ start Start a SNMP agent daemon stop Stop the SNMP agent daemon restart Restart the SNMP agent daemon + run Run the agent in foreground (for docker) examples: snmpagent-unity --help diff --git a/snmpagent_unity/commands/__init__.py b/snmpagent_unity/commands/__init__.py index 6393b53..fd891c2 100644 --- a/snmpagent_unity/commands/__init__.py +++ b/snmpagent_unity/commands/__init__.py @@ -3,6 +3,6 @@ CMD = [user.AddUser, user.UpdateUser, user.DeleteUser, user.ListUsers, community.CreateCommunity, community.DeleteCommunity, crypto.Encrypt, crypto.Decrypt, - service.Start, service.Stop, service.Restart] + service.Start, service.Stop, service.Restart, service.Run] CMD_DICT = {cmd.name: cmd for cmd in CMD} diff --git a/snmpagent_unity/commands/service.py b/snmpagent_unity/commands/service.py index 86ba4f3..044d4d6 100644 --- a/snmpagent_unity/commands/service.py +++ b/snmpagent_unity/commands/service.py @@ -13,6 +13,10 @@ def _stop(): return agentd.agent_daemon.stop() +def _run(conf_file): + return agentd.main(conf_file) + + def _get_running_conf(): return agentd.agent_daemon.get_running_conf() @@ -56,6 +60,27 @@ def do(self): return _stop() +class Run(base.BaseCommand): + """ +Dell-EMC SNMP agent: run the SNMP agent. + +usage: + snmpagent-unity run --conf_file /tmp/agent.conf + +options: + --conf_file Agent configuration file path + +examples: + snmpagent-unity run --conf_file /tmp/agent.conf + """ + name = 'run' + log_to_stdout = True + + @utils.log_command_exception + def do(self): + return _run(self.args['--conf_file']) + + class Restart(base.BaseCommand): """ Dell-EMC SNMP agent: Restart the running SNMP agent. From 065cfe9b91cf7b7dad15a3997c7e771d6e4c2f09 Mon Sep 17 00:00:00 2001 From: Stepan Mazurov Date: Fri, 4 Oct 2019 16:20:31 -0600 Subject: [PATCH 2/4] add docker building --- .github/workflows/dockerimage.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/dockerimage.yml diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml new file mode 100644 index 0000000..1514588 --- /dev/null +++ b/.github/workflows/dockerimage.yml @@ -0,0 +1,14 @@ +name: Docker Image CI + +on: [push] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) From eaa6622c2f09f92c3bc6d8d799a383cbec5d57dd Mon Sep 17 00:00:00 2001 From: Stepan Mazurov Date: Fri, 4 Oct 2019 16:48:52 -0600 Subject: [PATCH 3/4] add pushing to dockerhub --- .github/workflows/dockerimage.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml index 1514588..45ed19d 100644 --- a/.github/workflows/dockerimage.yml +++ b/.github/workflows/dockerimage.yml @@ -11,4 +11,11 @@ jobs: steps: - uses: actions/checkout@v1 - name: Build the Docker image - run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) + run: docker build . --file Dockerfile --tag fctninc/snmp-unity-agent:$(date +%s) + + - name: Publish to Registry + uses: elgohr/Publish-Docker-Github-Action@master + with: + name: fctninc/snmp-unity-agent + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} From 3525dc0fac60d1c784dcdd7c41693544bcbef843 Mon Sep 17 00:00:00 2001 From: Stepan Mazurov Date: Mon, 7 Oct 2019 10:19:14 -0600 Subject: [PATCH 4/4] Re-triggering build --- .github/workflows/dockerimage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml index 45ed19d..1079b1b 100644 --- a/.github/workflows/dockerimage.yml +++ b/.github/workflows/dockerimage.yml @@ -13,7 +13,7 @@ jobs: - name: Build the Docker image run: docker build . --file Dockerfile --tag fctninc/snmp-unity-agent:$(date +%s) - - name: Publish to Registry + - name: Publish to Docker Hub uses: elgohr/Publish-Docker-Github-Action@master with: name: fctninc/snmp-unity-agent