@@ -10,6 +10,36 @@ import {
10
10
import { getApiUrl } from "../helpers/urls.ts" ;
11
11
import { isFeatureEnabled } from "./posthog.ts" ;
12
12
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
+
13
43
export async function registerVM ( program : Command ) {
14
44
const isEnabled = await isFeatureEnabled ( "vms" ) ;
15
45
@@ -64,35 +94,56 @@ export async function registerVM(program: Command) {
64
94
) ;
65
95
} ) ;
66
96
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" )
70
100
. 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
+ } ) ;
77
105
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" ) ;
79
126
const response = await fetch ( url , {
80
- method : "POST " ,
127
+ method : "GET " ,
81
128
headers : {
82
- "Content-Type" : "application/json" ,
83
129
Authorization : `Bearer ${ await getAuthToken ( ) } ` ,
84
130
} ,
85
- body : JSON . stringify ( { script } ) ,
86
131
} ) ;
87
132
88
133
if ( ! response . ok ) {
89
134
if ( response . status === 401 ) {
90
135
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
+ ) ;
91
142
}
92
- logAndQuit ( `Failed to upload script: ${ response . statusText } ` ) ;
143
+ } else {
144
+ const data = await response . json ( ) ;
145
+ console . log ( data . script ) ;
93
146
}
94
-
95
- console . log ( "Successfully uploaded startup script" ) ;
96
147
} ) ;
97
148
98
149
vm . command ( "logs" )
0 commit comments