From 6896943e384b00aaace85653137bc94dfcedf691 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 30 Sep 2020 21:56:00 +0200 Subject: [PATCH] initial commit --- .drone.jsonnet | 154 ++++++++++++++++++ .flake8 | 5 + .gitignore | 11 ++ .later.yml | 18 ++ .prettierignore | 1 + LICENSE | 21 +++ README.md | 16 ++ defaults/main.yml | 116 +++++++++++++ meta/main.yml | 24 +++ molecule/centos7/converge.yml | 36 ++++ molecule/centos7/create.yml | 89 ++++++++++ molecule/centos7/destroy.yml | 56 +++++++ molecule/centos7/molecule.yml | 22 +++ molecule/centos7/prepare.yml | 9 + molecule/centos7/tests/test_default.py | 15 ++ molecule/default | 1 + molecule/pytest.ini | 3 + molecule/requirements.yml | 11 ++ tasks/main.yml | 6 + tasks/setup.yml | 55 +++++++ .../droneci_autoscaler_compose.yml.j2 | 89 ++++++++++ templates/services/userdata.yml.j2 | 61 +++++++ 22 files changed, 819 insertions(+) create mode 100644 .drone.jsonnet create mode 100644 .flake8 create mode 100644 .gitignore create mode 100644 .later.yml create mode 100644 .prettierignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/centos7/converge.yml create mode 100644 molecule/centos7/create.yml create mode 100644 molecule/centos7/destroy.yml create mode 100644 molecule/centos7/molecule.yml create mode 100644 molecule/centos7/prepare.yml create mode 100644 molecule/centos7/tests/test_default.py create mode 120000 molecule/default create mode 100644 molecule/pytest.ini create mode 100644 molecule/requirements.yml create mode 100644 tasks/main.yml create mode 100644 tasks/setup.yml create mode 100644 templates/services/droneci_autoscaler_compose.yml.j2 create mode 100644 templates/services/userdata.yml.j2 diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..22d7062 --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,154 @@ +local PipelineLinting = { + kind: 'pipeline', + name: 'linting', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'ansible-later', + image: 'xoxys/ansible-later', + commands: [ + 'ansible-later', + ], + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, +}; + +local PipelineDeployment(scenario='centos7') = { + kind: 'pipeline', + name: 'testing-' + scenario, + platform: { + os: 'linux', + arch: 'amd64', + }, + concurrency: { + limit: 1, + }, + workspace: { + base: '/drone/src', + path: '${DRONE_REPO_NAME}', + }, + steps: [ + { + name: 'ansible-molecule', + image: 'xoxys/molecule:3', + environment: { + DO_API_KEY: { from_secret: 'do_api_key' }, + MOLECULE_CUSTOM_MODULES_REPO: 'https://gitea.rknet.org/ansible/custom_modules', + MOLECULE_CUSTOM_FILTERS_REPO: 'https://gitea.rknet.org/ansible/custom_filters', + }, + commands: [ + 'molecule test -s' + scenario, + ], + }, + ], + depends_on: [ + 'linting', + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +local PipelineDocumentation = { + kind: 'pipeline', + name: 'documentation', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'generate', + image: 'xoxys/ansible-doctor', + environment: { + ANSIBLE_DOCTOR_LOG_LEVEL: 'INFO', + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true, + ANSIBLE_DOCTOR_EXCLUDE_FILES: 'molecule/', + ANSIBLE_DOCTOR_TEMPLATE: 'hugo-book', + ANSIBLE_DOCTOR_ROLE_NAME: '${DRONE_REPO_NAME#*.}', + ANSIBLE_DOCTOR_OUTPUT_DIR: '_docs/', + }, + }, + { + name: 'publish', + image: 'plugins/gh-pages', + settings: { + remote_url: 'https://gitea.rknet.org/ansible/${DRONE_REPO_NAME}', + netrc_machine: 'gitea.rknet.org', + username: { from_secret: 'gitea_username' }, + password: { from_secret: 'gitea_token' }, + pages_directory: '_docs/', + target_branch: 'docs', + }, + when: { + ref: ['refs/heads/master'], + }, + }, + { + name: 'trigger', + image: 'plugins/downstream', + settings: { + server: 'https://drone.rknet.org', + token: { from_secret: 'drone_token' }, + fork: true, + repositories: [ + 'ansible/ansible-galaxy', + ], + }, + when: { + ref: ['refs/heads/master'], + }, + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, + depends_on: [ + 'testing-centos7', + ], +}; + +local PipelineNotification = { + kind: 'pipeline', + name: 'notification', + platform: { + os: 'linux', + arch: 'amd64', + }, + clone: { + disable: true, + }, + steps: [ + { + name: 'matrix', + image: 'plugins/matrix', + settings: { + homeserver: { from_secret: 'matrix_homeserver' }, + roomid: { from_secret: 'matrix_roomid' }, + template: 'Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}', + username: { from_secret: 'matrix_username' }, + password: { from_secret: 'matrix_password' }, + }, + }, + ], + depends_on: [ + 'documentation', + ], + trigger: { + status: ['success', 'failure'], + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +[ + PipelineLinting, + PipelineDeployment(scenario='centos7'), + PipelineDocumentation, + PipelineNotification, +] diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..b41e1fc --- /dev/null +++ b/.flake8 @@ -0,0 +1,5 @@ +[flake8] +ignore = D101, D102, D103, D105, D107, E402, W503 +max-line-length = 99 +inline-quotes = double +exclude = .git,.tox,__pycache__,build,dist,tests,*.pyc,*.egg-info,.cache,.eggs,env* diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d97b7cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# ---> Ansible +*.retry +plugins +library + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + diff --git a/.later.yml b/.later.yml new file mode 100644 index 0000000..45911c9 --- /dev/null +++ b/.later.yml @@ -0,0 +1,18 @@ +--- +ansible: + custom_modules: + - iptables_raw + - openssl_pkcs12 + - proxmox_kvm2 + - ucr + - yum_versionlock + +rules: + exclude_files: + - molecule/ + - "LICENSE*" + - "**/*.md" + - "**/*.ini" + + exclude_filter: + - LINT0009 diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..ef05acb --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +.drone* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..44e4d67 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Robert Kaussow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b8e7ba --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# xoxys.droneci_autoscaler + +[![Build Status](https://img.shields.io/drone/build/ansible/xoxys.droneci_autoscaler?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.droneci_autoscaler) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?label=license)](LICENSE) + +Setup [Drone CI autoscaler](https://autoscale.drone.io/). + +You can find the full documentation at [https://galaxy.geekdocs.de](https://galaxy.geekdocs.de/roles/cloud/droneci_autoscaler/). + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +## Maintainers and Contributors + +[Robert Kaussow](https://gitea.rknet.org/xoxys) diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..0d8da16 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,116 @@ +--- +droneci_autoscaler_version: latest +droneci_autoscaler_image: "drone/autoscaler:latest" + +droneci_autoscaler_service_directory: /var/lib/docker/services/droneci_autoscaler +droneci_autoscaler_container_name: droneci_autoscaler +droneci_autoscaler_restart_policy: always +droneci_autoscaler_service_stopped: False + +# @var droneci_autoscaler_sshkey:description: > +# SSH private key that will by used by the autoscaler to connect to the agents. +# If you dont provide a private key, a keypair will be generated automatically. +# @end +# @var droneci_autoscaler_sshkey: $ "_unset_" + +droneci_autoscaler_interval: 1m + +droneci_autoscaler_logs_color: False +droneci_autoscaler_logs_pretty: True +droneci_autoscaler_logs_debug: False + +droneci_autoscaler_watchtower_enabled: False +droneci_autoscaler_gc_enabled: False + +droneci_autoscaler_pool_min: 0 +droneci_autoscaler_pool_max: 1 +droneci_autoscaler_pool_ttl: 50m + +droneci_autoscaler_server_host: droneci +droneci_autoscaler_server_proto: http +droneci_autoscaler_server_token: myveryownsecret + +# @var droneci_autoscaler_db_type:description: > +# Database type to use (default sqlite). All other DB variable will ony be used +# it DB type is **not** sqlite. +droneci_autoscaler_db_type: sqlite + +# @var droneci_autoscaler_db_server:exaple: $ "localhost" +# @var droneci_autoscaler_db_port:exaple: $ "5432" +# @var droneci_autoscaler_db_name:exaple: $ "droneci_autoscaler" +# @var droneci_autoscaler_db_user:exaple: $ "pgdroneci" +# @var droneci_autoscaler_db_password:exaple: $ "secure" +# @var droneci_autoscaler_db_ssl_mode:exaple: $ "disable" + +droneci_autoscaler_agent_token: myveryownsecret +droneci_autoscaler_agent_image: drone/drone-runner-docker:1.5 +droneci_autoscaler_agent_concurrency: 2 +droneci_autoscaler_agent_os: linux +droneci_autoscaler_agent_architecture: amd64 +droneci_autoscaler_agent_volumes: [] +droneci_autoscaler_agent_package_upgrade: False + +droneci_autoscaler_instances: [] +# @var droneci_autoscaler_instances:example: > +# droneci_autoscaler_instances: +# - name: scaler-amd64 +# pool_max: 50 +# pool_ttl: 120m +# agent_concurrency: 15 +# agent_architecture: amd64 +# sshkey: "dummy" +# provider_config: +# - name: DRONE_HETZNERCLOUD_DATACENTER +# value: fsn1 +# - name: DRONE_HETZNERCLOUD_TOKEN +# value: abc123 +# - name: DRONE_HETZNERCLOUD_IMAGE +# value: ubuntu-18.04 +# - name: DRONE_HETZNERCLOUD_TYPE +# value: +# # SSH key id from hcloud API +# - name: DRONE_HETZNERCLOUD_SSHKEY +# value: +# - name: DRONE_HETZNERCLOUD_USERDATA_FILE +# value: /etc/scaler/userdata.yml +# @end + +droneci_autoscaler_runner_volumes: [] +droneci_autoscaler_runner_privileged: [] + +# @var droneci_autoscaler_volumes:description: > +# Define required docker volumes. If you don't use sqlite you could remove the default volume. +# @end +# @var droneci_autoscaler_volumes:example: > +# droneci_autoscaler_volumes: +# # Instead of the name you could specify a path on the container host system, +# # but you also have to enable bind mount for this volume +# - name: data +# # target location inside the container +# dest: /var/lib/drone_autoscaler +# # enable bind mount, if false volume will be configured as named volume +# # keep in mind you MUST set bind in any case +# bind: True +# @end +droneci_autoscaler_volumes: + - name: data + dest: /var/lib/drone_autoscaler + bind: False + +# @var droneci_autoscaler_memory_limit:example: $ "512m" +# @var droneci_autoscaler_memory_limit: $ "_unset_" +# @var droneci_autoscaler_memory_reservation:example: $ "256m" +# @var droneci_autoscaler_memory_reservation: $ "_unset_" + +# @var droneci_autoscaler_networks:example: > +# droneci_autoscaler_networks: +# - name: default +# # optional network driver, defaults to 'bride' +# driver: host +# @end +droneci_autoscaler_networks: + - name: droneci_default + external: True + +droneci_autoscaler_networks_applied: + - droneci_default diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..ab73859 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,24 @@ +# Standards: 0.1 +--- +galaxy_info: + # @meta author:value: [Robert Kaussow](https://gitea.rknet.org/xoxys) + author: "Robert Kaussow " + # @meta description: > + # [![Source Code](https://img.shields.io/badge/gitea-source%20code-blue?logo=gitea&logoColor=white)](https://gitea.rknet.org/ansible/xoxys.droneci_autoscaler) + # [![Build Status](https://img.shields.io/drone/build/ansible/xoxys.droneci_autoscaler?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.droneci_autoscaler) + # [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?label=license)](LICENSE) + # + # Setup [Drone CI autoscaler](https://autoscale.drone.io/). + # @end + description: Setup Drone CI autoscaler + license: MIT + min_ansible_version: 2.8 + platforms: + - name: EL + versions: + - 7 + galaxy_tags: +dependencies: [] +collections: + - community.general + - community.crypto diff --git a/molecule/centos7/converge.yml b/molecule/centos7/converge.yml new file mode 100644 index 0000000..2b0218b --- /dev/null +++ b/molecule/centos7/converge.yml @@ -0,0 +1,36 @@ +--- +- name: Converge + hosts: all + environment: + PYTHONPATH: /opt/python2/ansible-deps/lib/python2.7/site-packages + vars: + dockerengine_packages_extra: + - epel-release + - python-pip + - python-virtualenv + dockerengine_networks: + - name: droneci_default + droneci_autoscaler_service_stopped: True + droneci_autoscaler_instances: + - name: scaler-amd64 + pool_max: 50 + pool_ttl: 120m + agent_concurrency: 15 + agent_architecture: amd64 + provider_config: + - name: DRONE_HETZNERCLOUD_DATACENTER + value: fsn1 + - name: DRONE_HETZNERCLOUD_TOKEN + value: mytoken + - name: DRONE_HETZNERCLOUD_IMAGE + value: ubuntu-18.04 + - name: DRONE_HETZNERCLOUD_TYPE + value: cx11 + - name: DRONE_HETZNERCLOUD_SSHKEY + value: keyid1 + - name: DRONE_HETZNERCLOUD_USERDATA_FILE + value: /etc/scaler/userdata.yml + + roles: + - role: xoxys.docker_engine + - role: xoxys.droneci_autoscaler diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml new file mode 100644 index 0000000..8874298 --- /dev/null +++ b/molecule/centos7/create.yml @@ -0,0 +1,89 @@ +--- + +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + ssh_user: root + ssh_port: 22 + + keypair_name: molecule_key + keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" + tasks: + - name: Create local keypair + user: + name: "{{ lookup('env', 'USER') }}" + generate_ssh_key: true + ssh_key_file: "{{ keypair_path }}" + register: local_keypair + + - name: Create remote keypair + digital_ocean_sshkey: + name: "{{ keypair_name }}" + ssh_pub_key: "{{ local_keypair.ssh_public_key }}" + state: present + register: remote_keypair + + - name: Create molecule instance(s) + digital_ocean_droplet: + name: "{{ item.name }}" + unique_name: true + region: "{{ item.region_id }}" + image: "{{ item.image_id }}" + size: "{{ item.size_id }}" + ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" + wait: true + wait_timeout: 300 + state: present + register: server + loop: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) creation to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: { + 'instance': "{{ item.data.droplet.name }}", + 'address': "{{ item.data.ip_address }}", + 'user': "{{ ssh_user }}", + 'port': "{{ ssh_port }}", + 'identity_file': "{{ keypair_path }}", + 'droplet_id': "{{ item.data.droplet.id }}", + 'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}", + } + loop: "{{ digitalocean_jobs.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool + + - name: Wait for SSH + wait_for: + port: "{{ ssh_port }}" + host: "{{ item.address }}" + search_regex: SSH + delay: 10 + timeout: 320 + loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml new file mode 100644 index 0000000..74de1c5 --- /dev/null +++ b/molecule/centos7/destroy.yml @@ -0,0 +1,56 @@ +--- + +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + tasks: + - block: + - name: Populate instance config + set_fact: + instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + skip_instances: false + rescue: + - name: Populate instance config when file missing + set_fact: + instance_conf: {} + skip_instances: true + + - name: Destroy molecule instance(s) + digital_ocean_droplet: + name: "{{ item.instance }}" + id: "{{ item.droplet_id }}" + state: absent + register: server + loop: "{{ instance_conf | flatten(levels=1) }}" + when: not skip_instances + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - name: Delete remote keypair + digital_ocean_sshkey: + fingerprint: "{{ item.ssh_key_id }}" + state: absent + loop: "{{ instance_conf | flatten(levels=1) }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + content: "{{ instance_conf | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool + diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml new file mode 100644 index 0000000..a4cdf90 --- /dev/null +++ b/molecule/centos7/molecule.yml @@ -0,0 +1,22 @@ +--- +dependency: + name: galaxy + options: + role-file: molecule/requirements.yml + requirements-file: molecule/requirements.yml +driver: + name: delegated +platforms: + - name: centos7-droneci-autoscaler + region_id: fra1 + image_id: centos-7-x64 + size_id: s-1vcpu-1gb +lint: | + /usr/local/bin/flake8 +provisioner: + name: ansible + env: + ANSIBLE_FILTER_PLUGINS: ${ANSIBLE_FILTER_PLUGINS:-./plugins/filter} + ANSIBLE_LIBRARY: ${ANSIBLE_LIBRARY:-./library} +verifier: + name: testinfra diff --git a/molecule/centos7/prepare.yml b/molecule/centos7/prepare.yml new file mode 100644 index 0000000..559de83 --- /dev/null +++ b/molecule/centos7/prepare.yml @@ -0,0 +1,9 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Install python for Ansible + raw: test -e /usr/bin/python || (dnf -y install python3 && alternatives --set python /usr/bin/python3) + become: true + changed_when: false diff --git a/molecule/centos7/tests/test_default.py b/molecule/centos7/tests/test_default.py new file mode 100644 index 0000000..3ce709c --- /dev/null +++ b/molecule/centos7/tests/test_default.py @@ -0,0 +1,15 @@ +import os + +import testinfra.utils.ansible_runner + +import warnings +warnings.filterwarnings("ignore", category=DeprecationWarning) + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_droneci_compose_file(host): + f = host.file( + '/var/lib/docker/services/droneci_autoscaler_amd64/docker-compose.yml') + assert f.exists diff --git a/molecule/default b/molecule/default new file mode 120000 index 0000000..2fdf3e8 --- /dev/null +++ b/molecule/default @@ -0,0 +1 @@ +centos7 \ No newline at end of file diff --git a/molecule/pytest.ini b/molecule/pytest.ini new file mode 100644 index 0000000..c24fe5b --- /dev/null +++ b/molecule/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/molecule/requirements.yml b/molecule/requirements.yml new file mode 100644 index 0000000..dad353e --- /dev/null +++ b/molecule/requirements.yml @@ -0,0 +1,11 @@ +--- +collections: + - name: https://gitea.rknet.org/ansible/xoxys.general/releases/download/v1.2.0/xoxys-general-1.2.0.tar.gz + - name: community.general + - name: community.crypto + +roles: + - src: https://gitea.rknet.org/ansible/xoxys.docker_engine.git + name: xoxys.docker_engine + scm: git + version: master diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..2c6ed96 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- include_tasks: + file: setup.yml + loop: "{{ droneci_autoscaler_instances }}" + loop_control: + loop_var: inst diff --git a/tasks/setup.yml b/tasks/setup.yml new file mode 100644 index 0000000..f8033d5 --- /dev/null +++ b/tasks/setup.yml @@ -0,0 +1,55 @@ +--- +- name: Register working directory + set_fact: + __droneci_autoscaler_workdir: "{{ droneci_autoscaler_service_directory }}_{{ inst.name }}" + +- name: Register SSH key + set_fact: + __droneci_autoscaler_sshkey: "{{ inst.ssh_key | default(droneci_autoscaler_sshkey) | default(False) }}" + +- block: + - name: Ensure service directory exists + file: + path: "{{ __droneci_autoscaler_workdir }}" + state: directory + mode: 0755 + + - name: Deploy compose file to '{{ __droneci_autoscaler_workdir }}' + template: + src: "services/droneci_autoscaler_compose.yml.j2" + dest: "{{ __droneci_autoscaler_workdir }}/docker-compose.yml" + owner: root + group: root + mode: 0640 + validate: "docker-compose -f %s config -q" + + - name: Write SSH private key + copy: + content: "{{ item }}" + dest: "{{ __droneci_autoscaler_workdir }}/ssh_key" + mode: 0600 + loop: "{{ inst.ssh_key | default(droneci_autoscaler_sshkey) }}" + when: __droneci_autoscaler_sshkey + no_log: True + + - name: Generate SSH key pair if missing + openssh_keypair: + path: "{{ __droneci_autoscaler_workdir }}/ssh_key" + type: ed25519 + force: False + state: present + + - name: Write userdata + template: + src: services/userdata.yml.j2 + dest: "{{ __droneci_autoscaler_workdir }}/userdata.yml" + + - name: Ensure scaler is up and running + docker_compose: + project_src: "{{ __droneci_autoscaler_workdir }}" + pull: yes + remove_orphans: yes + stopped: "{{ inst.service_stopped | default(droneci_autoscaler_service_stopped) }}" + state: present + become: True + become_user: root diff --git a/templates/services/droneci_autoscaler_compose.yml.j2 b/templates/services/droneci_autoscaler_compose.yml.j2 new file mode 100644 index 0000000..7a64ead --- /dev/null +++ b/templates/services/droneci_autoscaler_compose.yml.j2 @@ -0,0 +1,89 @@ +#jinja2:lstrip_blocks: True +{{ ansible_managed | comment }} +version: "2.4" + +services: + droneci_autoscaler_{{ inst.name }}: + container_name: {{ droneci_autoscaler_container_name }} + image: {{ droneci_autoscaler_image }} + restart: {{ droneci_autoscaler_restart_policy }} + volumes: + - "{{ __droneci_autoscaler_workdir }}/userdata.yml:/etc/scaler/userdata.yml" + - "{{ __droneci_autoscaler_workdir }}/ssh_key:/root/.ssh/id_rsa:ro" + {% for volume in droneci_autoscaler_volumes %} + - "{{ volume.name }}:{{ volume.dest }}" + {% endfor %} + {% if droneci_autoscaler_networks_applied | default([]) %} + networks: + {% for network in droneci_autoscaler_networks_applied %} + - {{ network }} + {% endfor %} + {% endif %} + environment: + - DRONE_INTERVAL={{ inst.interval | default(droneci_autoscaler_interval) }} + {% if (inst.slack_webhook | default(droneci_autoscaler_slack_webhook)) is defined %} + - DRONE_SLACK_WEBHOOK={{ inst.slack_webhook | default(droneci_autoscaler_slack_webhook) }} + - DRONE_SLACK_CREATE={{ (inst.slack_create | default(droneci_autoscaler_slack_create)) | bool | lower }} + - DRONE_SLACK_DESTROY={{ (inst.slack_destroy | default(droneci_autoscaler_slack_destroy)) | bool | lower }} + - DRONE_SLACK_ERROR={{ (inst.slack_error | default(droneci_autoscaler_slack_error)) | bool | lower }} + {% endif %} + - DRONE_LOGS_COLOR={{ (inst.logs_color | default(droneci_autoscaler_logs_color)) | bool | lower }} + - DRONE_LOGS_PRETTY={{ (inst.logs_pretty | default(droneci_autoscaler_logs_pretty)) | bool | lower }} + - DRONE_LOGS_DEBUG={{ (inst.logs_debug | default(droneci_autoscaler_logs_debug)) | bool | lower }} + - DRONE_POOL_MIN={{ inst.pool_min | default(droneci_autoscaler_pool_min) }} + - DRONE_POOL_MAX={{ inst.pool_max | default(droneci_autoscaler_pool_max) }} + - DRONE_POOL_MIN_AGE={{ inst.pool_ttl | default(droneci_autoscaler_pool_ttl) }} + - DRONE_SERVER_HOST={{ droneci_autoscaler_server_host }} + - DRONE_SERVER_PROTO={{ droneci_autoscaler_server_proto }} + - DRONE_SERVER_TOKEN={{ droneci_autoscaler_server_token }} + - DRONE_AGENT_TOKEN={{ inst.agent_token | default(droneci_autoscaler_agent_token) }} + - DRONE_AGENT_IMAGE={{ inst.agent_image | default(droneci_autoscaler_agent_image) }} + - DRONE_AGENT_CONCURRENCY={{ inst.agent_concurrency | default(droneci_autoscaler_agent_concurrency) }} + - DRONE_AGENT_OS={{ inst.agent_os | default(droneci_autoscaler_agent_os) }} + - DRONE_AGENT_ARCH={{ inst.agent_architecture | default(droneci_autoscaler_agent_architecture) }} + {% if inst.agent_volumes | default(droneci_autoscaler_agent_volumes) | default(False) %} + - DRONE_AGENT_VOLUMES={{ inst.agent_volumes | default(droneci_autoscaler_agent_volumes) | join(",") }} + {% endif %} + {% if inst.runner_volumes | default(droneci_autoscaler_runner_volumes) | default(False) %} + - DRONE_RUNNER_VOLUMES={{ inst.runner_volumes | default(droneci_autoscaler_runner_volumes) | join(",") }} + {% endif %} + - DRONE_RUNNER_PRIVILEGED_IMAGES={{ inst.runner_privileged | default(droneci_autoscaler_runner_privileged) | join(",") }} + - DRONE_GC_ENABLED={{ (inst.gc_enabled | default(droneci_autoscaler_gc_enabled)) | bool | lower }} + - DRONE_GC_DEBUG=false + - DRONE_REAPER_ENABLED=true + - DRONE_REAPER_INTERVAL=30m + - DRONE_WATCHTOWER_ENABLED={{ (inst.watchtower_enabled | default(droneci_autoscaler_watchtower_enabled)) | bool | lower }} + {% if (inst.prometheus_token | default(droneci_autoscaler_prometheus_token)) is defined %} + - DRONE_PROMETHEUS_AUTH_TOKEN={{ inst.prometheus_token | default(droneci_autoscaler_prometheus_token) }} + {% endif %} + {% if droneci_autoscaler_db_type == "pgsql" %} + - DRONE_DATABASE_DRIVER=postgres + - DRONE_DATABASE_DATASOURCE=postgres://{{ droneci_autoscaler_db_user }}:{{ droneci_autoscaler_db_password }}@{{ droneci_autoscaler_db_server }}/{{ droneci_autoscaler_db_name }}?sslmode={{ droneci_autoscaler_db_ssl_mode }} + {% endif %} + {% for item in inst.provider_config %} + - {{ item.name | upper }}={{ item.value }} + {% endfor %} + {% if droneci_autoscaler_memory_limit is defined %} + mem_limit: {{ droneci_autoscaler_memory_limit }} + {% endif %} + {% if droneci_autoscaler_memory_reservation is defined %} + mem_reservation: {{ droneci_autoscaler_memory_reservation }} + {% endif %} +{% if droneci_autoscaler_networks | default([]) | length > 0 %} +{% if droneci_autoscaler_volumes | default([]) | rejectattr("bind") | list | length > 0 %} + +volumes: + {% for volume in droneci_autoscaler_volumes | rejectattr("bind") %} + {{ volume.name }}: + {% endfor %} +{% endif %} +networks: + {% for network in droneci_autoscaler_networks %} + {{ network.name }}: + {% if network.external | default(False) | bool %} + external: true + {% else %} + driver: {{ network.backend | default("bridge") }} + {% endif %} + {% endfor %} +{% endif %} diff --git a/templates/services/userdata.yml.j2 b/templates/services/userdata.yml.j2 new file mode 100644 index 0000000..c0ac30f --- /dev/null +++ b/templates/services/userdata.yml.j2 @@ -0,0 +1,61 @@ +#jinja2: lstrip_blocks: True +#cloud-config + +apt_reboot_if_required: false +package_update: true +package_upgrade: {{ droneci_autoscaler_agent_package_upgrade | bool | lower }} +{% if inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm" %} + +bootcmd: + - [ dpkg, --add-architecture, armhf ] +{% endif %} + +apt: + sources: + docker.list: + source: deb [arch={% if inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm" %}armhf{% elif inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm64" %}arm64{% else %}amd64{% endif %}] https://download.docker.com/linux/ubuntu $RELEASE stable + keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88 + +packages: + - bridge-utils + - 'docker-ce{{ ':armhf' if inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm" else '' }}' + +write_files: + - path: /etc/systemd/system/docker.service.d/override.conf + content: | + [Service] + ExecStart= + ExecStart={% if inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm" %}/usr/bin/setarch linux32 -B {% endif %}/usr/bin/dockerd + + - path: /etc/systemd/system/containerd.service.d/override.conf + content: | + [Service] + ExecStart= + ExecStart={% if inst.agent_architecture | default(droneci_autoscaler_agent_architecture) == "arm" %}/usr/bin/setarch linux32 -B {% endif %}/usr/bin/containerd + + - path: /etc/default/docker + content: | + DOCKER_OPTS="" + + - path: /etc/docker/daemon.json + content: | + { + "hosts": ["0.0.0.0:2376", "unix:///var/run/docker.sock"], + "tls": true, + "tlsverify": true, + "tlscacert": "/etc/docker/ca.pem", + "tlscert": "/etc/docker/server-cert.pem", + "tlskey": "/etc/docker/server-key.pem" + } + + - path: /etc/docker/ca.pem + encoding: b64 + content: {% raw %}{{ .CACert | base64 }}{% endraw %} + + - path: /etc/docker/server-cert.pem + encoding: b64 + content: {% raw %}{{ .TLSCert | base64 }}{% endraw %} + + - path: /etc/docker/server-key.pem + encoding: b64 + content: {% raw %}{{ .TLSKey | base64 }}{% endraw %}