fix(arvados-controller): add a loop to verify the service is ready
[arvados-formula.git] / arvados / api / resources / virtual_machines.sls
1 # -*- coding: utf-8 -*-
2 # vim: ft=sls
3
4 {#- Get the `tplroot` from `tpldir` #}
5 {%- set tplroot = tpldir.split('/')[0] %}
6 {%- set sls_config_file = tplroot ~ '.config.file' %}
7 {%- from tplroot ~ "/map.jinja" import arvados with context %}
8 {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
9
10 {%- set virtual_machines = arvados.cluster.resources.virtual_machines | default({}) %}
11 {%- set api_token = arvados.cluster.tokens.system_root | yaml_encode %}
12 {%- set api_host = arvados.cluster.Services.Controller.ExternalURL | regex_replace('^http(s?)://', '', ignorecase=true) %}
13
14 include:
15   - ..package
16   - {{ sls_config_file }}
17   - ..service
18
19 arvados-api-resources-virtual-machines-jq-pkg-installed:
20   pkg.installed:
21     - name: jq
22
23 {%- for vm, vm_params in virtual_machines.items() %}
24   {%- set vm_name = vm_params.name | default(vm) %}
25
26   {%- set cmd_query_vm_uuid = 'ARVADOS_API_TOKEN=' ~ api_token ~
27                               ' ARVADOS_API_HOST=' ~ api_host ~
28                               ' arv --short virtual_machine list' ~
29                               ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
30   %}
31
32 # Create the virtual machine record
33 arvados-api-resources-virtual-machines-{{ vm }}-record-cmd-run:
34   cmd.run:
35     - env:
36       - ARVADOS_API_TOKEN: {{ api_token }}
37       - ARVADOS_API_HOST: {{ api_host }}
38     - name: |
39         arv --format=uuid \
40           virtual_machine \
41           create \
42           --virtual-machine '{"hostname":"{{ vm_name }}" }'
43     - unless: |
44           {{ cmd_query_vm_uuid }} | \
45           /bin/grep -qE "fixme-2x53u-[a-z0-9]{15}"
46
47   # As we need the UUID generated in the previous command, we need to
48   # iterate again in order to get them
49   {% set vm_uuid = salt['cmd.shell'](cmd_query_vm_uuid) %}
50
51   {%- set scoped_token_url = '/arvados/v1/virtual_machines/' ~ vm_uuid ~ '/logins' %}
52
53   # There's no direct way to query the scoped_token for a given virtual_machine
54   # so we need to parse the api_client_authorization list through some jq
55   {%- set cmd_query_scoped_token_url = 'ARVADOS_API_TOKEN=' ~ api_token ~
56                                        ' ARVADOS_API_HOST=' ~ api_host ~
57                                        ' arv api_client_authorization list |' ~
58                                        ' jq -e \'.items[].scopes[] | select(. == "GET ' ~
59                                        scoped_token_url ~ '")\''
60   %}
61 # Create the VM scoped tokens
62 arvados-api-resources-virtual-machines-{{ vm }}-scoped-token-cmd-run:
63   cmd.run:
64     - env:
65       - ARVADOS_API_TOKEN: {{ api_token }}
66       - ARVADOS_API_HOST: {{ api_host }}
67     - name: |
68         arv --format=uuid \
69           api_client_authorization \
70           create \
71           --api-client-authorization '{"scopes":["GET {{ scoped_token_url }}"]}'
72     - require:
73       - pkg: arvados-api-resources-virtual-machines-jq-pkg-installed
74     - unless: {{ cmd_query_scoped_token_url }}
75
76 {%- endfor %}