Add copyright headers to comply with Arvados
[arvados-formula.git] / arvados / controller / 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-controller-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
30   {%- set cmd_query_vm_uuid = 'ARVADOS_API_TOKEN=' ~ api_token ~
31                               ' ARVADOS_API_HOST=' ~ api_host ~
32                               ' arv --short virtual_machine list' ~
33                               ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
34   %}
35
36 # Create the virtual machine record
37 arvados-controller-resources-virtual-machines-{{ vm }}-record-cmd-run:
38   cmd.run:
39     - env:
40       - ARVADOS_API_TOKEN: {{ api_token }}
41       - ARVADOS_API_HOST: {{ api_host }}
42     - name: |
43         arv --format=uuid \
44           virtual_machine \
45           create \
46           --virtual-machine '{"hostname":"{{ vm_name }}" }'
47     - unless: |
48           {{ cmd_query_vm_uuid }} | \
49           /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}"
50     - require:
51       - pkg: arvados-controller-package-install-pkg-installed
52       - cmd: arvados-controller-service-running-service-ready-cmd-run
53
54 # We need to use the UUID generated in the previous command to see if there's a
55 # scoped token for it. There's no easy way to pass the value from a shellout
56 # to another state, so we store it in a temp file and use that in the next
57 # command. Flaky, mostly because the `unless` clause is just checking thatg
58 # the file content is a token uuid :|
59 arvados-controller-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run:
60   cmd.run:
61     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
62     - require:
63       - cmd: arvados-controller-resources-virtual-machines-{{ vm }}-record-cmd-run
64     - unless:
65       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
66
67   # There's no direct way to query the scoped_token for a given virtual_machine
68   # so we need to parse the api_client_authorization list through some jq
69   {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/' ~ vm ~ ') && ' ~
70                                        ' ARVADOS_API_TOKEN=' ~ api_token ~
71                                        ' ARVADOS_API_HOST=' ~ api_host ~
72                                        ' arv api_client_authorization list |' ~
73                                        ' /usr/bin/jq -e \'.items[].scopes[] | select(. == "GET ' ~
74                                        '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins")\' && ' ~
75                                        'unset VM_UUID'
76   %}
77
78 # Create the VM scoped tokens
79 arvados-controller-resources-virtual-machines-{{ vm }}-scoped-token-cmd-run:
80   cmd.run:
81     - env:
82       - ARVADOS_API_TOKEN: {{ api_token }}
83       - ARVADOS_API_HOST: {{ api_host }}
84     - name: |
85         VM_UUID=$(cat /tmp/{{ vm }}) &&
86         arv --format=uuid \
87           api_client_authorization \
88           create \
89           --api-client-authorization '{"scopes":["GET /arvados/v1/virtual_machines/'${VM_UUID}'/logins"]}'
90     - unless: {{ cmd_query_scoped_token_url }}
91     - require:
92       - pkg: arvados-controller-package-install-pkg-installed
93       - pkg: arvados-controller-resources-virtual-machines-jq-pkg-installed
94       - cmd: arvados-controller-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run
95
96 {%- endfor %}