@alwaysai/device-agent 1.5.0 → 2.0.1

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 (305) hide show
  1. package/lib/application-control/config.d.ts.map +1 -1
  2. package/lib/application-control/config.js +8 -3
  3. package/lib/application-control/config.js.map +1 -1
  4. package/lib/application-control/environment-variables.d.ts +5 -5
  5. package/lib/application-control/environment-variables.d.ts.map +1 -1
  6. package/lib/application-control/environment-variables.js +25 -38
  7. package/lib/application-control/environment-variables.js.map +1 -1
  8. package/lib/application-control/environment-variables.test.js +27 -7
  9. package/lib/application-control/environment-variables.test.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 +1 -4
  13. package/lib/application-control/index.js.map +1 -1
  14. package/lib/application-control/install.d.ts +4 -1
  15. package/lib/application-control/install.d.ts.map +1 -1
  16. package/lib/application-control/install.js +24 -8
  17. package/lib/application-control/install.js.map +1 -1
  18. package/lib/application-control/models.d.ts +0 -11
  19. package/lib/application-control/models.d.ts.map +1 -1
  20. package/lib/application-control/models.js +5 -54
  21. package/lib/application-control/models.js.map +1 -1
  22. package/lib/application-control/utils.d.ts +0 -4
  23. package/lib/application-control/utils.d.ts.map +1 -1
  24. package/lib/application-control/utils.js +12 -22
  25. package/lib/application-control/utils.js.map +1 -1
  26. package/lib/cloud-connection/base-message-handler.d.ts +27 -0
  27. package/lib/cloud-connection/base-message-handler.d.ts.map +1 -0
  28. package/lib/cloud-connection/base-message-handler.js +72 -0
  29. package/lib/cloud-connection/base-message-handler.js.map +1 -0
  30. package/lib/cloud-connection/bootstrap-provision.js +3 -2
  31. package/lib/cloud-connection/bootstrap-provision.js.map +1 -1
  32. package/lib/cloud-connection/connection-manager.d.ts +21 -0
  33. package/lib/cloud-connection/connection-manager.d.ts.map +1 -0
  34. package/lib/cloud-connection/connection-manager.js +158 -0
  35. package/lib/cloud-connection/connection-manager.js.map +1 -0
  36. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +9 -30
  37. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  38. package/lib/cloud-connection/device-agent-cloud-connection.js +69 -508
  39. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  40. package/lib/cloud-connection/device-agent-message-handler.d.ts +22 -0
  41. package/lib/cloud-connection/device-agent-message-handler.d.ts.map +1 -0
  42. package/lib/cloud-connection/device-agent-message-handler.js +357 -0
  43. package/lib/cloud-connection/device-agent-message-handler.js.map +1 -0
  44. package/lib/cloud-connection/device-agent.d.ts.map +1 -1
  45. package/lib/cloud-connection/device-agent.js +11 -9
  46. package/lib/cloud-connection/device-agent.js.map +1 -1
  47. package/lib/cloud-connection/live-updates-handler.d.ts +19 -28
  48. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
  49. package/lib/cloud-connection/live-updates-handler.js +60 -172
  50. package/lib/cloud-connection/live-updates-handler.js.map +1 -1
  51. package/lib/cloud-connection/live-updates-handler.test.js +71 -165
  52. package/lib/cloud-connection/live-updates-handler.test.js.map +1 -1
  53. package/lib/cloud-connection/message-dispatcher.d.ts +10 -0
  54. package/lib/cloud-connection/message-dispatcher.d.ts.map +1 -0
  55. package/lib/cloud-connection/message-dispatcher.js +27 -0
  56. package/lib/cloud-connection/message-dispatcher.js.map +1 -0
  57. package/lib/cloud-connection/passthrough-handler.d.ts +4 -1
  58. package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
  59. package/lib/cloud-connection/passthrough-handler.js +30 -11
  60. package/lib/cloud-connection/passthrough-handler.js.map +1 -1
  61. package/lib/cloud-connection/shadow-handler.d.ts +11 -3
  62. package/lib/cloud-connection/shadow-handler.d.ts.map +1 -1
  63. package/lib/cloud-connection/shadow-handler.js +133 -28
  64. package/lib/cloud-connection/shadow-handler.js.map +1 -1
  65. package/lib/cloud-connection/shadow-handler.test.js +45 -57
  66. package/lib/cloud-connection/shadow-handler.test.js.map +1 -1
  67. package/lib/cloud-connection/shadow.d.ts.map +1 -1
  68. package/lib/cloud-connection/shadow.js +2 -1
  69. package/lib/cloud-connection/shadow.js.map +1 -1
  70. package/lib/cloud-connection/transaction-manager.d.ts +12 -3
  71. package/lib/cloud-connection/transaction-manager.d.ts.map +1 -1
  72. package/lib/cloud-connection/transaction-manager.js +29 -28
  73. package/lib/cloud-connection/transaction-manager.js.map +1 -1
  74. package/lib/cloud-connection/transaction-manager.test.js +46 -5
  75. package/lib/cloud-connection/transaction-manager.test.js.map +1 -1
  76. package/lib/device-control/device-control.d.ts +8 -8
  77. package/lib/device-control/device-control.d.ts.map +1 -1
  78. package/lib/device-control/device-control.js +95 -71
  79. package/lib/device-control/device-control.js.map +1 -1
  80. package/lib/docker/docker-compose.d.ts.map +1 -1
  81. package/lib/docker/docker-compose.js +2 -1
  82. package/lib/docker/docker-compose.js.map +1 -1
  83. package/lib/infrastructure/agent-config.d.ts +2 -1
  84. package/lib/infrastructure/agent-config.d.ts.map +1 -1
  85. package/lib/infrastructure/agent-config.js +7 -7
  86. package/lib/infrastructure/agent-config.js.map +1 -1
  87. package/lib/infrastructure/agent-config.test.js +3 -1
  88. package/lib/infrastructure/agent-config.test.js.map +1 -1
  89. package/lib/infrastructure/config-check-utility.d.ts +6 -0
  90. package/lib/infrastructure/config-check-utility.d.ts.map +1 -0
  91. package/lib/infrastructure/config-check-utility.js +67 -0
  92. package/lib/infrastructure/config-check-utility.js.map +1 -0
  93. package/lib/infrastructure/config-check-utility.test.d.ts +2 -0
  94. package/lib/infrastructure/config-check-utility.test.d.ts.map +1 -0
  95. package/lib/infrastructure/config-check-utility.test.js +109 -0
  96. package/lib/infrastructure/config-check-utility.test.js.map +1 -0
  97. package/lib/infrastructure/device-certificate.d.ts +10 -0
  98. package/lib/infrastructure/device-certificate.d.ts.map +1 -0
  99. package/lib/infrastructure/device-certificate.js +47 -0
  100. package/lib/infrastructure/device-certificate.js.map +1 -0
  101. package/lib/infrastructure/device-certificate.test.d.ts +2 -0
  102. package/lib/infrastructure/device-certificate.test.d.ts.map +1 -0
  103. package/lib/infrastructure/device-certificate.test.js +24 -0
  104. package/lib/infrastructure/device-certificate.test.js.map +1 -0
  105. package/lib/infrastructure/legacy-migration/legacy-file.test.d.ts +2 -0
  106. package/lib/infrastructure/legacy-migration/legacy-file.test.d.ts.map +1 -0
  107. package/lib/infrastructure/legacy-migration/legacy-file.test.js +61 -0
  108. package/lib/infrastructure/legacy-migration/legacy-file.test.js.map +1 -0
  109. package/lib/infrastructure/legacy-migration/legacy-files.d.ts +75 -0
  110. package/lib/infrastructure/legacy-migration/legacy-files.d.ts.map +1 -0
  111. package/lib/infrastructure/legacy-migration/legacy-files.js +75 -0
  112. package/lib/infrastructure/legacy-migration/legacy-files.js.map +1 -0
  113. package/lib/infrastructure/legacy-migration/legacy-migration.d.ts +6 -0
  114. package/lib/infrastructure/legacy-migration/legacy-migration.d.ts.map +1 -0
  115. package/lib/infrastructure/legacy-migration/legacy-migration.js +149 -0
  116. package/lib/infrastructure/legacy-migration/legacy-migration.js.map +1 -0
  117. package/lib/infrastructure/legacy-migration/legacy-migration.test.d.ts +2 -0
  118. package/lib/infrastructure/legacy-migration/legacy-migration.test.d.ts.map +1 -0
  119. package/lib/infrastructure/legacy-migration/legacy-migration.test.js +226 -0
  120. package/lib/infrastructure/legacy-migration/legacy-migration.test.js.map +1 -0
  121. package/lib/infrastructure/require-files-present-ready.test.d.ts +2 -0
  122. package/lib/infrastructure/require-files-present-ready.test.d.ts.map +1 -0
  123. package/lib/infrastructure/require-files-present-ready.test.js +44 -0
  124. package/lib/infrastructure/require-files-present-ready.test.js.map +1 -0
  125. package/lib/infrastructure/required-config-checks.d.ts +2 -0
  126. package/lib/infrastructure/required-config-checks.d.ts.map +1 -0
  127. package/lib/infrastructure/required-config-checks.js +30 -0
  128. package/lib/infrastructure/required-config-checks.js.map +1 -0
  129. package/lib/infrastructure/tokens-and-device-cfg.d.ts.map +1 -1
  130. package/lib/infrastructure/tokens-and-device-cfg.js +11 -8
  131. package/lib/infrastructure/tokens-and-device-cfg.js.map +1 -1
  132. package/lib/jobs/job-handler.d.ts +23 -0
  133. package/lib/jobs/job-handler.d.ts.map +1 -0
  134. package/lib/jobs/job-handler.js +131 -0
  135. package/lib/jobs/job-handler.js.map +1 -0
  136. package/lib/local-connection/rabbitmq-connection.d.ts.map +1 -1
  137. package/lib/local-connection/rabbitmq-connection.js +14 -14
  138. package/lib/local-connection/rabbitmq-connection.js.map +1 -1
  139. package/lib/secure-tunneling/secure-tunnel-message-handler.d.ts +8 -0
  140. package/lib/secure-tunneling/secure-tunnel-message-handler.d.ts.map +1 -0
  141. package/lib/secure-tunneling/secure-tunnel-message-handler.js +42 -0
  142. package/lib/secure-tunneling/secure-tunnel-message-handler.js.map +1 -0
  143. package/lib/secure-tunneling/secure-tunneling.d.ts +9 -9
  144. package/lib/secure-tunneling/secure-tunneling.d.ts.map +1 -1
  145. package/lib/secure-tunneling/secure-tunneling.js +21 -16
  146. package/lib/secure-tunneling/secure-tunneling.js.map +1 -1
  147. package/lib/secure-tunneling/secure-tunneling.test.js +11 -13
  148. package/lib/secure-tunneling/secure-tunneling.test.js.map +1 -1
  149. package/lib/subcommands/app/analytics.d.ts.map +1 -1
  150. package/lib/subcommands/app/analytics.js +1 -2
  151. package/lib/subcommands/app/analytics.js.map +1 -1
  152. package/lib/subcommands/app/env-vars.d.ts +4 -0
  153. package/lib/subcommands/app/env-vars.d.ts.map +1 -1
  154. package/lib/subcommands/app/env-vars.js +52 -6
  155. package/lib/subcommands/app/env-vars.js.map +1 -1
  156. package/lib/subcommands/app/index.d.ts.map +1 -1
  157. package/lib/subcommands/app/index.js +1 -3
  158. package/lib/subcommands/app/index.js.map +1 -1
  159. package/lib/subcommands/app/models.d.ts +0 -11
  160. package/lib/subcommands/app/models.d.ts.map +1 -1
  161. package/lib/subcommands/app/models.js +2 -58
  162. package/lib/subcommands/app/models.js.map +1 -1
  163. package/lib/subcommands/app/shadow.d.ts.map +1 -1
  164. package/lib/subcommands/app/shadow.js +6 -5
  165. package/lib/subcommands/app/shadow.js.map +1 -1
  166. package/lib/subcommands/app/version.d.ts +2 -0
  167. package/lib/subcommands/app/version.d.ts.map +1 -1
  168. package/lib/subcommands/app/version.js +16 -6
  169. package/lib/subcommands/app/version.js.map +1 -1
  170. package/lib/subcommands/config.d.ts +2 -0
  171. package/lib/subcommands/config.d.ts.map +1 -0
  172. package/lib/subcommands/config.js +39 -0
  173. package/lib/subcommands/config.js.map +1 -0
  174. package/lib/subcommands/device/clean.d.ts +1 -1
  175. package/lib/subcommands/device/clean.d.ts.map +1 -1
  176. package/lib/subcommands/device/clean.js +23 -13
  177. package/lib/subcommands/device/clean.js.map +1 -1
  178. package/lib/subcommands/device/index.d.ts.map +1 -1
  179. package/lib/subcommands/device/index.js +3 -1
  180. package/lib/subcommands/device/index.js.map +1 -1
  181. package/lib/subcommands/device/init.js +8 -8
  182. package/lib/subcommands/device/init.js.map +1 -1
  183. package/lib/subcommands/device/migrate.d.ts +2 -0
  184. package/lib/subcommands/device/migrate.d.ts.map +1 -0
  185. package/lib/subcommands/device/migrate.js +24 -0
  186. package/lib/subcommands/device/migrate.js.map +1 -0
  187. package/lib/subcommands/device/refresh.d.ts.map +1 -1
  188. package/lib/subcommands/device/refresh.js +1 -0
  189. package/lib/subcommands/device/refresh.js.map +1 -1
  190. package/lib/subcommands/index.d.ts +1 -1
  191. package/lib/subcommands/index.d.ts.map +1 -1
  192. package/lib/subcommands/index.js +3 -1
  193. package/lib/subcommands/index.js.map +1 -1
  194. package/lib/subcommands/rabbitmq-connection.d.ts +1 -1
  195. package/lib/subcommands/rabbitmq-connection.d.ts.map +1 -1
  196. package/lib/util/aai-error.d.ts +12 -0
  197. package/lib/util/aai-error.d.ts.map +1 -0
  198. package/lib/util/aai-error.js +11 -0
  199. package/lib/util/aai-error.js.map +1 -0
  200. package/lib/util/aws-regions.d.ts +2 -0
  201. package/lib/util/aws-regions.d.ts.map +1 -0
  202. package/lib/util/{cloud-mode-ready.js → aws-regions.js} +2 -20
  203. package/lib/util/aws-regions.js.map +1 -0
  204. package/lib/util/check-for-updates.d.ts.map +1 -1
  205. package/lib/util/check-for-updates.js +5 -28
  206. package/lib/util/check-for-updates.js.map +1 -1
  207. package/lib/util/clean-certs.d.ts.map +1 -1
  208. package/lib/util/clean-certs.js +5 -4
  209. package/lib/util/clean-certs.js.map +1 -1
  210. package/lib/util/directories.d.ts +4 -18
  211. package/lib/util/directories.d.ts.map +1 -1
  212. package/lib/util/directories.js +18 -32
  213. package/lib/util/directories.js.map +1 -1
  214. package/lib/util/file.d.ts +4 -0
  215. package/lib/util/file.d.ts.map +1 -1
  216. package/lib/util/file.js +65 -4
  217. package/lib/util/file.js.map +1 -1
  218. package/lib/util/get-device-id.d.ts.map +1 -1
  219. package/lib/util/get-device-id.js +7 -1
  220. package/lib/util/get-device-id.js.map +1 -1
  221. package/lib/util/http-client.js +3 -3
  222. package/lib/util/http-client.js.map +1 -1
  223. package/package.json +19 -17
  224. package/readme.md +12 -32
  225. package/src/application-control/config.ts +9 -12
  226. package/src/application-control/environment-variables.test.ts +28 -7
  227. package/src/application-control/environment-variables.ts +42 -59
  228. package/src/application-control/index.ts +3 -16
  229. package/src/application-control/install.ts +39 -13
  230. package/src/application-control/models.ts +6 -87
  231. package/src/application-control/utils.ts +10 -25
  232. package/src/cloud-connection/base-message-handler.ts +118 -0
  233. package/src/cloud-connection/bootstrap-provision.ts +7 -7
  234. package/src/cloud-connection/connection-manager.ts +187 -0
  235. package/src/cloud-connection/device-agent-cloud-connection.ts +130 -723
  236. package/src/cloud-connection/device-agent-message-handler.ts +642 -0
  237. package/src/cloud-connection/device-agent.ts +16 -7
  238. package/src/cloud-connection/live-updates-handler.test.ts +121 -189
  239. package/src/cloud-connection/live-updates-handler.ts +105 -232
  240. package/src/cloud-connection/message-dispatcher.ts +33 -0
  241. package/src/cloud-connection/passthrough-handler.ts +55 -18
  242. package/src/cloud-connection/shadow-handler.test.ts +45 -57
  243. package/src/cloud-connection/shadow-handler.ts +224 -54
  244. package/src/cloud-connection/shadow.ts +4 -1
  245. package/src/cloud-connection/transaction-manager.test.ts +68 -6
  246. package/src/cloud-connection/transaction-manager.ts +69 -41
  247. package/src/device-control/device-control.ts +102 -70
  248. package/src/docker/docker-compose.ts +3 -2
  249. package/src/infrastructure/agent-config.test.ts +6 -2
  250. package/src/infrastructure/agent-config.ts +8 -7
  251. package/src/infrastructure/config-check-utility.test.ts +154 -0
  252. package/src/infrastructure/config-check-utility.ts +77 -0
  253. package/src/infrastructure/device-certificate.test.ts +40 -0
  254. package/src/infrastructure/device-certificate.ts +58 -0
  255. package/src/infrastructure/legacy-migration/legacy-file.test.ts +88 -0
  256. package/src/infrastructure/legacy-migration/legacy-files.ts +101 -0
  257. package/src/infrastructure/legacy-migration/legacy-migration.test.ts +396 -0
  258. package/src/infrastructure/legacy-migration/legacy-migration.ts +229 -0
  259. package/src/infrastructure/require-files-present-ready.test.ts +53 -0
  260. package/src/infrastructure/required-config-checks.ts +33 -0
  261. package/src/infrastructure/tokens-and-device-cfg.ts +12 -10
  262. package/src/jobs/job-handler.ts +146 -0
  263. package/src/local-connection/rabbitmq-connection.ts +22 -17
  264. package/src/secure-tunneling/secure-tunnel-message-handler.ts +56 -0
  265. package/src/secure-tunneling/secure-tunneling.test.ts +20 -22
  266. package/src/secure-tunneling/secure-tunneling.ts +41 -29
  267. package/src/subcommands/app/analytics.ts +2 -4
  268. package/src/subcommands/app/env-vars.ts +72 -9
  269. package/src/subcommands/app/index.ts +3 -11
  270. package/src/subcommands/app/models.ts +5 -81
  271. package/src/subcommands/app/shadow.ts +6 -5
  272. package/src/subcommands/app/version.ts +23 -6
  273. package/src/subcommands/config.ts +42 -0
  274. package/src/subcommands/device/clean.ts +31 -17
  275. package/src/subcommands/device/index.ts +3 -1
  276. package/src/subcommands/device/init.ts +11 -11
  277. package/src/subcommands/device/migrate.ts +20 -0
  278. package/src/subcommands/device/refresh.ts +1 -0
  279. package/src/subcommands/index.ts +3 -1
  280. package/src/util/aai-error.ts +20 -0
  281. package/src/util/{cloud-mode-ready.ts → aws-regions.ts} +0 -24
  282. package/src/util/check-for-updates.ts +14 -30
  283. package/src/util/clean-certs.ts +8 -4
  284. package/src/util/directories.ts +23 -67
  285. package/src/util/file.ts +83 -3
  286. package/src/util/get-device-id.ts +7 -7
  287. package/src/util/http-client.ts +2 -2
  288. package/lib/util/cloud-mode-ready.d.ts +0 -3
  289. package/lib/util/cloud-mode-ready.d.ts.map +0 -1
  290. package/lib/util/cloud-mode-ready.js.map +0 -1
  291. package/lib/util/download-file.d.ts +0 -6
  292. package/lib/util/download-file.d.ts.map +0 -1
  293. package/lib/util/download-file.js +0 -25
  294. package/lib/util/download-file.js.map +0 -1
  295. package/lib/util/fetch-with-timeout.d.ts +0 -4
  296. package/lib/util/fetch-with-timeout.d.ts.map +0 -1
  297. package/lib/util/fetch-with-timeout.js +0 -30
  298. package/lib/util/fetch-with-timeout.js.map +0 -1
  299. package/lib/util/parsing.d.ts +0 -2
  300. package/lib/util/parsing.d.ts.map +0 -1
  301. package/lib/util/parsing.js +0 -17
  302. package/lib/util/parsing.js.map +0 -1
  303. package/src/util/download-file.ts +0 -25
  304. package/src/util/fetch-with-timeout.ts +0 -35
  305. package/src/util/parsing.ts +0 -11
@@ -4,13 +4,13 @@ import {
4
4
  CliStringInput,
5
5
  CliTerseError
6
6
  } from '@alwaysai/alwayscli';
7
- import { EnvVars, getAllEnvs } from '../../application-control';
7
+ import { getAllEnvs } from '../../application-control';
8
8
  import { DeviceAgentCloudConnection } from '../../cloud-connection/device-agent-cloud-connection';
9
9
  import sleep from '../../util/sleep';
10
- import { logger } from '../../util/logger';
11
10
  import {
12
- buildUpdateProjectShadowMessage,
13
- getShadowTopic
11
+ buildUpdateShadowMessage,
12
+ getShadowTopic,
13
+ EnvVars
14
14
  } from '@alwaysai/device-agent-schemas';
15
15
 
16
16
  export const getAllEnvsCliLeaf = CliLeaf({
@@ -48,27 +48,90 @@ export const setEnvCliLeaf = CliLeaf({
48
48
  },
49
49
  async action(args, opts) {
50
50
  const { project: projectId, service } = opts;
51
- const envVars: EnvVars = { [service]: {} };
51
+ const currentEnvs = await getAllEnvs({ projectId });
52
+ const envVars: EnvVars = { [service]: { ...currentEnvs[service] } };
53
+
52
54
  args.forEach((arg: string) => {
53
55
  const nameVal = arg.split('=');
54
56
  if (nameVal.length !== 2) {
55
57
  throw new CliTerseError(`Invalid argument: ${arg}`);
56
58
  }
57
- const value = nameVal[1] === '' ? null : nameVal[1];
59
+ const value = nameVal[1];
58
60
  envVars[service][nameVal[0]] = value;
59
61
  });
60
62
  const deviceAgent = new DeviceAgentCloudConnection();
61
- await deviceAgent.setupHandlers();
62
63
 
63
64
  const toDesire = {
64
65
  [projectId]: {
65
- envVars
66
+ envVars: JSON.stringify(envVars)
67
+ }
68
+ };
69
+ deviceAgent.publisher.publish(
70
+ getShadowTopic(deviceAgent.getClientId(), 'projects', 'update'),
71
+ JSON.stringify(
72
+ buildUpdateShadowMessage({
73
+ clientId: 'client',
74
+ desired: toDesire
75
+ })
76
+ )
77
+ );
78
+ // Sleep for extra time to ensure time for shadow response
79
+ await sleep(10000);
80
+
81
+ while (deviceAgent.isCmdInProgress(projectId)) {
82
+ await sleep(1000);
83
+ }
84
+ await deviceAgent.stop();
85
+ }
86
+ });
87
+
88
+ export const rmEnvCliLeaf = CliLeaf({
89
+ name: 'rm-env',
90
+ description: 'Remove an environment variable from an application',
91
+ positionalInput: CliStringInput({
92
+ description: 'The key of the environment variable to be removed',
93
+ required: true
94
+ }),
95
+ namedInputs: {
96
+ project: CliStringInput({
97
+ description: 'Project Id',
98
+ required: true
99
+ }),
100
+ service: CliStringInput({
101
+ description:
102
+ 'Docker compose service to remove an environment variable from',
103
+ required: true
104
+ })
105
+ },
106
+ async action(arg, opts) {
107
+ const { project: projectId, service } = opts;
108
+ const envVar = arg;
109
+ const currentEnvs = await getAllEnvs({ projectId: projectId });
110
+
111
+ const newEnvs = { ...currentEnvs };
112
+ if (Object.keys(newEnvs[service]).includes(envVar)) {
113
+ delete newEnvs[service][envVar];
114
+ } else {
115
+ throw new CliTerseError(
116
+ `Could not find key "${envVar}" in service ${service}. Current keys are:\n ${JSON.stringify(
117
+ Object.keys(currentEnvs[service]),
118
+ null,
119
+ 2
120
+ )}.\nPlease check the key and try again.`
121
+ );
122
+ }
123
+
124
+ const deviceAgent = new DeviceAgentCloudConnection();
125
+
126
+ const toDesire = {
127
+ [projectId]: {
128
+ envVars: JSON.stringify(newEnvs)
66
129
  }
67
130
  };
68
131
  deviceAgent.publisher.publish(
69
132
  getShadowTopic(deviceAgent.getClientId(), 'projects', 'update'),
70
133
  JSON.stringify(
71
- buildUpdateProjectShadowMessage({
134
+ buildUpdateShadowMessage({
72
135
  clientId: 'client',
73
136
  desired: toDesire
74
137
  })
@@ -1,12 +1,6 @@
1
1
  import { CliBranch } from '@alwaysai/alwayscli';
2
- import { getAllEnvsCliLeaf, setEnvCliLeaf } from './env-vars';
3
- import {
4
- showAppModelsCliLeaf,
5
- addModelCliLeaf,
6
- removeModelCliLeaf,
7
- replaceModelsCliLeaf,
8
- updateModelsCliLeaf
9
- } from './models';
2
+ import { getAllEnvsCliLeaf, setEnvCliLeaf, rmEnvCliLeaf } from './env-vars';
3
+ import { showAppModelsCliLeaf, addModelCliLeaf } from './models';
10
4
  import {
11
5
  getAppStatusCliLeaf,
12
6
  startAppCliLeaf,
@@ -38,11 +32,9 @@ export const appCliBranch = CliBranch({
38
32
  rollbackAppCliLeaf,
39
33
  showAppModelsCliLeaf,
40
34
  addModelCliLeaf,
41
- removeModelCliLeaf,
42
- replaceModelsCliLeaf,
43
- updateModelsCliLeaf,
44
35
  getAllEnvsCliLeaf,
45
36
  setEnvCliLeaf,
37
+ rmEnvCliLeaf,
46
38
  getAnalyticsCfgCliLeaf,
47
39
  setAnalyticsCfgCliLeaf,
48
40
  getShadowCliLeaf,
@@ -1,23 +1,10 @@
1
- import {
2
- CliLeaf,
3
- CliNumberInput,
4
- CliStringArrayInput,
5
- CliStringInput
6
- } from '@alwaysai/alwayscli';
7
- import {
8
- getAppModels,
9
- readAppCfgFile,
10
- removeModel,
11
- replaceModels,
12
- updateModels
13
- } from '../../application-control';
1
+ import { CliLeaf, CliStringInput } from '@alwaysai/alwayscli';
2
+ import { getAppModels, readAppCfgFile } from '../../application-control';
14
3
  import { DeviceAgentCloudConnection } from '../../cloud-connection/device-agent-cloud-connection';
15
4
  import sleep from '../../util/sleep';
16
- import { logger } from '../../util/logger';
17
5
  import {
18
- buildUpdateProjectShadowMessage,
19
- getShadowTopic,
20
- validateShadowProjectsUpdateAll
6
+ buildUpdateShadowMessage,
7
+ getShadowTopic
21
8
  } from '@alwaysai/device-agent-schemas';
22
9
 
23
10
  export const showAppModelsCliLeaf = CliLeaf({
@@ -56,7 +43,6 @@ export const addModelCliLeaf = CliLeaf({
56
43
  async action(_, opts) {
57
44
  const { project: projectId, model, version } = opts;
58
45
  const deviceAgent = new DeviceAgentCloudConnection();
59
- await deviceAgent.setupHandlers();
60
46
 
61
47
  // Update the shadow as a client
62
48
 
@@ -71,7 +57,7 @@ export const addModelCliLeaf = CliLeaf({
71
57
  deviceAgent.publisher.publish(
72
58
  getShadowTopic(deviceAgent.getClientId(), 'projects', 'update'),
73
59
  JSON.stringify(
74
- buildUpdateProjectShadowMessage({
60
+ buildUpdateShadowMessage({
75
61
  clientId: 'client',
76
62
  desired: toDesire
77
63
  })
@@ -86,65 +72,3 @@ export const addModelCliLeaf = CliLeaf({
86
72
  await deviceAgent.stop();
87
73
  }
88
74
  });
89
-
90
- export const removeModelCliLeaf = CliLeaf({
91
- name: 'remove-model',
92
- description: 'Remove a model from an alwaysAI app',
93
- namedInputs: {
94
- project: CliStringInput({
95
- description: 'Project ID',
96
- required: true
97
- }),
98
- model: CliStringInput({
99
- description: 'Model ID',
100
- required: true
101
- })
102
- },
103
- async action(_, opts) {
104
- const { project, model } = opts;
105
- // TODO: Replace with handleMessage()
106
- await removeModel({ projectId: project, modelId: model });
107
- }
108
- });
109
-
110
- export const replaceModelsCliLeaf = CliLeaf({
111
- name: 'replace-models',
112
- description: 'Replace all models of an alwaysAI app with new models',
113
- namedInputs: {
114
- project: CliStringInput({
115
- description: 'Project Id',
116
- required: true
117
- }),
118
- models: CliStringArrayInput({
119
- description: 'One or more model IDs',
120
- required: true
121
- })
122
- },
123
- hidden: true,
124
- async action(_, opts) {
125
- const { project, models } = opts;
126
- console.log(
127
- 'This command is deprecated and will be removed in a future release'
128
- );
129
- await replaceModels({ projectId: project, modelIds: models });
130
- }
131
- });
132
-
133
- export const updateModelsCliLeaf = CliLeaf({
134
- name: 'update-models',
135
- description: 'Update all models for an alwaysAI app',
136
- namedInputs: {
137
- project: CliStringInput({
138
- description: 'Project Id',
139
- required: true
140
- })
141
- },
142
- hidden: true,
143
- async action(_, opts) {
144
- const { project } = opts;
145
- console.log(
146
- 'This command is deprecated and will be removed in a future release'
147
- );
148
- await updateModels({ projectId: project });
149
- }
150
- });
@@ -14,10 +14,9 @@ export const getShadowCliLeaf = CliLeaf({
14
14
  async action(_, opts) {
15
15
  const { project } = opts;
16
16
  const deviceAgent = new DeviceAgentCloudConnection();
17
- await deviceAgent.setupHandlers();
17
+ deviceAgent.shadowHandler.getProjectShadowUpdates();
18
18
 
19
- // The Device Agent always gets the latest shadow and updates
20
- // itself. 5 seconds should cover the initial response wait time
19
+ // 5 seconds should cover the response wait time
21
20
  await sleep(5000);
22
21
  while (deviceAgent.isCmdInProgress(project)) {
23
22
  await sleep(1000);
@@ -38,11 +37,13 @@ export const updateShadowCliLeaf = CliLeaf({
38
37
  async action(_, opts) {
39
38
  const { project } = opts;
40
39
  const deviceAgent = new DeviceAgentCloudConnection();
41
- await deviceAgent.setupHandlers();
42
- await deviceAgent.updateProjectShadow(project);
40
+ await deviceAgent.shadowHandler.updateProjectShadow(project);
43
41
 
44
42
  // 5 seconds should cover the response wait time
45
43
  await sleep(5000);
44
+ while (deviceAgent.isCmdInProgress(project)) {
45
+ await sleep(1000);
46
+ }
46
47
  await deviceAgent.stop();
47
48
  }
48
49
  });
@@ -2,6 +2,7 @@ import { CliLeaf, CliStringInput, CliTerseError } from '@alwaysai/alwayscli';
2
2
  import {
3
3
  AppVersionControlMessage,
4
4
  generateTxId,
5
+ getToDeviceTopic,
5
6
  keyMirrors
6
7
  } from '@alwaysai/device-agent-schemas';
7
8
  import { rollbackApp } from '../../application-control';
@@ -36,10 +37,26 @@ export const installAppCliLeaf = CliLeaf({
36
37
  description: 'Release Hash',
37
38
  required: false,
38
39
  hidden: true
40
+ }),
41
+ appCfg: CliStringInput({
42
+ description: 'Application Configuration',
43
+ required: false,
44
+ hidden: true
45
+ }),
46
+ envVars: CliStringInput({
47
+ description: 'Environment Variables configuration',
48
+ hidden: true,
49
+ required: false
39
50
  })
40
51
  },
41
52
  async action(_, opts) {
42
- const { project, releaseHash, 'release-hash': releaseHashNew } = opts;
53
+ const {
54
+ project,
55
+ releaseHash,
56
+ 'release-hash': releaseHashNew,
57
+ appCfg,
58
+ envVars
59
+ } = opts;
43
60
  if (releaseHash) {
44
61
  logger.warn(
45
62
  `--releaseHash is deprecated and will be removed in a future release. Please switch to --release-hash`
@@ -50,8 +67,7 @@ export const installAppCliLeaf = CliLeaf({
50
67
  throw new CliTerseError('--release-hash flag is required!');
51
68
  }
52
69
  const deviceAgent = new DeviceAgentCloudConnection();
53
- await deviceAgent.setupHandlers();
54
- const topic = deviceAgent.getToDeviceTopic();
70
+ const topic = getToDeviceTopic(deviceAgent.getClientId());
55
71
  const message: AppVersionControlMessage = {
56
72
  timestamp: '',
57
73
  topic,
@@ -60,7 +76,9 @@ export const installAppCliLeaf = CliLeaf({
60
76
  payload: {
61
77
  baseCommand: keyMirrors.appVersionControl.install,
62
78
  projectId: project,
63
- appReleaseHash: releaseHashResolved
79
+ appReleaseHash: releaseHashResolved,
80
+ appCfg,
81
+ envVars
64
82
  }
65
83
  };
66
84
  await deviceAgent.handleMessage(topic, message);
@@ -83,8 +101,7 @@ export const uninstallAppCliLeaf = CliLeaf({
83
101
  async action(_, opts) {
84
102
  const { project } = opts;
85
103
  const deviceAgent = new DeviceAgentCloudConnection();
86
- await deviceAgent.setupHandlers();
87
- const topic = deviceAgent.getToDeviceTopic();
104
+ const topic = getToDeviceTopic(deviceAgent.getClientId());
88
105
  const message: AppVersionControlMessage = {
89
106
  timestamp: '',
90
107
  topic,
@@ -0,0 +1,42 @@
1
+ import { CliBranch, CliLeaf, CliOneOfInput } from '@alwaysai/alwayscli';
2
+ import { SYSTEM_IDS } from 'alwaysai/lib/constants';
3
+ import { ALWAYSAI_SYSTEM_ID } from 'alwaysai/lib/environment';
4
+ import { getSystemId, setSystemId } from 'alwaysai/lib/infrastructure';
5
+ import { echo } from 'alwaysai/lib/util';
6
+ import { ALWAYSAI_SHOW_HIDDEN } from '../environment';
7
+
8
+ const show = CliLeaf({
9
+ name: 'show',
10
+ description: `Show your current configuration`,
11
+ action() {
12
+ if (ALWAYSAI_SYSTEM_ID) {
13
+ echo(
14
+ '[WARNING] System ID is overridden by environment variable "ALWAYSAI_SYSTEM_ID"'
15
+ );
16
+ }
17
+ return { systemId: getSystemId() };
18
+ }
19
+ });
20
+
21
+ const set = CliLeaf({
22
+ name: 'set',
23
+ description: 'Set an configuration value',
24
+ namedInputs: {
25
+ systemId: CliOneOfInput({ values: SYSTEM_IDS, required: true })
26
+ },
27
+ action(_, { systemId }) {
28
+ if (ALWAYSAI_SYSTEM_ID) {
29
+ echo(
30
+ 'WARNING] System ID is overridden by environment variable "ALWAYSAI_SYSTEM_ID"'
31
+ );
32
+ }
33
+ return setSystemId(systemId);
34
+ }
35
+ });
36
+
37
+ export const configBranch = CliBranch({
38
+ name: 'config',
39
+ hidden: !ALWAYSAI_SHOW_HIDDEN,
40
+ description: `Show or set configuration values`,
41
+ subcommands: [show, set]
42
+ });
@@ -1,20 +1,25 @@
1
1
  import { CliLeaf } from '@alwaysai/alwayscli';
2
- import { logger } from '../../util/logger';
3
- import { AgentConfigFile } from '../../infrastructure/agent-config';
4
2
  import { DeviceConfigFile } from 'alwaysai/lib/core/device';
5
3
  import {
6
- APP_ROOT,
7
- CREDENTIALS_FILE_PATH,
8
- DEVICE_AGENT_CFG_PATH,
9
- DEVICE_AGENT_DOCKER_COMPOSE_PATH
10
- } from '../../util/directories';
4
+ LOCAL_CERT_AND_KEY_DIR,
5
+ LocalLegacyDeviceCertificates
6
+ } from 'alwaysai/lib/infrastructure';
7
+ import { stringifyError } from 'alwaysai/lib/util';
8
+ import { readdir } from 'fs/promises';
9
+ import { uninstallApp } from '../../application-control';
10
+ import { AgentConfigFile } from '../../infrastructure/agent-config';
11
11
  import {
12
12
  checkRabbitMQContainerRunning,
13
13
  stopRabbitMQContainer
14
14
  } from '../../local-connection/rabbitmq-connection';
15
- import { stopApp } from '../../application-control';
16
- import { LOCAL_CERT_AND_KEY_DIR } from 'alwaysai/lib/paths';
15
+ import {
16
+ APP_ROOT,
17
+ CREDENTIALS_FILE_PATH,
18
+ getDeviceAgentConfigPath,
19
+ getDeviceAgentDockerComposePath
20
+ } from '../../util/directories';
17
21
  import { safeRimraf } from '../../util/file';
22
+ import { logger } from '../../util/logger';
18
23
 
19
24
  export const cleanCliLeaf = CliLeaf({
20
25
  name: 'clean',
@@ -27,23 +32,27 @@ export const cleanCliLeaf = CliLeaf({
27
32
  }
28
33
  } catch (e) {
29
34
  logger.error(
30
- `You may need to manually stop the container by running docker-compose down in the following directory: ${DEVICE_AGENT_DOCKER_COMPOSE_PATH}`
35
+ `You may need to manually stop the container by running docker-compose down in the following directory: ${getDeviceAgentDockerComposePath()}`
36
+ );
37
+ logger.debug(
38
+ `Error in checking / stopping RabbitMQ container!\n${stringifyError(e)}`
31
39
  );
32
- logger.debug(`Error in checking / stopping RabbitMQ container: ${e}`);
33
40
  }
34
41
 
35
- await safeRimraf(DEVICE_AGENT_CFG_PATH);
42
+ await safeRimraf(getDeviceAgentConfigPath());
36
43
 
37
44
  logger.debug('Checking for alwaysAI applications still running');
38
45
  const apps = await AgentConfigFile().getApps();
39
- const stopAppPromises: Promise<void>[] = [];
46
+ const uninstallAppPromises: Promise<void>[] = [];
40
47
  for (const app of apps) {
41
48
  const { projectId } = app;
42
- stopAppPromises.push(stopApp({ projectId }));
49
+ uninstallAppPromises.push(uninstallApp({ projectId }));
43
50
  }
44
- await Promise.all(stopAppPromises);
51
+ await Promise.all(uninstallAppPromises);
45
52
 
46
53
  await safeRimraf(LOCAL_CERT_AND_KEY_DIR);
54
+ const legacyCertificates = new LocalLegacyDeviceCertificates();
55
+ await safeRimraf(legacyCertificates.getCertificateDirectoryPath());
47
56
 
48
57
  logger.debug(`Removing ${AgentConfigFile().path}`);
49
58
  AgentConfigFile().remove();
@@ -51,8 +60,13 @@ export const cleanCliLeaf = CliLeaf({
51
60
  DeviceConfigFile().remove();
52
61
 
53
62
  await safeRimraf(CREDENTIALS_FILE_PATH);
54
- await safeRimraf(APP_ROOT);
55
-
63
+ const files = await readdir(APP_ROOT);
64
+ logger.debug(`${APP_ROOT} folder contents ${JSON.stringify(files.length)}`);
65
+ // This won't handle hidden files, but will remove if truly empty, which is safer
66
+ if (files.length === 0) {
67
+ logger.debug('Applications directory is empty, removing.');
68
+ await safeRimraf(APP_ROOT);
69
+ }
56
70
  logger.info('Device configuration cleaned');
57
71
  }
58
72
  });
@@ -4,6 +4,7 @@ import { cleanCliLeaf } from './clean';
4
4
  import { initCliLeaf } from './init';
5
5
  import { restartCliLeaf } from './restart';
6
6
  import { refreshCliLeaf } from './refresh';
7
+ import { migrateCliLeaf } from './migrate';
7
8
 
8
9
  export const deviceCliBranch = CliBranch({
9
10
  name: 'device',
@@ -13,6 +14,7 @@ export const deviceCliBranch = CliBranch({
13
14
  getInfoCliLeaf,
14
15
  refreshCliLeaf,
15
16
  cleanCliLeaf,
16
- restartCliLeaf
17
+ restartCliLeaf,
18
+ migrateCliLeaf
17
19
  ]
18
20
  });
@@ -1,21 +1,21 @@
1
1
  import { CliLeaf, CliStringInput } from '@alwaysai/alwayscli';
2
2
  import { getTargetHardwareUuid } from 'alwaysai/lib/core/app';
3
- import { v4 as uuidv4 } from 'uuid';
4
- import { CliAuthenticationClient } from 'alwaysai/lib/infrastructure';
3
+ import {
4
+ CliAuthenticationClient,
5
+ DEVICE_PRIVATE_KEY_FILE_PATH,
6
+ LOCAL_CERT_AND_KEY_DIR
7
+ } from 'alwaysai/lib/infrastructure';
5
8
  import { existsSync } from 'fs';
9
+ import { v4 as uuidv4 } from 'uuid';
10
+ import { AWS_ROOT_CERTIFICATE_FILE_NAME } from '../../util/directories';
6
11
  import { httpClient, microServiceHttpClient } from '../../util/http-client';
7
- import {
8
- AWS_ROOT_CERTIFICATE_FILE_NAME,
9
- BOOTSTRAP_CERTIFICATES_DIR_PATH,
10
- DEVICE_PRIVATE_KEY_FILE_PATH
11
- } from '../../util/directories';
12
12
 
13
+ import { DeviceConfigFile } from 'alwaysai/lib/core/device';
13
14
  import { JsSpawner } from 'alwaysai/lib/util';
15
+ import { getBootstrapCertificateDirectoryPath } from '../../infrastructure/device-certificate';
14
16
  import { writeTokenAndDeviceCfg } from '../../infrastructure/tokens-and-device-cfg';
15
17
  import { logger } from '../../util/logger';
16
- import { DeviceConfigFile } from 'alwaysai/lib/core/device';
17
18
  import { requireLoggedInAndPaidPlan } from '../../util/require-logged-in-and-paid-plan';
18
- import { LOCAL_CERT_AND_KEY_DIR } from 'alwaysai/lib/paths';
19
19
 
20
20
  export const initCliLeaf = CliLeaf({
21
21
  name: 'init',
@@ -79,14 +79,14 @@ export const initCliLeaf = CliLeaf({
79
79
  deviceUuid: response.deviceUuid
80
80
  });
81
81
 
82
- await JsSpawner().mkdirp(BOOTSTRAP_CERTIFICATES_DIR_PATH());
82
+ await JsSpawner().mkdirp(getBootstrapCertificateDirectoryPath());
83
83
 
84
84
  await JsSpawner({ path: LOCAL_CERT_AND_KEY_DIR }).writeFile(
85
85
  AWS_ROOT_CERTIFICATE_FILE_NAME,
86
86
  ROOT_CERT_AWS
87
87
  );
88
88
  const certSpawner = JsSpawner({
89
- path: BOOTSTRAP_CERTIFICATES_DIR_PATH()
89
+ path: getBootstrapCertificateDirectoryPath()
90
90
  });
91
91
  const writeCertFilePromises: Promise<void>[] = [];
92
92
  for (const key in response.claimCertificate) {
@@ -0,0 +1,20 @@
1
+ import { CliLeaf } from '@alwaysai/alwayscli';
2
+ import { logger } from '../../util/logger';
3
+ import { requiredConfigFilesPresentAndValid } from '../../infrastructure/required-config-checks';
4
+ import { migrateFromLegacyCertsAndTokens } from '../../infrastructure/legacy-migration/legacy-migration';
5
+
6
+ export const migrateCliLeaf = CliLeaf({
7
+ name: 'migrate',
8
+ description: 'Migrate configuration to the latest',
9
+ namedInputs: {},
10
+ async action(_, opts) {
11
+ const filesAlreadyMigrated = await requiredConfigFilesPresentAndValid();
12
+ if (filesAlreadyMigrated) {
13
+ logger.info('Configuration already migrated');
14
+ } else {
15
+ logger.info('Attempting configuration file migration');
16
+ await migrateFromLegacyCertsAndTokens();
17
+ logger.info('Migration complete');
18
+ }
19
+ }
20
+ });
@@ -3,6 +3,7 @@ import { writeTokenAndDeviceCfg } from '../../infrastructure/tokens-and-device-c
3
3
  import { logger } from '../../util/logger';
4
4
  import { DeviceConfigFile } from 'alwaysai/lib/core/device';
5
5
 
6
+ // FIXME: Do we need this command now that tokens aren't used outside of provisioning step?
6
7
  export const refreshCliLeaf = CliLeaf({
7
8
  name: 'refresh',
8
9
  description: 'Refresh the device tokens',
@@ -1,11 +1,13 @@
1
1
  import { appCliBranch } from './app';
2
+ import { configBranch } from './config';
2
3
  import { deviceCliBranch } from './device';
3
- import { loginCliLeaf } from './login';
4
4
  import { getModelPackageCliLeaf } from './get-model-package';
5
+ import { loginCliLeaf } from './login';
5
6
  import { stopRabbitMQContainerCliLeaf } from './rabbitmq-connection';
6
7
 
7
8
  export const subcommands = [
8
9
  loginCliLeaf,
10
+ configBranch,
9
11
  appCliBranch,
10
12
  deviceCliBranch,
11
13
  getModelPackageCliLeaf,
@@ -0,0 +1,20 @@
1
+ import { ErrorObject } from 'ajv';
2
+
3
+ type ErrorOption = ErrorObject<string, Record<string, any>, unknown>;
4
+
5
+ type Details = ErrorOption | ErrorOption[] | Error | null;
6
+
7
+ interface Options {
8
+ cause?: Details;
9
+ }
10
+
11
+ class AaiError extends Error {
12
+ public detailed?: Details;
13
+ constructor(message: string, options?: Options) {
14
+ super(message);
15
+ this.name = 'AaiError';
16
+ this.detailed = options?.cause;
17
+ }
18
+ }
19
+
20
+ export default AaiError;
@@ -1,10 +1,3 @@
1
- import { existsSync } from 'fs';
2
- import {
3
- AWS_ROOT_CERTIFICATE_FILE_PATH,
4
- DEVICE_PRIVATE_KEY_FILE_PATH,
5
- DEVICE_CERTIFICATE_FILE_PATH
6
- } from '../util/directories';
7
-
8
1
  const VALID_AWS_REGIONS = [
9
2
  // 'af-south-1', // Africa (Cape Town)
10
3
  // 'ap-east-1', // Asia Pacific (Hong Kong)
@@ -40,20 +33,3 @@ const VALID_AWS_REGIONS = [
40
33
  export function isValidAwsRegion(region: string): boolean {
41
34
  return VALID_AWS_REGIONS.includes(region);
42
35
  }
43
-
44
- export function cloudModeReady() {
45
- let ready = true;
46
- const requiredFiles = [
47
- DEVICE_CERTIFICATE_FILE_PATH,
48
- DEVICE_PRIVATE_KEY_FILE_PATH,
49
- AWS_ROOT_CERTIFICATE_FILE_PATH
50
- ];
51
-
52
- for (const path of requiredFiles) {
53
- if (!existsSync(path)) {
54
- ready = false;
55
- break;
56
- }
57
- }
58
- return ready;
59
- }