-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (141 loc) · 4.06 KB
/
index.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
#!/usr/bin/env node
const argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 [options]')
.boolean(['p', 'l', 'r', 'd'])
.alias('p', 'push')
.describe('p', 'Push to docker repository')
.alias('l', 'latest')
.describe('l', 'Push to docker repository with latest tag')
.alias('r', 'remove')
.describe('r', 'Remove docker image')
.alias('d', 'dry-run')
.describe('d', 'Dry run')
.alias('x', 'prefix')
.describe('x', 'Prefix of tag(ex. rep.example.com:5000/, procube/)')
.demandCommand(1,1)
.default({f: 'package.json', x: ''})
.help('h')
.alias('h', 'help')
.strict()
.epilog('copyright 2021 Procube Co.,Ltd')
.argv;
const fs = require('fs')
const { spawn, execFileSync } = require('child_process')
const shellescape = require('shell-escape')
const base_image = 'node:16-alpine'
const wait_interval = 20
const retries = 10
function spawnPromise(modulePath, args, stdinFile = null) {
return new Promise((resolve, reject) => {
console.log(`Execute: ${modulePath} ${args.join(' ')}.`)
const proc = spawn(modulePath, args)
proc.stdout.on('data', (data) => {
console.log(data.toString())
})
proc.stderr.on('data', (data) => {
console.error(data.toString())
})
proc.on('close', (code) => {
if (code !== 0) {
reject(`The process exited with code ${code}.`)
}
resolve('The process succeeded.')
});
if (stdinFile) {
proc.stdin.write(stdinFile)
}
proc.stdin.end()
});
}
function getCmd(bin, baseName) {
if (typeof(bin) == "string") return baseName
return Object.keys(bin)[0]
}
async function main() {
const name = argv._[0]
var viewJson
try {
viewJson = execFileSync('npm', ['view', name, '--json']).toString()
} catch (err) {
throw `Fail to execute command: "npm view ${name} --json"`
}
var package
try {
package = JSON.parse(viewJson)
} catch (err) {
throw `Fail to parse output of: "npm view ${name} --json"; error=${err}; string=${viewJson}`
}
const {version, bin} = package
const baseName = name.split('/').slice(-1)[0]
var tag = `${baseName}:${version}`
var latesttag = `${baseName}:latest`
if (argv.x) {
tag = argv.x + tag
latesttag = argv.x + latesttag
}
const npmrc = `${process.env.HOME}/.npmrc`
var npmrcContents = null
if (fs.existsSync(npmrc)) {
npmrcContents = fs.readFileSync(npmrc, 'utf8')
}
var dockerfile = `FROM ${base_image}\nWORKDIR /root\n`
if (npmrcContents) {
dockerfile += `RUN echo -e $'${npmrcContents.replace(/'/g, `'\\''`).replace(/\r?\n/g, '\\n\\\n')}' >> /root/.npmrc &&\\\n` +
`yarn global add ${name}@${version} --exact &&\\\n` +
'rm /root/.npmrc\n'
} else {
dockerfile += `RUN yarn global add ${name}@${version} --exact\n`
}
dockerfile += `CMD ${getCmd(bin, baseName)}\n`
if (! argv.d) {
console.log(await spawnPromise('docker', ['build', '-t', tag, '-'], stdinFile = dockerfile))
} else {
console.log(`Execute: "docker build -t ${tag}"`)
console.log(`Dockerfile:\n${dockerfile}`)
}
if (argv.p) {
await doPush(tag)
}
if (argv.l) {
await doPush(tag, alias=latesttag)
}
if (argv.r) {
if (argv.l) {
if (argv.d) {
console.log(`Execute: "docker image rm ${latesttag}"`)
} else {
console.log(await spawnPromise('docker', ['image', 'rm', latesttag]))
}
}
if (argv.d) {
console.log(`Execute: "docker image rm ${tag}"`)
} else {
console.log(await spawnPromise('docker', ['image', 'rm', tag]))
}
}
return 'Complete!'
}
async function doPush(tag, alias=null) {
if (alias) {
if (argv.d) {
console.log(`Execute: "docker tag ${tag} ${alias}"`)
} else {
console.log(await spawnPromise('docker', ['tag', tag, alias]))
}
tag = alias
}
if (argv.d) {
console.log(`Execute: "docker push ${tag}"`)
} else {
console.log(await spawnPromise('docker', ['push', tag]))
}
}
main()
.then((message) => {
if (message) console.log(message)
process.exit(0)
})
.catch((message) => {
if (message) console.error(message)
process.exit(1)
})