4b98d7b237d2e139add2787dff61fefd1a0e49e6
[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 # ARVADOS_VIRTUAL_MACHINE_UUID for the host and configure the arvados login-sync cron
8 # as described in https://doc.arvados.org/main/install/install-shell-server.html
9
10 {%- set curr_tpldir = tpldir %}
11 {%- set tpldir = 'arvados' %}
12 {%- from "arvados/map.jinja" import arvados with context %}
13 {%- from "arvados/libtofs.jinja" import files_switch with context %}
14 {%- set tpldir = curr_tpldir %}
15
16 {%- set virtual_machines = arvados.cluster.resources.virtual_machines | default({}) %}
17 {%- set api_token = arvados.cluster.tokens.system_root | yaml_encode %}
18 {%- set api_host = arvados.cluster.Services.Controller.ExternalURL | regex_replace('^http(s?)://', '', ignorecase=true) %}
19
20 extra_shell_cron_add_login_sync_add_jq_pkg_installed:
21   pkg.installed:
22     - name: jq
23
24 {%- for vm, vm_params in virtual_machines.items() %}
25   {%- set vm_name = vm_params.name | default(vm) %}
26
27   # Check if any of the specified virtual_machines parameters corresponds to this instance
28   # It should be an error if we get more than one occurrence
29   {%- if vm_name in [grains['id'], grains['host'], grains['fqdn'], grains['nodename']] or
30          vm_params.backend in [grains['id'], grains['host'], grains['fqdn'], grains['nodename']] +
31                                grains['ipv4'] + grains['ipv6'] %}
32
33     # We need to query the VM UUID
34     {%- set cmd_query_vm_uuid = 'arv --short virtual_machine list' ~
35                                 ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
36     %}
37
38 extra_shell_cron_add_login_sync_add_{{ vm }}_get_vm_uuid_cmd_run:
39   cmd.run:
40     - env:
41       - ARVADOS_API_TOKEN: {{ api_token }}
42       - ARVADOS_API_HOST: {{ api_host }}
43       - ARVADOS_API_HOST_INSECURE: {{ arvados.cluster.tls.insecure | default(false) }}
44     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/vm_uuid_{{ vm }}
45     - unless:
46       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
47
48 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_host_cron_env_present:
49   cron.env_present:
50     - name: ARVADOS_API_HOST
51     - value: {{ api_host }}
52     - onlyif:
53       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
54
55 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_token_cron_env_present:
56   cron.env_present:
57     - name: ARVADOS_API_TOKEN
58     - value: {{ api_token }}
59     - onlyif:
60       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
61
62 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_host_insecure_cron_env_present:
63   cron.env_present:
64     - name: ARVADOS_API_HOST_INSECURE
65     - value: {{ arvados.cluster.tls.insecure | default(false) }}
66     - onlyif:
67       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
68
69 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_virtual_machine_uuid_cron_env_present:
70   cron.env_present:
71     - name: ARVADOS_VIRTUAL_MACHINE_UUID
72     - value: __slot__:salt:cmd.run("cat /tmp/vm_uuid_{{ vm }}")
73     - onlyif:
74       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
75
76 extra_shell_cron_add_login_sync_add_{{ vm }}_arvados_login_sync_cron_present:
77   cron.present:
78     - name: arvados-login-sync
79     - minute: '*/2'
80     - onlyif:
81       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
82
83   {%- endif %}
84 {%- endfor %}