/** * 文件名称:public_production.js * 文件说明:用于向移动云快速发布前端应用 */ const { Client } = require("ssh2") const path = require("path") const fs = require("fs") /** 目标机会话定义 */ const hosts = [ { name: '移动云直播应用服务1', ip: '36.139.83.55', port: 65022, username: "suabapp", password: "suabapp@95599" },{ name: '移动云直播应用服务3', ip: '36.156.140.91', port: 65022, username: "suabapp", password: "suabapp@95599" },{ name: '移动云直播应用服务4', ip: '36.138.118.173', port: 65022, username: "suabapp", password: "suabapp@95599" } ] /** * 更新应用配置 */ const apps = { "ManageFront": { target_dir: "/home/suabapp/temp", target_name: "SzLivePlatformManageFront.zip", update_cmd: "sh /home/suabapp/updaters/ManageFront.sh", proc_dir: path.join(__dirname, "dist/SzLivePlatformManageFront.zip"), hosts: [0, 1, 2] } } const readline = require('readline'); const std = readline.createInterface({ input: process.stdin, output: process.stdout }); /** * 程序 * @param {*} app 要上传的应用 */ function start(app) { // 遍历应用部署的生产机 for (let i = 0; i < app.hosts.length; i ++) { const host = hosts[app.hosts[i]]; const conn = new Client(); console.log(`正在连接${host.name}...`); conn.on('ready', () => { console.log(`已连接到${host.name}...`); console.log("正在准备更新环境"); conn.exec(`mkdir ${app.target_dir}`, (pre_err, pre_stream) => { console.log(pre_err); if (pre_err) throw pre_err; pre_stream.on('close', () => { console.log(`准备上传更新包...`); conn.sftp((err, sftp) => { if (err) throw err; console.log("更新包路径", app.proc_dir) console.log("远端路径", path.join(app.target_dir, app.target_name)) const readStream = fs.createReadStream(app.proc_dir); const writeStream = sftp.createWriteStream(app.target_dir + "/" + app.target_name) readStream.pipe(writeStream); writeStream.on('close', () => { console.log("更新包上传完成..."); // conn.end(); console.log("执行更新命令..."); conn.exec(app.update_cmd, (err_exec, stream) => { if (err_exec) throw err_exec; stream.on('close', () => { console.log("更新完成") conn.end(); conn.destroy(); }).on('data', (data) => { console.log("STDOUT: " + data); }).stderr.on('data', (data) => { console.log("STDERR: " + data); }) }) }) // sftp.fastPut(app.proc_dir, path.join(app.target_dir, app.target_name), (result) => { // console.log(result); // console.log("更新包上传完成..."); // // conn.end(); // console.log("执行更新命令..."); // conn.exec(app.update_cmd, (err_exec, stream) => { // if (err_exec) throw err_exec; // stream.on('close', () => { // console.log("更新完成") // conn.end(); // conn.destroy(); // }).on('data', (data) => { // console.log("STDOUT: " + data); // }).stderr.on('data', (data) => { // console.log("STDERR: " + data); // }) // }) // }) }) }).on('data', (data) => { console.log("STDOUT: " + data); }).stderr.on('data', (data) => { console.log("STDERR: " + data); }) }) // console.log(`已连接到${host.name}...`); // console.log("正在删除旧版本应用..."); // conn.exec(`rm -rf ${app.target_dir}`, (err, stream) => { // if (err) throw err; // stream.on('close', (code, signal) => { // console.log("已删除旧版应用"); // conn.exec(`mkdir ${app.target_dir}`) // }).on('data', (data) => { // console.log("STDOUT: " + data); // }).stderr.on('data', (data) => { // console.log("STDERR: " + data); // }) // }) }).connect({ host: host.ip, username: host.username, port: host.port, password: host.password }); } } /** * 更新程序 * @param {*} appName 要更新的应用名 */ function update(appName) { let app = apps[appName]; if (app == null) { console.log(`${appName} 不存在`) return; } if (!fs.statSync(app.proc_dir)) { console.log("应用还未打包,请先打包"); return } start(app); } std.question("请输入要更新的应用:", (answer) => { console.log(`正在准备更新${answer}`) update(answer); std.close(); })