Skip to content

Commit f697b47

Browse files
Rename sf script to sf cloud-init user-data
1 parent e48cf26 commit f697b47

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

src/lib/vm.ts

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ import {
1010
import { getApiUrl } from "../helpers/urls.ts";
1111
import { isFeatureEnabled } from "./posthog.ts";
1212

13+
async function cloudInitUserDataSet(file: string) {
14+
let userData: string;
15+
try {
16+
userData = readFileSync(file, "utf-8");
17+
} catch {
18+
logAndQuit("Failed to read user-data file");
19+
}
20+
21+
const url = await getApiUrl("vms_script_post");
22+
const response = await fetch(url, {
23+
method: "POST",
24+
headers: {
25+
"Content-Type": "application/json",
26+
Authorization: `Bearer ${await getAuthToken()}`,
27+
},
28+
body: JSON.stringify({ script: userData }),
29+
});
30+
31+
if (!response.ok) {
32+
if (response.status === 401) {
33+
await logSessionTokenExpiredAndQuit();
34+
}
35+
logAndQuit(
36+
`Failed to upload cloud-init user-data: ${response.statusText}`,
37+
);
38+
}
39+
40+
console.log("Successfully uploaded cloud-init user-data");
41+
}
42+
1343
export async function registerVM(program: Command) {
1444
const isEnabled = await isFeatureEnabled("vms");
1545

@@ -64,35 +94,56 @@ export async function registerVM(program: Command) {
6494
);
6595
});
6696

67-
vm.command("script")
68-
.description("Push a startup script to VMs")
69-
.requiredOption("-f, --file <file>", "Path to startup script file")
97+
vm.command("script").description(
98+
"OBSOLETE - Now an alias for `sf vm cloud-init user-data set`",
99+
).requiredOption("-f, --file <file>", "Path to user-data file")
70100
.action(async (options) => {
71-
let script: string;
72-
try {
73-
script = readFileSync(options.file, "utf-8");
74-
} catch {
75-
logAndQuit("Failed to read script file");
76-
}
101+
console.error("OBSOLETE - Please use `sf vm cloud-init user-data set`.");
102+
console.error("Calling `sf vm cloud-init user-data set` on your behalf");
103+
await cloudInitUserDataSet(options.file);
104+
});
77105

78-
const url = await getApiUrl("vms_script_post");
106+
const cloudInit = vm.command("cloud-init").description(
107+
"Manage cloud-init related VM settings",
108+
);
109+
const userData = cloudInit.command("user-data").description(
110+
"Manage cloud-init user-data information",
111+
);
112+
113+
userData.command("set").description(
114+
"Upload a cloud-init user-data file used with VMs",
115+
)
116+
.requiredOption("-f, --file <file>", "Path to user-data file")
117+
.action(async (options) => {
118+
await cloudInitUserDataSet(options.file);
119+
});
120+
121+
userData.command("get").description(
122+
"Retrieve the cloud-init user-data file used with VMs",
123+
)
124+
.action(async () => {
125+
const url = await getApiUrl("vms_script_get");
79126
const response = await fetch(url, {
80-
method: "POST",
127+
method: "GET",
81128
headers: {
82-
"Content-Type": "application/json",
83129
Authorization: `Bearer ${await getAuthToken()}`,
84130
},
85-
body: JSON.stringify({ script }),
86131
});
87132

88133
if (!response.ok) {
89134
if (response.status === 401) {
90135
await logSessionTokenExpiredAndQuit();
136+
} else if (response.status === 404) {
137+
console.log("No user-data set");
138+
} else {
139+
logAndQuit(
140+
`Failed to retrive cloud-init user-data: ${response.statusText}`,
141+
);
91142
}
92-
logAndQuit(`Failed to upload script: ${response.statusText}`);
143+
} else {
144+
const data = await response.json();
145+
console.log(data.script);
93146
}
94-
95-
console.log("Successfully uploaded startup script");
96147
});
97148

98149
vm.command("logs")

0 commit comments

Comments
 (0)