@ossy/deployment-tools 0.0.81 → 0.0.83

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/cdk.context.json CHANGED
@@ -72,5 +72,9 @@
72
72
  "hosted-zone:account=858451553223:domainName=oskarssylwan.se:region=eu-north-1": {
73
73
  "Id": "/hostedzone/Z06541531WYSO7D1IVDTT",
74
74
  "Name": "oskarssylwan.se."
75
+ },
76
+ "hosted-zone:account=858451553223:domainName=oskarssylwan.com:region=eu-north-1": {
77
+ "Id": "/hostedzone/Z07488011P8FXGWA11LTS",
78
+ "Name": "oskarssylwan.com."
75
79
  }
76
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ossy/deployment-tools",
3
- "version": "0.0.81",
3
+ "version": "0.0.83",
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",
package/src/cms/cli.js CHANGED
@@ -1,37 +1,45 @@
1
1
  #!/usr/bin/env node
2
+ const { resolve } = require('path')
2
3
  const { readFileSync } = require('fs')
3
4
  const arg = require('arg')
4
5
  const fetch = require('node-fetch')
5
- const { PlatformDeploymentService } = require('./platform-deployment')
6
6
  const { logInfo, logError } = require('../log')
7
7
 
8
8
  const getTokenPayload = token =>
9
9
  JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
10
10
 
11
-
12
- const import = options => {
11
+ const importResourceTemplates = options => {
13
12
 
14
13
  const parsedArgs = arg({
15
14
  '--authentication': String,
16
15
  '--a': '--authentication',
17
16
 
18
- '--resource-templates': String,
19
- '-t': '--resource-templates'
17
+ '--ossy-file': String,
18
+ '-o': '--ossy-file',
20
19
  }, { argv: options })
21
20
 
22
21
  logInfo({ message: '[CMS] reading files' })
23
22
  const token = parsedArgs['--authentication']
24
23
  const tokenPayload = getTokenPayload(token)
25
- const templates = readFileSync(resolve(parsedArgs['--resource-templates']), 'utf8')
24
+ const ossyfile = JSON.parse(readFileSync(resolve(parsedArgs['--ossy-file']), 'utf8'))
25
+
26
+ console.log('ossyfile', ossyfile)
26
27
 
27
28
  if (!token) return logError({ message: '[CMS] No token provided with --authentication'})
28
- if (!templates) return logError({ message: '[CMS] No templates provided with --resource-templates'})
29
+ if (!ossyfile?.workspaceId) return logError({ message: '[CMS] No workspaceId provided in ossy.json'})
30
+ if (!ossyfile?.resourceTemplates) return logError({ message: '[CMS] No resource templates provided in ossy.json'})
29
31
 
30
- logInfo({ message: '[CMS] uploading files' })
32
+ logInfo({ message: '[CMS] uploading resource templates' })
33
+
34
+ const fetchOptions = {
35
+ method: 'POST',
36
+ headers: { 'Authorization': token, 'Content-Type': 'application/json' },
37
+ body: JSON.stringify(ossyfile.resourceTemplates)
38
+ }
31
39
 
32
40
  fetch(
33
- `https://cms.ossy.se/api/v0/workspaces/${tokenPayload?.workspaceId}/resource-templates`,
34
- { method: 'POST', headers: { 'Authorization': token }, body: templates }
41
+ `http://localhost:3001/api/v0/workspaces/${ossyfile.workspaceId}/resource-templates`,
42
+ fetchOptions
35
43
  )
36
44
  .then(() => logInfo({ message: '[CMS] done' }))
37
45
  .catch(error => logError({ message: '[CMS] Error', error }))
@@ -40,7 +48,7 @@ const import = options => {
40
48
  module.exports = {
41
49
  handler: ([command, ...options]) => {
42
50
  !!command
43
- ? { deploy }[command](options)
51
+ ? { 'import-resource-templates': importResourceTemplates }[command](options)
44
52
  : logError({ message: '[CMS] No command provided' })
45
53
  }
46
54
  }
@@ -28,7 +28,7 @@ class PlatformDeploymentService {
28
28
  .then(([deploymentTemplates, platformConfigs]) => {
29
29
 
30
30
  const platformConfig = platformConfigs.find(({ platformName }) => platformName === targetPlatform)
31
- const deploymentTemplatesForTargetPlatform = deploymentTemplates[targetPlatform] || []
31
+ const deploymentTemplatesForTargetPlatform = deploymentTemplates[targetPlatform] || [] //todo remove deploymentTargetPlatform
32
32
  const deploymentTemplate = deploymentTemplatesForTargetPlatform.find(({ domain }) => domain === targetDomain)
33
33
 
34
34
  if (!platformConfig) {
@@ -39,9 +39,16 @@ class DockerService {
39
39
  .catch(() => {}) // no worries if container isn't there
40
40
  }
41
41
 
42
+ static pullImage({ image, registry }) {
43
+ const imageUrl = !!registry ? `${registry}/${image}` : image
44
+ logInfo({ message: `[DockerService] Pulling image for ${image}:latest` })
45
+ return exec(`docker image pull ${imageUrl}:latest`)
46
+ .catch(logErrorAndReject(`[DockerService] Could pull image ${image}:latest`))
47
+ }
48
+
42
49
  static startContainer(platformConfig, { image, containerPort, hostPort, registry, env, domain }) {
43
50
  const imageUrl = !!registry ? `${registry}/${image}` : image
44
- const envsAsString = Object.entries(env || {}).reduce((envs, [name, value]) => `${envs} --env ${name}=${value}`, '')
51
+ const envsAsString = Object.entries(env || {}).reduce((envs, [name, value]) => `${envs} --env ${name}="${value}"`, '')
45
52
  logInfo({ message: `[DockerService] Starting container for ${domain} with port mapping ${hostPort}:${containerPort} and source ${imageUrl}` })
46
53
  return exec(`docker run -d -p ${hostPort}:${containerPort} --name=${domain} --network=${platformConfig.ciDockerNetworkName} --network-alias=${domain} --rm ${envsAsString} ${imageUrl}`)
47
54
  .catch(logErrorAndReject(`[DockerService] Could not start container ${image}`))
@@ -67,6 +74,7 @@ class DockerService {
67
74
  return DockerService.resolveCredentials(deploymentRequest)
68
75
  .then(() => DockerService.stopContainer(deploymentRequest))
69
76
  .then(() => DockerService.removeImage(deploymentRequest))
77
+ .then(() => DockerService.pullImage(deploymentTemplate))
70
78
  .then(() => DockerService.startContainer(platformConfig, deploymentRequest))
71
79
  }
72
80
 
@@ -1,6 +1,18 @@
1
1
  const { DockerService } = require('./docker-service')
2
2
 
3
+ const platformConfig = {
4
+ ciDockerNetworkName: 'foo'
5
+ }
3
6
 
4
- DockerService.removeImage(
5
- { image: 'ossy-se/cms-client-web', registry: 'ghcr.io', domain: 'oskars.com', hostPort: '3005', containerPort: '3000' }
7
+ const deploymentTemplate = {
8
+ "domain": "api.qa.ossy.se",
9
+ "image": "ossy-se/cms-api",
10
+ "targetDeploymentPlatform": "ossybot",
11
+ "type": "CONTAINER",
12
+ "hostPort": "3001",
13
+ "containerPort": "3000",
14
+ }
15
+
16
+ DockerService.pullImage(
17
+ deploymentTemplate
6
18
  )
@@ -10,7 +10,9 @@ const {
10
10
  SecurityGroup,
11
11
  Peer,
12
12
  Port,
13
- UserData
13
+ UserData,
14
+ BlockDevice,
15
+ BlockDeviceVolume
14
16
  } = require('aws-cdk-lib/aws-ec2')
15
17
  const { Role, ServicePrincipal, Policy, PolicyStatement, Effect } = require('aws-cdk-lib/aws-iam')
16
18
  const { Queue } = require('aws-cdk-lib/aws-sqs')
@@ -73,7 +75,7 @@ class ContainerDeploymentTarget extends Construct {
73
75
  })
74
76
 
75
77
  const platformConfigDeployment = new BucketDeployment(this, 'PlatformConfigDeployment', {
76
- sources: [Source.jsonData('platform-config.json', props.config)],
78
+ sources: [Source.jsonData('platform-config.json', { ...props.config, awsRoleToAssume: undefined })],
77
79
  destinationBucket: props.bucket
78
80
  })
79
81
 
@@ -143,11 +145,17 @@ class ContainerDeploymentTarget extends Construct {
143
145
  role,
144
146
  instanceType: InstanceType.of(
145
147
  InstanceClass.T3,
146
- InstanceSize.MICRO
148
+ InstanceSize.LARGE
147
149
  ),
148
150
  machineImage: new GenericLinuxImage({
149
151
  [SupportedRegions.North]: InstanceImages.UBUNTU
150
152
  }),
153
+ blockDevices: [
154
+ {
155
+ deviceName: '/dev/sda1',
156
+ volume: BlockDeviceVolume.ebs(50)
157
+ }
158
+ ],
151
159
  keyName: props.config.awsKeyPairName
152
160
  })
153
161