1#!/usr/bin/env nix-shell
2#!nix-shell update-pulumi-shell.nix -i bash
3# shellcheck shell=bash
4# Bash 3 compatible for Darwin
5
6if [ -z "${GITHUB_TOKEN}" ] || [ $# -ne 1 ]; then
7 echo >&2 "usage: GITHUB_TOKEN=… ./update.sh pulumi-version"
8 exit 1
9fi
10
11SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
12
13# Version of Pulumi from
14# https://www.pulumi.com/docs/get-started/install/versions/
15VERSION=$1
16
17# An array of plugin names. The respective repository inside Pulumi's
18# Github organization is called pulumi-$name by convention.
19
20declare -a pulumi_repos
21pulumi_repos=(
22 "aiven"
23 "akamai"
24 "alicloud"
25 "artifactory"
26 "auth0"
27 "aws"
28 "azure"
29 "azuread"
30 "azuredevops"
31 "cloudflare"
32 "consul"
33 "datadog"
34 "digitalocean"
35 "docker"
36 "equinix-metal"
37 "fastly"
38 "gcp"
39 "github"
40 "gitlab"
41 "google-native"
42 "hcloud"
43 "kubernetes"
44 "linode"
45 "mailgun"
46 "mysql"
47 "openstack"
48 "postgresql"
49 "random"
50 "snowflake"
51 "spotinst"
52 "sumologic"
53 "tailscale"
54 "tls"
55 "vault"
56 "venafi"
57 "vsphere"
58 "wavefront"
59 "yandex"
60)
61
62# Contains latest release ${VERSION} from
63# https://github.com/pulumi/pulumi-${NAME}/releases
64
65# Dynamically builds the plugin array, using the GitHub API for getting the
66# latest version.
67plugin_num=1
68plugins=()
69for key in "${pulumi_repos[@]}"; do
70 plugin="${key}=$(gh api "repos/pulumi/pulumi-${key}/releases/latest" --jq '.tag_name | sub("^v"; "")')"
71 printf "%20s: %s of %s\r" "${plugin}" "${plugin_num}" "${#pulumi_repos[@]}"
72 plugins+=("${plugin}")
73 sleep 1
74 ((++plugin_num))
75done
76printf "\n"
77
78function genMainSrc() {
79 local url="https://get.pulumi.com/releases/sdk/pulumi-v${VERSION}-${1}-${2}.tar.gz"
80 local sha256
81 sha256=$(nix-prefetch-url "$url")
82 echo " {"
83 echo " url = \"${url}\";"
84 echo " sha256 = \"$sha256\";"
85 echo " }"
86}
87
88function genSrc() {
89 local url="${1}"
90 local plug="${2}"
91 local tmpdir="${3}"
92
93 local sha256
94 sha256=$(nix-prefetch-url "$url")
95
96 {
97 if [ -n "$sha256" ]; then # file exists
98 echo " {"
99 echo " url = \"${url}\";"
100 echo " sha256 = \"$sha256\";"
101 echo " }"
102 else
103 echo " # pulumi-resource-${plug} skipped (does not exist on remote)"
104 fi
105 } > "${tmpdir}/${plug}.nix"
106}
107
108function genSrcs() {
109 local tmpdir
110 tmpdir="$(mktemp -d)"
111
112 for plugVers in "${plugins[@]}"; do
113 local plug=${plugVers%=*}
114 local version=${plugVers#*=}
115 # url as defined here
116 # https://github.com/pulumi/pulumi/blob/06d4dde8898b2a0de2c3c7ff8e45f97495b89d82/pkg/workspace/plugins.go#L197
117 local url="https://api.pulumi.com/releases/plugins/pulumi-resource-${plug}-v${version}-${1}-${2}.tar.gz"
118 genSrc "${url}" "${plug}" "${tmpdir}" &
119 done
120
121 wait
122
123 find "${tmpdir}" -name '*.nix' -print0 | sort -z | xargs -r0 cat
124 rm -r "${tmpdir}"
125}
126
127{
128 cat << EOF
129# DO NOT EDIT! This file is generated automatically by update.sh
130{ }:
131{
132 version = "${VERSION}";
133 pulumiPkgs = {
134EOF
135
136 echo " x86_64-linux = ["
137 genMainSrc "linux" "x64"
138 genSrcs "linux" "amd64"
139 echo " ];"
140
141 echo " x86_64-darwin = ["
142 genMainSrc "darwin" "x64"
143 genSrcs "darwin" "amd64"
144 echo " ];"
145
146 echo " aarch64-linux = ["
147 genMainSrc "linux" "arm64"
148 genSrcs "linux" "arm64"
149 echo " ];"
150
151 echo " aarch64-darwin = ["
152 genMainSrc "darwin" "arm64"
153 genSrcs "darwin" "arm64"
154 echo " ];"
155
156 echo " };"
157 echo "}"
158
159} > "${SCRIPT_DIR}/data.nix"