UNPKG

youzanyun-devtool-worker

Version:

148 lines (147 loc) 5.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const download_1 = tslib_1.__importDefault(require("download")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let SshService = class SshService { constructor() { } async start() { this.userGitInfoCache = {}; this.sshClientDir = this.configService.getConfig('sshClientDir'); this.sshAgentPort = this.configService.getConfig('sshClientPort') || 35021; this.sshClientUrl = this.configService.getConfig('sshClientUrl') || 'https://b.yzcdn.cn/1.0.0.13-SNAPSHOT/cloud-ssh-client-deploy-1.0.0-SNAPSHOT.tar.gz'; } async getCmdPath(relativePath) { let cmdPath; if (process.platform === "darwin") { cmdPath = path_1.default.resolve(this.sshClientDir, `${relativePath}.sh`); await this.executeService.promiseExec(`chmod +x ${cmdPath}`, {}); } else { cmdPath = path_1.default.resolve(this.sshClientDir, `${relativePath}.bat`); } return cmdPath; } async startSshAgent() { await this.checkSshVersion(this.sshClientUrl); const cmdPath = await this.getCmdPath('bin/start'); const sshClientProfile = this.configService.getConfig("sshClientProfile") || "prod"; const args = ["-P", sshClientProfile, "--proxy_enabled", this.configService.isUseProxy() ? "true" : "false", "--proxy_host", this.configService.getProxyHost(), "--proxy_port", this.configService.getProxyPort(), ]; console.log('startSshAgent', cmdPath, args); this.executeService.promiseSpawm(cmdPath, args); } async stopSshAgent() { console.log("stopSshAgent"); const cmdPath = await this.getCmdPath('bin/stop'); const stdout = await this.executeService.promiseSpawm(cmdPath); console.log(stdout); return stdout; } async getAuthInfo(projectId) { const sid = await this.userService.getSid(); console.log(sid); let userGitInfo = {}; if (this.userGitInfoCache[sid]) { userGitInfo = this.userGitInfoCache[sid]; } else { let result = await this.requestService.get('https://diy.youzanyun.com/api/user/get-user-info-from-state', {}); if (result.code != 0) { throw new Error(`获取用户token出错 ${result.msg}`); } const data = result.data; userGitInfo = { "gitToken": data.token, "mobile": data.mobile }; this.userGitInfoCache[sid] = userGitInfo; } const { proName } = await this.projectService.getProjectById(projectId); return Object.assign(Object.assign({}, userGitInfo), { appName: proName }); } async isSshAgentStarted() { let started = true; if (!this.sshAgentPort) { return false; } return started; } async getSshAgentStatus(projectId) { const authInfo = await this.getAuthInfo(projectId); const reuslt = await this.requestService.post(`http://127.0.0.1:${this.sshAgentPort}/tunnel/detail`, authInfo, {}); if (!reuslt.success) { throw new Error(reuslt.message); } return reuslt.data; } async startTunnel(projectId) { const authInfo = await this.getAuthInfo(projectId); const reuslt = await this.requestService.post(`http://127.0.0.1:${this.sshAgentPort}/tunnel/open`, authInfo, { timeout: 20000, }); if (!reuslt.success) { throw new Error(reuslt.message); } } async stopTunnel(projectId) { const authInfo = await this.getAuthInfo(projectId); const reuslt = await this.requestService.post(`http://127.0.0.1:${this.sshAgentPort}/tunnel/close`, authInfo, { timeout: 10000, }); if (!reuslt.success) { throw new Error(reuslt.message); } } async checkSshVersion(sshClientUrl) { let exist = await fs_extra_1.default.pathExists(this.sshClientDir); if (exist) { try { let guideInfo = await fs_extra_1.default.readJson(path_1.default.resolve(this.sshClientDir, 'meta.json')); if (guideInfo.url == sshClientUrl) { return; } } catch (e) { console.log(`ssh client升级异常`, e); } } await this.downloadGuideConfig(sshClientUrl); } async downloadGuideConfig(sshClientUrl) { await fs_extra_1.default.remove(this.sshClientDir); await (0, download_1.default)(sshClientUrl, this.sshClientDir, { extract: true, strip: 1, }); let metaJson = { url: sshClientUrl, }; await fs_extra_1.default.writeJson(path_1.default.resolve(this.sshClientDir, 'meta.json'), metaJson, { spaces: 2 }); } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], SshService.prototype, "configService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], SshService.prototype, "projectService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], SshService.prototype, "requestService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], SshService.prototype, "executeService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], SshService.prototype, "userService", void 0); SshService = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], SshService); exports.default = SshService;