diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml new file mode 100644 index 0000000..1079b1b --- /dev/null +++ b/.github/workflows/dockerimage.yml @@ -0,0 +1,21 @@ +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 fctninc/snmp-unity-agent:$(date +%s) + + - name: Publish to Docker Hub + uses: elgohr/Publish-Docker-Github-Action@master + with: + name: fctninc/snmp-unity-agent + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} 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.