-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
exist-install
executable file
·126 lines (104 loc) · 3.25 KB
/
exist-install
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
#!/usr/bin/env node
const { connect } = require("../../index")
const { argv } = require("process") // eslint-disable-line node/prefer-global/process
const {readFileSync} = require("fs")
const {basename} = require("path")
// read connection options from ENV
const connectionOptions = require('../connection')
const db = connect(connectionOptions)
async function getUserInfo(db) {
const {user} = db.client.options.basic_auth
return await db.users.getUserInfo(user)
}
function serverName(db) {
const {isSecure, options} = db.client
const protocol = `http${isSecure ? 's' : ''}:`
const isStdPort =
(isSecure && options.port === 443) ||
(!isSecure && options.port === 80)
if (isStdPort) {
return `${protocol}//${options.host}`
}
return `${protocol}//${options.host}:${options.port}`
}
async function install (db, localFilePath) {
try {
const xarName = basename(localFilePath)
const contents = readFileSync(localFilePath)
process.stdout.write(`Install ${xarName} on ${serverName(db)}\n`)
process.stdout.write("uploading...")
const uploadResult = await db.app.upload(contents, xarName)
if (!uploadResult.success) {
throw new Error(uploadResult.error)
}
process.stdout.clearLine()
process.stdout.cursorTo(0)
process.stdout.write("✔︎ uploaded\n");
process.stdout.write("installing...")
const installResult = await db.app.install(xarName)
if (!installResult.success) {
throw new Error(installResult.error)
}
process.stdout.clearLine()
process.stdout.cursorTo(0)
const installationMessage = installResult.result.update ? "updated" : "installed"
process.stdout.write(`✔︎ ${installationMessage}\n`);
return 0
}
catch (e) {
let message = e.faultString ? e.faultString : e.message
console.error(message)
return 1
}
}
async function run() {
const cli = require("yargs")
.command("$0", "Install XAR package", (yargs) => {
yargs.demandCommand(1, 1).usage(`
Usage:
exist-install package`);
})
.option("h", { alias: "help"})
.strict(false)
.exitProcess(false);
try {
const parsed = cli.parse(argv.slice(2));
if (Boolean(parsed.help)) {
return 0;
}
// main
const xarPath = parsed._[0]
if (!xarPath.match(/\.xar$/i)) {
throw Error("Package must have the file extension .xar!")
}
// check permissions (and therefore implicitly the connection)
const accountInfo = await getUserInfo(db)
const isAdmin = accountInfo.groups.includes('dba')
if (!isAdmin) {
throw Error(`Package installation failed. User "${accountInfo.name}" is not a member of the "dba" group.`)
}
return install(db, xarPath)
} catch (error) {
if (
error.code === 'EPROTO' ||
error.code === 'ECONNREFUSED' ||
error.code === 'ECONNRESET'
) {
console.error(`Could not connect to DB at ${serverName(db)} - Reason: ${error.code}`)
return 1
}
if (error.name !== 'YError') {
const msg = error.message ? error.message : error.faultString ? error.faultString : error
console.error(msg)
}
return 1
}
}
run()
.then((exitCode) => {
process.exitCode = exitCode
})
.catch((error) => {
console.error(error)
process.exitCode = 1
})