@ossy/deployment-tools 0.0.54 → 0.0.55

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ossy/deployment-tools",
3
- "version": "0.0.54",
3
+ "version": "0.0.55",
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 = 3000
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: '[ContainerManagerServer] Could not handle incoming deployment request', error }))
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 Docker = require('dockerode')
1
+ const { exec: execBash } = require('child_process')
2
+ const { logInfo, logDebug } = require('../log')
3
3
 
4
- const { logInfo } = require('../log')
5
-
6
- // const exec = command => new Promise((resolve, reject) => {
7
- // execBash('command', (error, stdout, stderr) => {
8
- //
9
- // if (error) {
10
- // console.error(`exec error: ${error}`)
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(`sudo docker network create ${platformConfig.ciDockerNetworkName}`)
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(`sudo docker stop ${name}`)
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,28 @@ 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(`sudo docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${platformConfig.ciDockerNetworkName} --network-alias=${name} --rm ${envsAsString} ${imageUrl}`)
34
+ return exec(`docker run -d -p ${hostPort}:${containerPort} --name=${name} --network=${platformConfig.ciDockerNetworkName} --network-alias=${name} --rm ${envsAsString} ${imageUrl}`)
50
35
  }
51
36
 
52
- static pullImage({ registry, username, authentication, image }) {
53
- let authconfig
37
+ static resolveCredentials({ registry, username, authentication }) {
54
38
  const shouldAuthenticate = username || authentication
55
39
 
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
40
  shouldAuthenticate
67
- ? logInfo({ message: '[DockerService] Docker credentials provided, trying to pull from private repository' })
41
+ ? logInfo({ message: '[DockerService] Docker credentials provided, logging in to private repository' })
68
42
  : logInfo({ message: '[DockerService] No docker credentials provided, assuming image is publicly hosted' })
69
43
 
70
- const docker = new Docker()
71
-
72
- return docker.pull(image, { authconfig })
73
- // .then()
74
- // .catch()
75
-
76
- // logInfo({ message: `[DockerService] Resolving docker credentials for ${registry}` })
77
- // exec(`sudo docker login ${registry} -u ${username} -p ${authentication}`)
44
+ return shouldAuthenticate
45
+ ? exec(`docker login ${registry} -u ${username} -p ${authentication}`)
46
+ : Promise.resolve()
78
47
  }
79
48
 
80
49
  static deploy(platformConfig, deploymentRequest) {
81
50
  logInfo({ message: '[DockerService] Starting docker deployment sequence' })
82
- DockerService.stopContainer(deploymentRequest)
83
- DockerService.pullImage(deploymentRequest)
84
- DockerService.startContainer(platformConfig, deploymentRequest)
51
+ logDebug({ message: '[DockerService] deploymentRequest', data: deploymentRequest })
52
+
53
+ return DockerService.resolveCredentials(deploymentRequest)
54
+ .then(() => DockerService.stop(deploymentRequest))
55
+ .then(() => DockerService.startContainer(platformConfig, deploymentRequest))
85
56
  }
86
57
 
87
58
  }
@@ -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=caddy
9
- Group=caddy
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,7 +26,7 @@ const logError = logInput => {
26
26
  }
27
27
 
28
28
  const logDebug = logInput => {
29
- const isDebugOn = process.env.DEBUG || false
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}`)
@@ -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 service-name.service
10
+ // journalctl -u deployment-tools.service
11
11
  /**
12
12
  * @class
13
13
  */