17750: Ensure rvm-exec is called for arv when using rvm
[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 {%- set arv_command = '/usr/local/rvm/bin/rvm-exec default arv'
19                       if arvados.ruby.manage_ruby and arvados.ruby.use_rvm
20                       else 'arv' %}
21 include:
22   - ..package
23   - {{ sls_config_file }}
24   - ..service
25
26 arvados-controller-resources-virtual-machines-jq-pkg-installed:
27   pkg.installed:
28     - name: jq
29
30 {%- for vm, vm_params in virtual_machines.items() %}
31   {%- set vm_name = vm_params.name | default(vm) %}
32
33   {%- set cmd_query_vm_uuid = 'ARVADOS_API_TOKEN=' ~ api_token ~
34                               ' ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
35                               arv_command ~ ' --short virtual_machine list' ~
36                               ' --filters \'[["hostname", "=", "' ~ vm_name ~ '"]]\''
37   %}
38
39 # Create the virtual machine record
40 arvados-controller-resources-virtual-machines-{{ vm }}-record-cmd-run:
41   cmd.run:
42     - env:
43       - ARVADOS_API_TOKEN: {{ api_token }}
44       - ARVADOS_API_HOST: {{ api_host }}
45     - name: |
46         {{ arv_command }} --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-controller-package-install-pkg-installed
55       - cmd: arvados-controller-service-running-service-ready-cmd-run
56       - gem: arvados-controller-package-install-gem-arvados-cli-installed
57
58 # We need to use the UUID generated in the previous command to see if there's a
59 # scoped token for it. There's no easy way to pass the value from a shellout
60 # to another state, so we store it in a temp file and use that in the next
61 # command. Flaky, mostly because the `unless` clause is just checking thatg
62 # the file content is a token uuid :|
63 arvados-controller-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run:
64   cmd.run:
65     {%- if arvados.ruby.manage_ruby and arvados.ruby.use_rvm %}
66     - prepend_path: /usr/local/rvm/gems/{{ arvados.ruby.pkg }}/bin
67     {%- endif %}
68     - name: {{ cmd_query_vm_uuid }} | head -1 | tee /tmp/{{ vm }}
69     - require:
70       - cmd: arvados-controller-resources-virtual-machines-{{ vm }}-record-cmd-run
71       - gem: arvados-controller-package-install-gem-arvados-cli-installed
72     - unless:
73       - /bin/grep -qE "[a-z0-9]{5}-2x53u-[a-z0-9]{15}" /tmp/{{ vm }}
74
75   # There's no direct way to query the scoped_token for a given virtual_machine
76   # so we need to parse the api_client_authorization list through some jq
77   {%- set cmd_query_scoped_token_url = 'VM_UUID=$(cat /tmp/' ~ vm ~ ') && ' ~
78                                        ' ARVADOS_API_TOKEN=' ~ api_token ~
79                                        ' ARVADOS_API_HOST=' ~ api_host ~ ' ' ~
80                                        arv_command ~ ' api_client_authorization list |' ~
81                                        ' /usr/bin/jq -e \'.items[].scopes[] | select(. == "GET ' ~
82                                        '/arvados/v1/virtual_machines/\'${VM_UUID}\'/logins")\' && ' ~
83                                        'unset VM_UUID'
84   %}
85
86 # Create the VM scoped tokens
87 arvados-controller-resources-virtual-machines-{{ vm }}-scoped-token-cmd-run:
88   cmd.run:
89     - env:
90       - ARVADOS_API_TOKEN: {{ api_token }}
91       - ARVADOS_API_HOST: {{ api_host }}
92     - name: |
93         VM_UUID=$(cat /tmp/{{ vm }}) &&
94         {{ arv_command }} --format=uuid \
95           api_client_authorization \
96           create \
97           --api-client-authorization '{"scopes":["GET /arvados/v1/virtual_machines/'${VM_UUID}'/logins"]}'
98     - unless: {{ cmd_query_scoped_token_url }}
99     - require:
100       - pkg: arvados-controller-package-install-pkg-installed
101       - pkg: arvados-controller-resources-virtual-machines-jq-pkg-installed
102       - cmd: arvados-controller-resources-virtual-machines-{{ vm }}-get-vm_uuid-cmd-run
103       - gem: arvados-controller-package-install-gem-arvados-cli-installed
104
105 {%- endfor %}