-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.js
188 lines (149 loc) · 5.35 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const path = require('path')
const { reporter } = require('@dhis2/cli-helpers-engine')
const defaults = require('./defaults')
const clusterDir = 'clusters'
const dockerComposeCacheName = 'docker-compose'
const cacheFile = 'config.json'
module.exports.initDockerComposeCache = async ({
composeProjectName,
cache,
dockerComposeRepository,
dockerComposeDirectory,
force,
}) => {
const cacheDir = path.join(
clusterDir,
composeProjectName,
dockerComposeCacheName
)
const cachePath = path.join(cacheDir, dockerComposeDirectory)
const exists = await cache.exists(cachePath)
if (exists && !force) {
reporter.debug(
'Skipping docker compose repo initialization, found cached dir'
)
} else {
reporter.info('Initializing Docker Compose repository...')
try {
await cache.get(dockerComposeRepository, cacheDir, {
force: true,
})
const created = await cache.exists(cachePath)
if (created) {
reporter.debug(`Cache created at: ${cachePath}`)
}
} catch (e) {
reporter.error('Initialization failed!')
return null
}
}
return cache.getCacheLocation(cachePath)
}
module.exports.substituteVersion = (string, version) =>
replacer(string, 'version', version)
function makeDockerImage(string = '', substitutes = {}, variant = '') {
let res = string
// the stable channel is just dhis2/core, so if the channel is
// unspecified or 'stable', we should just strip it out from the
// image tag
if (!substitutes.channel || substitutes.channel === 'stable') {
res = replacer(res, 'channel', '')
}
for (const token in substitutes) {
const value =
token === 'channel'
? `-${substitutes[token]}` // add a leading '-' to channel
: substitutes[token]
res = replacer(res, token, value)
}
if (variant) {
res = `${res}-${variant}`
}
return res
}
function replacer(string, token, value) {
const regexp = new RegExp(`{${token}}`, 'g')
return string.replace(regexp, value)
}
async function resolveConfiguration(argv = {}) {
const file = path.join(clusterDir, argv.name, cacheFile)
let currentCache
try {
currentCache = JSON.parse(await argv.getCache().read(file))
} catch (e) {
reporter.debug('JSON parse of cache file failed', e)
}
let currentConfig
if (argv.cluster && argv.cluster.clusters) {
currentConfig = argv.cluster.clusters[argv.name]
}
// order matters! it defines the precedence of configuration
const cache = Object.assign({}, currentConfig, currentCache)
const config = argv.cluster
const args = argv
// order matters! it defines the precedence of configuration
const resolved = Object.assign({}, defaults, config, cache, args)
// resolve specials...
resolved.dhis2Version = resolved.dhis2Version || resolved.name
resolved.dbVersion = resolved.dbVersion || resolved.dhis2Version
resolved.contextPath = resolved.customContext ? `/${resolved.name}` : ''
resolved.dockerImage = makeDockerImage(
resolved.image,
{
channel: resolved.channel,
version: resolved.dhis2Version,
},
resolved.variant
)
reporter.debug('Resolved configuration\n', resolved)
await argv.getCache().write(
file,
JSON.stringify(
{
channel: resolved.channel,
dbVersion: resolved.dbVersion,
dhis2Version: resolved.dhis2Version,
dhis2Config: resolved.dhis2Config,
customContext: resolved.customContext,
image: resolved.image,
port: resolved.port,
},
null,
4
)
)
return resolved
}
module.exports.cleanCache = async ({ cache, name }) =>
await cache.purge(path.join(clusterDir, name))
module.exports.makeEnvironment = cfg => {
const env = {
DHIS2_CORE_NAME: cfg.name,
DHIS2_CORE_IMAGE: cfg.dockerImage,
DHIS2_CORE_CONTEXT_PATH: cfg.contextPath,
DHIS2_CORE_VERSION: cfg.dhis2Version,
DHIS2_CORE_DB_VERSION: cfg.dbVersion,
DHIS2_CORE_PORT: cfg.port,
}
if (cfg.dhis2Config) {
env.DHIS2_CORE_CONFIG = cfg.dhis2Config
}
reporter.debug('Runtime environment\n', env)
return env
}
// This has to match the normalization done by docker-compose to reliably get container statuses
// from https://github.com/docker/compose/blob/c8279bc4db56f49cf2e2b80c8734ced1c418b856/compose/cli/command.py#L154
const normalizeName = name => name.replace(/[^-_a-z0-9]/g, '')
module.exports.makeComposeProject = name => `d2-cluster-${normalizeName(name)}`
module.exports.listClusters = async argv => {
const cache = argv.getCache()
const exists = await cache.exists(clusterDir)
if (!exists) return []
const stat = await cache.stat(clusterDir)
const promises = Object.keys(stat.children)
.filter(name => cache.exists(path.join(clusterDir, name)))
.map(name => resolveConfiguration({ name, getCache: argv.getCache }))
return await Promise.all(promises)
}
module.exports.makeDockerImage = makeDockerImage
module.exports.resolveConfiguration = resolveConfiguration