18631: update shell login-sync cron example
[arvados-formula.git] / test / salt / states / examples / arvados / 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 examples_arvados_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 examples_arvados_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     - require:
48       - gem: arvados-shell-package-install-gem-arvados-cli-installed
49
50 examples_arvados_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_host_cron_env_present:
51   cron.env_present:
52     - name: ARVADOS_API_HOST
53     - value: {{ api_host }}
54     - onlyif:
55       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
56
57 examples_arvados_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_token_cron_env_present:
58   cron.env_present:
59     - name: ARVADOS_API_TOKEN
60     - value: {{ api_token }}
61     - onlyif:
62       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
63
64 examples_arvados_shell_cron_add_login_sync_add_{{ vm }}_arvados_api_host_insecure_cron_env_present:
65   cron.env_present:
66     - name: ARVADOS_API_HOST_INSECURE
67     - value: {{ arvados.cluster.tls.insecure | default(false) }}
68     - onlyif:
69       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
70
71 examples_arvados_shell_cron_add_login_sync_add_{{ vm }}_arvados_virtual_machine_uuid_cron_env_present:
72   cron.env_present:
73     - name: ARVADOS_VIRTUAL_MACHINE_UUID
74     - value: __slot__:salt:cmd.run("cat /tmp/vm_uuid_{{ vm }}")
75     - onlyif:
76       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
77
78 examples_arvados_shell_cron_add_login_sync_add_{{ vm }}_arvados_login_sync_cron_present:
79   cron.present:
80     - name: /usr/local/bin/arvados-login-sync
81     - minute: '*/2'
82     - onlyif:
83       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/vm_uuid_{{ vm }}
84
85   {%- endif %}
86 {%- endfor %}