Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit e86d3d7

Browse files
authored
Merge pull request #34 from OSAS/container
Support running in container
2 parents 68e25a4 + 205092c commit e86d3d7

File tree

7 files changed

+104
-51
lines changed

7 files changed

+104
-51
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This will deploy the build of https://git.example.org/website.git by
3232
using rsync as rsync_user on www.example.org, copying in /var/www/html. If you do
3333
not provide it, then no deployment the default builder deployment method is used
3434
instead, if any. The OpenShift deployment has been removed as version 2 is EOL.
35+
It is also possible to only provide a rsync_location without the other parameters
36+
for a local sync.
3537

3638
The script will not sync if build failed, and will not send email (that's on the
3739
TODO list, see end of the file). Nevertheless, failures caught by Cron can be
@@ -54,6 +56,8 @@ The role will rebuild the website on a regular basis, every 6h
5456
by default. This can be changed with the parameter `rebuild_interval`, which express
5557
the time between automated rebuild attempts if nothing changed, expressed in hours.
5658

59+
This feature is disabled on containers.
60+
5761
# Debug the build
5862

5963
In order to debug a non working build, the easiest is to connect to the
@@ -73,6 +77,15 @@ submodule_commits: {}
7377
Then, the build script can be run with `/usr/local/bin/build_deploy.py -d -f -n ~/website_example_org.yml`,
7478
which would force a build (-f) without pushing (-n) with debug turned on (-d).
7579

80+
This feature is disabled on containers.
81+
82+
# Containers
83+
84+
If running in a container this role will not create crontabs but instead build the website at once. You may
85+
use the rsync feature to locally install the resulting pages in the proper place. In this case the publishing
86+
space may need to be created in between the builder is setup and the website built, so you may set
87+
`builder_container_build_now` to False and use the `build` entrypoint manually when convenient.
88+
7689
# Jenkins integration
7790

7891
If you wish to use the role with a external system to trigger such as Jenkins, you will need to disable
@@ -90,3 +103,6 @@ While being already used in production, several options are missing
90103
- proper logging of error
91104
- handling automatically some errors (like rebuilding gems)
92105
- change the schedule of automated rebuild
106+
- make result directory available to subsequent roles to avoid
107+
copying files when unnecessary
108+

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ irc_server: False
99
# in hours
1010
rebuild_interval: 6
1111
external_trigger: False
12+
builder_container_build_now: True
13+

molecule/_resources/playbook.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- name: Install Web Builder
44
hosts: ansible-test-builder
55
tasks:
6-
- name: Testing role
6+
- name: "Testing role (host)"
77
include_role:
88
name: ansible-role-web_builder
99
vars:
@@ -13,4 +13,10 @@
1313
rsync_server: ansible-test-web
1414
rsync_user: web_builder
1515
rsync_location: /var/www/www.example.com
16+
- name: "Testing role (container)"
17+
include_role:
18+
name: ansible-role-web_builder
19+
vars:
20+
builder_name: testbuilder_container
21+
builder_username: web_builder
1622

tasks/build.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
3+
- name: "Build now (container)"
4+
vars:
5+
builder_options: '-f -d'
6+
block:
7+
- name: "Build the website"
8+
command: "{{ builder_command }}"
9+
become_user: "{{ builder_username }}"
10+
become: True
11+
changed_when: True
12+

tasks/main.yml

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
dest: "{{ checkout_dir }}"
7373
version: "{{ git_version | default( 'HEAD' ) }}"
7474
force: yes # needed because of submodules to avoid "Local modifications exist in repository"
75+
umask: '0022'
7576
become: yes
7677
become_user: '{{ builder_username }}'
7778
become_method: 'su'
@@ -98,69 +99,32 @@
9899
set_fact:
99100
rsync_url: "nosync"
100101

101-
- name: Compute RSYNC URL
102+
- name: "Compute RSYNC URL (remote)"
102103
set_fact:
103104
rsync_url: "{{ rsync_user }}@{{ rsync_server }}:{{ rsync_location }}"
104-
when: rsync_server is defined and rsync_location is defined and rsync_user is defined
105+
when: rsync_location is defined and rsync_server is defined and rsync_user is defined
106+
107+
- name: "Compute RSYNC URL (local)"
108+
set_fact:
109+
rsync_url: "{{ rsync_location }}"
110+
when: rsync_location is defined and rsync_server is undefined and rsync_user is undefined
105111

106112
- name: Deploy yaml config for {{ builder_name }}
107113
template:
108114
dest: "/srv/builder/{{ builder_name }}.yml"
109115
src: builder.yml.j2
110116

111-
- name: Add cron to build {{ builder_name }}
112-
cron:
113-
name: "build and deploy {{ builder_name }}"
114-
job: "/usr/local/bin/build_deploy.py /srv/builder/{{ builder_name }}.yml"
115-
user: "{{ builder_username }}"
116-
state: "{% if external_trigger %}absent{% else %}present{% endif %}"
117-
118-
- name: Setup cron email
119-
cronvar:
120-
user: "{{ builder_username }}"
121-
name: MAILTO
122-
value: "{{ cron_error_email }}"
123-
when: cron_error_email is defined
124-
125-
# needed to detect package manager (at least) on the other host
126-
# without cache or if it expired, the play would fail
127-
- name: Get facts for the other side
128-
setup:
129-
delegate_to: "{{ rsync_server }}"
130-
delegate_facts: True
131-
when: rsync_url != 'nosync' and rsync_server not in play_hosts
132-
133-
# TODO add ip restriction
134-
- name: Copy the key on the other side
135-
authorized_key:
136-
key: "{{ result.ssh_public_key }}"
137-
key_options: "{{ sync_ssh_key_options }}"
138-
user: "{{ rsync_user }}"
139-
delegate_to: "{{ rsync_server }}"
140-
when: rsync_url != 'nosync'
141-
142117
- name: Install Rsync
143118
package:
144119
name: "{{ rsync_package }}"
145120
state: present
146121
when: rsync_url != 'nosync'
147122

148-
- name: Install Rsync on the other side
149-
package:
150-
name: "{{ rsync_package }}"
151-
state: present
152-
delegate_to: "{{ rsync_server }}"
153-
when: rsync_url != 'nosync'
123+
- name: "Schedule build and sync"
124+
include_tasks: schedule_build_and_sync.yml
125+
when: ansible_env.container is undefined
154126

155-
- name: Create SSH config for rsync sync
156-
blockinfile:
157-
path: /srv/builder/.ssh/config
158-
owner: "{{ builder_username }}"
159-
group: "{{ builder_username }}"
160-
block: |
161-
Match Host {{ rsync_server }} User {{ rsync_user }}
162-
IdentityFile /srv/builder/.ssh/{{ builder_name }}_id.rsa
163-
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ builder_name }}"
164-
create: true
165-
when: rsync_url != 'nosync'
127+
- name: "Build now (container)"
128+
include_tasks: build.yml
129+
when: ansible_env.container is defined and builder_container_build_now|bool
166130

tasks/schedule_build_and_sync.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
3+
- name: Add cron to build {{ builder_name }}
4+
cron:
5+
name: "build and deploy {{ builder_name }}"
6+
job: "{{ builder_command }}"
7+
user: "{{ builder_username }}"
8+
state: "{% if external_trigger %}absent{% else %}present{% endif %}"
9+
10+
- name: Setup cron email
11+
cronvar:
12+
user: "{{ builder_username }}"
13+
name: MAILTO
14+
value: "{{ cron_error_email }}"
15+
when: cron_error_email is defined
16+
17+
# needed to detect package manager (at least) on the other host
18+
# without cache or if it expired, the play would fail
19+
- name: Get facts for the other side
20+
setup:
21+
delegate_to: "{{ rsync_server }}"
22+
delegate_facts: True
23+
when: rsync_url != 'nosync' and rsync_server not in play_hosts
24+
25+
# TODO add ip restriction
26+
- name: Copy the key on the other side
27+
authorized_key:
28+
key: "{{ result.ssh_public_key }}"
29+
key_options: "{{ sync_ssh_key_options }}"
30+
user: "{{ rsync_user }}"
31+
delegate_to: "{{ rsync_server }}"
32+
when: rsync_url != 'nosync'
33+
34+
- name: Install Rsync on the other side
35+
package:
36+
name: "{{ rsync_package }}"
37+
state: present
38+
delegate_to: "{{ rsync_server }}"
39+
when: rsync_url != 'nosync'
40+
41+
- name: Create SSH config for rsync sync
42+
blockinfile:
43+
path: /srv/builder/.ssh/config
44+
owner: "{{ builder_username }}"
45+
group: "{{ builder_username }}"
46+
block: |
47+
Match Host {{ rsync_server }} User {{ rsync_user }}
48+
IdentityFile /srv/builder/.ssh/{{ builder_name }}_id.rsa
49+
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ builder_name }}"
50+
create: true
51+
when: rsync_url != 'nosync'
52+

vars/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ builder_repo:
77
writable_subdirs:
88
- themes
99
sync_ssh_key_options: "command=\"rsync --server -vlogtrze.isf --delete-after --omit-dir-times . {{ rsync_location }}\",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty"
10+
builder_command: "/usr/local/bin/build_deploy.py {{ builder_options | default('') }} /srv/builder/{{ builder_name }}.yml"
1011

0 commit comments

Comments
 (0)