1 # -*- coding: utf-8 -*-
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
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 %}
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) %}
19 examples-arvados-shell-cron-add-login-sync-add-jq-pkg-installed:
23 {%- for vm, vm_params in virtual_machines.items() %}
24 {%- set vm_name = vm_params.name | default(vm) %}
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'] %}
32 {%- set cmd_query_vm_uuid = 'arv --short virtual_machine list' ~
33 ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
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:
44 - ARVADOS_API_TOKEN: {{ api_token }}
45 - ARVADOS_API_HOST: {{ api_host }}
46 - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
48 - cmd: examples-arvados-shell-cron-add-login-sync-add-resources-virtual-machines-{{ vm }}-record-cmd-run
50 - /bin/grep -qE "fixme-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
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 ~ ' && ' ~
62 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-get-scoped_token-cmd-run:
65 - ARVADOS_API_TOKEN: {{ api_token }}
66 - ARVADOS_API_HOST: {{ api_host }}
67 - name: {{ cmd_query_scoped_token_url }}
69 - cmd: examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-get-vm_uuid-cmd-run
71 - test -s /tmp/sctk{{ vm }}
73 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-host-cron-env-present:
75 - name: ARVADOS_API_HOST
76 - value: {{ api_host }}
78 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-api-cron-token-env-present:
80 - name: ARVADOS_API_TOKEN
81 - value: __slot__:salt:cmd.run(cat /tmp/sctk{{ vm }})
83 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-api-cron-token-env-present:
85 - name: ARVADOS_VIRTUAL_MACHINE_UUID
86 - value: __slot__:salt:cmd.run(cat /tmp/{{ vm }})
88 examples-arvados-shell-cron-add-login-sync-add-{{ vm }}-arvados-login-sync-cron-present:
91 cmd: arvados-login-sync