|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const output = require('./output') |
|
4
|
|
|
const jsonfile = require('jsonfile') |
|
5
|
|
|
const mkdirp = require('mkdirp') |
|
6
|
|
|
const path = require('path') |
|
7
|
|
|
const fs = require('fs') |
|
8
|
|
|
|
|
9
|
|
|
var Container = {} |
|
10
|
|
|
|
|
11
|
|
|
Container.getVersion = function () { |
|
12
|
|
|
if (!fs.existsSync(path.join('.inc', 'version'))) { |
|
13
|
|
|
if (fs.existsSync('.scho' + 'lica')) { |
|
14
|
|
|
return 10 |
|
15
|
|
|
} |
|
16
|
|
|
return false |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
return parseInt(fs.readFileSync(path.join('.inc', 'version'), 'utf8')) || false |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
Container.clean = function () { |
|
23
|
|
|
try { |
|
24
|
|
|
fs.unlinkSync(path.join('.inc', 'containers', 'default.json')) |
|
25
|
|
|
} catch (e) {} |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
Container.performUpgrade = function () { |
|
29
|
|
|
var version = this.getVersion() |
|
30
|
|
|
if (!version) { |
|
31
|
|
|
return |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (version > global.containerVersion) { |
|
35
|
|
|
output.warn('Warning: project metadata is created using a newer version of the CLI, please upgrade') |
|
36
|
|
|
return |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (version < global.containerVersion) { |
|
40
|
|
|
var upgradeFile = path.join(global.base, 'lib', 'upgrades', version + '-' + global.containerVersion + '.js') |
|
41
|
|
|
try { |
|
42
|
|
|
if (!fs.existsSync(upgradeFile)) { |
|
43
|
|
|
throw new Error('no upgrade path from version ' + version + ' to ' + global.containerVersion) |
|
44
|
|
|
} |
|
45
|
|
|
require(upgradeFile)() |
|
46
|
|
|
output.warn('Upgraded project metadata from ' + version + ' to ' + global.containerVersion) |
|
47
|
|
|
} catch (e) { |
|
48
|
|
|
output.warn('Error upgrading project: ' + e.message) |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
Container.validateContainerContent = function (config) { |
|
54
|
|
|
if (!('containerToken' in config) && 'container_token' in config) { |
|
55
|
|
|
config.containerToken = config.container_token |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ('containerToken' in config) { |
|
59
|
|
|
Container.applyConfig(config) |
|
60
|
|
|
return true |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return false |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
Container.applyConfig = function (config) { |
|
67
|
|
|
if ('containerEndpoint' in config) { |
|
68
|
|
|
global.containerEndpoint = config.containerEndpoint |
|
69
|
|
|
} |
|
70
|
|
|
if ('version' in config) { |
|
71
|
|
|
global.apiVersion = config.version |
|
72
|
|
|
} |
|
73
|
|
|
if ('stability' in config) { |
|
74
|
|
|
global.apiStability = config.stability |
|
75
|
|
|
} |
|
76
|
|
|
if ('containerEndpointCheckSSL' in config && !config.containerEndpointCheckSSL) { |
|
77
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
Container.pathForConfig = function (name) { |
|
82
|
|
|
return path.join('.inc', 'containers', name + '.json') |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
Container.getConfig = function (name, cb) { |
|
86
|
|
|
jsonfile.readFile(Container.pathForConfig(name), function (err, config) { |
|
87
|
|
|
// Use existing container |
|
88
|
|
|
if (!err && typeof config === 'object' && Container.validateContainerContent(config)) { |
|
89
|
|
|
return cb(null, config) |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Try default file if in `beta` mode |
|
93
|
|
|
if (name === 'beta') { |
|
94
|
|
|
Container.getConfig('default', cb) |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
cb(err || new Error('Container config not found or invalid'), null) |
|
|
|
|
|
|
98
|
|
|
}) |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
Container.writeConfig = function (name, content) { |
|
102
|
|
|
mkdirp(path.join('.inc', 'containers'), function () { |
|
103
|
|
|
jsonfile.writeFile(Container.pathForConfig(name), content) |
|
104
|
|
|
if (!fs.existsSync(path.join('.inc', 'version'))) { |
|
105
|
|
|
fs.writeFileSync(path.join('.inc', 'version'), '11', 'utf8') |
|
106
|
|
|
} |
|
107
|
|
|
}) |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
module.exports = Container |
|
111
|
|
|
|