/* * get token */ async getToken(username) { const file = this.app.baseDir + '/storage/admin.json'; const res = this.fileSyncInstance(file) .get('user') .find({ username: username }) .value(); return res; }
/* * stop应用 */ async stopApp(appid) { const res = { code: 1000, msg: 'unknown error', }; const appIsExist = await this.appIsInstall(appid); if (!appIsExist) { res.msg = '应用不存在'; return res; } const isRunning = await this.service.docker.appIsRunning(appid); if (!isRunning) { res.msg = '应用没有在运行'; return res; } const dirpath = this.app.baseDir + '/docker/dev/' + appid; const stopRes = await this.service.dockerCompose.stop(dirpath); return stopRes; }
/* * 修改密码 */ async modifyPwd(username, newPwd) { const file = this.app.baseDir + '/storage/admin.json'; const res = this.fileSyncInstance(file) .get('user') .find({ username: username }) .assign({ pwd: newPwd}) .write(); return res; }
/* * dev app count */ async getDevAppNum() { const addonsDir = this.app.baseDir + '/docker/dev'; const lsDir = utils.getDirs(addonsDir); const index = lsDir.indexOf('example'); if (index > -1) { lsDir.splice(index, 1); } return lsDir.length; }
/* * fileSyncInstance */ fileSyncInstance(file = null) { if (!file) { file = this.app.baseDir + '/storage/db.json'; } const isDir = fs.existsSync(file); if (!isDir) { return null; } const adapter = new FileSync(file); const db = low(adapter); return db; }
/* * 获取dapps */ async getDapps() { const file = this.app.baseDir + '/app/common/dapps.json'; const info = this.fileSyncInstance(file) .get('dapps') .value(); return info; }
/* * 删除应用文件 */ async delAppFile(appid) { const dirpath = this.app.baseDir + '/docker/addons/' + appid; const isDir = fs.existsSync(dirpath); if (isDir) { utils.delDir(dirpath); return true; } return false; }
/* * 添加admin user */ async addAdminUser(username, pwd) { const file = this.app.baseDir + '/storage/admin.json'; const userInfo = { username, pwd, }; const res = this.fileSyncInstance(file) .get('user') .push(userInfo) .write(); return res; }
/* * 更新 dapps */ async updateDapps(version = '') { const file = this.app.baseDir + '/app/common/dapps.json'; let key = ''; let value = ''; if (version) { key = 'dapps.version'; value = version; } const res = this.fileSyncInstance(file) .set(key, value) .write(); return res; }
/* * 应用是否安装 */ async appIsInstall(appid) { const dirpath = this.app.baseDir + '/docker/addons/' + appid; const isDir = fs.existsSync(dirpath); if (isDir) { return true; } return false; }
/* * set token */ async setToken(username, token) { const file = this.app.baseDir + '/storage/admin.json'; const res = this.fileSyncInstance(file) .get('user') .find({ username: username }) .assign({ token: token}) .write(); return res; }
/* * get user */ async getAdminUser(username, pwd) { const file = this.app.baseDir + '/storage/admin.json'; const info = this.fileSyncInstance(file) .get('user') .find({ username: username, pwd: pwd }) .value(); return info; }
/* * 应用是否安装 */ async appIsInstall(appid) { const dirpath = this.app.baseDir + '/docker/dev/' + appid; const isDir = fs.existsSync(dirpath); if (isDir) { return true; } return false; }
/* * 我的应用总数 local */ async myAppTotalLocal() { let total = 0; const addonsDir = this.app.baseDir + '/docker/addons'; const lsDir = utils.getDirs(addonsDir); const index = lsDir.indexOf('example'); if (index > -1) { lsDir.splice(index, 1); } total = lsDir.length; return total; }
/* * 我的应用总数 */ async myAppTotal() { let total = 0; const addonsDir = this.app.baseDir + '/docker/addons'; const lsDir = utils.getDirs(addonsDir); const index = lsDir.indexOf('example'); if (index > -1) { lsDir.splice(index, 1); } total = lsDir.length; return total; }