~verterok/canonical-identity-provider/bionic-support

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
---
- hosts: localhost

  vars:
    user: "ubunet"
    hostdir: /srv/{{ hostname }}
    basedir: /srv/{{ hostname }}/{{ deployment }}
    code_dir: "{{ basedir }}/code"
    archive_dir: "{{ basedir }}/code/archives"
    current_dir: "{{ code_dir }}/{{ build_label }}"
    venv: "{{ current_dir }}/env"
    python: "{{ venv }}/bin/python"
    wheel_dir: "{{ current_dir }}/branches/wheels"
    logs_dir: "{{ basedir }}/logs"
    sso_log: "{{ logs_dir }}/sso.log"
    oops_dir: "{{ basedir }}/logs/www-oops"
    conf_dir: "{{ basedir }}/etc"
    run_dir: "{{ basedir }}/run"
    bin_dir: "{{ basedir }}/bin"
    migrate_log_dir: "{{ logs_dir }}/schema-updates"
    db_readonly_path: "{{ run_dir }}"
    port: 8080
    conn_check_script: "{{ current_dir }}/scripts/settings-to-conncheck.py"
    google_saml_key_path: "{{ conf_dir }}/saml-google-key.pem"
    google_saml_cert_path: "{{ conf_dir }}/saml-google-cert.pem"
    user_authorized_key_dir: "/etc/ssh/user-authorized-keys/"
    user_authorized_key_path: "{{ user_authorized_key_dir }}/{{ user }}"
    manage: django_project/manage.py
    DJANGO_SETTINGS_MODULE: "{{ django_settings_module }}"
    syslog_keep_days: 90

  pre_tasks:
    - name: detect if talisker is available
      tags:
          - config-changed
          - wsgi-file-relation-changed
      stat: path={{ code_dir }}/current/env/bin/talisker
      register: talisker

  roles:
    - role: nrpe-external-master
      check_name: check_http
      check_params: "-I 127.0.0.1 -H {{ hostname }} -p {{ port }} -e ' 200 OK' -u '/'"
      service_description: "Verify sso is responding."
      service_context: "{{ relations['nrpe-external-master'][0].nagios_host_context }}"
      service_groups: "{{ nagios_servicegroups }}"
      when: relations['nrpe-external-master'] and 'nagios_host_context' in relations['nrpe-external-master'][0]

    - role: directories-and-permissions
      user: "ubunet"
      readonly_dirs:
        - "{{ code_dir }}"
        - "{{ archive_dir }}"
        - "{{ conf_dir }}"
        - "{{ bin_dir }}"
      writable_dirs:
        - "{{ logs_dir }}"
        - "{{ run_dir }}"
        - "{{ oops_dir }}"
        - "{{ migrate_log_dir }}"

    - role: payload
      group: "{{ user }}"
      payload_dir: "{{ code_dir }}"
      payload_archive: "{{ build_label }}/canonical-identity-provider.tbz2"
      base_uri: "{{ asset_base_uri }}"
      # passed through automatically, but documenting
      # swift_auth_url: "{{ swift_auth_url }}"
      # swift_credentials: "{{ swift_credentials }}"
      when: build_label != ""

    - role: wsgi-app
      current_symlink: "{{ build_label }}"
      service_name: "{{ hostname }}"
      listen_port: "{{ port }}"
      log_dir: "{{ logs_dir }}"
      wsgi_user: "{{ user }}"
      wsgi_group: "{{ user }}"
      wsgi_application: django_project.wsgi
      env_extra: "DJANGO_SETTINGS_MODULE={{ DJANGO_SETTINGS_MODULE }}"
      # clean up any old config in the relation
      wsgi_extra: ""
      wsgi_extra_config: |
            reload = {{ 'True' if deployment == 'devel' else 'False' }}
            statsd_prefix = '{{ statsd_prefix }}'
            {% if statsd_hostport %}statsd_host = '{{ statsd_hostport }}'{% endif %}
      python_path: "{{ conf_dir }}"
      # hardcode to always run the current symlink
      gunicorn_path: "{{ code_dir }}/current/env/bin/{{ 'talisker' if talisker.stat.exists else 'gunicorn' }}"
      when: build_label != ""


  tasks:

    - name: check we have a working payload
      tags: 
        - config-changed
        - memcached-relation-changed
        - conn-check-relation-changed
        - wsgi-file-relation-changed
        - preload
        - migrate
      stat: path={{ current_dir }}
      register: payload

    - name: install required charm packages
      tags:
        - install
        - upgrade-charm
      apt: pkg={{ item }}
      with_items:
        - unzip
        - python-pip
        # workaround to fix CI basing off broken staging missing libpq
        - libpq5

    - name: install virtualenv-tools
      tags: 
        - install
        - upgrade-charm
      pip:
        executable: pip2
        chdir: "{{ charm_dir }}"
        name: lib/virtualenv-tools
        extra_args: "--upgrade --no-index"

    - name: check postfix relayhost
      tags: config-changed
      shell: postconf relayhost
      when: email_hostport != ""
      register: relayhost

    - name: enable postfix forwarder
      tags: config-changed
      shell: postconf -e relayhost={{ email_hostport }} && service postfix restart
      when: email_hostport != "" and email_hostport not in relayhost.stdout

    - name: readonly file
      tags: 
        - config-changed
      file: 
        path: "{{ db_readonly_path }}/db.readonly"
        owner: "{{ user }}"
        group: "{{ user }}"
        state: "{% if readonly %}touch{% else %}absent{% endif %}"

    # temporary task to handle migration to talisker logging
    - name: test for linked logfile
      tags:
          - config-changed
      stat: path={{ sso_log }}
      register: log_file
      when: talisker.stat.exists

    # temporary task to handle migration to talisker logging
    - name: mv old log file
      tags:
          - config-changed
      command: mv {{ sso_log }} {{ sso_log }}.bak
      when: talisker.stat.exists and not log_file.stat.islnk

    - name: link log file
      tags:
          - config-changed
      file:
        path: "{{ logs_dir }}/sso.log"
        src: /var/log/upstart/gunicorn.log
        state: link
      # temporary when until migrated to talisker
      when: talisker.stat.exists

    # the next 2 tasks should really only run when the payload has *changed*
    - name: Install required sso packages
      tags:
        - config-changed
        - preload
      shell: xargs apt-get install -y < {{ current_dir }}/dependencies.txt
      when: build_label != "" and payload.stat.exists

    - name: relocate venv
      tags:
        - config-changed
        - preload
      shell: virtualenv-tools --update-path=auto {{ venv }}
      when: build_label != "" and payload.stat.exists 

    - name: Write charm config
      tags:
        - config-changed
        - memcached-relation-changed
        - memcached-relation-departed
        - migrate
      template:
        src: "{{ charm_dir }}/templates/settings.py.j2"
        dest: "{{ conf_dir }}/test_settings.py"
      when: build_label != "" and payload.stat.exists

    - name: verify settings (will fail if combined config does not parse)
      tags:
        - config-changed
        - memcached-relation-changed
        - memcached-relation-departed
        - migrate
      sudo: yes
      sudo_user: "{{ user }}"
      command: "{{ python }} {{ manage }} check --pythonpath={{ conf_dir }} --settings=test_settings"
      args:
        chdir: "{{ current_dir }}"
      when: build_label != "" and payload.stat.exists

    - name: set config file
      tags:
        - config-changed
        - memcached-relation-changed
        - memcached-relation-departed
        - migrate
      command: mv "{{ conf_dir }}/test_settings.py" "{{ conf_dir }}/settings.py"
      when: build_label != "" and payload.stat.exists
      notify: Restart wsgi

    - name: clean up test settings
      tags:
        - config-changed
        - memcached-relation-changed
        - memcached-relation-departed
        - migrate
      command: rm -f {{ conf_dir }}/test_settings.*
      when: build_label != "" and payload.stat.exists

    - name: Write manage.py runner
      tags:
        - config-changed
      template:
        src: "{{ charm_dir }}/templates/manage.j2"
        dest: "{{ bin_dir }}/manage"
        mode: 0750
      when: build_label != "" and payload.stat.exists

    - name: register leadership data
      tags:
        - config-changed
        - leader-elected
        - leader-settings-changed
      command: is-leader
      register: is_leader

    - name: create maintenance crontabs
      tags:
        - config-changed
        - leader-elected
        - leader-settings-changed
      cron:
        name: "sso maintenance tasks - {{item.name}}"
        special_time: '{{item.when}}'
        job: "IS_LEADER='{{ is_leader.stdout }}'; if [ $IS_LEADER = 'True' ]; then {{ bin_dir }}/manage {{ item.command }} 2>&1 | logger -t sso-maintenance; fi"
        cron_file: sso-periodic-maintenance
        user: "{{ user }}"
      with_items:
        - name: clean_old_authtokens
          command: "clean_old_authtokens"
          when: "daily"
        - name: cleanup --sessions
          command: "cleanup --sessions"
          when: "hourly"
        - name: cleanup --nonces
          command: "cleanup --nonces"
          when: "daily"
        - name: cleanup --testdata
          command: "cleanup --testdata"
          when: "daily"
        - name: check_unverified_accounts_consistency
          command: "check_unverified_accounts_consistency"
          when: "daily"
        - name: suspend_unverified_accounts
          command: "suspend_unverified_accounts"
          when: "daily"
        - name: delete_suspended_accounts
          command: "delete_suspended_accounts"
          when: "daily"

    - name: migration
      tags:
        - migrate
      include: roles/django/tasks/main.yaml
      sudo: yes
      sudo_user: "{{ user }}"
      vars:
        src_dir: "{{ current_dir }}"
        grant_user: "{{ db_user }}"
        migrate_env:
          DB_USER: "{{ migrate_user }}"
          DB_PASSWORD: "{{ migrate_password }}"
          SSO_LOGS_DIR: "{{ migrate_log_dir }}"
          PYTHONPATH: "{{ conf_dir }}"
          DJANGO_SETTINGS_MODULE: "{{ DJANGO_SETTINGS_MODULE }}"
      when: build_label != "" and payload.stat.exists

    - name: Write google SAML key from config
      tags: config-changed
      copy: 
        content: "{{ google_saml_key | b64decode }}"
        dest: "{{ google_saml_key_path }}"
        group: "{{ user }}"
        mode: 0440
      when: google_saml_key != ""
      notify: Restart wsgi

    - name: Write google SAML cert from config
      tags: config-changed
      copy:
        content: "{{ google_saml_cert | b64decode }}"
        dest: "{{ google_saml_cert_path }}"
        group: "{{ user }}"
        mode: 0440
      when: google_saml_cert != ""
      notify: Restart wsgi

    - name: website name
      tags:
        - website-relation-changed
      command: >
        relation-set -r {{ item.__relid__ }}
        hostname={{ local_unit.split('/')[0] }}
        port={{ port }}
      with_items: relations.website

    - name: write conn-check config
      tags:
        - config-changed
        - conn-check-relation-changed
      shell: >
          relation-set -r {{ item.__relid__ }} 
          config="$({{python }} {{ conn_check_script }} -m {{ DJANGO_SETTINGS_MODULE }} --print | grep -v WARNING)" 
          nagios_servicegroups={{ nagios_servicegroups }}
      args:
        chdir: "{{ current_dir }}"
      environment:
          PYTHONPATH: ".:lib:{{ conf_dir }}"
          DJANGO_SETTINGS_MODULE: "{{ DJANGO_SETTINGS_MODULE }}"
      with_items: relations['conn-check']
      when: build_label != "" and payload.stat.exists

    - name: Write rsyslog logrotate configuration
      tags:
        - config-changed
      template:
        src: "{{ charm_dir }}/templates/rsyslog.j2"
        dest: "/etc/logrotate.d/rsyslog"
        mode: 0644

    - name: Create authorized key directory 
      tags: config-changed
      file: 
        path: "{{ user_authorized_key_dir }}"
        state: directory
        mode: 0755
        owner: root
        group: root
      when: user_authorized_key != ""

    - name: Write authorized SSH key from config
      tags: config-changed
      copy: 
        content: "{{ user_authorized_key }}"
        dest: "{{ user_authorized_key_path }}"
        mode: 0444
      when: user_authorized_key != ""