@ossy/deployment-tools 0.0.62 → 0.0.64

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.62",
3
+ "version": "0.0.64",
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",
@@ -0,0 +1,96 @@
1
+ const Matchers = {
2
+ host: host => ({ host: [host] }),
3
+ path: path => ({ path: [path] })
4
+ }
5
+
6
+ const Handlers = {
7
+ reverseProxy: upstreamPort => ({
8
+ handler: 'reverse_proxy',
9
+ upstreams: [{ dial: `localhost:${upstreamPort}` }]
10
+ }),
11
+ subroute: routes => ({
12
+ handler: 'subroute',
13
+ routes
14
+ })
15
+ }
16
+
17
+ /**
18
+ * @class
19
+ */
20
+ class CaddyConfigService {
21
+
22
+ static getRouteConfig(platformConfig, deploymentRequest) {
23
+
24
+ const url = [
25
+ deploymentRequest.subdomain,
26
+ platformConfig.activeEnvironment !== 'prod' ? platformConfig.activeEnvironment : undefined,
27
+ platformConfig.domain
28
+ ]
29
+ .filter(x => !!x)
30
+ .join('.')
31
+
32
+ return {
33
+ match: [Matchers.host(url)],
34
+ handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(deploymentRequest.hostPort)]}])]
35
+ }
36
+ }
37
+
38
+ static getDefaultConfig(platformConfig) {
39
+ return {
40
+ apps: {
41
+ http: {
42
+ servers: {
43
+ [platformConfig.ciServerName]: {
44
+ listen: [':80', ':443'],
45
+ routes: [
46
+ {
47
+ match: [Matchers.host(`${platformConfig.ciSubDomain}.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
48
+ handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(platformConfig.ciInternalServerPort)]}])]
49
+ }
50
+ ]
51
+ }
52
+ }
53
+ },
54
+ tls: platformConfig.activeEnvironment === 'local-dev' ? undefined : {
55
+ automation: {
56
+ policies: [
57
+ {
58
+ subjects:[`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`],
59
+ issuers:[
60
+ {
61
+ challenges:{
62
+ dns:{
63
+ provider:{
64
+ 'max_retries': 10,
65
+ name: 'route53',
66
+ 'aws_profile': 'ci-client'
67
+ }
68
+ }
69
+ },
70
+ module: 'acme'
71
+ },
72
+ {
73
+ challenges:{
74
+ dns:{
75
+ provider:{
76
+ 'max_retries': 10,
77
+ name: 'route53'
78
+ }
79
+ }
80
+ },
81
+ module: 'zerossl'
82
+ }
83
+ ]
84
+ }
85
+ ]
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ }
93
+
94
+ module.exports = {
95
+ CaddyConfigService
96
+ }
@@ -0,0 +1,63 @@
1
+ const fetch = require('node-fetch')
2
+ const { CaddyConfigService } = require('./caddy-config')
3
+ const { logInfo, logError, logDebug } = require('../log')
4
+
5
+ /**
6
+ * @class
7
+ */
8
+ class CaddyService {
9
+
10
+ static addDeployment(platformConfig, deploymentRequest) {
11
+ const routeConfig = CaddyConfigService.getRouteConfig(platformConfig, deploymentRequest)
12
+ const host = routeConfig.match[0].host[0]
13
+ logInfo({ message: `[CaddyService] Updating caddy config to route ${host} to localhost:${deploymentRequest.hostPort}` })
14
+
15
+ return fetch(`http://localhost:2019/config/apps/http/servers/${platformConfig.ciServerName}/routes`, {
16
+ method: 'POST',
17
+ headers: { 'Content-Type': 'application/json' },
18
+ body: JSON.stringify(routeConfig)
19
+ })
20
+ .then(response => response?.error && logError({ message: `[CaddyService] Could not update caddy config to include ${host}`, error }))
21
+ .catch(error => logError({ message: `[CaddyService] Could not update caddy config to include ${host}`, error }))
22
+ }
23
+
24
+ static applyDefaultConfig(platformConfig) {
25
+ return CaddyService.fetchServerConfig(platformConfig)
26
+ .then(caddyConfig => {
27
+
28
+ if (caddyConfig) {
29
+ logInfo({ message: '[CaddyService] Server config already exist, not applying default config' })
30
+ return
31
+ }
32
+
33
+ logInfo({ message: '[CaddyService] Applying default caddy config' })
34
+ const defaultConfig = CaddyConfigService.getDefaultConfig(platformConfig)
35
+ return fetch('http://localhost:2019/load', {
36
+ method: 'POST',
37
+ headers: { 'Content-Type': 'application/json' },
38
+ body: JSON.stringify(defaultConfig)
39
+ })
40
+ })
41
+ .then(response => response?.error && logError({ message: '[CaddyService] Could not apply default caddy config', error }))
42
+ .catch(error => logError({ message: '[CaddyService] Could not apply default caddy config', error }))
43
+
44
+ }
45
+
46
+ static fetchServerConfig(platformConfig) {
47
+ logInfo({ message: '[CaddyService] Fetching server config' })
48
+ return fetch(`http://localhost:2019/config/apps/http/servers/${platformConfig.ciServerName}`, {
49
+ method: 'GET',
50
+ headers: { 'Content-Type': 'application/json' }
51
+ })
52
+ .then(x => x.json())
53
+ .then(response => {
54
+ if (!response?.error) return response
55
+ logInfo({ message: '[CaddyService] No server config found' })
56
+ logDebug({ message: '[CaddyService] Error while fetching server config', data: response.error })
57
+ })
58
+ }
59
+ }
60
+
61
+ module.exports = {
62
+ CaddyService
63
+ }
@@ -0,0 +1,21 @@
1
+ const { CaddyService } = require('./caddy')
2
+
3
+ const config = {
4
+ activeEnvironment: 'prod',
5
+ domain: 'localhost',
6
+ ciServerName: 'ossy',
7
+ ciSubDomain: 'ci',
8
+ ciInternalServerPort: '3000'
9
+ }
10
+
11
+ const deploymentRequest = {
12
+ subdomain: 'cc',
13
+ hostPort: '3000'
14
+ }
15
+
16
+ CaddyService.applyDefaultConfig(config)
17
+
18
+ // CaddyService.addDeployment(config, deploymentRequest)
19
+
20
+ // CaddyService.fetchServerConfig(config)
21
+ // .then(console.log)
@@ -0,0 +1 @@
1
+ module.exports = require('./caddy.js')
@@ -1,4 +1,3 @@
1
-
2
1
  const getInstallDocker = () => [
3
2
  'apt-get remove docker docker-engine docker.io containerd runc',
4
3
  `apt-get install \
package/src/log.js CHANGED
@@ -21,16 +21,20 @@ const logInfo = logInput => {
21
21
 
22
22
  const logError = logInput => {
23
23
  const messagePrefix = prefixTo(TypeOfMessage.Error, logInput.message)
24
+ log('\n')
24
25
  log(`${messagePrefix}${logInput.message}`)
25
- logInput.error && log('\n[Reason]:', logInput.error, '\n')
26
+ logInput.error && log('[Reason]:', logInput.error)
27
+ log('\n')
26
28
  }
27
29
 
28
30
  const logDebug = logInput => {
29
31
  const isDebugOn = process.env.DEBUG || true
30
32
  if (!isDebugOn) return
31
33
  const messagePrefix = prefixTo(TypeOfMessage.Debug, logInput.message)
34
+ log('\n')
32
35
  log(`${messagePrefix}${logInput.message}`)
33
- logInput.data && log('\n[Debug data]:', logInput.data, '\n')
36
+ logInput.data && log('[DEBUG DATA]:', logInput.data)
37
+ log('\n')
34
38
  }
35
39
 
36
40
  const logErrorAndReject = message => error => {
@@ -1,4 +1,4 @@
1
- const { CaddyService } = require('./caddy')
1
+ const { CaddyService } = require('../caddy')
2
2
  const { DockerService } = require('../docker')
3
3
  const { RestApiService } = require('./rest-api')
4
4
 
@@ -8,6 +8,7 @@ const { DeploymentQueueService } = require('../deployment-queue')
8
8
  const { logError } = require('../log')
9
9
 
10
10
  // journalctl -u deployment-tools.service
11
+ // docker logs
11
12
  /**
12
13
  * @class
13
14
  */
@@ -0,0 +1,10 @@
1
+ // const { PlatformServerService } = require('./platform-server')
2
+ const { RestApiService } = require('./rest-api')
3
+
4
+ // PlatformServerService.start({
5
+ //
6
+ // })
7
+
8
+ RestApiService.start({
9
+ ciInternalServerPort: 3000
10
+ })
@@ -1,114 +0,0 @@
1
- const fetch = require('node-fetch')
2
- const { logInfo, logError } = require('../log')
3
-
4
- const Matchers = {
5
- host: host => ({ host: [host] }),
6
- path: path => ({ path: [path] })
7
- }
8
-
9
- const Handlers = {
10
- reverseProxy: upstreamPort => ({
11
- handler: 'reverse_proxy',
12
- upstreams: [{ dial: `localhost:${upstreamPort}` }]
13
- }),
14
- subroute: routes => ({
15
- handler: 'subroute',
16
- routes
17
- })
18
- }
19
-
20
- /**
21
- * @class
22
- */
23
- class CaddyService {
24
-
25
- static addDeployment(platformConfig, deploymentRequest) {
26
-
27
- const url = `${deploymentRequest.subdomain}.${platformConfig.activeEnvironment}.${platformConfig.domain}`
28
-
29
- logInfo({ message: `[CaddyService] Updating caddy config to route ${url} to localhost:${deploymentRequest.hostPort}` })
30
-
31
- return fetch(`http://localhost:2019/config/apps/http/servers/${platformConfig.ciServerName}/routes/0/handle`, {
32
- method: 'POST',
33
- headers: { 'Content-Type': 'application/json' },
34
- body: JSON.stringify(Handlers.subroute([
35
- {
36
- match: [Matchers.host(url)],
37
- handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(deploymentRequest.hostPort)]}])]
38
- }
39
- ]))
40
- })
41
- .catch(error => logError({ message: `[CaddyService] Could not update caddy config to include ${url}`, error }))
42
- }
43
-
44
- static applyDefaultConfig(platformConfig) {
45
- logInfo({ message: '[CaddyService] Applying default caddy config' })
46
- return fetch('http://localhost:2019/load', {
47
- method: 'POST',
48
- headers: { 'Content-Type': 'application/json' },
49
- body: JSON.stringify({
50
- apps: {
51
- http: {
52
- servers: {
53
- [platformConfig.ciServerName]: {
54
- listen: [':80', ':443'],
55
- routes: [
56
- {
57
- match: [Matchers.host(`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
58
- handle: [Handlers.subroute([
59
- {
60
- match: [Matchers.host(`${platformConfig.ciSubDomain}.${platformConfig.activeEnvironment}.${platformConfig.domain}`)],
61
- handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(platformConfig.ciInternalServerPort)]}])]
62
- }
63
- ])],
64
- terminal: true
65
- }
66
- ]
67
- }
68
- }
69
- },
70
- tls: {
71
- automation: {
72
- policies: [
73
- {
74
- subjects:[`*.${platformConfig.activeEnvironment}.${platformConfig.domain}`],
75
- issuers:[
76
- {
77
- challenges:{
78
- dns:{
79
- provider:{
80
- 'max_retries': 10,
81
- name: 'route53',
82
- 'aws_profile': 'ci-client'
83
- }
84
- }
85
- },
86
- module: 'acme'
87
- },
88
- {
89
- challenges:{
90
- dns:{
91
- provider:{
92
- 'max_retries': 10,
93
- name: 'route53'
94
- }
95
- }
96
- },
97
- module: 'zerossl'
98
- }
99
- ]
100
- }
101
- ]
102
- }
103
- }
104
- }
105
- })
106
- })
107
- .catch(error => logError({ message: '[CaddyService] Could not apply default caddy config', error }))
108
- }
109
-
110
- }
111
-
112
- module.exports = {
113
- CaddyService
114
- }