1 # -*- coding: utf-8 -*-
4 # Copyright (C) The Arvados Authors. All rights reserved.
6 # SPDX-License-Identifier: Apache-2.0
8 {#- Get the `tplroot` from `tpldir` #}
9 {%- set tplroot = tpldir.split('/')[0] %}
10 {#- set sls_config_file = tplroot ~ '.config.file' #}
11 {%- from tplroot ~ "/map.jinja" import arvados with context %}
12 {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
14 {%- set virtual_machines = arvados.cluster.resources.virtual_machines | default({}) %}
15 {%- set api_token = arvados.cluster.tokens.system_root | yaml_encode %}
16 {%- set api_host = arvados.cluster.Services.Controller.ExternalURL | regex_replace('^http(s?)://', '', ignorecase=true) %}
18 {%- set arv_command = '/usr/local/rvm/bin/rvm-exec default arv'
19 if arvados.ruby.manage_ruby and arvados.ruby.use_rvm
23 {# - {{ sls_config_file }} #}
26 arvados-shell-resources-virtual-machines-jq-pkg-installed:
30 {%- for vm, vm_params in virtual_machines.items() %}
31 {%- set vm_name = vm_params.name | default(vm) %}
32 {%- set arvados_api_host_insecure = arvados.cluster.tls.insecure | default(false) %}
34 {%- set cmd_query_vm_uuid = 'ARVADOS_API_TOKEN=' ~ api_token ~ ' ' ~
35 'ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
36 'ARVADOS_API_HOST_INSECURE=' ~ arvados_api_host_insecure ~ ' ' ~
37 arv_command ~ ' --short virtual_machine list' ~
38 ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
41 # Create the virtual machine record
42 arvados-shell-resources-virtual-machines-{{ vm }}-record-cmd-run:
45 - ARVADOS_API_TOKEN: {{ api_token }}
46 - ARVADOS_API_HOST: {{ api_host }}
47 - ARVADOS_API_HOST_INSECURE: {{ arvados.cluster.tls.insecure | default(false) }}
49 {{ arv_command }} --format=uuid \
52 --virtual-machine '{"hostname":"{{ vm_name }}" }'
54 {{ cmd_query_vm_uuid }} | \
55 /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}"
57 - pkg: arvados-shell-package-install-pkg-installed
58 - gem: arvados-shell-package-install-gem-arvados-cli-installed
60 # We need to use the UUID generated in the previous command to see if there's a
61 # scoped token for it. There's no easy way to pass the value from a shellout
62 # to another state, so we store it in a temp file and use that in the next
63 # command. Flaky, mostly because the `unless` clause is just checking thatg
64 # the file content is a token uuid :|
65 arvados-shell-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run:
67 {%- if arvados.ruby.manage_ruby and arvados.ruby.use_rvm %}
68 - prepend_path: /usr/local/rvm/gems/{{ arvados.ruby.pkg }}/bin
70 - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
72 - cmd: arvados-shell-resources-virtual-machines-{{ vm }}-record-cmd-run
73 - gem: arvados-shell-package-install-gem-arvados-cli-installed
75 - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
77 # There's no direct way to query the scoped_token for a given virtual_machine
78 # so we need to parse the api_client_authorization list through some jq
79 {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/' ~ vm ~ ') && ' ~
80 'ARVADOS_API_TOKEN=' ~ api_token ~ ' ' ~
81 'ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
82 'ARVADOS_API_HOST_INSECURE=' ~ arvados_api_host_insecure ~ ' ' ~
83 arv_command ~ ' api_client_authorization list |' ~
84 ' /usr/bin/jq -e \'.items[].scopes[] | select(. == "GET ' ~
85 '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins")\' && ' ~
89 # Create the VM scoped tokens
90 arvados-shell-resources-virtual-machines-{{ vm }}-scoped-token-cmd-run:
93 - ARVADOS_API_TOKEN: {{ api_token }}
94 - ARVADOS_API_HOST: {{ api_host }}
95 - ARVADOS_API_HOST_INSECURE: {{ arvados.cluster.tls.insecure | default(false) }}
97 VM_UUID=$(cat /tmp/{{ vm }}) &&
98 {{ arv_command }} --format=uuid \
99 api_client_authorization \
101 --api-client-authorization '{"scopes":["GET /arvados/v1/virtual_machines/'${VM_UUID}'/logins"]}'
102 - unless: {{ cmd_query_scoped_token_url }}
104 - pkg: arvados-shell-package-install-pkg-installed
105 - pkg: arvados-shell-resources-virtual-machines-jq-pkg-installed
106 - cmd: arvados-shell-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run
107 - gem: arvados-shell-package-install-gem-arvados-cli-installed