@ossy/deployment-tools 0.0.54 → 0.0.56
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 +1 -1
- package/src/deployment-queue/deployment-queue.js +3 -3
- package/src/docker/docker-service.js +25 -52
- package/src/infrastructure/container-server/container-server.js +3 -0
- package/src/infrastructure/container-server/deployment-tools.service.js +2 -2
- package/src/log.js +7 -1
- package/src/server/platform-server.js +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/deployment-tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.56",
|
|
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
|
"source": "./src/index.js",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -35,14 +35,14 @@ class DeploymentQueueService {
|
|
|
35
35
|
static pollForDeploymentRequests(platformConfig, handleDeploymentRequest) {
|
|
36
36
|
logInfo({ message: '[DeploymentQueueService] Starting polling for deployment requests' })
|
|
37
37
|
DeploymentQueueService.createAwsSqsClient(platformConfig).then(sqsClient => {
|
|
38
|
-
const FIVE_MINUTES =
|
|
38
|
+
const FIVE_MINUTES = 300000
|
|
39
39
|
|
|
40
40
|
setInterval(() => {
|
|
41
41
|
|
|
42
42
|
const receiveMessageCommand = new ReceiveMessageCommand({ QueueUrl: platformConfig.awsDeploymentSqsArn })
|
|
43
43
|
|
|
44
44
|
sqsClient.send(receiveMessageCommand)
|
|
45
|
-
.then(data => data.Messages.map(message => {
|
|
45
|
+
.then(data => data && data.Messages && data.Messages.map(message => {
|
|
46
46
|
|
|
47
47
|
logInfo({ message: '[DeploymentQueueService] Received deployment request' })
|
|
48
48
|
|
|
@@ -60,7 +60,7 @@ class DeploymentQueueService {
|
|
|
60
60
|
})
|
|
61
61
|
|
|
62
62
|
}))
|
|
63
|
-
.catch(error => logError({ message: '[
|
|
63
|
+
.catch(error => logError({ message: '[DeploymentQueueService] Could not handle incoming deployment request', error }))
|
|
64
64
|
}, FIVE_MINUTES)
|
|
65
65
|
|
|
66
66
|
})
|
|
@@ -1,29 +1,13 @@
|
|
|
1
|
-
const { exec } = require('child_process')
|
|
2
|
-
const
|
|
1
|
+
const { exec: execBash } = require('child_process')
|
|
2
|
+
const { logInfo, logDebug, logErrorAndReject } = require('../log')
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// return
|
|
12
|
-
// }
|
|
13
|
-
//
|
|
14
|
-
// console.log(`stdout: ${stdout}`)
|
|
15
|
-
// console.error(`stderr: ${stderr}`)
|
|
16
|
-
//
|
|
17
|
-
// command.stdout.on('data', data => {
|
|
18
|
-
// logInfo({ message: `[DockerService]: ${data}` })
|
|
19
|
-
// })
|
|
20
|
-
//
|
|
21
|
-
// command.stderr.on('data', (data) => {
|
|
22
|
-
// logError({ message: `[DockerService]: ${data}` })
|
|
23
|
-
// })
|
|
24
|
-
//
|
|
25
|
-
// })
|
|
26
|
-
// })
|
|
4
|
+
const exec = command => new Promise((resolve, reject) => {
|
|
5
|
+
execBash(command, (error, stdout, stderr) => {
|
|
6
|
+
if (error) return reject(error)
|
|
7
|
+
if (stderr) return reject(stderr)
|
|
8
|
+
resolve(stdout)
|
|
9
|
+
})
|
|
10
|
+
})
|
|
27
11
|
|
|
28
12
|
/**
|
|
29
13
|
* @class
|
|
@@ -32,13 +16,14 @@ class DockerService {
|
|
|
32
16
|
|
|
33
17
|
static createDockerNetworkForContainerManagerServer(platformConfig) {
|
|
34
18
|
logInfo({ message: '[DockerService] Creating docker network for comunication between containers' })
|
|
35
|
-
exec(`
|
|
19
|
+
return exec(`docker network create ${platformConfig.ciDockerNetworkName}`)
|
|
36
20
|
}
|
|
37
21
|
|
|
38
22
|
static stopContainer(deploymentRequest) {
|
|
39
23
|
const name = deploymentRequest.image.replaceAll('/', '_')
|
|
40
24
|
logInfo({ message: `[DockerService] Running docker stop for image with the name of ${name}` })
|
|
41
|
-
exec(`
|
|
25
|
+
return exec(`docker stop ${name}`)
|
|
26
|
+
.catch(() => {}) // no worries if container isn't there
|
|
42
27
|
}
|
|
43
28
|
|
|
44
29
|
static startContainer(platformConfig, { image, containerPort, hostPort, registry, env }) {
|
|
@@ -46,42 +31,30 @@ class DockerService {
|
|
|
46
31
|
const imageUrl = !!registry ? `${registry}/${image}` : image
|
|
47
32
|
const envsAsString = Object.entries(env || {}).reduce((envs, [name, value]) => `${envs} --env ${name}=${value}`, '')
|
|
48
33
|
logInfo({ message: `[DockerService] Running docker start for image with the name of ${name} with port mapping ${hostPort}:${containerPort} and source ${imageUrl}` })
|
|
49
|
-
exec(`
|
|
34
|
+
return exec(`docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${platformConfig.ciDockerNetworkName} --network-alias=${name} --rm ${envsAsString} ${imageUrl}`)
|
|
35
|
+
.cath(logErrorAndReject(`[DockerService] Could not start container ${image}`))
|
|
50
36
|
}
|
|
51
37
|
|
|
52
|
-
static
|
|
53
|
-
let authconfig
|
|
38
|
+
static resolveCredentials({ registry, username, authentication }) {
|
|
54
39
|
const shouldAuthenticate = username || authentication
|
|
55
40
|
|
|
56
|
-
if (shouldAuthenticate) {
|
|
57
|
-
authconfig = {
|
|
58
|
-
username: username,
|
|
59
|
-
password: authentication,
|
|
60
|
-
// auth: '',
|
|
61
|
-
// email: 'your@email.email',
|
|
62
|
-
serveraddress: registry
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
41
|
shouldAuthenticate
|
|
67
|
-
? logInfo({ message: '[DockerService] Docker credentials provided,
|
|
42
|
+
? logInfo({ message: '[DockerService] Docker credentials provided, logging in to private repository' })
|
|
68
43
|
: logInfo({ message: '[DockerService] No docker credentials provided, assuming image is publicly hosted' })
|
|
69
44
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// .catch()
|
|
75
|
-
|
|
76
|
-
// logInfo({ message: `[DockerService] Resolving docker credentials for ${registry}` })
|
|
77
|
-
// exec(`sudo docker login ${registry} -u ${username} -p ${authentication}`)
|
|
45
|
+
return !shouldAuthenticate
|
|
46
|
+
? Promise.resolve()
|
|
47
|
+
: exec(`docker login ${registry} -u ${username} -p ${authentication}`)
|
|
48
|
+
.cath(logErrorAndReject(`[DockerService] Could not authenticate with ${registry}`))
|
|
78
49
|
}
|
|
79
50
|
|
|
80
51
|
static deploy(platformConfig, deploymentRequest) {
|
|
81
52
|
logInfo({ message: '[DockerService] Starting docker deployment sequence' })
|
|
82
|
-
DockerService
|
|
83
|
-
|
|
84
|
-
DockerService.
|
|
53
|
+
logDebug({ message: '[DockerService] deploymentRequest', data: deploymentRequest })
|
|
54
|
+
|
|
55
|
+
return DockerService.resolveCredentials(deploymentRequest)
|
|
56
|
+
.then(() => DockerService.stop(deploymentRequest))
|
|
57
|
+
.then(() => DockerService.startContainer(platformConfig, deploymentRequest))
|
|
85
58
|
}
|
|
86
59
|
|
|
87
60
|
}
|
|
@@ -111,6 +111,9 @@ class ContainerServer extends Construct {
|
|
|
111
111
|
const userData = UserData.forLinux()
|
|
112
112
|
|
|
113
113
|
userData.addCommands(
|
|
114
|
+
'sudo groupadd docker',
|
|
115
|
+
'sudo usermod -aG docker ubuntu',
|
|
116
|
+
'newgrp docker',
|
|
114
117
|
'sudo apt update -y',
|
|
115
118
|
...getInstallNodeJs(),
|
|
116
119
|
...getInstallNpm(),
|
|
@@ -5,8 +5,8 @@ After=network.target caddy-route53.service
|
|
|
5
5
|
|
|
6
6
|
[Service]
|
|
7
7
|
EnvironmentFile=/etc/environment
|
|
8
|
-
User=
|
|
9
|
-
Group=
|
|
8
|
+
User=ubuntu
|
|
9
|
+
Group=docker
|
|
10
10
|
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
|
11
11
|
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
|
|
12
12
|
ExecStart=/usr/bin/npx --yes @ossy/deployment-tools server start --platforms /home/ubuntu/platform-config.json
|
package/src/log.js
CHANGED
|
@@ -26,15 +26,21 @@ const logError = logInput => {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
const logDebug = logInput => {
|
|
29
|
-
const isDebugOn = process.env.DEBUG ||
|
|
29
|
+
const isDebugOn = process.env.DEBUG || true
|
|
30
30
|
if (!isDebugOn) return
|
|
31
31
|
const messagePrefix = prefixTo(TypeOfMessage.Debug, logInput.message)
|
|
32
32
|
log(`${messagePrefix}${logInput.message}`)
|
|
33
33
|
logInput.data && log('\n[Debug data]:', logInput.data, '\n')
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
const logErrorAndReject = message => error => {
|
|
37
|
+
logError({ message, error })
|
|
38
|
+
return Promise.reject(error)
|
|
39
|
+
}
|
|
40
|
+
|
|
36
41
|
module.exports = {
|
|
37
42
|
logInfo,
|
|
38
43
|
logError,
|
|
44
|
+
logErrorAndReject,
|
|
39
45
|
logDebug
|
|
40
46
|
}
|
|
@@ -7,7 +7,7 @@ const { PlatformConfigService } = require('../config')
|
|
|
7
7
|
const { DeploymentQueueService } = require('../deployment-queue')
|
|
8
8
|
const { logError } = require('../log')
|
|
9
9
|
|
|
10
|
-
// journalctl -u
|
|
10
|
+
// journalctl -u deployment-tools.service
|
|
11
11
|
/**
|
|
12
12
|
* @class
|
|
13
13
|
*/
|
|
@@ -24,7 +24,7 @@ class PlatformServerService {
|
|
|
24
24
|
platformConfig,
|
|
25
25
|
deploymentRequest => {
|
|
26
26
|
DockerService.deploy(platformConfig, deploymentRequest)
|
|
27
|
-
|
|
27
|
+
.then(() => CaddyService.addDeployment(platformConfig, deploymentRequest))
|
|
28
28
|
return Promise.resolve()
|
|
29
29
|
}
|
|
30
30
|
)
|