about summary refs log tree commit diff
path: root/run-vultr.nix
blob: 4d20b0e34235a91f51f4431f55b2ff4a6b07eeb2 (plain) (blame)
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
# SPDX-FileCopyrightText: 2022-2023 Alyssa Ross <hi@alyssa.is>
# SPDX-License-Identifier: MIT

{ pkgs ? import <nixpkgs> {} }:

let
  inherit (pkgs) lib;

  ipxe = import ./ipxe.nix { inherit lib; };
in

pkgs.writeShellScript "spectrum-infra-run-vultr" ''
  set -ue

  script_id="$(jq -n '{
    name: "spectrum-infra-boot",
    type: "pxe",
    script: $script | @base64,
  }' --arg script ${lib.escapeShellArg ipxe} |
  curl -fsLS https://api.vultr.com/v2/startup-scripts \
    -H "Authorization: Bearer $vultr_key" \
    -H "Content-Type: application/json" \
    --data-binary @- |
  jq -r .startup_script.id)"

  read instance_id main_ip <<EOF
  $(
  for region in fra ams cdg mad sto waw; do
    jq -n '{
      region: $region,
      plan: "voc-g-16c-64gb-320s-amd",
      os_id: 159,
      script_id: $script_id,
      user_data: $user_data | @base64,
      enable_ipv6: true,
      persistent_pxe: true,
    }' \
      --arg region "$region" \
      --arg script_id "$script_id" \
      --arg user_data "$(< user-data)" |
    curl -fsLS https://api.vultr.com/v2/instances \
      -H "Authorization: Bearer $vultr_key" \
      -H "Content-Type: application/json" \
      --data-binary @- |
    jq -r '.instance | "\(.id) \(.main_ip)"' &&
    break
  done
  )
  EOF
  echo "Instance ID: $instance_id"
  echo -n "Main IP: "
  while [ "$main_ip" = 0.0.0.0 ]; do
    sleep 1
    main_ip="$(curl -fsLSH "Authorization: Bearer $vultr_key" \
        "https://api.vultr.com/v2/instances/$instance_id" |
        jq -r .instance.main_ip)"
  done
  printf "%s\n" "$main_ip"
''