@alwaysai/device-agent 0.0.11 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/lib/application-control/backup.d.ts.map +1 -1
  2. package/lib/application-control/backup.js +8 -2
  3. package/lib/application-control/backup.js.map +1 -1
  4. package/lib/application-control/config.d.ts +12 -4
  5. package/lib/application-control/config.d.ts.map +1 -1
  6. package/lib/application-control/config.js +59 -16
  7. package/lib/application-control/config.js.map +1 -1
  8. package/lib/application-control/environment-variables.d.ts.map +1 -1
  9. package/lib/application-control/environment-variables.js.map +1 -1
  10. package/lib/application-control/index.d.ts +4 -4
  11. package/lib/application-control/index.d.ts.map +1 -1
  12. package/lib/application-control/index.js +4 -3
  13. package/lib/application-control/index.js.map +1 -1
  14. package/lib/application-control/install.d.ts.map +1 -1
  15. package/lib/application-control/install.js +28 -14
  16. package/lib/application-control/install.js.map +1 -1
  17. package/lib/application-control/models.d.ts +7 -1
  18. package/lib/application-control/models.d.ts.map +1 -1
  19. package/lib/application-control/models.js +69 -39
  20. package/lib/application-control/models.js.map +1 -1
  21. package/lib/application-control/status.d.ts.map +1 -1
  22. package/lib/application-control/status.js +18 -14
  23. package/lib/application-control/status.js.map +1 -1
  24. package/lib/application-control/utils.d.ts +0 -2
  25. package/lib/application-control/utils.d.ts.map +1 -1
  26. package/lib/application-control/utils.js +7 -16
  27. package/lib/application-control/utils.js.map +1 -1
  28. package/lib/cloud-connection/app-install-status.d.ts +16 -0
  29. package/lib/cloud-connection/app-install-status.d.ts.map +1 -0
  30. package/lib/cloud-connection/app-install-status.js +53 -0
  31. package/lib/cloud-connection/app-install-status.js.map +1 -0
  32. package/lib/cloud-connection/bootstrap-provision.d.ts +2 -0
  33. package/lib/cloud-connection/bootstrap-provision.d.ts.map +1 -0
  34. package/lib/cloud-connection/bootstrap-provision.js +34 -0
  35. package/lib/cloud-connection/bootstrap-provision.js.map +1 -0
  36. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +12 -35
  37. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  38. package/lib/cloud-connection/device-agent-cloud-connection.js +170 -387
  39. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  40. package/lib/cloud-connection/device-agent.d.ts.map +1 -1
  41. package/lib/cloud-connection/device-agent.js +22 -26
  42. package/lib/cloud-connection/device-agent.js.map +1 -1
  43. package/lib/cloud-connection/live-updates-handler.d.ts +34 -0
  44. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -0
  45. package/lib/cloud-connection/live-updates-handler.js +167 -0
  46. package/lib/cloud-connection/live-updates-handler.js.map +1 -0
  47. package/lib/cloud-connection/messages.d.ts +14 -0
  48. package/lib/cloud-connection/messages.d.ts.map +1 -0
  49. package/lib/cloud-connection/messages.js +38 -0
  50. package/lib/cloud-connection/messages.js.map +1 -0
  51. package/lib/cloud-connection/publisher.d.ts +14 -0
  52. package/lib/cloud-connection/publisher.d.ts.map +1 -0
  53. package/lib/cloud-connection/publisher.js +44 -0
  54. package/lib/cloud-connection/publisher.js.map +1 -0
  55. package/lib/cloud-connection/shadow-handler.d.ts +34 -0
  56. package/lib/cloud-connection/shadow-handler.d.ts.map +1 -0
  57. package/lib/cloud-connection/shadow-handler.js +94 -0
  58. package/lib/cloud-connection/shadow-handler.js.map +1 -0
  59. package/lib/cloud-connection/shadow.d.ts +16 -0
  60. package/lib/cloud-connection/shadow.d.ts.map +1 -0
  61. package/lib/cloud-connection/shadow.js +36 -0
  62. package/lib/cloud-connection/shadow.js.map +1 -0
  63. package/lib/device-control/device-control.d.ts.map +1 -1
  64. package/lib/device-control/device-control.js +1 -0
  65. package/lib/device-control/device-control.js.map +1 -1
  66. package/lib/docker/docker-cmd.js +1 -1
  67. package/lib/docker/docker-compose-cmd.d.ts.map +1 -1
  68. package/lib/docker/docker-compose-cmd.js +1 -1
  69. package/lib/docker/docker-compose-cmd.js.map +1 -1
  70. package/lib/endpoints.js +10 -10
  71. package/lib/endpoints.js.map +1 -1
  72. package/lib/environment.d.ts.map +1 -1
  73. package/lib/environment.js.map +1 -1
  74. package/lib/infrastructure/agent-config.d.ts +4 -14
  75. package/lib/infrastructure/agent-config.d.ts.map +1 -1
  76. package/lib/infrastructure/agent-config.js +22 -15
  77. package/lib/infrastructure/agent-config.js.map +1 -1
  78. package/lib/infrastructure/agent-config.test.js +26 -18
  79. package/lib/infrastructure/agent-config.test.js.map +1 -1
  80. package/lib/infrastructure/system-id.d.ts +2 -0
  81. package/lib/infrastructure/system-id.d.ts.map +1 -0
  82. package/lib/infrastructure/system-id.js +21 -0
  83. package/lib/infrastructure/system-id.js.map +1 -0
  84. package/lib/infrastructure/tokens-and-device-cfg.d.ts +4 -0
  85. package/lib/infrastructure/tokens-and-device-cfg.d.ts.map +1 -0
  86. package/lib/infrastructure/tokens-and-device-cfg.js +31 -0
  87. package/lib/infrastructure/tokens-and-device-cfg.js.map +1 -0
  88. package/lib/infrastructure/urls.d.ts.map +1 -1
  89. package/lib/infrastructure/urls.js +3 -3
  90. package/lib/infrastructure/urls.js.map +1 -1
  91. package/lib/root.d.ts.map +1 -1
  92. package/lib/root.js +2 -7
  93. package/lib/root.js.map +1 -1
  94. package/lib/subcommands/app/app.d.ts.map +1 -1
  95. package/lib/subcommands/app/app.js +62 -60
  96. package/lib/subcommands/app/app.js.map +1 -1
  97. package/lib/subcommands/app/index.js +2 -2
  98. package/lib/subcommands/device/clean.d.ts +2 -0
  99. package/lib/subcommands/device/clean.d.ts.map +1 -0
  100. package/lib/subcommands/device/clean.js +29 -0
  101. package/lib/subcommands/device/clean.js.map +1 -0
  102. package/lib/subcommands/device/device.d.ts.map +1 -1
  103. package/lib/subcommands/device/device.js +40 -27
  104. package/lib/subcommands/device/device.js.map +1 -1
  105. package/lib/subcommands/device/index.d.ts.map +1 -1
  106. package/lib/subcommands/device/index.js +2 -1
  107. package/lib/subcommands/device/index.js.map +1 -1
  108. package/lib/subcommands/get-model-package.js +5 -5
  109. package/lib/subcommands/index.js +1 -1
  110. package/lib/subcommands/login.js +8 -8
  111. package/lib/subcommands/login.js.map +1 -1
  112. package/lib/util/clean-certs.d.ts +2 -0
  113. package/lib/util/clean-certs.d.ts.map +1 -0
  114. package/lib/util/clean-certs.js +16 -0
  115. package/lib/util/clean-certs.js.map +1 -0
  116. package/lib/util/directories.d.ts +16 -15
  117. package/lib/util/directories.d.ts.map +1 -1
  118. package/lib/util/directories.js +45 -26
  119. package/lib/util/directories.js.map +1 -1
  120. package/lib/util/get-device-id.d.ts +1 -1
  121. package/lib/util/get-device-id.d.ts.map +1 -1
  122. package/lib/util/get-device-id.js +14 -19
  123. package/lib/util/get-device-id.js.map +1 -1
  124. package/lib/util/http-client.d.ts +1 -1
  125. package/lib/util/http-client.d.ts.map +1 -1
  126. package/lib/util/http-client.js +10 -8
  127. package/lib/util/http-client.js.map +1 -1
  128. package/lib/util/logger.d.ts.map +1 -1
  129. package/lib/util/logger.js +4 -5
  130. package/lib/util/logger.js.map +1 -1
  131. package/lib/util/run-in-dir.d.ts.map +1 -1
  132. package/lib/util/run-in-dir.js +1 -0
  133. package/lib/util/run-in-dir.js.map +1 -1
  134. package/package.json +18 -8
  135. package/src/application-control/backup.ts +8 -3
  136. package/src/application-control/config.ts +75 -13
  137. package/src/application-control/environment-variables.ts +3 -3
  138. package/src/application-control/index.ts +19 -6
  139. package/src/application-control/install.ts +52 -28
  140. package/src/application-control/models.ts +100 -56
  141. package/src/application-control/status.ts +26 -21
  142. package/src/application-control/utils.ts +9 -20
  143. package/src/cloud-connection/app-install-status.ts +62 -0
  144. package/src/cloud-connection/bootstrap-provision.ts +40 -0
  145. package/src/cloud-connection/device-agent-cloud-connection.ts +258 -527
  146. package/src/cloud-connection/device-agent.ts +31 -38
  147. package/src/cloud-connection/live-updates-handler.ts +226 -0
  148. package/src/cloud-connection/messages.ts +39 -0
  149. package/src/cloud-connection/publisher.ts +65 -0
  150. package/src/cloud-connection/shadow-handler.ts +154 -0
  151. package/src/cloud-connection/shadow.ts +50 -0
  152. package/src/device-control/device-control.ts +1 -0
  153. package/src/docker/docker-cmd.ts +1 -1
  154. package/src/docker/docker-compose-cmd.ts +5 -2
  155. package/src/endpoints.ts +9 -9
  156. package/src/environment.ts +8 -3
  157. package/src/infrastructure/agent-config.test.ts +34 -23
  158. package/src/infrastructure/agent-config.ts +33 -20
  159. package/src/infrastructure/system-id.ts +18 -0
  160. package/src/infrastructure/tokens-and-device-cfg.ts +39 -0
  161. package/src/infrastructure/urls.ts +4 -2
  162. package/src/root.ts +2 -8
  163. package/src/subcommands/app/app.ts +64 -62
  164. package/src/subcommands/app/index.ts +3 -3
  165. package/src/subcommands/device/clean.ts +26 -0
  166. package/src/subcommands/device/device.ts +66 -50
  167. package/src/subcommands/device/index.ts +2 -1
  168. package/src/subcommands/get-model-package.ts +5 -5
  169. package/src/subcommands/index.ts +1 -1
  170. package/src/subcommands/login.ts +8 -8
  171. package/src/util/clean-certs.ts +12 -0
  172. package/src/util/directories.ts +68 -52
  173. package/src/util/get-device-id.ts +16 -18
  174. package/src/util/http-client.ts +18 -13
  175. package/src/util/logger.ts +6 -6
  176. package/src/util/run-in-dir.ts +2 -1
  177. package/lib/infrastructure/certificates-and-tokens.d.ts +0 -6
  178. package/lib/infrastructure/certificates-and-tokens.d.ts.map +0 -1
  179. package/lib/infrastructure/certificates-and-tokens.js +0 -43
  180. package/lib/infrastructure/certificates-and-tokens.js.map +0 -1
  181. package/src/infrastructure/certificates-and-tokens.ts +0 -53
@@ -2,7 +2,7 @@ import {
2
2
  CliLeaf,
3
3
  CliStringArrayInput,
4
4
  CliStringInput,
5
- CliTerseError,
5
+ CliTerseError
6
6
  } from '@alwaysai/alwayscli';
7
7
  import {
8
8
  addModel,
@@ -21,7 +21,7 @@ import {
21
21
  startApp,
22
22
  stopApp,
23
23
  uninstallApp,
24
- updateModels,
24
+ updateModels
25
25
  } from '../../application-control';
26
26
  import { AgentConfigFile } from '../../infrastructure/agent-config';
27
27
  import { logger } from '../../util/logger';
@@ -33,7 +33,7 @@ export const listAppsCliLeaf = CliLeaf({
33
33
  async action(_, opts) {
34
34
  const apps = await AgentConfigFile().getApps();
35
35
  console.table(apps);
36
- },
36
+ }
37
37
  });
38
38
 
39
39
  export const listAppReleasesCliLeaf = CliLeaf({
@@ -42,14 +42,14 @@ export const listAppReleasesCliLeaf = CliLeaf({
42
42
  namedInputs: {
43
43
  project: CliStringInput({
44
44
  description: 'Project ID',
45
- required: true,
46
- }),
45
+ required: true
46
+ })
47
47
  },
48
48
  async action(_, opts) {
49
49
  const { project } = opts;
50
50
  const releaseHistory = await listAppReleases({ projectId: project });
51
51
  logger.info(releaseHistory);
52
- },
52
+ }
53
53
  });
54
54
 
55
55
  export const listAppLatestReleaseCliLeaf = CliLeaf({
@@ -58,17 +58,19 @@ export const listAppLatestReleaseCliLeaf = CliLeaf({
58
58
  namedInputs: {
59
59
  project: CliStringInput({
60
60
  description: 'Project ID',
61
- required: true,
62
- }),
61
+ required: true
62
+ })
63
63
  },
64
64
  async action(_, opts) {
65
65
  const { project } = opts;
66
- const latestReleaseHash = await listAppLatestRelease({ projectId: project });
66
+ const latestReleaseHash = await listAppLatestRelease({
67
+ projectId: project
68
+ });
67
69
  if (latestReleaseHash === undefined) {
68
70
  throw new CliTerseError('This application has not been published yet');
69
71
  }
70
72
  logger.info(latestReleaseHash);
71
- },
73
+ }
72
74
  });
73
75
 
74
76
  export const installAppCliLeaf = CliLeaf({
@@ -77,11 +79,11 @@ export const installAppCliLeaf = CliLeaf({
77
79
  namedInputs: {
78
80
  project: CliStringInput({
79
81
  description: 'Project ID',
80
- required: true,
82
+ required: true
81
83
  }),
82
84
  releaseHash: CliStringInput({
83
- description: 'Release Hash',
84
- }),
85
+ description: 'Release Hash'
86
+ })
85
87
  },
86
88
  async action(_, opts) {
87
89
  const project = opts.project;
@@ -93,7 +95,7 @@ export const installAppCliLeaf = CliLeaf({
93
95
  throw new CliTerseError('This application has not been published yet');
94
96
  }
95
97
  await installApp({ projectId: project, appReleaseHash: releaseHash });
96
- },
98
+ }
97
99
  });
98
100
 
99
101
  export const getAppStatusCliLeaf = CliLeaf({
@@ -102,14 +104,14 @@ export const getAppStatusCliLeaf = CliLeaf({
102
104
  namedInputs: {
103
105
  project: CliStringInput({
104
106
  description: 'Project ID',
105
- required: true,
106
- }),
107
+ required: true
108
+ })
107
109
  },
108
110
  async action(_, opts) {
109
111
  const { project } = opts;
110
112
  const appStatus = await getAppStatus({ projectId: project });
111
113
  logger.info(appStatus);
112
- },
114
+ }
113
115
  });
114
116
 
115
117
  export const startAppCliLeaf = CliLeaf({
@@ -118,16 +120,16 @@ export const startAppCliLeaf = CliLeaf({
118
120
  namedInputs: {
119
121
  project: CliStringInput({
120
122
  description: 'Project ID',
121
- required: true,
123
+ required: true
122
124
  }),
123
125
  dockerLoginToken: CliStringInput({
124
- description: 'Docker login token',
125
- }),
126
+ description: 'Docker login token'
127
+ })
126
128
  },
127
129
  async action(_, opts) {
128
130
  const { project, dockerLoginToken } = opts;
129
131
  await startApp({ projectId: project, dockerLoginToken });
130
- },
132
+ }
131
133
  });
132
134
 
133
135
  export const getAppLogsCliLeaf = CliLeaf({
@@ -136,8 +138,8 @@ export const getAppLogsCliLeaf = CliLeaf({
136
138
  namedInputs: {
137
139
  project: CliStringInput({
138
140
  description: 'Project ID',
139
- required: true,
140
- }),
141
+ required: true
142
+ })
141
143
  },
142
144
  async action(_, opts) {
143
145
  const { project } = opts;
@@ -146,7 +148,7 @@ export const getAppLogsCliLeaf = CliLeaf({
146
148
  for await (const chunk of readable) {
147
149
  logger.info(chunk);
148
150
  }
149
- },
151
+ }
150
152
  });
151
153
 
152
154
  export const stopAppCliLeaf = CliLeaf({
@@ -155,13 +157,13 @@ export const stopAppCliLeaf = CliLeaf({
155
157
  namedInputs: {
156
158
  project: CliStringInput({
157
159
  description: 'Project ID',
158
- required: true,
159
- }),
160
+ required: true
161
+ })
160
162
  },
161
163
  async action(_, opts) {
162
164
  const { project } = opts;
163
165
  await stopApp({ projectId: project });
164
- },
166
+ }
165
167
  });
166
168
 
167
169
  export const restartAppCliLeaf = CliLeaf({
@@ -170,13 +172,13 @@ export const restartAppCliLeaf = CliLeaf({
170
172
  namedInputs: {
171
173
  project: CliStringInput({
172
174
  description: 'Project ID',
173
- required: true,
174
- }),
175
+ required: true
176
+ })
175
177
  },
176
178
  async action(_, opts) {
177
179
  const { project } = opts;
178
180
  await restartApp({ projectId: project });
179
- },
181
+ }
180
182
  });
181
183
 
182
184
  export const uninstallAppCliLeaf = CliLeaf({
@@ -185,13 +187,13 @@ export const uninstallAppCliLeaf = CliLeaf({
185
187
  namedInputs: {
186
188
  project: CliStringInput({
187
189
  description: 'Project ID',
188
- required: true,
189
- }),
190
+ required: true
191
+ })
190
192
  },
191
193
  async action(_, opts) {
192
194
  const { project } = opts;
193
195
  await uninstallApp({ projectId: project });
194
- },
196
+ }
195
197
  });
196
198
 
197
199
  export const rollbackAppCliLeaf = CliLeaf({
@@ -200,13 +202,13 @@ export const rollbackAppCliLeaf = CliLeaf({
200
202
  namedInputs: {
201
203
  project: CliStringInput({
202
204
  description: 'Project ID',
203
- required: true,
204
- }),
205
+ required: true
206
+ })
205
207
  },
206
208
  async action(_, opts) {
207
209
  const { project } = opts;
208
210
  await rollbackApp({ projectId: project });
209
- },
211
+ }
210
212
  });
211
213
 
212
214
  export const showAppModelsCliLeaf = CliLeaf({
@@ -215,14 +217,14 @@ export const showAppModelsCliLeaf = CliLeaf({
215
217
  namedInputs: {
216
218
  project: CliStringInput({
217
219
  description: 'Project ID',
218
- required: true,
219
- }),
220
+ required: true
221
+ })
220
222
  },
221
223
  async action(_, opts) {
222
224
  const { project } = opts;
223
225
  const appModels = await getAppModels({ projectId: project });
224
226
  console.table(appModels);
225
- },
227
+ }
226
228
  });
227
229
 
228
230
  export const addModelCliLeaf = CliLeaf({
@@ -231,17 +233,17 @@ export const addModelCliLeaf = CliLeaf({
231
233
  namedInputs: {
232
234
  project: CliStringInput({
233
235
  description: 'Project ID',
234
- required: true,
236
+ required: true
235
237
  }),
236
238
  model: CliStringInput({
237
239
  description: 'Model ID',
238
- required: true,
239
- }),
240
+ required: true
241
+ })
240
242
  },
241
243
  async action(_, opts) {
242
244
  const { project, model } = opts;
243
245
  await addModel({ projectId: project, modelId: model });
244
- },
246
+ }
245
247
  });
246
248
 
247
249
  export const removeModelCliLeaf = CliLeaf({
@@ -250,17 +252,17 @@ export const removeModelCliLeaf = CliLeaf({
250
252
  namedInputs: {
251
253
  project: CliStringInput({
252
254
  description: 'Project ID',
253
- required: true,
255
+ required: true
254
256
  }),
255
257
  model: CliStringInput({
256
258
  description: 'Model ID',
257
- required: true,
258
- }),
259
+ required: true
260
+ })
259
261
  },
260
262
  async action(_, opts) {
261
263
  const { project, model } = opts;
262
264
  await removeModel({ projectId: project, modelId: model });
263
- },
265
+ }
264
266
  });
265
267
 
266
268
  export const replaceModelsCliLeaf = CliLeaf({
@@ -269,17 +271,17 @@ export const replaceModelsCliLeaf = CliLeaf({
269
271
  namedInputs: {
270
272
  project: CliStringInput({
271
273
  description: 'Project Id',
272
- required: true,
274
+ required: true
273
275
  }),
274
276
  models: CliStringArrayInput({
275
277
  description: 'One or more model IDs',
276
- required: true,
277
- }),
278
+ required: true
279
+ })
278
280
  },
279
281
  async action(_, opts) {
280
282
  const { project, models } = opts;
281
283
  await replaceModels({ projectId: project, modelIds: models });
282
- },
284
+ }
283
285
  });
284
286
 
285
287
  export const updateModelsCliLeaf = CliLeaf({
@@ -288,13 +290,13 @@ export const updateModelsCliLeaf = CliLeaf({
288
290
  namedInputs: {
289
291
  project: CliStringInput({
290
292
  description: 'Project Id',
291
- required: true,
292
- }),
293
+ required: true
294
+ })
293
295
  },
294
296
  async action(_, opts) {
295
297
  const { project } = opts;
296
298
  await updateModels({ projectId: project });
297
- },
299
+ }
298
300
  });
299
301
 
300
302
  export const getAllEnvsCLiLeaf = CliLeaf({
@@ -303,13 +305,13 @@ export const getAllEnvsCLiLeaf = CliLeaf({
303
305
  namedInputs: {
304
306
  project: CliStringInput({
305
307
  description: 'Project Id',
306
- required: true,
307
- }),
308
+ required: true
309
+ })
308
310
  },
309
311
  async action(_, opts) {
310
312
  const { project } = opts;
311
313
  logger.info(await getAllEnvs({ projectId: project }));
312
- },
314
+ }
313
315
  });
314
316
 
315
317
  export const setEnvCLiLeaf = CliLeaf({
@@ -317,21 +319,21 @@ export const setEnvCLiLeaf = CliLeaf({
317
319
  description: 'Set environment variables for a service',
318
320
  positionalInput: CliStringArrayInput({
319
321
  placeholder: '<NAME=VALUE> [<NAME=VALUE> ...]',
320
- required: true,
322
+ required: true
321
323
  }),
322
324
  namedInputs: {
323
325
  project: CliStringInput({
324
326
  description: 'Project Id',
325
- required: true,
327
+ required: true
326
328
  }),
327
329
  service: CliStringInput({
328
330
  description:
329
331
  'The name of the docker-compose service to apply environment variable to',
330
- required: false,
331
- }),
332
+ required: false
333
+ })
332
334
  },
333
335
  async action(args, opts) {
334
336
  const { project, service } = opts;
335
337
  await setEnv({ projectId: project, vars: args, service });
336
- },
338
+ }
337
339
  });
@@ -17,7 +17,7 @@ import {
17
17
  replaceModelsCliLeaf,
18
18
  showAppModelsCliLeaf,
19
19
  getAllEnvsCLiLeaf,
20
- setEnvCLiLeaf,
20
+ setEnvCLiLeaf
21
21
  } from './app';
22
22
 
23
23
  export const appCliBranch = CliBranch({
@@ -41,6 +41,6 @@ export const appCliBranch = CliBranch({
41
41
  replaceModelsCliLeaf,
42
42
  updateModelsCliLeaf,
43
43
  getAllEnvsCLiLeaf,
44
- setEnvCLiLeaf,
45
- ],
44
+ setEnvCLiLeaf
45
+ ]
46
46
  });
@@ -0,0 +1,26 @@
1
+ import { CliLeaf } from '@alwaysai/alwayscli';
2
+ import * as rimraf from 'rimraf';
3
+ import { LOCAL_CERT_AND_KEY_DIR } from 'alwaysai/lib/constants';
4
+ import { logger } from '../../util/logger';
5
+ import { AgentConfigFile } from '../../infrastructure/agent-config';
6
+ import { DeviceConfigFile } from 'alwaysai/lib/core/device';
7
+ import { APP_ROOT, CREDENTIALS_FILE_PATH } from '../../util/directories';
8
+
9
+ export const cleanCliLeaf = CliLeaf({
10
+ name: 'clean',
11
+ description: 'Remove all provisioning files',
12
+ async action(_, opts) {
13
+ logger.info('Cleaning device configuration');
14
+ logger.debug(`Removing ${LOCAL_CERT_AND_KEY_DIR}`);
15
+ rimraf.sync(LOCAL_CERT_AND_KEY_DIR);
16
+ logger.debug(`Removing ${AgentConfigFile().path}`);
17
+ AgentConfigFile().remove();
18
+ logger.debug(`Removing ${DeviceConfigFile().path}`);
19
+ DeviceConfigFile().remove();
20
+ logger.debug(`Removing ${CREDENTIALS_FILE_PATH}`);
21
+ rimraf.sync(CREDENTIALS_FILE_PATH);
22
+ logger.debug(`Removing ${APP_ROOT}`);
23
+ rimraf.sync(APP_ROOT);
24
+ logger.info('Device configuration cleaned');
25
+ }
26
+ });
@@ -1,45 +1,49 @@
1
- import { CliLeaf, CliStringInput } from "@alwaysai/alwayscli";
2
- import { checkUserIsLoggedInComponent } from "alwaysai/lib/components/user";
3
- import { checkPaidPlan } from "alwaysai/lib/core/project";
4
- import { v4 as uuidv4 } from "uuid";
5
- import { CliAuthenticationClient } from "alwaysai/lib/infrastructure";
6
-
7
- import { httpClient, microServiceHttpClient } from "../../util/http-client";
1
+ import { CliLeaf, CliStringInput } from '@alwaysai/alwayscli';
2
+ import { checkUserIsLoggedInComponent } from 'alwaysai/lib/components/user';
3
+ import { getTargetHardwareUuid } from 'alwaysai/lib/core/app';
4
+ import { checkPaidPlan } from 'alwaysai/lib/core/project';
5
+ import { v4 as uuidv4 } from 'uuid';
6
+ import { CliAuthenticationClient } from 'alwaysai/lib/infrastructure';
7
+ import { existsSync } from 'fs';
8
+ import { httpClient, microServiceHttpClient } from '../../util/http-client';
8
9
  import {
9
10
  AWS_ROOT_CERTIFICATE_FILE_NAME,
10
11
  BOOTSTRAP_CERTIFICATES_DIR_PATH,
11
- } from "../../util/directories";
12
+ getPrivateKeyFilePath
13
+ } from '../../util/directories';
12
14
 
13
- import { LOCAL_CERT_AND_KEY_DIR } from "alwaysai/lib/constants";
14
- import fetch from "node-fetch";
15
- import { JsSpawner } from "alwaysai/lib/util";
15
+ import { LOCAL_CERT_AND_KEY_DIR } from 'alwaysai/lib/constants';
16
+ import { JsSpawner } from 'alwaysai/lib/util';
16
17
  import {
17
18
  getCpuUtil,
18
19
  getDiskUtil,
19
- getMemUtil,
20
- } from "../../device-control/device-control";
21
- import {
22
- getTargetHardwareUuid,
23
- writeCertificateAndToken,
24
- } from "../../infrastructure/certificates-and-tokens";
25
- import { logger } from "../../util/logger";
20
+ getMemUtil
21
+ } from '../../device-control/device-control';
22
+ import { writeTokenAndDeviceCfg } from '../../infrastructure/tokens-and-device-cfg';
23
+ import { logger } from '../../util/logger';
24
+ import { DeviceConfigFile } from 'alwaysai/lib/core/device';
26
25
 
27
26
  export const initCliLeaf = CliLeaf({
28
- name: "init",
29
- description: "Initialize device",
27
+ name: 'init',
28
+ description: 'Initialize device',
30
29
  namedInputs: {
31
30
  name: CliStringInput({
32
- description: "Device name",
33
- required: true,
31
+ description: 'Device name',
32
+ required: true
34
33
  }),
35
34
  description: CliStringInput({
36
- description: "Device description",
37
- required: false,
38
- }),
35
+ description: 'Device description',
36
+ required: false
37
+ })
39
38
  },
40
39
  async action(_, opts) {
41
40
  const { name, description } = opts;
42
- logger.info("Initializing device");
41
+ if (DeviceConfigFile().exists() || existsSync(getPrivateKeyFilePath())) {
42
+ throw new Error(
43
+ "Device has been previously provisioned. Run 'aai-agent device clean' to re-provision"
44
+ );
45
+ }
46
+ logger.info(`Initializing device ${name}`);
43
47
  await checkUserIsLoggedInComponent({ yes: true });
44
48
  if (!(await checkPaidPlan())) {
45
49
  throw new Error(
@@ -47,54 +51,66 @@ export const initCliLeaf = CliLeaf({
47
51
  );
48
52
  }
49
53
  const { username } = await CliAuthenticationClient().getInfo();
50
- const spawner = JsSpawner();
51
- const hardwareId = await getTargetHardwareUuid(spawner);
54
+ const hardwareId = await getTargetHardwareUuid(JsSpawner());
52
55
  const device = {
53
- deviceMode: "production",
56
+ deviceMode: 'production',
54
57
  hardwareId,
55
58
  txId: uuidv4(),
56
59
  userName: username,
57
- friendlyName: name,
60
+ friendlyName: name
58
61
  };
59
62
 
60
- const response = await microServiceHttpClient(
61
- "fleet-provision",
62
- "addDevice",
63
- "POST",
64
- JSON.stringify(device)
65
- );
63
+ const response: { deviceUuid: string; claimCertificate: string[] } =
64
+ await microServiceHttpClient(
65
+ 'fleet-provision',
66
+ 'addDevice',
67
+ 'POST',
68
+ JSON.stringify(device)
69
+ );
70
+ logger.debug(`addDevice Response: ${JSON.stringify(response, null, 2)}`);
66
71
 
72
+ if (!Object.hasOwn(response, 'claimCertificate')) {
73
+ throw new Error(
74
+ "Device cannot be provisioned. Run 'aai-agent device clean' and retry."
75
+ );
76
+ }
67
77
  const ROOT_CERT_AWS = await httpClient(
68
- "https://www.amazontrust.com/repository/AmazonRootCA1.pem",
69
- "GET"
78
+ 'https://www.amazontrust.com/repository/AmazonRootCA1.pem',
79
+ 'GET',
80
+ {}
70
81
  );
71
82
 
72
- await writeCertificateAndToken({ deviceUuid: response.deviceUuid });
83
+ await writeTokenAndDeviceCfg({
84
+ deviceUuid: response.deviceUuid
85
+ });
73
86
 
74
- await JsSpawner().mkdirp(BOOTSTRAP_CERTIFICATES_DIR_PATH);
75
- JsSpawner({ path: LOCAL_CERT_AND_KEY_DIR }).writeFile(
87
+ await JsSpawner().mkdirp(BOOTSTRAP_CERTIFICATES_DIR_PATH());
88
+
89
+ await JsSpawner({ path: LOCAL_CERT_AND_KEY_DIR }).writeFile(
76
90
  AWS_ROOT_CERTIFICATE_FILE_NAME,
77
91
  ROOT_CERT_AWS
78
92
  );
79
- const certSpawner = JsSpawner({ path: BOOTSTRAP_CERTIFICATES_DIR_PATH });
93
+ const certSpawner = JsSpawner({
94
+ path: BOOTSTRAP_CERTIFICATES_DIR_PATH()
95
+ });
80
96
  for (const key in response.claimCertificate) {
81
97
  await certSpawner.writeFile(key, response.claimCertificate[key]);
82
98
  }
83
99
 
84
- logger.info(`Initialized device as ${response.deviceUuid}`);
85
- },
100
+ logger.info(`Initialized device ${name}: UUID=${response.deviceUuid}`);
101
+ }
86
102
  });
87
103
 
88
104
  export const getInfoCliLeaf = CliLeaf({
89
- name: "get-info",
90
- description: "Get device info",
105
+ name: 'get-info',
106
+ description: 'Get device info',
91
107
  namedInputs: {},
92
108
  async action(_, opts) {
93
109
  const deviceInfo = [
94
- ["CPU Utilization", `${String(await getCpuUtil())} %`],
95
- ["Disk Utilization", `${String(await getDiskUtil())} %`],
96
- ["Memory Utilization", `${String(await getMemUtil())} %`],
110
+ ['CPU Utilization', `${String(await getCpuUtil())} %`],
111
+ ['Disk Utilization', `${String(await getDiskUtil())} %`],
112
+ ['Memory Utilization', `${String(await getMemUtil())} %`]
97
113
  ];
98
114
  console.table(deviceInfo);
99
- },
115
+ }
100
116
  });
@@ -1,8 +1,9 @@
1
1
  import { CliBranch } from '@alwaysai/alwayscli';
2
2
  import { getInfoCliLeaf, initCliLeaf } from './device';
3
+ import { cleanCliLeaf } from './clean';
3
4
 
4
5
  export const deviceCliBranch = CliBranch({
5
6
  name: 'device',
6
7
  description: 'Manage current device',
7
- subcommands: [initCliLeaf, getInfoCliLeaf],
8
+ subcommands: [initCliLeaf, getInfoCliLeaf, cleanCliLeaf]
8
9
  });
@@ -10,17 +10,17 @@ export const getModelPackageCliLeaf = CliLeaf({
10
10
  positionalInput: CliStringInput({
11
11
  description: 'For example, "alwaysai/mobilenet_ssd"',
12
12
  required: true,
13
- placeholder: '<id>',
13
+ placeholder: '<id>'
14
14
  }),
15
15
  namedInputs: {
16
16
  version: CliNumberInput({
17
17
  description: 'The version number of the model',
18
- required: false,
18
+ required: false
19
19
  }),
20
20
  path: CliStringInput({
21
21
  description: 'The output path to write model package to',
22
- required: false,
23
- }),
22
+ required: false
23
+ })
24
24
  },
25
25
  async action(id, opts) {
26
26
  const path = opts.path || process.cwd();
@@ -29,5 +29,5 @@ export const getModelPackageCliLeaf = CliLeaf({
29
29
  opts.version || (await CliRpcClient().getModelVersion({ id })).version;
30
30
  await appInstallModel(spawner, id, version);
31
31
  logger.info(`Completed downloading ${id} to ${path}`);
32
- },
32
+ }
33
33
  });
@@ -7,5 +7,5 @@ export const subcommands = [
7
7
  loginCliLeaf,
8
8
  appCliBranch,
9
9
  deviceCliBranch,
10
- getModelPackageCliLeaf,
10
+ getModelPackageCliLeaf
11
11
  ];
@@ -1,6 +1,6 @@
1
1
  import { CliLeaf, CliStringInput } from '@alwaysai/alwayscli';
2
2
  import { alwaysaiUserLoginYesComponent } from 'alwaysai/lib/components/user';
3
- import { writeCertificateAndToken } from '../infrastructure/certificates-and-tokens';
3
+ import { writeTokenAndDeviceCfg } from '../infrastructure/tokens-and-device-cfg';
4
4
 
5
5
  export const loginCliLeaf = CliLeaf({
6
6
  name: 'login',
@@ -8,26 +8,26 @@ export const loginCliLeaf = CliLeaf({
8
8
  namedInputs: {
9
9
  email: CliStringInput({
10
10
  description: 'alwaysAI email',
11
- required: true,
11
+ required: true
12
12
  }),
13
13
  password: CliStringInput({
14
14
  description: 'Account password',
15
- required: true,
15
+ required: true
16
16
  }),
17
17
  device: CliStringInput({
18
18
  description: 'The device UUID',
19
- required: false,
20
- }),
19
+ required: false
20
+ })
21
21
  },
22
22
  async action(_, opts) {
23
23
  const { email, password, device } = opts;
24
24
 
25
25
  await alwaysaiUserLoginYesComponent({
26
26
  alwaysaiUserEmail: email,
27
- alwaysaiUserPassword: password,
27
+ alwaysaiUserPassword: password
28
28
  });
29
29
  if (device) {
30
- await writeCertificateAndToken({ deviceUuid: device });
30
+ await writeTokenAndDeviceCfg({ deviceUuid: device });
31
31
  }
32
- },
32
+ }
33
33
  });
@@ -0,0 +1,12 @@
1
+ import { JsSpawner } from 'alwaysai/lib/util';
2
+ import { logger } from '../util/logger';
3
+ import { LOCAL_CERT_AND_KEY_DIR } from 'alwaysai/lib/constants';
4
+ import { BOOTSTRAP_CERTIFICATES_DIR_PATH } from '../util/directories';
5
+
6
+ export const rmBootstrapCertsAndClose = async () => {
7
+ const spawner = JsSpawner();
8
+ await spawner.rimraf(BOOTSTRAP_CERTIFICATES_DIR_PATH());
9
+ await spawner.rimraf(LOCAL_CERT_AND_KEY_DIR);
10
+ logger.error('Could not provision device. Try again.');
11
+ process.exit(1);
12
+ };