18631: add login-sync cron to provision script
[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     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/vm_uuid_{{ vm }}
53     - require:
54       - cmd: arvados-controller-resources-virtual-machines-{{ vm }}-record-cmd-run
55     - unless:
56       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
57
58   # There's no direct way to query the scoped_token for a given virtual_machine
59   # so we need to parse the api_client_authorization list through some jq
60   {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/vm_uuid_' ~ vm ~ ') && ' ~
61                                        'arv api_client_authorization list | ' ~
62                                        '/usr/bin/jq -e \'.items[]| select(.scopes[] == "GET ' ~
63                                        '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins") | ' ~
64                                        '.api_token\' | head -1 | tee /tmp/scoped_token_' ~ vm ~ ' && ' ~
65                                        'unset VM_UUID'
66   %}
67
68 extra_shell_cron_add_login_sync_add_{{ vm }}_get_scoped_token_cmd_run:
69   cmd.run:
70     - env:
71       - ARVADOS_API_TOKEN: {{ api_token }}
72       - ARVADOS_API_HOST: {{ api_host }}
73     - name: {{ cmd_query_scoped_token_url }}
74     - require:
75       - cmd: extra_shell_cron_add_login_sync_add_{{ vm }}_get_vm_uuid_cmd_run
76     - unless:
77       - test -s /tmp/scoped_token_{{ vm }}
78
79 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_host_cron_env_present:
80   cron.env_present:
81     - name: ARVADOS_API_HOST
82     - value: {{ api_host }}
83
84 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_token_cron_env_present:
85   cron.env_present:
86     - name: ARVADOS_API_TOKEN
87     - value: __slot__:salt:cmd.run("cat /tmp/scoped_token_{{ vm }}")
88
89 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_virtual_machine_uuid_cron_env_present:
90   cron.env_present:
91     - name: ARVADOS_VIRTUAL_MACHINE_UUID
92     - value: __slot__:salt:cmd.run("cat /tmp/scoped_token_{{ vm }}")
93
94 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_login_sync_cron_present:
95   cron.present:
96     - name: arvados-login-sync
97     - minute: '*/2'
98
99   {%- endif %}
100 {%- endfor %}