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