From f3f226c8110d6049b75596fa5051a6927af5faa1 Mon Sep 17 00:00:00 2001 From: Henning Brinkmann Date: Mon, 5 Apr 2021 21:12:08 +0200 Subject: [PATCH] initial --- README.md | 39 +++++++++++++++- defaults/main.yml | 2 + handlers/main.yml | 2 + meta/main.yml | 52 +++++++++++++++++++++ tasks/main.yml | 31 +++++++++++++ templates/docker-compose.yml.j2 | 43 ++++++++++++++++++ templates/init_letsencrypt.sh | 80 +++++++++++++++++++++++++++++++++ templates/nginx.conf.j2 | 31 +++++++++++++ tests/inventory | 2 + tests/test.yml | 5 +++ vars/main.yml | 2 + 11 files changed, 287 insertions(+), 2 deletions(-) create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/docker-compose.yml.j2 create mode 100644 templates/init_letsencrypt.sh create mode 100644 templates/nginx.conf.j2 create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md index 01da9ab..225dd44 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,38 @@ -# drone +Role Name +========= -Ansible role for Drone.io \ No newline at end of file +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..8449e77 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for drone diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..18a1452 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for drone diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..f7d84cc --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,31 @@ +--- +# tasks file for drone + +- name: Copy docker-compose.yml + ansible.builtin.template: + src: docker-compose.yml.j2 + dest: docker-compose.yml + +- name: Create directory data + file: + path: data/nginx + state: directory + +- name: Create nginx.conf + ansible.builtin.template: + src: nginx.conf.j2 + dest: data/nginx/nginx.conf + +- name: Create init_letsencrypt.sh + ansible.builtin.template: + src: init_letsencrypt.sh + dest: init_letsencrypt.sh + mode: 0700 + +- name: Init letsencrypt and start + command: ./init_letsencrypt.sh + environment: "{{ drone_env }}" + +- name: docker-compose up + command: docker-compose up -d + environment: "{{ drone_env }}" diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..7e686ed --- /dev/null +++ b/templates/docker-compose.yml.j2 @@ -0,0 +1,43 @@ +version: '3' + +services: + nginx: + image: nginx:1.15-alpine + restart: unless-stopped + volumes: + - ./data/nginx:/etc/nginx/conf.d + - ./data/certbot/conf:/etc/letsencrypt + - ./data/certbot/www:/var/www/certbot + ports: + - "80:80" + - "443:443" + command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'" + certbot: + image: certbot/certbot + restart: unless-stopped + volumes: + - ./data/certbot/conf:/etc/letsencrypt + - ./data/certbot/www:/var/www/certbot + entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" + drone: + image: drone/drone:1 + environment: + - DRONE_GITEA_SERVER=${DRONE_GITEA_SERVER} + - DRONE_GITEA_CLIENT_ID=${DRONE_GITEA_CLIENT_ID} + - DRONE_GITEA_CLIENT_SECRET=${DRONE_GITEA_CLIENT_SECRET} + - DRONE_RPC_SECRET=${DRONE_RPC_SECRET} + - DRONE_SERVER_HOST={{ inventory_hostname }} + - DRONE_SERVER_PROTO=https + restart: always + drone-runner: + image: drone/drone-runner-docker:1 + environment: + - DRONE_RPC_PROTO=https + - DRONE_RPC_HOST={{ inventory_hostname }} + - DRONE_RPC_SECRET=${DRONE_RPC_SECRET} + - DRONE_RPC_SKIP_VERIFY=true + - DRONE_RUNNER_CAPACITY=2 + - DRONE_RUNNER_NAME=drone-runner + volumes: + - /var/run/docker.sock:/var/run/docker.sock + restart: always diff --git a/templates/init_letsencrypt.sh b/templates/init_letsencrypt.sh new file mode 100644 index 0000000..ad3d221 --- /dev/null +++ b/templates/init_letsencrypt.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +if ! [ -x "$(command -v docker-compose)" ]; then + echo 'Error: docker-compose is not installed.' >&2 + exit 1 +fi + +domains=({{ inventory_hostname }}) +rsa_key_size=4096 +data_path="./data/certbot" +email="{{ letsencrypt.email }}" +staging="{{ letsencrypt.staging }}" # Set to 1 if you're testing your setup to avoid hitting request limits + +if [ -d "$data_path" ]; then + echo "Existing data found for $domains. Keeping existing certificate" decision + exit +fi + + +if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then + echo "### Downloading recommended TLS parameters ..." + mkdir -p "$data_path/conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem" + echo +fi + +echo "### Creating dummy certificate for $domains ..." +path="/etc/letsencrypt/live/$domains" +mkdir -p "$data_path/conf/live/$domains" +docker-compose run --rm --entrypoint "\ + openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\ + -keyout '$path/privkey.pem' \ + -out '$path/fullchain.pem' \ + -subj '/CN=localhost'" certbot +echo + + +echo "### Starting nginx ..." +docker-compose up --force-recreate -d nginx +echo + +echo "### Deleting dummy certificate for $domains ..." +docker-compose run --rm --entrypoint "\ + rm -Rf /etc/letsencrypt/live/$domains && \ + rm -Rf /etc/letsencrypt/archive/$domains && \ + rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot +echo + + +echo "### Requesting Let's Encrypt certificate for $domains ..." +#Join $domains to -d args +domain_args="" +for domain in "${domains[@]}"; do + domain_args="$domain_args -d $domain" +done + +# Select appropriate email arg +case "$email" in + "") email_arg="--register-unsafely-without-email" ;; + *) email_arg="--email $email" ;; +esac + +# Enable staging mode if needed +if [ $staging != "0" ]; then staging_arg="--staging"; fi + +mkdir -p $data_path/certbot/www + +docker-compose run --rm --entrypoint "\ + certbot certonly --webroot -w /var/www/certbot \ + $staging_arg \ + $email_arg \ + $domain_args \ + --rsa-key-size $rsa_key_size \ + --agree-tos \ + --force-renewal" certbot +echo + +echo "### Reloading nginx ..." +docker-compose exec nginx nginx -s reload \ No newline at end of file diff --git a/templates/nginx.conf.j2 b/templates/nginx.conf.j2 new file mode 100644 index 0000000..301d70f --- /dev/null +++ b/templates/nginx.conf.j2 @@ -0,0 +1,31 @@ +server { + listen 80; + server_name example.org; + server_tokens off; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name {{ inventory_hostname }}; + server_tokens off; + + ssl_certificate /etc/letsencrypt/live/{{ inventory_hostname }}/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/{{ inventory_hostname }}/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + location / { + proxy_pass http://drone:80; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +} diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..e12fb0b --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - drone diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..fdc897f --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for drone