Merge branch '21905-no-rvm'
[arvados-formula.git] / arvados / shell / resources / virtual_machines.sls
1 # -*- coding: utf-8 -*-
2 # vim: ft=sls
3
4 # Copyright (C) The Arvados Authors. All rights reserved.
5 #
6 # SPDX-License-Identifier: Apache-2.0
7
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 %}
13
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) %}
17
18 include:
19   - ..package
20   {# - {{ sls_config_file }} #}
21   # - ..service
22
23 arvados-shell-resources-virtual-machines-jq-pkg-installed:
24   pkg.installed:
25     - name: jq
26
27 {%- for vm, vm_params in virtual_machines.items() %}
28   {%- set vm_name = vm_params.name | default(vm) %}
29   {%- set arvados_api_host_insecure = arvados.cluster.tls.insecure | default(false) %}
30
31   {%- set cmd_query_vm_uuid = 'ARVADOS_API_TOKEN=' ~ api_token ~ ' ' ~
32                               'ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
33                               'ARVADOS_API_HOST_INSECURE=' ~ arvados_api_host_insecure ~
34                               ' arv --short virtual_machine list' ~
35                               ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
36   %}
37
38 # Create the virtual machine record
39 arvados-shell-resources-virtual-machines-{{ vm }}-record-cmd-run:
40   cmd.run:
41     - env:
42       - ARVADOS_API_TOKEN: {{ api_token }}
43       - ARVADOS_API_HOST: {{ api_host }}
44       - ARVADOS_API_HOST_INSECURE: {{ arvados.cluster.tls.insecure | default(false) }}
45     - name: |
46         arv --format=uuid \
47           virtual_machine \
48           create \
49           --virtual-machine '{"hostname":"{{ vm_name }}" }'
50     - unless: |
51           {{ cmd_query_vm_uuid }} | \
52           /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}"
53     - require:
54       - pkg: arvados-shell-package-install-pkg-installed
55       - gem: arvados-shell-package-install-gem-arvados-cli-installed
56
57 # We need to use the UUID generated in the previous command to see if there's a
58 # scoped token for it. There's no easy way to pass the value from a shellout
59 # to another state, so we store it in a temp file and use that in the next
60 # command. Flaky, mostly because the `unless` clause is just checking thatg
61 # the file content is a token uuid :|
62 arvados-shell-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run:
63   cmd.run:
64     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
65     - require:
66       - cmd: arvados-shell-resources-virtual-machines-{{ vm }}-record-cmd-run
67       - gem: arvados-shell-package-install-gem-arvados-cli-installed
68     - unless:
69       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
70
71   # There's no direct way to query the scoped_token for a given virtual_machine
72   # so we need to parse the api_client_authorization list through some jq
73   {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/' ~ vm ~ ') && ' ~
74                                        'ARVADOS_API_TOKEN=' ~ api_token ~ ' ' ~
75                                        'ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
76                                        'ARVADOS_API_HOST_INSECURE=' ~ arvados_api_host_insecure ~
77                                        ' arv api_client_authorization list |' ~
78                                        ' /usr/bin/jq -e \'.items[].scopes[] | select(. == "GET ' ~
79                                        '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins")\' && ' ~
80                                        'unset VM_UUID'
81   %}
82
83 # Create the VM scoped tokens
84 arvados-shell-resources-virtual-machines-{{ vm }}-scoped-token-cmd-run:
85   cmd.run:
86     - env:
87       - ARVADOS_API_TOKEN: {{ api_token }}
88       - ARVADOS_API_HOST: {{ api_host }}
89       - ARVADOS_API_HOST_INSECURE: {{ arvados.cluster.tls.insecure | default(false) }}
90     - name: |
91         VM_UUID=$(cat /tmp/{{ vm }}) &&
92         arv --format=uuid \
93           api_client_authorization \
94           create \
95           --api-client-authorization '{"scopes":["GET /arvados/v1/virtual_machines/'${VM_UUID}'/logins"]}'
96     - unless: {{ cmd_query_scoped_token_url }}
97     - require:
98       - pkg: arvados-shell-package-install-pkg-installed
99       - pkg: arvados-shell-resources-virtual-machines-jq-pkg-installed
100       - cmd: arvados-shell-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run
101       - gem: arvados-shell-package-install-gem-arvados-cli-installed
102
103 {%- endfor %}