public_production.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * 文件名称:public_production.js
  3. * 文件说明:用于向移动云快速发布前端应用
  4. */
  5. const { Client } = require("ssh2")
  6. const path = require("path")
  7. const fs = require("fs")
  8. /** 目标机会话定义 */
  9. const hosts = [
  10. {
  11. name: '移动云直播应用服务1',
  12. ip: '36.139.83.55',
  13. port: 65022,
  14. username: "suabapp",
  15. password: "suabapp@95599"
  16. },{
  17. name: '移动云直播应用服务3',
  18. ip: '36.156.140.91',
  19. port: 65022,
  20. username: "suabapp",
  21. password: "suabapp@95599"
  22. },{
  23. name: '移动云直播应用服务4',
  24. ip: '36.138.118.173',
  25. port: 65022,
  26. username: "suabapp",
  27. password: "suabapp@95599"
  28. }
  29. ]
  30. /**
  31. * 更新应用配置
  32. */
  33. const apps = {
  34. "ManageFront": {
  35. target_dir: "/home/suabapp/temp",
  36. target_name: "SzLivePlatformManageFront.zip",
  37. update_cmd: "sh /home/suabapp/updaters/ManageFront.sh",
  38. proc_dir: path.join(__dirname, "dist/SzLivePlatformManageFront.zip"),
  39. hosts: [0, 1, 2]
  40. }
  41. }
  42. const readline = require('readline');
  43. const std = readline.createInterface({
  44. input: process.stdin,
  45. output: process.stdout
  46. });
  47. /**
  48. * 程序
  49. * @param {*} app 要上传的应用
  50. */
  51. function start(app) {
  52. // 遍历应用部署的生产机
  53. for (let i = 0; i < app.hosts.length; i ++) {
  54. const host = hosts[app.hosts[i]];
  55. const conn = new Client();
  56. console.log(`正在连接${host.name}...`);
  57. conn.on('ready', () => {
  58. console.log(`已连接到${host.name}...`);
  59. console.log("正在准备更新环境");
  60. conn.exec(`mkdir ${app.target_dir}`, (pre_err, pre_stream) => {
  61. console.log(pre_err);
  62. if (pre_err) throw pre_err;
  63. pre_stream.on('close', () => {
  64. console.log(`准备上传更新包...`);
  65. conn.sftp((err, sftp) => {
  66. if (err) throw err;
  67. console.log("更新包路径", app.proc_dir)
  68. console.log("远端路径", path.join(app.target_dir, app.target_name))
  69. const readStream = fs.createReadStream(app.proc_dir);
  70. const writeStream = sftp.createWriteStream(app.target_dir + "/" + app.target_name)
  71. readStream.pipe(writeStream);
  72. writeStream.on('close', () => {
  73. console.log("更新包上传完成...");
  74. // conn.end();
  75. console.log("执行更新命令...");
  76. conn.exec(app.update_cmd, (err_exec, stream) => {
  77. if (err_exec) throw err_exec;
  78. stream.on('close', () => {
  79. console.log("更新完成")
  80. conn.end();
  81. conn.destroy();
  82. }).on('data', (data) => {
  83. console.log("STDOUT: " + data);
  84. }).stderr.on('data', (data) => {
  85. console.log("STDERR: " + data);
  86. })
  87. })
  88. })
  89. // sftp.fastPut(app.proc_dir, path.join(app.target_dir, app.target_name), (result) => {
  90. // console.log(result);
  91. // console.log("更新包上传完成...");
  92. // // conn.end();
  93. // console.log("执行更新命令...");
  94. // conn.exec(app.update_cmd, (err_exec, stream) => {
  95. // if (err_exec) throw err_exec;
  96. // stream.on('close', () => {
  97. // console.log("更新完成")
  98. // conn.end();
  99. // conn.destroy();
  100. // }).on('data', (data) => {
  101. // console.log("STDOUT: " + data);
  102. // }).stderr.on('data', (data) => {
  103. // console.log("STDERR: " + data);
  104. // })
  105. // })
  106. // })
  107. })
  108. }).on('data', (data) => {
  109. console.log("STDOUT: " + data);
  110. }).stderr.on('data', (data) => {
  111. console.log("STDERR: " + data);
  112. })
  113. })
  114. // console.log(`已连接到${host.name}...`);
  115. // console.log("正在删除旧版本应用...");
  116. // conn.exec(`rm -rf ${app.target_dir}`, (err, stream) => {
  117. // if (err) throw err;
  118. // stream.on('close', (code, signal) => {
  119. // console.log("已删除旧版应用");
  120. // conn.exec(`mkdir ${app.target_dir}`)
  121. // }).on('data', (data) => {
  122. // console.log("STDOUT: " + data);
  123. // }).stderr.on('data', (data) => {
  124. // console.log("STDERR: " + data);
  125. // })
  126. // })
  127. }).connect({
  128. host: host.ip,
  129. username: host.username,
  130. port: host.port,
  131. password: host.password
  132. });
  133. }
  134. }
  135. /**
  136. * 更新程序
  137. * @param {*} appName 要更新的应用名
  138. */
  139. function update(appName) {
  140. let app = apps[appName];
  141. if (app == null) {
  142. console.log(`${appName} 不存在`)
  143. return;
  144. }
  145. if (!fs.statSync(app.proc_dir)) {
  146. console.log("应用还未打包,请先打包");
  147. return
  148. }
  149. start(app);
  150. }
  151. std.question("请输入要更新的应用:", (answer) => {
  152. console.log(`正在准备更新${answer}`)
  153. update(answer);
  154. std.close();
  155. })