ssh-deploy/src/index.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-03 05:52:52 +08:00
#!/usr/bin/env node
const { sshDeploy } = require('./rsyncCli');
const { remoteCmdBefore, remoteCmdAfter } = require('./remoteCmd');
2023-01-03 00:52:55 +08:00
const { addSshKey, getPrivateKeyPath, updateKnownHosts } = require('./sshKey');
const { validateRequiredInputs } = require('./helpers');
const inputs = require('./inputs');
2019-10-03 05:52:52 +08:00
2023-01-02 19:57:35 +08:00
const run = async () => {
const {
source, remoteUser, remoteHost, remotePort,
deployKeyName, sshPrivateKey,
2023-01-02 08:33:48 +08:00
args, exclude, sshCmdArgs,
scriptBefore, scriptAfter,
2023-01-02 09:13:02 +08:00
rsyncServer
} = inputs;
// Validate required inputs
validateRequiredInputs({ sshPrivateKey, remoteHost, remoteUser });
// Add SSH key
2023-01-02 18:41:31 +08:00
addSshKey(sshPrivateKey, deployKeyName);
const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName);
2023-01-03 00:52:55 +08:00
// Update known hosts if ssh command is present to avoid prompt
if (scriptBefore || scriptAfter) {
updateKnownHosts(remoteHost);
}
// Check Script before
if (scriptBefore) {
2023-01-02 19:57:35 +08:00
await remoteCmdBefore(scriptBefore);
}
/* eslint-disable object-property-newline */
2023-01-02 19:57:35 +08:00
await sshDeploy({
2023-01-02 09:13:02 +08:00
source, rsyncServer, exclude, remotePort,
2023-01-02 19:57:35 +08:00
privateKeyPath, args, sshCmdArgs
});
2023-01-02 19:57:35 +08:00
// Check script after
if (scriptAfter) {
await remoteCmdAfter(scriptAfter);
}
2019-10-03 05:52:52 +08:00
};
2023-01-02 19:57:35 +08:00
run()
2023-01-03 00:57:33 +08:00
.then((data) => {
2023-01-03 01:05:04 +08:00
console.log('[DONE]', data);
2023-01-02 19:57:35 +08:00
})
2023-01-03 00:57:33 +08:00
.catch((error) => {
2023-01-03 01:05:04 +08:00
console.error('[ERROR]', error.message);
2023-01-03 00:57:33 +08:00
process.exit(1);
2023-01-02 19:57:35 +08:00
});