Skip to content

Commit

Permalink
add scaleway pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Feb 21, 2024
1 parent fb892fa commit e148d6b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
13 changes: 3 additions & 10 deletions dev/vbrowser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@ apt-get install -y dnsutils
# disable unattended-upgrades
# apt-get remove -y unattended-upgrades

# disable ipv6 on reboot (fixes region detection sometimes)
# echo '[Unit]
# Description=Disable IPV6
# [Service]
# Type=oneshot
# ExecStart=/bin/sh -c "sysctl -w net.ipv6.conf.all.disable_ipv6=1 && sysctl -w net.ipv6.conf.default.disable_ipv6=1"
# [Install]
# WantedBy=multi-user.target' > /etc/systemd/system/ipv6.service
# systemctl enable ipv6.service

# determine if this is a large or not via nproc
# This systemd config starts the vbrowser on reboot (or instances created from a snapshot of this init vm)
echo '
#!/bin/bash
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 5000
# create random password
# uuidgen > ~/password
# PASSWORD=$(cat ~/password)
PASSWORD=$(hostname)
#RESOLUTION=$(if [ "$(nproc)" -le "2" ]; then echo "1280x720@30"; else echo "1920x1080@30"; fi)
RESOLUTION="1280x720@30"
Expand Down
14 changes: 10 additions & 4 deletions server/vm/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,20 @@ export abstract class VMManager {
}
}
console.log('[RESET]', this.getPoolName(), vmid, roomId);
await this.rebootVM(vmid);
// we could crash here and then row will remain in used state
// Once the heartbeat becomes stale cleanup will reset it

// We generally want to reuse if the provider has per-hour billing
// Since most user sessions are less than an hour
// Otherwise if it's per-second or Docker, it's easier to just terminate it on reboot
if (this.reuseVMs) {
// To reset without API calls:
// Update vbrowser image to generate password and save to file
// vbrowser uses file content as password
// ssh to vbrowser and sudo reboot
// Image serves file content at secret endpoint
// on vmWorker, checkvmready checks secret endpoint for password and updates DB
// add option to force reimage VM (in case of chrome updates)
await this.rebootVM(vmid);
// we could crash here and then row will remain in used state
// Once the heartbeat becomes stale cleanup will reset it
const result = await postgres.query(
`
INSERT INTO vbrowser(pool, vmid, "creationTime", state)
Expand Down
48 changes: 26 additions & 22 deletions server/vm/scaleway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,32 @@ export class Scaleway extends VMManager {
};

listVMs = async (filter?: string) => {
const mapping: StringDict = {
available: 'available',
inUse: 'inUse',
};
let tags = mapping[filter as string];
// console.log(filter, tags);
const response = await axios({
method: 'GET',
url: `https://api.scaleway.com/instance/v1/zones/${region}/servers`,
headers: {
'X-Auth-Token': SCW_SECRET_KEY,
'Content-Type': 'application/json',
},
params: {
// TODO need to update if over 100 results
per_page: 100,
tags,
},
});
return response.data.servers
.map(this.mapServerObject)
.filter((server: VM) => server.tags.includes(this.getTag()));
const limit = this.getLimitSize();
const pageCount = Math.ceil((limit || 1) / 100);
const pages = Array.from(Array(pageCount).keys()).map((i) => i + 1);
const responses: any[] = await Promise.all(
pages.map((page) =>
axios({
method: 'GET',
url: `https://api.scaleway.com/instance/v1/zones/${region}/servers`,
headers: {
'X-Auth-Token': SCW_SECRET_KEY,
'Content-Type': 'application/json',
},
params: {
page,
per_page: 100,
tags: filter,
},
}),
),
);
const responsesMapped = responses.map((response) =>
response.data.servers
.map(this.mapServerObject)
.filter((server: VM) => server.tags.includes(this.getTag())),
);
return responsesMapped.flat();
};

powerOn = async (_id: string) => {};
Expand Down

0 comments on commit e148d6b

Please sign in to comment.