@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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alwaysai/device-agent",
3
3
  "description": "The alwaysAI Device Agent",
4
- "version": "0.0.11",
4
+ "version": "0.0.13",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "publishConfig": {
@@ -20,18 +20,23 @@
20
20
  "clean": "rimraf lib packages",
21
21
  "build:clean": "npm run clean && npm run build",
22
22
  "build:watch": "tsc --project . --watch",
23
- "lint": "tslint --project tsconfig.build.json",
23
+ "lint": "eslint . --ext .ts",
24
24
  "lint:fix": "npm run lint -- --fix",
25
+ "prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
25
26
  "test": "npm run test:unit",
26
27
  "test:unit": "jest --coverage src",
27
28
  "test:watch": "npm run test:unit -- --watch",
28
29
  "preversion": "npm test && npm run build:clean",
29
- "postversion": "npm publish"
30
+ "postversion": "npm publish",
31
+ "publish-provisioning:dev": "node ./scripts/publish-provisioning-scripts.js dev",
32
+ "publish-provisioning:qa": "node ./scripts/publish-provisioning-scripts.js qa",
33
+ "publish-provisioning:prod": "node ./scripts/publish-provisioning-scripts.js prod"
30
34
  },
31
35
  "dependencies": {
32
- "@alwaysai/alwayscli": "0.3.1",
33
- "@alwaysai/config-nodejs": "0.1.0",
34
- "@alwaysai/device-agent-schemas": "1.1.0",
36
+ "@alwaysai/alwayscli": "0.3.2",
37
+ "@alwaysai/app-configuration-schemas": "0.0.1",
38
+ "@alwaysai/config-nodejs": "0.3.0",
39
+ "@alwaysai/device-agent-schemas": "1.1.2",
35
40
  "@carnesen/coded-error": "0.4.0",
36
41
  "@types/mkdirp": "1.0.2",
37
42
  "@types/pump": "1.1.1",
@@ -39,7 +44,7 @@
39
44
  "@types/signal-exit": "3.0.1",
40
45
  "@types/tar": "6.1.1",
41
46
  "ajv": "8.11.0",
42
- "alwaysai": "1.10.0",
47
+ "alwaysai": "1.12.3",
43
48
  "aws-iot-device-sdk": "2.2.12",
44
49
  "docker-compose": "0.23.17",
45
50
  "express": "4.17.3",
@@ -53,6 +58,7 @@
53
58
  "socket.io": "4.4.1",
54
59
  "tar": "6.1.11",
55
60
  "tree-kill": "1.2.2",
61
+ "uuid": "9.0.0",
56
62
  "winston": "3.3.3",
57
63
  "winston-daily-rotate-file": "4.5.5",
58
64
  "yaml": "2.1.1"
@@ -65,6 +71,8 @@
65
71
  "@types/jest": "27.0.3",
66
72
  "@types/node": "16.11.12",
67
73
  "@types/sinon": "10.0.6",
74
+ "@typescript-eslint/eslint-plugin": "^5.48.0",
75
+ "@typescript-eslint/parser": "^5.48.0",
68
76
  "aws-sdk": "^2.1046.0",
69
77
  "cp-cli": "2.0.0",
70
78
  "get-stream": "6.0.1",
@@ -74,7 +82,9 @@
74
82
  "ts-jest": "27.1.1",
75
83
  "ts-mock-imports": "1.3.8",
76
84
  "ts-node": "10.4.0",
77
- "tslint": "5.20.1",
85
+ "eslint": "^8.31.0",
86
+ "eslint-config-prettier": "^8.6.0",
87
+ "eslint-plugin-prettier": "^4.2.1",
78
88
  "typescript": "^4.5.3"
79
89
  },
80
90
  "files": [
@@ -3,7 +3,6 @@ import * as rimraf from 'rimraf';
3
3
  import { copyDir } from '../util/copy-dir';
4
4
  import { buildApp, getAppDir } from './utils';
5
5
  import { AgentConfigFile } from '../infrastructure/agent-config';
6
- import { runInDir } from '../util/run-in-dir';
7
6
  import { logger } from '../util/logger';
8
7
 
9
8
  export const BACKUP_EXT = '.bak';
@@ -25,12 +24,18 @@ export async function rollbackApp(props: { projectId: string }) {
25
24
  throw new Error(`Backup doesn't exist for ${projectId}`);
26
25
  }
27
26
  logger.info(`Rolling back app ${projectId} to ${config.version}`);
28
- await AgentConfigFile().setAppInstalling({ projectId, version: config.version });
27
+ await AgentConfigFile().setAppInstalling({
28
+ projectId,
29
+ version: config.version
30
+ });
29
31
  const appDir = getAppDir(projectId);
30
32
  const backupAppDir = `${appDir}${BACKUP_EXT}`;
31
33
  rimraf.sync(appDir);
32
34
  await copyDir({ srcPath: backupAppDir, destPath: appDir });
33
35
  await buildApp({ appDir });
34
- await AgentConfigFile().setAppInstalled({ projectId, version: config.version });
36
+ await AgentConfigFile().setAppInstalled({
37
+ projectId,
38
+ version: config.version
39
+ });
35
40
  logger.info(`Rolled back app ${projectId} to ${config.version}`);
36
41
  }
@@ -1,35 +1,97 @@
1
- import { AppConfig, AppJsonFile } from 'alwaysai/lib/core/app';
2
1
  import { AgentConfigFile } from '../infrastructure/agent-config';
3
2
  import { parse, stringify } from 'yaml';
4
- import { getAppDir } from './utils';
3
+ import { buildApp, getAppDir } from './utils';
5
4
  import { JsSpawner } from 'alwaysai/lib/util';
6
5
  import compose from 'docker-compose';
7
6
  import { assign, merge } from 'lodash';
7
+ import { AppJsonFile } from 'alwaysai/lib/core/app';
8
+ import { AppConfig } from '@alwaysai/app-configuration-schemas';
9
+ import { restartApp } from './status';
10
+ import { logger } from '../util/logger';
8
11
 
9
- export async function readAppCfg(props: { projectId: string }): Promise<AppConfig> {
12
+ export async function readAppCfgFile(props: {
13
+ projectId: string;
14
+ }): Promise<AppConfig> {
10
15
  const { projectId } = props;
11
- if (!(await AgentConfigFile().isAppReady({ projectId }))) {
12
- throw new Error(`App ${projectId} is not ready!`);
16
+ if (!(await AgentConfigFile().isAppPresent({ projectId }))) {
17
+ throw new Error(`App ${projectId} is not present!`);
13
18
  }
14
19
  const appDir = getAppDir(projectId);
15
20
  const appJson = AppJsonFile(appDir);
16
- return appJson.read();
21
+ if (!appJson.exists()) {
22
+ throw new Error(`App config for ${projectId} doesn't exist!`);
23
+ }
24
+ try {
25
+ return appJson.read();
26
+ } catch (err) {
27
+ throw new Error(
28
+ `Error reading app config for ${projectId}:\n${err}\n${appJson.getErrors()}`
29
+ );
30
+ }
17
31
  }
18
32
 
19
- export async function writeAppCfg(props: { projectId: string; appCfg: AppConfig }) {
33
+ export async function writeAppCfgFile(props: {
34
+ projectId: string;
35
+ appCfg: AppConfig;
36
+ }) {
20
37
  const { projectId, appCfg } = props;
21
- if (!(await AgentConfigFile().isAppReady({ projectId }))) {
22
- throw new Error(`App ${projectId} is not ready!`);
38
+ if (!(await AgentConfigFile().isAppPresent({ projectId }))) {
39
+ throw new Error(`App ${projectId} is not present!`);
23
40
  }
24
41
  const appDir = getAppDir(projectId);
25
42
  const appJson = AppJsonFile(appDir);
26
- appJson.write(appCfg);
43
+ try {
44
+ appJson.write(appCfg);
45
+ } catch (err) {
46
+ throw new Error(
47
+ `Error writing app config for ${projectId}:\n${err}\n${appJson.getErrors()}`
48
+ );
49
+ }
27
50
  }
28
51
 
29
- export async function updateAppConfig(appDir: string, newAppCfg: AppConfig) {
30
- const existingAppCfg = await readAppCfg({ projectId: appDir });
52
+ export async function updateAppCfgFile(props: {
53
+ projectId: string;
54
+ newAppCfg: any;
55
+ }) {
56
+ const { projectId, newAppCfg } = props;
57
+ const existingAppCfg = await readAppCfgFile({ projectId });
31
58
  const appCfg = assign(existingAppCfg, merge(existingAppCfg, newAppCfg));
32
- await writeAppCfg({ projectId: appDir, appCfg });
59
+ await writeAppCfgFile({ projectId, appCfg });
60
+ }
61
+
62
+ export async function updateAppCfg(props: {
63
+ projectId: string;
64
+ appReleaseHash: string;
65
+ newAppCfg: AppConfig;
66
+ }) {
67
+ const { projectId, appReleaseHash, newAppCfg } = props;
68
+ logger.info(`Updating app config for ${projectId}:${appReleaseHash}.`);
69
+ const appDir = getAppDir(projectId);
70
+
71
+ if (await AgentConfigFile().isAppPresent({ projectId })) {
72
+ if (!(await AgentConfigFile().isAppReady({ projectId }))) {
73
+ throw new Error('Application already has installation in progress!');
74
+ }
75
+ logger.info('Application is already installed, updating');
76
+ await AgentConfigFile().setAppInstalling({
77
+ projectId,
78
+ version: appReleaseHash
79
+ });
80
+ } else {
81
+ throw new Error('Application is not installed!');
82
+ }
83
+
84
+ await writeAppCfgFile({ projectId, appCfg: newAppCfg });
85
+ await buildApp({ appDir });
86
+
87
+ await AgentConfigFile().setAppInstalled({
88
+ projectId,
89
+ version: appReleaseHash
90
+ });
91
+
92
+ await restartApp({ projectId });
93
+
94
+ logger.info(`Updated app config and rebuilt ${projectId}:${appReleaseHash}.`);
33
95
  }
34
96
 
35
97
  export async function readDockerCompose(props: { projectId: string }) {
@@ -1,6 +1,4 @@
1
1
  import { JsSpawner } from 'alwaysai/lib/util';
2
- import compose from 'docker-compose';
3
- import { parse, stringify } from 'yaml';
4
2
  import { AgentConfigFile } from '../infrastructure/agent-config';
5
3
  import { readDockerCompose, writeDockerCompose } from './config';
6
4
  import { getAppDir } from './utils';
@@ -56,7 +54,9 @@ export async function getAllEnvs(props: { projectId: string }) {
56
54
  if ('env_file' in service) {
57
55
  const envFiles: string[] = service['env_file'];
58
56
  for (const ef of envFiles) {
59
- envVars[s] = envVars[s].concat((await spawner.readFile(ef)).split('\n'));
57
+ envVars[s] = envVars[s].concat(
58
+ (await spawner.readFile(ef)).split('\n')
59
+ );
60
60
  }
61
61
  }
62
62
  if ('environment' in service) {
@@ -1,4 +1,9 @@
1
- import { readAppCfg, writeAppCfg, readDockerCompose, writeDockerCompose } from './config';
1
+ import {
2
+ readAppCfgFile,
3
+ updateAppCfg,
4
+ readDockerCompose,
5
+ writeDockerCompose
6
+ } from './config';
2
7
  import { installApp, uninstallApp } from './install';
3
8
  import { rollbackApp } from './backup';
4
9
  import {
@@ -8,14 +13,14 @@ import {
8
13
  startApp,
9
14
  getAppLogs,
10
15
  stopApp,
11
- restartApp,
16
+ restartApp
12
17
  } from './status';
13
18
  import { ModelDetails } from './types';
14
19
  import { getAllEnvs, setEnv } from './environment-variables';
15
20
 
16
21
  export {
17
- readAppCfg,
18
- writeAppCfg,
22
+ readAppCfgFile,
23
+ updateAppCfg,
19
24
  readDockerCompose,
20
25
  writeDockerCompose,
21
26
  installApp,
@@ -30,7 +35,7 @@ export {
30
35
  restartApp,
31
36
  ModelDetails,
32
37
  getAllEnvs,
33
- setEnv,
38
+ setEnv
34
39
  };
35
40
 
36
41
  // CLI-mode only
@@ -40,6 +45,14 @@ import {
40
45
  removeModel,
41
46
  replaceModels,
42
47
  updateModels,
48
+ updateModelsWithPresignedUrls
43
49
  } from './models';
44
50
 
45
- export { addModel, getAppModels, removeModel, replaceModels, updateModels };
51
+ export {
52
+ addModel,
53
+ getAppModels,
54
+ removeModel,
55
+ replaceModels,
56
+ updateModels,
57
+ updateModelsWithPresignedUrls
58
+ };
@@ -3,22 +3,28 @@ import * as fs from 'fs';
3
3
  import * as path from 'path';
4
4
  import { JsSpawner, Spawner } from 'alwaysai/lib/util';
5
5
  import { getAppDir, downloadPackageUsingPresignedUrl, buildApp } from './utils';
6
- import { AppDetailsPacket, ModelInstallPayload } from '@alwaysai/device-agent-schemas';
7
- import { BACKUP_EXT, createAppBackup } from './backup';
6
+ import { AppDetailsPacket } from '@alwaysai/device-agent-schemas';
7
+ import { BACKUP_EXT } from './backup';
8
8
  import { stopApp } from './status';
9
9
  import { AgentConfigFile } from '../infrastructure/agent-config';
10
10
  import { ProjectJsonFile } from 'alwaysai/lib/core/project';
11
11
  import {
12
12
  getTargetHardwareType,
13
13
  AppJsonFile,
14
- TargetJsonFile,
14
+ TargetJsonFile
15
15
  } from 'alwaysai/lib/core/app';
16
- import { appCleanDockerComponent, buildDocker } from 'alwaysai/lib/components/app';
16
+ import {
17
+ appCleanDockerComponent,
18
+ buildDocker
19
+ } from 'alwaysai/lib/components/app';
17
20
  import {
18
21
  appInstallComponent,
19
- installVenv,
22
+ installVenv
20
23
  } from 'alwaysai/lib/components/app/app-install-component';
21
- import { DOCKERFILE, DOCKER_IMAGE_ID_INITIAL_VALUE } from 'alwaysai/lib/constants';
24
+ import {
25
+ DOCKERFILE,
26
+ DOCKER_IMAGE_ID_INITIAL_VALUE
27
+ } from 'alwaysai/lib/constants';
22
28
  import { appReleasePullComponent } from 'alwaysai/lib/components/release';
23
29
  import { runInDir } from '../util/run-in-dir';
24
30
  import { installModelsWithPresignedURLs } from './models';
@@ -51,6 +57,7 @@ export async function installApp(props: {
51
57
  signedUrlsPayload?: SignedUrlPayloadType;
52
58
  }): Promise<void> {
53
59
  const { projectId, appReleaseHash, signedUrlsPayload } = props;
60
+ logger.info(`Installing ${projectId}:${appReleaseHash}`);
54
61
 
55
62
  const appDir = getAppDir(projectId);
56
63
  const spawner = JsSpawner();
@@ -59,10 +66,16 @@ export async function installApp(props: {
59
66
  throw new Error('Application already has installation in progress!');
60
67
  }
61
68
  logger.info('Application is already installed, updating');
62
- await AgentConfigFile().setAppInstalling({ projectId, version: appReleaseHash });
69
+ await AgentConfigFile().setAppInstalling({
70
+ projectId,
71
+ version: appReleaseHash
72
+ });
63
73
  await spawner.rimraf(appDir);
64
74
  } else {
65
- await AgentConfigFile().setAppInstalling({ projectId, version: appReleaseHash });
75
+ await AgentConfigFile().setAppInstalling({
76
+ projectId,
77
+ version: appReleaseHash
78
+ });
66
79
  }
67
80
 
68
81
  await spawner.mkdirp(appDir);
@@ -76,14 +89,17 @@ export async function installApp(props: {
76
89
  {
77
90
  yes: true,
78
91
  project: projectId,
79
- releaseHash: appReleaseHash,
80
- },
92
+ releaseHash: appReleaseHash
93
+ }
81
94
  ],
82
- appDir,
95
+ appDir
83
96
  );
84
97
  } else {
85
98
  const { appSignedUrl } = signedUrlsPayload.appInstallPayload;
86
- await downloadPackageUsingPresignedUrl({ localDest, presignedUrl: appSignedUrl });
99
+ await downloadPackageUsingPresignedUrl({
100
+ localDest,
101
+ presignedUrl: appSignedUrl
102
+ });
87
103
  }
88
104
 
89
105
  // Unpack app package and remove tar file
@@ -105,26 +121,27 @@ export async function installApp(props: {
105
121
  pull: true,
106
122
  clean: true,
107
123
  skipModels: false,
108
- source: false,
109
- },
124
+ source: false
125
+ }
110
126
  ],
111
- appDir,
127
+ appDir
112
128
  );
113
129
  } else {
114
130
  await installAppBuildReqs({ appDir });
115
131
  await installModelsWithPresignedURLs(
116
132
  signedUrlsPayload.modelsInstallPayload,
117
- path.join(appDir, 'models'),
133
+ path.join(appDir, 'models')
118
134
  );
119
135
  }
120
136
 
121
137
  await buildApp({ appDir });
122
138
 
123
- await AgentConfigFile().setAppInstalled({ projectId, version: appReleaseHash });
139
+ await AgentConfigFile().setAppInstalled({
140
+ projectId,
141
+ version: appReleaseHash
142
+ });
124
143
 
125
- logger.info(
126
- `Installed app version ${appReleaseHash} from project ${projectId} to ${appDir}.`,
127
- );
144
+ logger.info(`Completed installing ${projectId}:${appReleaseHash}`);
128
145
  }
129
146
 
130
147
  async function installAppBuildReqs(props: { appDir: string }) {
@@ -145,21 +162,25 @@ async function installAppBuildReqs(props: { appDir: string }) {
145
162
  targetJson,
146
163
  targetJsonFile,
147
164
  targetHostSpawner,
148
- pull: true,
165
+ pull: true
149
166
  });
150
167
 
151
168
  await installVenv({
152
169
  targetJson,
153
170
  sourceSpawner: targetHostSpawner,
154
- targetJsonFile,
171
+ targetJsonFile
155
172
  });
156
173
  },
157
174
  [],
158
- appDir,
175
+ appDir
159
176
  );
160
177
  }
161
178
 
162
- async function unPackApp(props: { spawner: Spawner; localDest: string; appDir: string }) {
179
+ async function unPackApp(props: {
180
+ spawner: Spawner;
181
+ localDest: string;
182
+ appDir: string;
183
+ }) {
163
184
  const { spawner, localDest, appDir } = props;
164
185
  await spawner.untar(fs.createReadStream(localDest), appDir);
165
186
  await spawner.rimraf(localDest);
@@ -182,12 +203,15 @@ async function checkValidProjectFiles({ appDir }) {
182
203
  TargetJsonFile(appDir).write({
183
204
  targetProtocol: 'docker:',
184
205
  dockerImageId: DOCKER_IMAGE_ID_INITIAL_VALUE,
185
- targetHardware: await getTargetHardwareType({}),
206
+ targetHardware: await getTargetHardwareType({})
186
207
  });
187
208
  }
188
209
 
189
- export async function uninstallApp(props: { projectId: string }): Promise<void> {
210
+ export async function uninstallApp(props: {
211
+ projectId: string;
212
+ }): Promise<void> {
190
213
  const { projectId } = props;
214
+ logger.info(`Uninstalling ${projectId}`);
191
215
  if (!(await AgentConfigFile().isAppPresent({ projectId }))) {
192
216
  logger.info(`Application ${projectId} not installed`);
193
217
  return;
@@ -195,12 +219,12 @@ export async function uninstallApp(props: { projectId: string }): Promise<void>
195
219
  try {
196
220
  await stopApp({ projectId });
197
221
  } catch {
198
- logger.info('Failed to stop app, may be left running...');
222
+ logger.warn(`Failed to stop ${projectId}, may be left running...`);
199
223
  }
200
224
  await AgentConfigFile().setAppUninstalled({ projectId });
201
225
  // Delete application directory and backup
202
226
  const appDir = getAppDir(projectId);
203
227
  rimraf.sync(appDir);
204
228
  rimraf.sync(`${appDir}${BACKUP_EXT}`);
205
- logger.info('Uninstalled', projectId);
229
+ logger.info(`Completed uninstalling ${projectId}`);
206
230
  }