From 5b1bb4e0bfdb4d19000ea8e26dd66ff5d1f8f0c6 Mon Sep 17 00:00:00 2001 From: Michael Grote Date: Wed, 19 Jul 2023 20:46:20 +0200 Subject: [PATCH] first try --- .drone.yml | 54 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 16 ++++++++++++++ README.md | 43 ++++++++++++++++++++++++++++++++++++ app.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 13 +++++++++++ 5 files changed, 178 insertions(+) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 app.sh create mode 100644 docker-compose.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..24923ac --- /dev/null +++ b/.drone.yml @@ -0,0 +1,54 @@ +--- +kind: pipeline +type: docker +name: linting + +steps: + - name: gitleaks + image: plugins/gitleaks + settings: + path: . + when: + event: + exclude: + - tag + + - name: hadolint + image: hadolint/hadolint:latest-debian + commands: + - hadolint Dockerfile + +--- +kind: pipeline +type: docker +name: selfhosted +depends_on: # bezieht sich auf linting pipeline + - linting +steps: + - name: docker_build_and_push_selfhosted + image: plugins/docker + settings: + dockerfile: Dockerfile + repo: registry.mgrote.net/oxidized-selfmade + registry: registry.mgrote.net + tags: + - ${DRONE_COMMIT_SHA:0:8} + - ${DRONE_COMMIT_BRANCH} + - latest + when: + event: + exclude: + - pull_request + - tag + + - name: docker_build_and_push_selfhosted_tag + image: plugins/docker + settings: + dockerfile: Dockerfile + repo: registry.mgrote.net/oxidized-selfmade + registry: registry.mgrote.net + tags: + - ${DRONE_TAG} + when: + event: + - tag diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2bc04ed --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM ubuntu:latest + +# Install necessary packages +RUN apt-get update && apt-get install -y openssh-client git + +# Copy the script into the container +COPY app.sh /app/app.sh + +# Set the working directory +WORKDIR /app + +# Make the script executable +RUN chmod +x app.sh + +# Set the entrypoint to execute the script +ENTRYPOINT ["/app/app.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa8eb2d --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# python-api-server + +a small flask-application for storing and downloading stuff like small binaries + +## Variables + +- ``MAX_CONTENT_LENGTH``: maximal Filesize in MB; defaults to 5MB +- ``UPLOAD_DIRECTORY``: where to store the uploaded files; should be mapped to a volume; defaults to "/uploads" +- ``AUTH_TOKEN``: token used for authenticating + +## Example Docker-Compose + +```yaml +version: '3' +services: + python-api-server: + container_name: httpd-api + image: quotengrote/python-api-server:v2 + ports: + - "5040:5000" + volumes: + - uploads:/uploads + environment: + # FLASK_DEBUG: 1 # for debugging + # FLASK_APP: app # for debugging + MAX_CONTENT_LENGTH: 10 + UPLOAD_DIRECTORY: /uploads + AUTH_TOKEN: myuploadtoken + ENABLE_WEBSERVER: true # if enabled a list of files can be viewed in a webbrowser (see screenshot) + +volumes: + uploads: + +``` + +### ENABLE_WEBSERVER Screenshot + + +![](./assets/screenshot_webui.png) + +## API-Endpoints + +- see [Flasgger](https://github.com/flasgger/flasgger): ``http://:5040/apidocs/`` diff --git a/app.sh b/app.sh new file mode 100644 index 0000000..5b57164 --- /dev/null +++ b/app.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# RouterOS devices SSH connection settings +DEVICE_CONFIGS="$DEVICE_CONFIGS" # Comma-separated list of device configurations in the format: "device,username,/path/to/ssh/key" + +# Git repository settings +GIT_REPO_URL="$GIT_REPO_URL" # Git repository SSH URL +GIT_USERNAME="$GIT_USERNAME" # Git username for SSH authentication +GIT_SSH_KEY="$GIT_SSH_KEY" # Git SSH private key path (mounted in Docker container) +GIT_REPO_PATH="/app/config_repo" # Directory where the Git repository is cloned in the Docker container + +# Function to export RouterOS configuration +export_routeros_config() { + local device="$1" + local username="$2" + local ssh_key="$3" + + ssh -i "$ssh_key" "$username"@"$device" /export > "/app/${device}_config_export.txt" +} + +# Function to commit and push the configuration to the Git repository +commit_and_push_to_git() { + cd "$GIT_REPO_PATH" + + git config --global user.email "$GIT_USERNAME" + git config --global user.name "$GIT_USERNAME" + + git add . + git commit -m "Update configuration $(date +%Y-%m-%d)" + git push origin master +} + +# Main script +IFS=',' read -r -a devices <<< "$DEVICE_CONFIGS" +for device_config in "${devices[@]}"; do + IFS=' ' read -r -a config <<< "$device_config" + device="${config[0]}" + username="${config[1]}" + ssh_key="${config[2]}" + + export_routeros_config "$device" "$username" "$ssh_key" +done + +cd "$GIT_REPO_PATH" + +if [ -d "$GIT_REPO_PATH" ]; then + git pull origin master +else + git clone "$GIT_REPO_URL" "$GIT_REPO_PATH" +fi + +commit_and_push_to_git diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c15c807 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: "3" + +services: + routeros-config-export: + image: registry.mgrote.net/oxidized-selfmade:latest + environment: + - DEVICE_CONFIGS=device1,username1,/path/to/ssh/key1 device2,username2,/path/to/ssh/key2 + - GIT_REPO_URL=your_git_repo_ssh_url + - GIT_USERNAME=your_git_username + - GIT_SSH_KEY=/app/your_git_ssh_key_file + volumes: + - /path/to/your_git_ssh_key_file:/app/your_git_ssh_key_file:ro + # Add any other volumes or configurations needed