@ossy/deployment-tools 0.0.40 → 0.0.42

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 (97) hide show
  1. package/package.json +6 -10
  2. package/src/{aws-credentials-client.ts → aws-credentials-client.js} +8 -9
  3. package/src/{caddy-client.ts → caddy-client.js} +13 -14
  4. package/src/{ci-rest-api.ts → ci-rest-api.js} +4 -5
  5. package/src/cli-commands/deploy-handler.js +32 -0
  6. package/src/cli-commands/{index.ts → index.js} +5 -5
  7. package/src/cli-commands/{start-handler.ts → start-handler.js} +5 -5
  8. package/src/cli-commands/{status-handler.ts → status-handler.js} +2 -2
  9. package/src/cli-commands/{stop-handler.ts → stop-handler.js} +1 -1
  10. package/src/{deployment-queue-client.ts → deployment-queue-client.js} +17 -18
  11. package/src/{docker-client.ts → docker-client.js} +18 -19
  12. package/src/index.js +3 -0
  13. package/src/{log/index.ts → log.js} +11 -11
  14. package/src/{index.cli.ts → platform-cli.js} +1 -1
  15. package/src/platform-client.js +95 -0
  16. package/src/platform-config.js +34 -0
  17. package/src/platform-template.js +63 -0
  18. package/src/types.js +65 -0
  19. package/bin/619.index.js +0 -452
  20. package/bin/619.index.js.map +0 -1
  21. package/bin/aws-credentials-client.d.ts +0 -4
  22. package/bin/caddy-client.d.ts +0 -13
  23. package/bin/ci-rest-api.d.ts +0 -4
  24. package/bin/cli-commands/deploy-handler.d.ts +0 -1
  25. package/bin/cli-commands/index.d.ts +0 -4
  26. package/bin/cli-commands/start-handler.d.ts +0 -1
  27. package/bin/cli-commands/status-handler.d.ts +0 -1
  28. package/bin/cli-commands/stop-handler.d.ts +0 -1
  29. package/bin/deployment-platform-client.d.ts +0 -11
  30. package/bin/deployment-queue-client.d.ts +0 -7
  31. package/bin/dist/aws-credentials-client.d.ts +0 -4
  32. package/bin/dist/caddy-client.d.ts +0 -13
  33. package/bin/dist/ci-rest-api.d.ts +0 -4
  34. package/bin/dist/cli-commands/deploy-handler.d.ts +0 -1
  35. package/bin/dist/cli-commands/index.d.ts +0 -4
  36. package/bin/dist/cli-commands/start-handler.d.ts +0 -1
  37. package/bin/dist/cli-commands/status-handler.d.ts +0 -1
  38. package/bin/dist/cli-commands/stop-handler.d.ts +0 -1
  39. package/bin/dist/deployment-platform-client.d.ts +0 -11
  40. package/bin/dist/deployment-queue-client.d.ts +0 -7
  41. package/bin/dist/docker-client.d.ts +0 -8
  42. package/bin/dist/index.cli.d.ts +0 -1
  43. package/bin/dist/index.d.ts +0 -2
  44. package/bin/dist/log/index.d.ts +0 -3
  45. package/bin/dist/types/index.d.ts +0 -65
  46. package/bin/docker-client.d.ts +0 -8
  47. package/bin/index.cli.d.ts +0 -1
  48. package/bin/index.d.ts +0 -2
  49. package/bin/index.js +0 -362
  50. package/bin/index.js.map +0 -1
  51. package/bin/licenses.txt +0 -23
  52. package/bin/log/index.d.ts +0 -3
  53. package/bin/package.json +0 -3
  54. package/bin/sourcemap-register.cjs +0 -1
  55. package/bin/types/index.d.ts +0 -65
  56. package/dist/619.index.js +0 -452
  57. package/dist/619.index.js.map +0 -1
  58. package/dist/aws-credentials-client.d.ts +0 -4
  59. package/dist/caddy-client.d.ts +0 -13
  60. package/dist/ci-rest-api.d.ts +0 -4
  61. package/dist/cli-commands/deploy-handler.d.ts +0 -1
  62. package/dist/cli-commands/index.d.ts +0 -4
  63. package/dist/cli-commands/start-handler.d.ts +0 -1
  64. package/dist/cli-commands/status-handler.d.ts +0 -1
  65. package/dist/cli-commands/stop-handler.d.ts +0 -1
  66. package/dist/deployment-platform-client.d.ts +0 -11
  67. package/dist/deployment-queue-client.d.ts +0 -7
  68. package/dist/dist/aws-credentials-client.d.ts +0 -4
  69. package/dist/dist/caddy-client.d.ts +0 -13
  70. package/dist/dist/ci-rest-api.d.ts +0 -4
  71. package/dist/dist/cli-commands/deploy-handler.d.ts +0 -1
  72. package/dist/dist/cli-commands/index.d.ts +0 -4
  73. package/dist/dist/cli-commands/start-handler.d.ts +0 -1
  74. package/dist/dist/cli-commands/status-handler.d.ts +0 -1
  75. package/dist/dist/cli-commands/stop-handler.d.ts +0 -1
  76. package/dist/dist/deployment-platform-client.d.ts +0 -11
  77. package/dist/dist/deployment-queue-client.d.ts +0 -7
  78. package/dist/dist/docker-client.d.ts +0 -8
  79. package/dist/dist/index.cli.d.ts +0 -2
  80. package/dist/dist/index.d.ts +0 -2
  81. package/dist/dist/log/index.d.ts +0 -3
  82. package/dist/dist/types/index.d.ts +0 -65
  83. package/dist/docker-client.d.ts +0 -8
  84. package/dist/index.cli.d.ts +0 -2
  85. package/dist/index.d.ts +0 -2
  86. package/dist/index.js +0 -361
  87. package/dist/index.js.map +0 -1
  88. package/dist/licenses.txt +0 -23
  89. package/dist/log/index.d.ts +0 -3
  90. package/dist/package.json +0 -3
  91. package/dist/sourcemap-register.cjs +0 -1
  92. package/dist/types/index.d.ts +0 -65
  93. package/src/cli-commands/deploy-handler.ts +0 -32
  94. package/src/deployment-platform-client.ts +0 -141
  95. package/src/index.ts +0 -2
  96. package/src/types/index.ts +0 -65
  97. package/tsconfig.json +0 -11
package/package.json CHANGED
@@ -1,31 +1,27 @@
1
1
  {
2
2
  "name": "@ossy/deployment-tools",
3
- "version": "0.0.40",
3
+ "version": "0.0.42",
4
4
  "description": "Collection of scripts and tools to aid deployment of containers and static files to Amazon Web Services through GitHub Actions",
5
5
  "type": "module",
6
- "types": "dist/index.d.ts" ,
7
6
  "main": "dist/index.js",
8
7
  "scripts": {
9
8
  "test": "echo \"Error: no test specified\" && exit 1",
10
- "build": "npm run build:public-exports && npm run build:cli",
11
- "build:public-exports": "npx --yes @vercel/ncc build src/index.ts --source-map --out dist --license licenses.txt --minify",
12
- "build:cli": "npx --yes @vercel/ncc build src/index.cli.ts --source-map --out bin --license licenses.txt --minify"
9
+ "build": "",
10
+ "build:docs": "jsdoc src/index.js"
13
11
  },
14
12
  "author": "Ossy",
15
13
  "license": "ISC",
16
- "bin": "bin/index.js",
14
+ "bin": "dist/platform-cli.js",
17
15
  "dependencies": {
18
16
  "@actions/core": "^1.10.0",
19
17
  "@aws-sdk/client-sqs": "^3.186.0",
20
18
  "@aws-sdk/client-sts": "^3.188.0",
21
- "@aws-sdk/types": "^3.193.0",
22
19
  "arg": "^5.0.2",
23
20
  "express": "^4.18.1",
24
21
  "nanoid": "^4.0.0",
25
- "node-fetch": "^3.2.6",
26
- "typescript": "^4.8.4"
22
+ "node-fetch": "^3.2.6"
27
23
  },
28
24
  "devDependencies": {
29
- "@types/node": "^18.11.0"
25
+ "jsdoc": "^3.6.11"
30
26
  }
31
27
  }
@@ -1,26 +1,25 @@
1
1
  import * as core from '@actions/core'
2
2
  import { STSClient, AssumeRoleWithWebIdentityCommand } from '@aws-sdk/client-sts'
3
- import { DeploymentPlatform } from 'types'
4
- import { logInfo, logError } from 'log'
3
+ import { logInfo, logError } from './log.js'
5
4
 
6
5
  export class AwsCredentialsClient {
7
6
 
8
- static resolveAwsCredentials({ awsAccountId, awsRegion, awsRoleToAssume }: DeploymentPlatform) {
7
+ static resolveAwsCredentials(platformConfig) {
9
8
  // If awsRoleToAssume is present, then we assume we run in a github workflow
10
9
  // If awsRoleToAssume is not present, then we assume they are resolved localy by aws-sdk
11
- if (!awsRoleToAssume) {
12
- logInfo({ message: `[AwsCredentialsClient] No aws role to assume was found, leaving auth logic to @aws-sdk package` })
10
+ if (!platformConfig.awsRoleToAssume) {
11
+ logInfo({ message: '[AwsCredentialsClient] No aws role to assume was found, leaving auth logic to @aws-sdk package' })
13
12
  return Promise.resolve(undefined)
14
13
  }
15
14
 
16
- const stsClient = new STSClient({ region: awsRegion })
15
+ const stsClient = new STSClient({ region: platformConfig.awsRegion })
17
16
 
18
17
  logInfo({ message: '[AwsCredentialsClient] Fetching GitHub ID token' })
19
18
  return core.getIDToken('sts.amazonaws.com')
20
19
  .then(webIdentityToken => {
21
- logInfo({ message: `[AwsCredentialsClient] Attempting to resolve aws credentials by assuming the role: ${awsRoleToAssume}` })
20
+ logInfo({ message: `[AwsCredentialsClient] Attempting to resolve aws credentials by assuming the role: ${platformConfig.awsRoleToAssume}` })
22
21
  return stsClient.send(new AssumeRoleWithWebIdentityCommand({
23
- RoleArn: `arn:aws:iam::${awsAccountId}:role/${awsRoleToAssume}`,
22
+ RoleArn: `arn:aws:iam::${platformConfig.awsAccountId}:role/${platformConfig.awsRoleToAssume}`,
24
23
  RoleSessionName: 'GitHubActions',
25
24
  DurationSeconds: 15 * 60,
26
25
  WebIdentityToken: webIdentityToken
@@ -33,7 +32,7 @@ export class AwsCredentialsClient {
33
32
  SecretAccessKey: responseData.Credentials.SecretAccessKey,
34
33
  accessKeyId: responseData.Credentials.AccessKeyId,
35
34
  sessionToken: responseData.Credentials.SessionToken,
36
- secretAccessKey: responseData.Credentials.SecretAccessKey,
35
+ secretAccessKey: responseData.Credentials.SecretAccessKey
37
36
  }))
38
37
  .catch(error => {
39
38
  logError({ message: '[AwsCredentialsClient] Could not resolve temporary credentials', error })
@@ -1,6 +1,5 @@
1
1
  import fetch from 'node-fetch'
2
- import { logInfo, logError } from './log'
3
- import { DeploymentPlatform, ContainerDeploymentRequest } from 'types'
2
+ import { logInfo, logError } from './log.js'
4
3
 
5
4
  export const Matchers = {
6
5
  host: host => ({ host: [host] }),
@@ -20,13 +19,13 @@ const Handlers = {
20
19
 
21
20
  export class CaddyClient {
22
21
 
23
- static deploy(deploymentPlatform: DeploymentPlatform, deploymentRequest: ContainerDeploymentRequest) {
22
+ static deploy(platformConfig, deploymentRequest) {
24
23
 
25
- const url = `${deploymentRequest.subdomain}.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`
24
+ const url = `${deploymentRequest.subdomain}.${platformConfig.activeEnvironment}.${platformConfig.domain}`
26
25
 
27
26
  logInfo({ message: `[CaddyClient] Updating caddy config to route ${url} to localhost:${deploymentRequest.hostPort}` })
28
27
 
29
- return fetch(`http://localhost:2019/config/apps/http/servers/${deploymentPlatform.ciServerName}/routes/0/handle`, {
28
+ return fetch(`http://localhost:2019/config/apps/http/servers/${platformConfig.ciServerName}/routes/0/handle`, {
30
29
  method: 'POST',
31
30
  headers: { 'Content-Type': 'application/json' },
32
31
  body: JSON.stringify(Handlers.subroute([
@@ -36,11 +35,11 @@ export class CaddyClient {
36
35
  }
37
36
  ]))
38
37
  })
39
- .catch(error => logError({ message: `[CaddyClient] Could not update caddy config to include ${url}`, error }))
38
+ .catch(error => logError({ message: `[CaddyClient] Could not update caddy config to include ${url}`, error }))
40
39
  }
41
40
 
42
- static applyDefaultServerConfig(deploymentPlatform: DeploymentPlatform) {
43
- logInfo({ message: '[CaddyClient] Applying default caddy config' })
41
+ static applyDefaultServerConfig(platformConfig) {
42
+ logInfo({ message: '[CaddyClient] Applying default caddy config' })
44
43
  return fetch('http://localhost:2019/load', {
45
44
  method: 'POST',
46
45
  headers: { 'Content-Type': 'application/json' },
@@ -48,15 +47,15 @@ export class CaddyClient {
48
47
  apps: {
49
48
  http: {
50
49
  servers: {
51
- [deploymentPlatform.ciServerName]: {
50
+ [platformConfig.ciServerName]: {
52
51
  listen: [':80', ':443'],
53
52
  routes: [
54
53
  {
55
- match: [Matchers.host(`*.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`)],
54
+ match: [Matchers.host(`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
56
55
  handle: [Handlers.subroute([
57
56
  {
58
- match: [Matchers.host(`${deploymentPlatform.ciSubDomain}.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`)],
59
- handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(deploymentPlatform.ciInternalServerPort)]}])]
57
+ match: [Matchers.host(`${platformConfig.ciSubDomain}.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
58
+ handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(platformConfig.ciInternalServerPort)]}])]
60
59
  }
61
60
  ])],
62
61
  terminal: true
@@ -69,7 +68,7 @@ export class CaddyClient {
69
68
  automation: {
70
69
  policies: [
71
70
  {
72
- subjects:['*.qa.ossy.com'],
71
+ subjects:[`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`],
73
72
  issuers:[
74
73
  {
75
74
  challenges:{
@@ -102,7 +101,7 @@ export class CaddyClient {
102
101
  }
103
102
  })
104
103
  })
105
- .catch(error => logError({ message: '[CaddyClient] Could not apply default caddy config', error }))
104
+ .catch(error => logError({ message: '[CaddyClient] Could not apply default caddy config', error }))
106
105
  }
107
106
 
108
107
  }
@@ -1,10 +1,9 @@
1
1
  import express from 'express'
2
- import { DeploymentPlatform } from 'types'
3
- import { logInfo } from './log'
2
+ import { logInfo } from './log.js'
4
3
 
5
4
  export class CiRestApi {
6
5
 
7
- static start(deploymentPlatform: DeploymentPlatform) {
6
+ static start(platformConfig) {
8
7
  const server = express()
9
8
 
10
9
  server.use(express.json())
@@ -17,8 +16,8 @@ export class CiRestApi {
17
16
  res.send('Server is live')
18
17
  })
19
18
 
20
- server.listen(deploymentPlatform.ciInternalServerPort, () => {
21
- logInfo({ message: `[ContainerManagerServer] API is live on port ${deploymentPlatform.ciInternalServerPort}`})
19
+ server.listen(platformConfig.ciInternalServerPort, () => {
20
+ logInfo({ message: `[ContainerManagerServer] API is live on port ${platformConfig.ciInternalServerPort}`})
22
21
  })
23
22
  }
24
23
 
@@ -0,0 +1,32 @@
1
+ import arg from 'arg'
2
+ import { logInfo } from '../log.js'
3
+ import { PlatformClient } from '../platform-client.js'
4
+
5
+ export const deployHandler = cliArgs => {
6
+ logInfo({ message: 'Running deploy command' })
7
+
8
+ const parsedArgs = arg({
9
+ '--username': String,
10
+ '-u': '--username',
11
+
12
+ '--authentication': String,
13
+ '--a': '--authentication',
14
+
15
+ '--target-env': String,
16
+ '-t': '--target-env',
17
+
18
+ '--ossyfile': String,
19
+ '-o': '--ossyfile',
20
+
21
+ '--platforms': String,
22
+ '-p': '--platforms'
23
+ }, { argv: cliArgs })
24
+
25
+ PlatformClient.deploy({
26
+ username: parsedArgs['--username'],
27
+ authentication: parsedArgs['--authentication'],
28
+ targetEnvironment: parsedArgs['--target-env'],
29
+ pathToPlatformTemplates: parsedArgs['--platforms'],
30
+ pathToOssyFile: parsedArgs['--ossyfile']
31
+ })
32
+ }
@@ -1,8 +1,8 @@
1
- import { logError } from '../log'
2
- import { startHandler } from './start-handler'
3
- import { stopHandler } from './stop-handler'
4
- import { statusHandler } from './status-handler'
5
- import { deployHandler } from './deploy-handler'
1
+ import { logError } from '../log.js'
2
+ import { startHandler } from './start-handler.js'
3
+ import { stopHandler } from './stop-handler.js'
4
+ import { statusHandler } from './status-handler.js'
5
+ import { deployHandler } from './deploy-handler.js'
6
6
 
7
7
  export const runCliCommand = ({ name, args }) => {
8
8
 
@@ -1,10 +1,10 @@
1
1
  import arg from 'arg'
2
2
  import { platform } from 'os'
3
- import { logInfo, logError } from '../log'
4
- import { DeploymentPlatformClient } from 'deployment-platform-client'
3
+ import { logInfo, logError } from '../log.js'
4
+ import { PlatformClient } from '../platform-client.js'
5
5
 
6
6
  export const startHandler = cliArgs => {
7
- logInfo({ message: 'Running start command' })
7
+ logInfo({ message: 'Running start command' })
8
8
 
9
9
  const Platforms = {
10
10
  windows: 'win32',
@@ -12,7 +12,7 @@ export const startHandler = cliArgs => {
12
12
  }
13
13
 
14
14
  if ([Platforms.windows].includes(platform())) {
15
- return logError({ message: 'Deployment tools do not support this os' })
15
+ return logError({ message: 'Deployment tools do not support this os' })
16
16
  }
17
17
 
18
18
  const parsedArgs = arg({
@@ -20,5 +20,5 @@ export const startHandler = cliArgs => {
20
20
  '-p': '--platforms'
21
21
  }, { argv: cliArgs })
22
22
 
23
- DeploymentPlatformClient.start(parsedArgs['--platforms'])
23
+ PlatformClient.start(parsedArgs['--platforms'])
24
24
  }
@@ -1,7 +1,7 @@
1
1
  import { exec } from 'child_process'
2
- import { logInfo } from '../log'
2
+ import { logInfo } from '../log.js'
3
3
 
4
4
  export const statusHandler = () => {
5
- logInfo({ message: 'Running status command' })
5
+ logInfo({ message: 'Running status command' })
6
6
  exec('systemctl status deployment-tools.service')
7
7
  }
@@ -1,5 +1,5 @@
1
1
  import { exec } from 'child_process'
2
- import { logInfo } from '../log'
2
+ import { logInfo } from '../log.js'
3
3
 
4
4
  export const stopHandler = () => {
5
5
  logInfo({ message: 'Running stop command' })
@@ -4,19 +4,18 @@ import {
4
4
  DeleteMessageCommand,
5
5
  ReceiveMessageCommand
6
6
  } from '@aws-sdk/client-sqs'
7
- import { DeploymentPlatform, ContainerDeploymentRequest } from 'types'
8
- import { AwsCredentialsClient } from 'aws-credentials-client'
9
- import { logInfo, logError, logDebug } from 'log'
7
+ import { AwsCredentialsClient } from './aws-credentials-client.js'
8
+ import { logInfo, logError, logDebug } from './log.js'
10
9
 
11
10
  export class DeploymentQueueClient {
12
11
 
13
- static sendDeploymentRequest(deploymentPlatform: DeploymentPlatform, deploymentRequest: ContainerDeploymentRequest) {
12
+ static sendDeploymentRequest(platformConfig, deploymentRequest) {
14
13
  logInfo({ message: '[DeploymentQueueClient] Starting deployment sequence' })
15
- return DeploymentQueueClient.createAwsSqsClient(deploymentPlatform)
14
+ return DeploymentQueueClient.createAwsSqsClient(platformConfig)
16
15
  .then(sqsClient => {
17
16
 
18
17
  const sendMessageParams = {
19
- QueueUrl: deploymentPlatform.awsDeploymentSqsArn,
18
+ QueueUrl: platformConfig.awsDeploymentSqsArn,
20
19
  MessageBody: JSON.stringify(deploymentRequest)
21
20
  }
22
21
 
@@ -30,44 +29,44 @@ export class DeploymentQueueClient {
30
29
 
31
30
  }
32
31
 
33
- static pollForDeploymentRequests(deploymentPlatform: DeploymentPlatform, handleDeploymentRequest: (deploymentRequest: ContainerDeploymentRequest) => Promise<void>) {
32
+ static pollForDeploymentRequests(platformConfig, handleDeploymentRequest) {
34
33
  logInfo({ message: '[DeploymentQueueClient] Starting polling for deployment requests' })
35
- DeploymentQueueClient.createAwsSqsClient(deploymentPlatform).then(sqsClient => {
34
+ DeploymentQueueClient.createAwsSqsClient(platformConfig).then(sqsClient => {
36
35
  const FIVE_MINUTES = 3000
37
36
 
38
37
  setInterval(() => {
39
38
 
40
- const receiveMessageCommand = new ReceiveMessageCommand({ QueueUrl: deploymentPlatform.awsDeploymentSqsArn })
39
+ const receiveMessageCommand = new ReceiveMessageCommand({ QueueUrl: platformConfig.awsDeploymentSqsArn })
41
40
 
42
41
  sqsClient.send(receiveMessageCommand)
43
42
  .then(data => data.Messages.map(message => {
44
43
 
45
- logInfo({ message: '[DeploymentQueueClient] Received deployment request' })
44
+ logInfo({ message: '[DeploymentQueueClient] Received deployment request' })
46
45
 
47
46
  handleDeploymentRequest(JSON.parse(message.Body))
48
47
  .then(() => {
49
48
 
50
49
  const deleteMessageCommand = new DeleteMessageCommand({
51
- QueueUrl: deploymentPlatform.awsDeploymentSqsArn,
50
+ QueueUrl: platformConfig.awsDeploymentSqsArn,
52
51
  ReceiptHandle: message.ReceiptHandle
53
52
  })
54
53
 
55
54
  sqsClient.send(deleteMessageCommand)
56
- .then(() => logInfo({ message: '[DeploymentQueueClient] Removing deployment request from queue' }))
57
- .catch(error => logError({ message: '[DeploymentQueueClient] Could not delete message from queue', error }))
55
+ .then(() => logInfo({ message: '[DeploymentQueueClient] Removing deployment request from queue' }))
56
+ .catch(error => logError({ message: '[DeploymentQueueClient] Could not delete message from queue', error }))
58
57
  })
59
58
 
60
59
  }))
61
- .catch(error => logError({ message: '[ContainerManagerServer] Could not handle incoming deployment request', error }))
60
+ .catch(error => logError({ message: '[ContainerManagerServer] Could not handle incoming deployment request', error }))
62
61
  }, FIVE_MINUTES)
63
62
 
64
- })
63
+ })
65
64
  }
66
65
 
67
- static createAwsSqsClient(deploymentPlatform: DeploymentPlatform) {
68
- return AwsCredentialsClient.resolveAwsCredentials(deploymentPlatform)
66
+ static createAwsSqsClient(platformConfig) {
67
+ return AwsCredentialsClient.resolveAwsCredentials(platformConfig)
69
68
  .then(awsCredentials => new SQSClient({
70
- region: deploymentPlatform.awsRegion,
69
+ region: platformConfig.awsRegion,
71
70
  credentials: awsCredentials
72
71
  }))
73
72
  }
@@ -3,53 +3,52 @@ import fs from 'fs'
3
3
  import { nanoid } from 'nanoid'
4
4
  import path from 'path'
5
5
  import { fileURLToPath } from 'url'
6
- import { logInfo, logError } from './log'
7
- import { DeploymentPlatform, ContainerDeploymentRequest } from 'types'
6
+ import { logInfo, logError } from './log.js'
8
7
 
9
8
  const __filename = fileURLToPath(import.meta.url)
10
9
  const __dirname = path.dirname(__filename)
11
10
 
12
11
  export class DockerClient {
13
12
 
14
- static createDockerNetworkForContainerManagerServer(deploymentPlatform: DeploymentPlatform) {
15
- logInfo({ message: '[DockerClient] Creating docker network for comunication between containers' })
16
- exec(`sudo docker network create ${deploymentPlatform.ciDockerNetworkName}`)
13
+ static createDockerNetworkForContainerManagerServer(platformConfig) {
14
+ logInfo({ message: '[DockerClient] Creating docker network for comunication between containers' })
15
+ exec(`sudo docker network create ${platformConfig.ciDockerNetworkName}`)
17
16
  }
18
17
 
19
- static stopContainer(deploymentRequest: ContainerDeploymentRequest) {
18
+ static stopContainer(deploymentRequest) {
20
19
  const name = deploymentRequest.image.replaceAll('/', '_')
21
- logInfo({ message: `Running docker stop for image with the name of ${name}` })
20
+ logInfo({ message: `Running docker stop for image with the name of ${name}` })
22
21
  return `sudo docker stop ${name} ||`
23
22
  }
24
23
 
25
- static startContainer(deploymentPlatform, { image, containerPort, hostPort, registry, env }: ContainerDeploymentRequest) {
24
+ static startContainer(platformConfig, { image, containerPort, hostPort, registry, env }) {
26
25
  const name = image.replaceAll('/', '_')
27
26
  const imageUrl = !!registry ? `${registry}/${image}` : image
28
27
  const envsAsString = Object.entries(env || {}).reduce((envs, [name, value]) => `${envs} --env ${name}=${value}`, '')
29
- logInfo({ message: `Running docker start for image with the name of ${name} with port mapping ${hostPort}:${containerPort} and source ${imageUrl}` })
30
- return `sudo docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${deploymentPlatform.ciDockerNetworkName} --network-alias=${name} --rm ${envsAsString} ${imageUrl}`
28
+ logInfo({ message: `Running docker start for image with the name of ${name} with port mapping ${hostPort}:${containerPort} and source ${imageUrl}` })
29
+ return `sudo docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${platformConfig.ciDockerNetworkName} --network-alias=${name} --rm ${envsAsString} ${imageUrl}`
31
30
  }
32
31
 
33
- static resolveCredentials({ registry, username, authentication }: ContainerDeploymentRequest) {
32
+ static resolveCredentials({ registry, username, authentication }) {
34
33
  const shouldAuthenticate = username || authentication
35
34
 
36
35
  if (!shouldAuthenticate) {
37
- logInfo({ message: 'No docker credentials provided, assuming image is publicly hosted' })
36
+ logInfo({ message: 'No docker credentials provided, assuming image is publicly hosted' })
38
37
  return ''
39
38
  }
40
39
 
41
- logInfo({ message: `Resolving docker credentials for ${registry}` })
40
+ logInfo({ message: `Resolving docker credentials for ${registry}` })
42
41
  return `sudo docker login ${registry} -u ${username} -p ${authentication}`
43
42
  }
44
43
 
45
- static deploy(deploymentPlatform: DeploymentPlatform, deploymentRequest: ContainerDeploymentRequest): Promise<void> {
44
+ static deploy(platformConfig, deploymentRequest) {
46
45
  return new Promise(resolve => {
47
- logInfo({ message: 'Starting docker deployment sequence' })
46
+ logInfo({ message: 'Starting docker deployment sequence' })
48
47
 
49
48
  const dockerCommandScript = `'#!/bin/bash'
50
49
  ${DockerClient.stopContainer(deploymentRequest)}
51
50
  ${DockerClient.resolveCredentials(deploymentRequest)}
52
- ${DockerClient.startContainer(deploymentPlatform, deploymentRequest)}`
51
+ ${DockerClient.startContainer(platformConfig, deploymentRequest)}`
53
52
 
54
53
  const deploymentId = nanoid()
55
54
 
@@ -61,15 +60,15 @@ ${DockerClient.startContainer(deploymentPlatform, deploymentRequest)}`
61
60
  const command = exec(`bash ${FilePaths.DeploymentScript}`)
62
61
 
63
62
  command.stdout.on('data', data => {
64
- logInfo({ message: `[DockerClient]: ${data}` })
63
+ logInfo({ message: `[DockerClient]: ${data}` })
65
64
  })
66
65
 
67
66
  command.stderr.on('data', (data) => {
68
- logError({ message: `[DockerClient]: ${data}` })
67
+ logError({ message: `[DockerClient]: ${data}` })
69
68
  })
70
69
 
71
70
  command.on('close', code => {
72
- logInfo({ message: `[DockerClient] command exited with code ${code}` })
71
+ logInfo({ message: `[DockerClient] command exited with code ${code}` })
73
72
  fs.unlinkSync(FilePaths.DeploymentScript)
74
73
  resolve()
75
74
  })
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { PlatformClient } from './platform-client.js'
2
+ export { PlatformTemplateService } from './platform-template.js'
3
+ export { PlatformConfigService } from './platform-config.js'
@@ -1,18 +1,18 @@
1
- interface LogInput {
2
- type: TypeOfMessage;
3
- message: string;
4
- data?: any;
5
- dataPrefix: string;
6
- }
1
+ // interface LogInput {
2
+ // type: TypeOfMessage;
3
+ // message: string;
4
+ // data?: any;
5
+ // dataPrefix: string;
6
+ // }
7
7
 
8
- enum TypeOfMessage {
9
- Info = 'INFO',
10
- Error = 'ERROR',
11
- Debug = 'DEBUG'
8
+ const TypeOfMessage = {
9
+ Info: 'INFO',
10
+ Error: 'ERROR',
11
+ Debug: 'DEBUG'
12
12
  }
13
13
 
14
14
  const log = (...params) => { console.log(...params) }
15
- const prefixTo = (prefix: string, message: string) => `[${prefix}]${message.startsWith('[') ? '' : ': '}`
15
+ const prefixTo = (prefix, message) => `[${prefix}]${message.startsWith('[') ? '' : ': '}`
16
16
 
17
17
  export const logInfo = logInput => {
18
18
  const messagePrefix = prefixTo(TypeOfMessage.Info, logInput.message)
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { runCliCommand } from './cli-commands/index'
2
+ import { runCliCommand } from './cli-commands/index.js'
3
3
 
4
4
  //eslint-disable-next-line no-unused-vars
5
5
  const [_, __, command, ...restArgs] = process.argv
@@ -0,0 +1,95 @@
1
+ import { resolve } from 'path'
2
+ import { readFileSync } from 'fs'
3
+ import { SupportedDeploymentTypes } from './types.js'
4
+ import { PlatformTemplateService } from './platform-template.js'
5
+ import { PlatformConfigService } from './platform-config.js'
6
+ import { CaddyClient } from './caddy-client.js'
7
+ import { DockerClient } from './docker-client.js'
8
+ import { DeploymentQueueClient } from './deployment-queue-client.js'
9
+ import { CiRestApi } from './ci-rest-api.js'
10
+ import { logError } from './log.js'
11
+
12
+ // journalctl -u service-name.service
13
+ export class PlatformClient {
14
+
15
+ static start(platformTemplatesFilePath) {
16
+ PlatformTemplateService.readFromFile(platformTemplatesFilePath).then(([firstPlatformTemplateFound]) => {
17
+ const platformConfig = PlatformConfigService.from(firstPlatformTemplateFound)
18
+
19
+ CiRestApi.start(platformConfig)
20
+ CaddyClient.applyDefaultServerConfig(platformConfig)
21
+
22
+ DeploymentQueueClient.pollForDeploymentRequests(
23
+ platformConfig,
24
+ deploymentRequest => {
25
+ DockerClient.deploy(platformConfig, deploymentRequest)
26
+ CaddyClient.deploy(platformConfig, deploymentRequest)
27
+ return Promise.resolve()
28
+ }
29
+ )
30
+
31
+ })
32
+ .catch(error => logError({ message: '[PlatformClient] Could not start the deployment platform', error }))
33
+ }
34
+
35
+ //eslint-disable-next-line max-params
36
+ static deploy({
37
+ username,
38
+ authentication,
39
+ targetEnvironment,
40
+ pathToPlatformTemplates,
41
+ pathToOssyFile
42
+ }) {
43
+
44
+ const platformConfigRequest = PlatformTemplateService.readFromFile(pathToPlatformTemplates)
45
+ .then(templates => templates.map(x => ({ ...x, activeEnvironment: targetEnvironment })))
46
+ .then(templates => templates.map(x => PlatformConfigService.from(x)))
47
+
48
+ const deploymentTemplatesRequest = PlatformClient.getDeploymentTemplates(pathToOssyFile)
49
+
50
+ return Promise.all([platformConfigRequest, deploymentTemplatesRequest])
51
+ .then(([platformConfigs, deploymentTemplates]) => {
52
+ deploymentTemplates.map(deploymentTemplate => {
53
+
54
+ const platformConfig = platformConfigs.find(config => config.platformName === deploymentTemplate.targetDeploymentPlatform)
55
+
56
+ if (!platformConfig) {
57
+ logError({ message: `[PlatformClient] Could not find a deployment platform with the name ${deploymentTemplate.targetDeploymentPlatform}` })
58
+ return Promise.reject()
59
+ }
60
+
61
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
62
+
63
+ if (deploymentTemplate.type !== SupportedDeploymentTypes.Container) {
64
+ logError({ message: `[PlatformClient] Unsupported deployment type of ${deploymentTemplate.type}` })
65
+ return Promise.reject()
66
+ }
67
+
68
+ const deploymentRequest = {
69
+ ...deploymentTemplate,
70
+ env: PlatformClient.getEnvironmentVariables(targetEnvironment, deploymentTemplate),
71
+ username: username,
72
+ authentication: authentication
73
+ }
74
+
75
+ return DeploymentQueueClient.sendDeploymentRequest(platformConfig, deploymentRequest)
76
+
77
+ })
78
+ })
79
+ .catch(error => logError({ message: '[PlatformClient] Could not send deployment request', error }))
80
+ }
81
+
82
+ static getDeploymentTemplates(pathToOssyFile) {
83
+ const ossyfile = JSON.parse(readFileSync(resolve(pathToOssyFile), 'utf8'))
84
+ return Promise.resolve(ossyfile.deployments || [])
85
+ }
86
+
87
+ static getEnvironmentVariables(targetEnvironment, deploymentRequest) {
88
+ const envs = deploymentRequest.env || {}
89
+ return {
90
+ ...(envs.shared || {}),
91
+ ...(envs[targetEnvironment] || {})
92
+ }
93
+ }
94
+
95
+ }
@@ -0,0 +1,34 @@
1
+ import {
2
+ SupportedEnvironments,
3
+ SupportedRegions
4
+ } from './types.js'
5
+
6
+ export class PlatformConfigService {
7
+
8
+ static from(template) {
9
+
10
+ const withDefaults = {
11
+ platformName: SupportedEnvironments.LOCAL,
12
+ domain: 'localhost',
13
+ activeEnvironment: SupportedEnvironments.LOCAL,
14
+ supportedDeploymentTypes: ['CONTAINER'],
15
+ ciSubDomain: 'ci',
16
+ ciInternalServerPort: 3000,
17
+ ciServerName: 'ci-client',
18
+ ciDockerNetworkName: 'deployment-tools',
19
+ awsRegion: SupportedRegions.North,
20
+ ...template
21
+ }
22
+
23
+ const awsDeploymentSqsArn = `https://sqs.${withDefaults.awsRegion}.amazonaws.com/${withDefaults.awsAccountId}/${withDefaults.platformName}-${withDefaults.activeEnvironment}`
24
+ const awsRoleToAssume = process.env.CI ? 'github-ci-role' : undefined
25
+
26
+ return {
27
+ ...withDefaults,
28
+ awsDeploymentSqsArn,
29
+ awsRoleToAssume
30
+ }
31
+
32
+ }
33
+
34
+ }