aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/gce_setup/new_grpc_docker_builder_on_startup.sh
blob: 87e8aac1e72c21b8106466fee2f9a87bd56a33cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Startup script that initializes a grpc-dev GCE machine.
#
# A grpc-docker GCE machine is based on docker container image.
#
# On startup, it copies the grpc dockerfiles to a local directory, and update its address
# so that the docker containers within it have git-on-borg-access.

# _load_metadata curls a metadata url
_load_metadata() {
  local metadata_root=http://metadata/computeMetadata/v1
  local uri=$1
  [[ -n $uri ]] || { echo "missing arg: uri" >&2; return 1; }

  if [[ $uri =~ ^'attributes/' ]]
  then
    for a in $(curl -H "X-Google-Metadata-Request: True" $metadata_root/instance/attributes/)
    do
      [[ $uri =~ "/$a"$ ]] && { curl $metadata_root/instance/$uri -H "X-Google-Metadata-Request: True"; return; }
    done
  fi

  # if the uri is a full request uri
  [[ $uri =~ ^$metadata_root ]] && { curl $uri -H "X-Google-Metadata-Request: True"; return; }
}

_source_gs_script() {
  local script_attr=$1
  [[ -n $script_attr ]] || { echo "missing arg: script_attr" >&2; return 1; }

  local gs_uri=$(_load_metadata "attributes/$script_attr")
  [[ -n $gs_uri ]] || { echo "missing metadata: $script_attr" >&2; return 1; }

  local out_dir='/var/local/startup_scripts'
  local script_path=$out_dir/$(basename $gs_uri)
  mkdir -p $out_dir
  gsutil cp $gs_uri $script_path || {
    echo "could not cp $gs_uri -> $script_path"
    return 1
  }
  chmod a+rwx $out_dir $script_path
  source $script_path
}

main() {
    local script_attr='shared_startup_script_url'
    _source_gs_script $script_attr || {
      echo "halting, script 'attributes/$script_attr' could not be sourced"
      return 1
    }
    grpc_dockerfile_pull
    chmod -R a+rw /var/local/dockerfile

    # Install git and emacs
    apt-get update && apt-get install -y git emacs || return 1

    # Enable access to git repos on git-on-borg
    local git_root='/var/local/git'
    install_gob_daemon $git_root/gerrit-gcompute-tools || return 1

    # Startup the docker registry
    grpc_docker_launch_registry && grpc_docker_pull_known

    # Add a sentinel file to indicate that startup has completed.
    local sentinel_file=/var/log/GRPC_DOCKER_IS_UP
    touch $sentinel_file
}

set -x
main "$@"