From 3171f519b9a3289342bf4adf5788b40121272e4f Mon Sep 17 00:00:00 2001 From: Darius Stefan Date: Thu, 14 Nov 2024 16:24:02 +0200 Subject: [PATCH] Merge with Dockerfile repo --- .github/workflows/docker-push-image.yml | 22 ++++++---- .github/workflows/docker-readme.yml | 26 ++++++++++++ README.md | 7 ++++ docker/Dockerfile | 27 ++++++++++++ docker/Makefile | 13 ++++++ docker/docker.md | 55 +++++++++++++++++++++++++ docker/run.sh | 37 +++++++++++++++++ 7 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/docker-readme.yml create mode 100644 docker/Dockerfile create mode 100644 docker/Makefile create mode 100644 docker/docker.md create mode 100755 docker/run.sh diff --git a/.github/workflows/docker-push-image.yml b/.github/workflows/docker-push-image.yml index 7f49e57..a8db217 100644 --- a/.github/workflows/docker-push-image.yml +++ b/.github/workflows/docker-push-image.yml @@ -1,4 +1,4 @@ -name: Trigger OpenSIPS CLI Images build and publish +name: Push OpenSIPS CLI Images in Docker Hub on: push: @@ -6,15 +6,21 @@ on: workflow_dispatch: jobs: - build: - runs-on: ubuntu-latest steps: - - name: Repository Dispatch - uses: myrotvorets/trigger-repository-dispatch-action@1.1.0 + - uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v2.1.0 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 with: - token: ${{ secrets.DOCKER_OPENSIPS_CLI_PAT }} - repo: OpenSIPS/docker-opensips-cli - type: OpenSIPS CLI Trigger + context: ./docker/ + push: true + tags: opensips/opensips-cli:latest diff --git a/.github/workflows/docker-readme.yml b/.github/workflows/docker-readme.yml new file mode 100644 index 0000000..de5d388 --- /dev/null +++ b/.github/workflows/docker-readme.yml @@ -0,0 +1,26 @@ +--- +name: Update Docker Hub Description +on: + push: + branches: + - main + paths: + - ./docker/docker.md + - .github/workflows/docker-readme.yml + +jobs: + dockerHubDescription: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + + - name: Docker Hub Description + uses: peter-evans/dockerhub-description@v4 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + repository: opensips/opensips-cli + readme-filepath: ./docker/docker.md + short-description: ${{ github.event.repository.description }} + enable-url-completion: true diff --git a/README.md b/README.md index 3e6a567..8d4759d 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,13 @@ args = OpenSIPSCLIArgs(communcation_type = "http", ... ``` +### Docker Image + +The OpenSIPS CLI tool can be run in a Docker container. The image is available +on Docker Hub at [opensips/opensips-cli](https://hub.docker.com/r/opensips/opensips-cli). +For more information on how to run the tool in a Docker container, please refer to the +[OpenSIPS CLI Docker Image](docker/docker.md) documentation. + ## Configuration OpenSIPS CLI accepts a configuration file, formatted as an `ini` or `cfg` diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..aace39c --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.9-slim-buster +LABEL maintainer="Razvan Crainea " + +USER root + +# Set Environment Variables +ENV DEBIAN_FRONTEND noninteractive + +#install basic components +RUN apt-get -y update -qq && \ + apt-get -y install git default-libmysqlclient-dev gcc + +#add keyserver, repository +RUN git clone https://github.com/OpenSIPS/opensips-cli.git /usr/src/opensips-cli && \ + cd /usr/src/opensips-cli && \ + python3 setup.py install clean --all && \ + cd / && rm -rf /usr/src/opensips-cli + +RUN apt-get purge -y git gcc && \ + apt-get autoremove -y && \ + apt-get clean + +ADD "run.sh" "/run.sh" + +ENV PYTHONPATH /usr/lib/python3/dist-packages + +ENTRYPOINT ["/run.sh", "-o", "communication_type=http"] diff --git a/docker/Makefile b/docker/Makefile new file mode 100644 index 0000000..bb406f0 --- /dev/null +++ b/docker/Makefile @@ -0,0 +1,13 @@ +NAME ?= opensips-cli +OPENSIPS_DOCKER_TAG ?= latest + +all: build start + +.PHONY: build start +build: + docker build \ + --tag="opensips/opensips-cli:$(OPENSIPS_DOCKER_TAG)" \ + . + +start: + docker run -d --name $(NAME) opensips/opensips-cli:$(OPENSIPS_DOCKER_TAG) diff --git a/docker/docker.md b/docker/docker.md new file mode 100644 index 0000000..f689daf --- /dev/null +++ b/docker/docker.md @@ -0,0 +1,55 @@ +# OpenSIPS CLI Docker Image + +Docker recipe for running [OpenSIPS Command Line +Interface](https://github.com/OpenSIPS/opensips-cli). + +## Building the image +You can build the docker image by running: +``` +make build +``` + +This command will build a docker image with OpenSIPS CLI master version taken from +the git repository + +## Parameters + +The container receives parameters in the following format: +``` +[-o KEY=VALUE]* CMD [PARAMS]* +``` + +Meaning of the parameters is as it follows: + +* `-o KEY=VALUE` - used to tune `opensips-cli` at runtime; these parameters +will end up in opensips-cli config file, in the `default` section, as +`KEY: VALUE` lines +* `CMD` - the command used to run; if the `CMD` ends with `.sh` extension, it +will be run as a bash script, if the `CMD` ends with `.py` extension, it is +run as a python script, otherwise it is run as a `opensips-cli` command +* `PARAMS` - optional additional parameters passed to `CMD` + +## Run + +To run a bash script, simply pass the connector followed by the bash script: +``` +docker run -d --name opensips-cli opensips/opensips-cli:latest \ + -o url=http://8.8.8.8:8888/mi script.sh +``` + +Similarly, run a python script: +``` +docker run -d --name opensips-cli opensips/opensips-cli:latest \ + -o url=http://8.8.8.8:8888/mi script.py +``` + +To run a single MI command, use: +``` +docker run -d --name opensips-cli opensips/opensips-cli:latest \ + -o url=http://8.8.8.8:8888/mi -x mi ps +``` + +## DockerHub + +Docker images are available on +[DockerHub](https://hub.docker.com/r/opensips/opensips-cli). diff --git a/docker/run.sh b/docker/run.sh new file mode 100755 index 0000000..6d7e199 --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +OPTS= +CMD= +PARAMS= +CFG=/etc/opensips-cli.cfg + +echo "[default]" > "$CFG" + +while [[ $# -gt 0 ]]; do + case "$1" in + -o|--option) + shift + P=$(cut -d'=' -f1 <<<"$1") + V=$(cut -d'=' -f2- <<<"$1") + echo "$P: $V" >> "$CFG" + ;; + *) + if [ -z "$CMD" ]; then + CMD="$1" + else + PARAMS="${PARAMS} ${1}" + fi + ;; + esac + shift +done + +if [[ $CMD == *.py ]]; then + TOOL=python3 +elif [[ $CMD == *.sh ]]; then + TOOL=bash +else + TOOL=opensips-cli +fi + +exec $TOOL $CMD $PARAMS