Skip to content

Commit 1c1484e

Browse files
committed
ci: better interaction with scaleway build server
1 parent c81cec6 commit 1c1484e

File tree

2 files changed

+162
-18
lines changed

2 files changed

+162
-18
lines changed

scripts/build/run_build_macos_arm64.sh

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,61 @@
55
# This script is designed to run on a local machine: it will clone the repos
66
# remotely and execute the `build_macos_arm64.sh` script remotely, then will
77
# download the built packages. A tag to build must be specified.
8-
#
9-
# In order to run the script, the `m1` host must be specified in
10-
# `~/.ssh/config`; for instance:
11-
#
12-
# Host m1
13-
# User m1
14-
# HostName 1.2.3.4
8+
9+
# The script requires a Scaleway secret key in the SCW_SECRET_KEY env var:
10+
# It will use scaleway_m1.sh to provision a server and use it.
1511

1612
set -euo pipefail
1713
# set -x
1814

15+
function log {
16+
echo "$@" >&2
17+
}
18+
function error {
19+
# Print an error message and exit.
20+
log "ERROR: $@"
21+
exit 1
22+
}
23+
1924
tag=${1:-}
2025

2126
if [[ ! "${tag}" ]]; then
22-
echo "Usage: $0 TAG" >&2
23-
exit 2
27+
error "Usage: $0 REF"
2428
fi
2529

26-
rdir=psycobuild
30+
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2731

28-
# Clone the repos
29-
ssh m1 rm -rf "${rdir}"
30-
ssh m1 git clone https://github.com/psycopg/psycopg2.git --branch ${tag} "${rdir}"
32+
server=$("${dir}/scaleway_m1.sh" ensure)
3133

32-
# Allow sudoing without password, to allow brew to install
33-
ssh -t m1 bash -c \
34-
'test -f /etc/sudoers.d/m1 || echo "m1 ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/m1'
34+
status=$(echo "$server" | jq -r .status)
35+
if [[ "$status" != "ready" ]]; then
36+
error "server status is $status"
37+
fi
38+
39+
# Get user, password, ip from vnc url
40+
tmp=$(echo "$server" | jq -r .vnc_url) # vnc://m1:[email protected]:5900
41+
tmp=${tmp/vnc:\/\//} # m1:[email protected]:5900
42+
user=${tmp%%:*} # m1
43+
tmp=${tmp#*:} # [email protected]:5900
44+
password=${tmp%%@*} # PASS
45+
tmp=${tmp#*@} # 1.2.3.4:5900
46+
host=${tmp%%:*} # 1.2.3.4
47+
48+
ssh="ssh ${user}@${host} -o StrictHostKeyChecking=no"
49+
50+
# Allow the user to sudo without asking for password.
51+
echo "$password" | \
52+
$ssh sh -c "test -f /etc/sudoers.d/${user} \
53+
|| sudo -S --prompt= sh -c \
54+
'echo \"${user} ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/${user}'"
55+
56+
# Clone the repos
57+
rdir=psycobuild
58+
$ssh rm -rf "${rdir}"
59+
$ssh git clone https://github.com/psycopg/psycopg2.git --branch ${tag} "${rdir}"
3560

3661
# Build the wheel packages
37-
ssh m1 "${rdir}/scripts/build/build_macos_arm64.sh"
62+
$ssh "${rdir}/scripts/build/build_macos_arm64.sh"
3863

3964
# Transfer the packages locally
40-
scp -r "m1:${rdir}/wheelhouse" .
65+
scp -r "${user}@${host}:${rdir}/wheelhouse" .

scripts/build/scaleway_m1.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
# Implement the following commands:
4+
#
5+
# ensure:
6+
#
7+
# Get data about currently provisioned M1 server on Scaleway. If needed,
8+
# provision one.
9+
#
10+
# The script requires the SCW_SECRET_KEY env var set to a valid secret.
11+
#
12+
# If successful, return the response data on stdout. It may look like:
13+
#
14+
# {
15+
# "id": "8b196119-3cea-4a9d-b916-265037a85e60",
16+
# "type": "M1-M",
17+
# "name": "mac-m1-psycopg",
18+
# "project_id": "4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb",
19+
# "organization_id": "4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb",
20+
# "ip": "1.2.3.4",
21+
# "vnc_url": "vnc://m1:[email protected]:5900",
22+
# "status": "starting",
23+
# "created_at": "2023-09-22T18:00:18.754646Z",
24+
# "updated_at": "2023-09-22T18:00:18.754646Z",
25+
# "deletable_at": "2023-09-23T18:00:18.754646Z",
26+
# "zone": "fr-par-3"
27+
# }
28+
#
29+
# delete:
30+
#
31+
# Delete one provisioned server, if available.
32+
#
33+
# See https://www.scaleway.com/en/developers/api/apple-silicon/ for api docs.
34+
35+
set -euo pipefail
36+
# set -x
37+
38+
project_id="4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb"
39+
zone=fr-par-3
40+
servers_url="https://api.scaleway.com/apple-silicon/v1alpha1/zones/${zone}/servers"
41+
42+
function log {
43+
echo "$@" >&2
44+
}
45+
function error {
46+
log "ERROR: $@"
47+
exit 1
48+
}
49+
50+
function req {
51+
method=$1
52+
shift
53+
curl -sSL --fail-with-body -X $method \
54+
-H "Content-Type: application/json" \
55+
-H "X-Auth-Token: ${SCW_SECRET_KEY}" \
56+
"$@"
57+
}
58+
function get {
59+
req GET "$@"
60+
}
61+
function post {
62+
req POST "$@"
63+
}
64+
function delete {
65+
req DELETE "$@"
66+
}
67+
68+
function server_id {
69+
# Return the id of the first server available, else the empty string
70+
servers=$(get $servers_url || error "failed to request servers list")
71+
server_ids=$(echo "$servers" | jq -r ".servers[].id")
72+
for id in $server_ids; do
73+
echo $id
74+
break
75+
done
76+
}
77+
78+
function maybe_jq {
79+
# Process the output via jq if displaying on console, otherwise leave
80+
# it unprocessed.
81+
if [ -t 1 ]; then
82+
jq .
83+
else
84+
cat
85+
fi
86+
}
87+
88+
cmd=${1:-list}
89+
case $cmd in
90+
ensure)
91+
id=$(server_id)
92+
if [[ "$id" ]]; then
93+
log "You have servers."
94+
get "$servers_url/$id" | maybe_jq
95+
else
96+
log "Creating new server."
97+
post $servers_url -d "
98+
{
99+
\"name\": \"mac-m1-psycopg\",
100+
\"project_id\": \"$project_id\",
101+
\"type\": \"M1-M\"
102+
}" | maybe_jq
103+
fi
104+
;;
105+
delete)
106+
id=$(server_id)
107+
if [[ "$id" ]]; then
108+
log "Deleting server $id."
109+
delete "$servers_url/$id" | maybe_jq
110+
else
111+
log "No server found."
112+
fi
113+
;;
114+
list)
115+
get $servers_url | maybe_jq
116+
;;
117+
*)
118+
error "Usage: $(basename $0) [list|ensure|delete]"
119+
esac

0 commit comments

Comments
 (0)