@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.
- package/package.json +6 -10
- package/src/{aws-credentials-client.ts → aws-credentials-client.js} +8 -9
- package/src/{caddy-client.ts → caddy-client.js} +13 -14
- package/src/{ci-rest-api.ts → ci-rest-api.js} +4 -5
- package/src/cli-commands/deploy-handler.js +32 -0
- package/src/cli-commands/{index.ts → index.js} +5 -5
- package/src/cli-commands/{start-handler.ts → start-handler.js} +5 -5
- package/src/cli-commands/{status-handler.ts → status-handler.js} +2 -2
- package/src/cli-commands/{stop-handler.ts → stop-handler.js} +1 -1
- package/src/{deployment-queue-client.ts → deployment-queue-client.js} +17 -18
- package/src/{docker-client.ts → docker-client.js} +18 -19
- package/src/index.js +3 -0
- package/src/{log/index.ts → log.js} +11 -11
- package/src/{index.cli.ts → platform-cli.js} +1 -1
- package/src/platform-client.js +95 -0
- package/src/platform-config.js +34 -0
- package/src/platform-template.js +63 -0
- package/src/types.js +65 -0
- package/bin/619.index.js +0 -452
- package/bin/619.index.js.map +0 -1
- package/bin/aws-credentials-client.d.ts +0 -4
- package/bin/caddy-client.d.ts +0 -13
- package/bin/ci-rest-api.d.ts +0 -4
- package/bin/cli-commands/deploy-handler.d.ts +0 -1
- package/bin/cli-commands/index.d.ts +0 -4
- package/bin/cli-commands/start-handler.d.ts +0 -1
- package/bin/cli-commands/status-handler.d.ts +0 -1
- package/bin/cli-commands/stop-handler.d.ts +0 -1
- package/bin/deployment-platform-client.d.ts +0 -11
- package/bin/deployment-queue-client.d.ts +0 -7
- package/bin/dist/aws-credentials-client.d.ts +0 -4
- package/bin/dist/caddy-client.d.ts +0 -13
- package/bin/dist/ci-rest-api.d.ts +0 -4
- package/bin/dist/cli-commands/deploy-handler.d.ts +0 -1
- package/bin/dist/cli-commands/index.d.ts +0 -4
- package/bin/dist/cli-commands/start-handler.d.ts +0 -1
- package/bin/dist/cli-commands/status-handler.d.ts +0 -1
- package/bin/dist/cli-commands/stop-handler.d.ts +0 -1
- package/bin/dist/deployment-platform-client.d.ts +0 -11
- package/bin/dist/deployment-queue-client.d.ts +0 -7
- package/bin/dist/docker-client.d.ts +0 -8
- package/bin/dist/index.cli.d.ts +0 -1
- package/bin/dist/index.d.ts +0 -2
- package/bin/dist/log/index.d.ts +0 -3
- package/bin/dist/types/index.d.ts +0 -65
- package/bin/docker-client.d.ts +0 -8
- package/bin/index.cli.d.ts +0 -1
- package/bin/index.d.ts +0 -2
- package/bin/index.js +0 -362
- package/bin/index.js.map +0 -1
- package/bin/licenses.txt +0 -23
- package/bin/log/index.d.ts +0 -3
- package/bin/package.json +0 -3
- package/bin/sourcemap-register.cjs +0 -1
- package/bin/types/index.d.ts +0 -65
- package/dist/619.index.js +0 -452
- package/dist/619.index.js.map +0 -1
- package/dist/aws-credentials-client.d.ts +0 -4
- package/dist/caddy-client.d.ts +0 -13
- package/dist/ci-rest-api.d.ts +0 -4
- package/dist/cli-commands/deploy-handler.d.ts +0 -1
- package/dist/cli-commands/index.d.ts +0 -4
- package/dist/cli-commands/start-handler.d.ts +0 -1
- package/dist/cli-commands/status-handler.d.ts +0 -1
- package/dist/cli-commands/stop-handler.d.ts +0 -1
- package/dist/deployment-platform-client.d.ts +0 -11
- package/dist/deployment-queue-client.d.ts +0 -7
- package/dist/dist/aws-credentials-client.d.ts +0 -4
- package/dist/dist/caddy-client.d.ts +0 -13
- package/dist/dist/ci-rest-api.d.ts +0 -4
- package/dist/dist/cli-commands/deploy-handler.d.ts +0 -1
- package/dist/dist/cli-commands/index.d.ts +0 -4
- package/dist/dist/cli-commands/start-handler.d.ts +0 -1
- package/dist/dist/cli-commands/status-handler.d.ts +0 -1
- package/dist/dist/cli-commands/stop-handler.d.ts +0 -1
- package/dist/dist/deployment-platform-client.d.ts +0 -11
- package/dist/dist/deployment-queue-client.d.ts +0 -7
- package/dist/dist/docker-client.d.ts +0 -8
- package/dist/dist/index.cli.d.ts +0 -2
- package/dist/dist/index.d.ts +0 -2
- package/dist/dist/log/index.d.ts +0 -3
- package/dist/dist/types/index.d.ts +0 -65
- package/dist/docker-client.d.ts +0 -8
- package/dist/index.cli.d.ts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -361
- package/dist/index.js.map +0 -1
- package/dist/licenses.txt +0 -23
- package/dist/log/index.d.ts +0 -3
- package/dist/package.json +0 -3
- package/dist/sourcemap-register.cjs +0 -1
- package/dist/types/index.d.ts +0 -65
- package/src/cli-commands/deploy-handler.ts +0 -32
- package/src/deployment-platform-client.ts +0 -141
- package/src/index.ts +0 -2
- package/src/types/index.ts +0 -65
- 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.
|
|
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": "
|
|
11
|
-
"build:
|
|
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": "
|
|
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
|
-
"
|
|
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 {
|
|
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(
|
|
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:
|
|
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(
|
|
22
|
+
static deploy(platformConfig, deploymentRequest) {
|
|
24
23
|
|
|
25
|
-
const url = `${deploymentRequest.subdomain}.${
|
|
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/${
|
|
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
|
-
|
|
38
|
+
.catch(error => logError({ message: `[CaddyClient] Could not update caddy config to include ${url}`, error }))
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
static applyDefaultServerConfig(
|
|
43
|
-
logInfo({
|
|
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
|
-
[
|
|
50
|
+
[platformConfig.ciServerName]: {
|
|
52
51
|
listen: [':80', ':443'],
|
|
53
52
|
routes: [
|
|
54
53
|
{
|
|
55
|
-
match: [Matchers.host(`*.${
|
|
54
|
+
match: [Matchers.host(`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
|
|
56
55
|
handle: [Handlers.subroute([
|
|
57
56
|
{
|
|
58
|
-
match: [Matchers.host(`${
|
|
59
|
-
handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(
|
|
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:[
|
|
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({
|
|
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 {
|
|
3
|
-
import { logInfo } from './log'
|
|
2
|
+
import { logInfo } from './log.js'
|
|
4
3
|
|
|
5
4
|
export class CiRestApi {
|
|
6
5
|
|
|
7
|
-
static start(
|
|
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(
|
|
21
|
-
logInfo({ message: `[ContainerManagerServer] API is live on port ${
|
|
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 {
|
|
3
|
+
import { logInfo, logError } from '../log.js'
|
|
4
|
+
import { PlatformClient } from '../platform-client.js'
|
|
5
5
|
|
|
6
6
|
export const startHandler = cliArgs => {
|
|
7
|
-
logInfo({
|
|
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({
|
|
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
|
-
|
|
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({
|
|
5
|
+
logInfo({ message: 'Running status command' })
|
|
6
6
|
exec('systemctl status deployment-tools.service')
|
|
7
7
|
}
|
|
@@ -4,19 +4,18 @@ import {
|
|
|
4
4
|
DeleteMessageCommand,
|
|
5
5
|
ReceiveMessageCommand
|
|
6
6
|
} from '@aws-sdk/client-sqs'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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(
|
|
12
|
+
static sendDeploymentRequest(platformConfig, deploymentRequest) {
|
|
14
13
|
logInfo({ message: '[DeploymentQueueClient] Starting deployment sequence' })
|
|
15
|
-
return DeploymentQueueClient.createAwsSqsClient(
|
|
14
|
+
return DeploymentQueueClient.createAwsSqsClient(platformConfig)
|
|
16
15
|
.then(sqsClient => {
|
|
17
16
|
|
|
18
17
|
const sendMessageParams = {
|
|
19
|
-
QueueUrl:
|
|
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(
|
|
32
|
+
static pollForDeploymentRequests(platformConfig, handleDeploymentRequest) {
|
|
34
33
|
logInfo({ message: '[DeploymentQueueClient] Starting polling for deployment requests' })
|
|
35
|
-
DeploymentQueueClient.createAwsSqsClient(
|
|
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:
|
|
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({
|
|
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:
|
|
50
|
+
QueueUrl: platformConfig.awsDeploymentSqsArn,
|
|
52
51
|
ReceiptHandle: message.ReceiptHandle
|
|
53
52
|
})
|
|
54
53
|
|
|
55
54
|
sqsClient.send(deleteMessageCommand)
|
|
56
|
-
.then(() => logInfo({
|
|
57
|
-
.catch(error => logError({
|
|
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({
|
|
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(
|
|
68
|
-
return AwsCredentialsClient.resolveAwsCredentials(
|
|
66
|
+
static createAwsSqsClient(platformConfig) {
|
|
67
|
+
return AwsCredentialsClient.resolveAwsCredentials(platformConfig)
|
|
69
68
|
.then(awsCredentials => new SQSClient({
|
|
70
|
-
region:
|
|
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(
|
|
15
|
-
logInfo({
|
|
16
|
-
exec(`sudo docker network create ${
|
|
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
|
|
18
|
+
static stopContainer(deploymentRequest) {
|
|
20
19
|
const name = deploymentRequest.image.replaceAll('/', '_')
|
|
21
|
-
logInfo({
|
|
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(
|
|
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({
|
|
30
|
-
return `sudo docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${
|
|
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 }
|
|
32
|
+
static resolveCredentials({ registry, username, authentication }) {
|
|
34
33
|
const shouldAuthenticate = username || authentication
|
|
35
34
|
|
|
36
35
|
if (!shouldAuthenticate) {
|
|
37
|
-
logInfo({
|
|
36
|
+
logInfo({ message: 'No docker credentials provided, assuming image is publicly hosted' })
|
|
38
37
|
return ''
|
|
39
38
|
}
|
|
40
39
|
|
|
41
|
-
logInfo({
|
|
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(
|
|
44
|
+
static deploy(platformConfig, deploymentRequest) {
|
|
46
45
|
return new Promise(resolve => {
|
|
47
|
-
logInfo({
|
|
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(
|
|
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({
|
|
63
|
+
logInfo({ message: `[DockerClient]: ${data}` })
|
|
65
64
|
})
|
|
66
65
|
|
|
67
66
|
command.stderr.on('data', (data) => {
|
|
68
|
-
logError({
|
|
67
|
+
logError({ message: `[DockerClient]: ${data}` })
|
|
69
68
|
})
|
|
70
69
|
|
|
71
70
|
command.on('close', code => {
|
|
72
|
-
logInfo({
|
|
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
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
interface LogInput {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
1
|
+
// interface LogInput {
|
|
2
|
+
// type: TypeOfMessage;
|
|
3
|
+
// message: string;
|
|
4
|
+
// data?: any;
|
|
5
|
+
// dataPrefix: string;
|
|
6
|
+
// }
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
Info
|
|
10
|
-
Error
|
|
11
|
-
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
|
|
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)
|
|
@@ -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
|
+
}
|