Skip to content

Commit

Permalink
v1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrees committed Nov 25, 2021
1 parent 9ef37bc commit 2146609
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vultr-action",
"version": "1.8",
"version": "1.9",
"scripts": {
"build": "ncc build src/index.ts -o dist --license licenses.txt"
},
Expand Down
5 changes: 3 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const api =
})
.then(({ data }) => res(data))
.catch((err) => {
console.log(`https://api.vultr.com/v2/${path}`);
rej(
err.response?.data
? new Error(JSON.stringify(err.response.data))
Expand All @@ -36,10 +37,10 @@ export const listInstances = (perPage = 100) =>
`instances?per_page=${perPage}`
);

export const listRecords = (perPage = 100) =>
export const listRecords = (domain: string, perPage = 100) =>
api<{ records: Vultr.Record["record"][] }>(
"get",
`records?per_page=${perPage}`
`domains/${domain}/records?per_page=${perPage}`
);

export const destroyInstance = (id: string) => api("delete", `instances/${id}`);
Expand Down
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,36 @@ const go = async (action: string) => {
// XXX: check if DNS or instances exist, exit gracefully if they do
await Promise.all([
async () => {
const { records } = await listRecords(500)();
log("🔍 checking for existing DNS");
const { records } = await listRecords(domain, 500)();
const existing = records.filter(
({ type, name }) =>
(type === "CNAME" && name === `*.${id}`) ||
(type === "A" && name === id)
);
if (existing.length > 0) {
log("DNS records already exist");
log("DNS records already exist");
log(existing);
process.exit(0);
} else {
log("✅ no existing DNS records");
}
},
async () => {
log("🔍 checking for existing instances");
const { instances } = await listInstances(500)();
const existing = instances.filter((i) => i.label === fullDomain);
if (existing) {
log("Instances already exist");
log("❌ instances already exist");
log(existing);
process.exit(0);
} else {
log("✅ no existing instances");
}
},
]);

log("creating instance");
log("🏗️ creating instance");
const { instance } = await createInstance({
api_key: get("api_key"),
hostname: fullDomain,
Expand Down Expand Up @@ -74,21 +80,23 @@ const go = async (action: string) => {
setOutput("ip_address", ip);
setOutput("default_password", instance.default_password);
setOutput("instance", instance);
log("waiting for active status");
log("waiting for active status");
await confirmInstanceIsReady(instance.id);
// XXX: sometimes the server isn't immediately ready for an ssh session
log("instance active... waiting another 20s to ensure it's accessible");
log(
"✅ instance active... waiting another 20s to ensure it's accessible"
);
await sleep(20_000);
return;

case "destroy":
log("performing teardown");
log("🗑️ performing teardown");
const { instances } = await listInstances(500)();
const matchingInstances = instances.filter(
({ label }) => label === fullDomain
);

log(`found ${matchingInstances.length} servers`);
log(`🔍 found ${matchingInstances.length} servers`);
let count = 0;
// XXX: 'for of' so that await works
for (const instance of matchingInstances) {
Expand Down

0 comments on commit 2146609

Please sign in to comment.