From 0b919dccc7a116a5dfb09d069d954457482a600c Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 10 May 2019 10:55:14 +0200 Subject: [PATCH] use systemd to control docker-compose setup --- defaults/main.yml | 4 ++-- handlers/main.yml | 8 ++++---- tasks/main.yml | 1 + tasks/post.yml | 10 ++++++++++ tasks/setup.yml | 11 +++++++---- templates/etc/systemd/system/drone.service.j2 | 19 +++++++++++++++++++ templates/services/droneci-compose.yml.j2 | 1 + 7 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 tasks/post.yml create mode 100644 templates/etc/systemd/system/drone.service.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 6c7fc2a..bd4c613 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -7,8 +7,9 @@ droneci_secret: myveryownsecret # Path to the license key file # droneci_license_key: # defaults to not set +droneci_restart_policy: always + droneci_server_image: "drone/drone:{{ droneci_version }}" -droneci_server_restart_policy: always droneci_server_exposed_port: 8080 droneci_server_exposed_ip: 127.0.0.1 # droneci_server_memory_limit: 512m # defaults to not set @@ -16,7 +17,6 @@ droneci_server_exposed_ip: 127.0.0.1 # droneci_server_extra_hosts: [] droneci_agent_image: "drone/agent:{{ droneci_version }}" -droneci_agent_restart_policy: always dronevi_agent_capacity: 2 # droneci_agent_memory_limit: 512m # defaults to not set # droneci_agent_memory_reservation: 256m # defaults to not set diff --git a/handlers/main.yml b/handlers/main.yml index 3ab9bb7..1acfc2f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,9 +1,9 @@ --- - name: Restart container - docker_service: - project_src: "{{ droneci_service_directory }}" - build: no - restarted: true + systemd: + state: restarted + daemon_reload: yes + name: drone listen: __drone_restart become: True become_user: root diff --git a/tasks/main.yml b/tasks/main.yml index 3ee5511..6ebc669 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,3 +2,4 @@ - import_tasks: prepare.yml - import_tasks: setup.yml - import_tasks: nginx.yml +- import_tasks: post.yml diff --git a/tasks/post.yml b/tasks/post.yml new file mode 100644 index 0000000..a755c86 --- /dev/null +++ b/tasks/post.yml @@ -0,0 +1,10 @@ +--- +- block: + - name: Ensure drone service is up and running + systemd: + state: started + daemon_reload: yes + enabled: yes + name: drone + become: True + become_user: root diff --git a/tasks/setup.yml b/tasks/setup.yml index 7778fe9..6b3b398 100644 --- a/tasks/setup.yml +++ b/tasks/setup.yml @@ -13,15 +13,18 @@ mode: 0644 notify: __drone_restart + - name: Create systemd unit files + template: + src: "etc/systemd/system/drone.service.j2" + dest: "/etc/systemd/system/drone.service" + mode: 0644 + notify: __drone_restart + - name: Copy license key file is defined copy: src: "{{ droneci_license_key }}" dest: "{{ droneci_service_directory }}/{{ droneci_license_key | basename }}" mode: 0600 when: droneci_license_key is defined - - - name: Ensure drone ci containers are up and running - docker_service: - project_src: "{{ droneci_service_directory }}" become: True become_user: root diff --git a/templates/etc/systemd/system/drone.service.j2 b/templates/etc/systemd/system/drone.service.j2 new file mode 100644 index 0000000..81ae92b --- /dev/null +++ b/templates/etc/systemd/system/drone.service.j2 @@ -0,0 +1,19 @@ +#jinja2:lstrip_blocks: True +# {{ ansible_managed }} +[Unit] +Description=Drone CI compose service +Requires=docker.service +After=docker.service + +[Service] +Restart={{ droneci_restart_policy }} +WorkingDirectory={{ droneci_service_directory }} + +ExecStartPre=/bin/docker-compose down +ExecStartPre=/bin/docker-compose rm -f + +ExecStart=/usr/bin/docker-compose up +ExecStop=/usr/bin/docker-compose down + +[Install] +WantedBy=multi-user.target diff --git a/templates/services/droneci-compose.yml.j2 b/templates/services/droneci-compose.yml.j2 index 80f6f84..b4ee1c6 100644 --- a/templates/services/droneci-compose.yml.j2 +++ b/templates/services/droneci-compose.yml.j2 @@ -25,6 +25,7 @@ services: environment: - DRONE_SERVER_HOST={{ droneci_host | urlsplit('hostname') }} - DRONE_SERVER_PROTO={{ droneci_host | urlsplit('scheme') }} + - DRONE_AGENTS_ENABLED=true - DRONE_RPC_SECRET={{ droneci_secret }} - DRONE_USER_CREATE={{ droneci_admins | default(omit) | join(",") }} - DRONE_LOGS_COLOR=true