353ac6acbd9455ddc006a70c022ad122f69ddd5a
[arvados-formula.git] / test / salt / states / examples / arvados / shell / cron / add-login-sync.sls
1 # -*- coding: utf-8 -*-
2 # vim: ft=sls
3
4 # This state tries to query the controller using the parameters set in
5 # the `arvados.cluster.resources.virtual_machines` pillar, to get the
6 # scoped_token for the host and configure the arvados login-sync cron
7 # as described in https://doc.arvados.org/v2.0/install/install-shell-server.html
8
9 {#- Get the `tplroot` from `tpldir` #}
10 {%- set tplroot = tpldir.split('/')[0] %}
11 {%- set sls_config_file = tplroot ~ '.config.file' %}
12 {%- from tplroot ~ "/map.jinja" import arvados with context %}
13 {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
14
15 {%- set virtual_machines = arvados.cluster.resources.virtual_machines | default({}) %}
16 {%- set api_token = arvados.cluster.tokens.system_root | yaml_encode %}
17 {%- set api_host = arvados.cluster.Services.Controller.ExternalURL | regex_replace('^http(s?)://', '', ignorecase=true) %}
18
19 examples-arvados-shell-cron-add-login-sync-add-jq-pkg-installed:
20   pkg.installed:
21     - name: jq
22
23 {%- for vm, vm_params in virtual_machines.items() %}
24   {%- set vm_name = vm_params.name | default(vm) %}
25
26   # Check if any of the specified virtual_machines parameters corresponds to this instance
27   # It should be an error if we get more than one occurrence
28   {%- if vm_name in [grains['id'], grains['host'], grains['fqdn'], grains['nodename']] or
29          backend in [grains['id'], grains['host'], grains['fqdn'], grains['nodename']] +
30                     grains['ipv4'] + grains['ipv6'] %}
31
32     {%- set cmd_query_vm_uuid = 'arv --short virtual_machine list' ~
33                                 ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
34     %}
35
36 # We need to use the UUID generated in the previous command to see if there's a
37 # scoped token for it. There's no easy way to pass the value from a shellout
38 # to another state, so we store it in a temp file and use that in the next
39 # command. Flaky, mostly because the `unless` clause is just checking thatg
40 # the file content is a token uuid :|
41 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-get-vm_uuid-cmd-run:
42   cmd.run:
43     - env:
44       - ARVADOS_API_TOKEN: {{ api_token }}
45       - ARVADOS_API_HOST: {{ api_host }}
46     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
47     - require:
48       - cmd: examples-arvados-shell-cron-add-login-sync-add-resources-virtual-machines-{{ vm }}-record-cmd-run
49     - unless:
50       - /bin/grep -qE "fixme-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
51
52   # There's no direct way to query the scoped_token for a given virtual_machine
53   # so we need to parse the api_client_authorization list through some jq
54   {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/' ~ vm ~ ') && ' ~
55                                        'arv api_client_authorization list | ' ~
56                                        '/usr/bin/jq -e \'.items[]| select(.scopes[] == "GET ' ~
57                                        '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins") | ' ~
58                                        '.api_token\' | head -1 | tee /tmp/sctk' ~ vm ~ ' && ' ~
59                                        'unset VM_UUID'
60   %}
61
62 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-get-scoped_token-cmd-run:
63   cmd.run:
64     - env:
65       - ARVADOS_API_TOKEN: {{ api_token }}
66       - ARVADOS_API_HOST: {{ api_host }}
67     - name: {{ cmd_query_scoped_token_url }}
68     - require:
69       - cmd: examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-get-vm_uuid-cmd-run
70     - unless:
71       - test -s /tmp/sctk-{{ vm }}
72
73 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-host-cron-env-present:
74   cron.env_present:
75     - name: ARVADOS_API_HOST
76     - value: {{ api_host }}
77
78 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-api-cron-token-env-present:
79   cron.env_present:
80     - name: ARVADOS_API_TOKEN
81     - value: __slot__:salt:cmd.run(cat /tmp/sctk{{ vm }})
82
83 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-api-cron-token-env-present:
84   cron.env_present:
85     - name: ARVADOS_VIRTUAL_MACHINE_UUID
86     - value: __slot__:salt:cmd.run(cat /tmp/{{ vm }})
87
88 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-login-sync-cron-present:
89   cron.present:
90     minute: '*/2'
91     cmd: arvados-login-sync
92
93 {%- endfor %}