Merge branch '15380-full-text-search-docs'
[arvados.git] / tools / arvbox / lib / arvbox / docker / api-setup.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 exec 2>&1
7 set -ex -o pipefail
8
9 . /usr/local/lib/arvbox/common.sh
10 . /usr/local/lib/arvbox/go-setup.sh
11
12 cd /usr/src/arvados/services/api
13
14 if test -s /var/lib/arvados/api_rails_env ; then
15   export RAILS_ENV=$(cat /var/lib/arvados/api_rails_env)
16 else
17   export RAILS_ENV=development
18 fi
19
20 set -u
21
22 uuid_prefix=$(cat /var/lib/arvados/api_uuid_prefix)
23
24 if ! test -s /var/lib/arvados/api_secret_token ; then
25     ruby -e 'puts rand(2**400).to_s(36)' > /var/lib/arvados/api_secret_token
26 fi
27 secret_token=$(cat /var/lib/arvados/api_secret_token)
28
29 if ! test -s /var/lib/arvados/blob_signing_key ; then
30     ruby -e 'puts rand(2**400).to_s(36)' > /var/lib/arvados/blob_signing_key
31 fi
32 blob_signing_key=$(cat /var/lib/arvados/blob_signing_key)
33
34 if ! test -s /var/lib/arvados/management_token ; then
35     ruby -e 'puts rand(2**400).to_s(36)' > /var/lib/arvados/management_token
36 fi
37 management_token=$(cat /var/lib/arvados/management_token)
38
39 sso_app_secret=$(cat /var/lib/arvados/sso_app_secret)
40
41 if test -s /var/lib/arvados/vm-uuid ; then
42     vm_uuid=$(cat /var/lib/arvados/vm-uuid)
43 else
44     vm_uuid=$uuid_prefix-2x53u-$(ruby -e 'puts rand(2**400).to_s(36)[0,15]')
45     echo $vm_uuid > /var/lib/arvados/vm-uuid
46 fi
47
48 if ! test -f /var/lib/arvados/api_database_pw ; then
49     ruby -e 'puts rand(2**128).to_s(36)' > /var/lib/arvados/api_database_pw
50 fi
51 database_pw=$(cat /var/lib/arvados/api_database_pw)
52
53 if ! (psql postgres -c "\du" | grep "^ arvados ") >/dev/null ; then
54     psql postgres -c "create user arvados with password '$database_pw'"
55 fi
56 psql postgres -c "ALTER USER arvados WITH SUPERUSER;"
57
58 if test -a /usr/src/arvados/services/api/config/arvados_config.rb ; then
59     rm -f config/application.yml config/database.yml
60 else
61 cat >config/application.yml <<EOF
62 $RAILS_ENV:
63   uuid_prefix: $uuid_prefix
64   secret_token: $secret_token
65   blob_signing_key: $blob_signing_key
66   sso_app_secret: $sso_app_secret
67   sso_app_id: arvados-server
68   sso_provider_url: "https://$localip:${services[sso]}"
69   sso_insecure: false
70   workbench_address: "https://$localip/"
71   websocket_address: "wss://$localip:${services[websockets-ssl]}/websocket"
72   git_repo_ssh_base: "git@$localip:"
73   git_repo_https_base: "http://$localip:${services[arv-git-httpd]}/"
74   new_users_are_active: true
75   auto_admin_first_user: true
76   auto_setup_new_users: true
77   auto_setup_new_users_with_vm_uuid: $vm_uuid
78   auto_setup_new_users_with_repository: true
79   default_collection_replication: 1
80   docker_image_formats: ["v2"]
81   keep_web_service_url: https://$localip:${services[keep-web-ssl]}/
82   ManagementToken: $management_token
83 EOF
84
85 (cd config && /usr/local/lib/arvbox/yml_override.py application.yml)
86 sed "s/password:.*/password: $database_pw/" <config/database.yml.example >config/database.yml
87 fi
88
89 if ! test -f /var/lib/arvados/api_database_setup ; then
90    bundle exec rake db:setup
91    touch /var/lib/arvados/api_database_setup
92 fi
93
94 if ! test -s /var/lib/arvados/superuser_token ; then
95     superuser_tok=$(bundle exec ./script/create_superuser_token.rb)
96     echo "$superuser_tok" > /var/lib/arvados/superuser_token
97 fi
98
99 rm -rf tmp
100 mkdir -p tmp/cache
101
102 bundle exec rake db:migrate