Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 7b82805

Browse files
committed
fix hosting errors caused by wrong region
1 parent 168338a commit 7b82805

14 files changed

Lines changed: 465 additions & 323 deletions

File tree

bin/cloudbase.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22
const os = require('os')
3+
const path = require('path')
34
const chalk = require('chalk')
45
const Sentry = require('@sentry/node')
56
const program = require('commander')
@@ -60,11 +61,27 @@ if (processArgv.includes('--deb')) {
6061
)
6162
}
6263

64+
if (processArgv.includes('--tcb-test')) {
65+
console.log(
66+
chalk.bold.yellow(
67+
'====\n您已经进入 test 模式!\n移除 --tcb-test 选项退出 test 模式!\n===='
68+
)
69+
)
70+
try {
71+
const envs = require(path.join(process.cwd(), './tcb-test.js'))
72+
for (const key in envs) {
73+
process.env[key] = envs[key]
74+
}
75+
} catch (err) {
76+
console.log(err)
77+
}
78+
}
79+
6380
// debug 模式
6481
process.IS_DEBUG = processArgv.includes('--deb')
6582

6683
// 需要隐藏的选项
67-
const hideArgs = ['--deb']
84+
const hideArgs = ['--deb', '--tcb-test']
6885
hideArgs.forEach(arg => {
6986
const index = processArgv.indexOf(arg)
7087
if (index > -1) {

lib/commands/hosting.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ const HostingStatusMap = {
2424
process: '处理中',
2525
online: '上线',
2626
destroying: '销毁中',
27-
offline: '下线'
27+
offline: '下线',
28+
create_fail: '初始化失败',
29+
destroy_fail: '销毁失败'
2830
};
2931
commander_1.default
3032
.command('hosting:enable')
31-
.option('-e, --envId', '环境 Id')
33+
.option('-e, --envId [envId]', '环境 Id')
3234
.description('开启静态网站服务')
3335
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
3436
const { parent: { configFile }, envId } = options;
@@ -43,7 +45,7 @@ commander_1.default
4345
}));
4446
commander_1.default
4547
.command('hosting:detail')
46-
.option('-e, --envId', '环境 Id')
48+
.option('-e, --envId [envId] [envId]', '环境 Id')
4749
.description('查看静态网站服务信息')
4850
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
4951
const { parent: { configFile }, envId } = options;
@@ -53,12 +55,12 @@ commander_1.default
5355
if (!website) {
5456
throw new error_1.CloudBaseError('你还没有开启静态网站服务,请使用 cloudbase hosting:enable 命令启用静态网站服务!');
5557
}
56-
const url = `https://${website.CdnDomain}`;
57-
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}\n静态网站状态:${HostingStatusMap[website.Status]}`);
58+
const url = `https://${website.cdnDomain}`;
59+
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}\n静态网站状态:${HostingStatusMap[website.status]}`);
5860
}));
5961
commander_1.default
6062
.command('hosting:destroy')
61-
.option('-e, --envId', '环境 Id')
63+
.option('-e, --envId [envId]', '环境 Id')
6264
.description('关闭静态网站服务')
6365
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
6466
const { parent: { configFile }, envId } = options;
@@ -74,17 +76,22 @@ commander_1.default
7476
}
7577
const loading = utils_1.loadingFactory();
7678
loading.start('静态网站销毁中');
77-
const res = yield hosting_1.destroyHosting({ envId: assignEnvId });
78-
if (res.code === 0) {
79-
loading.succeed('静态网站服务销毁成功!');
79+
try {
80+
const res = yield hosting_1.destroyHosting({ envId: assignEnvId });
81+
if (res.code === 0) {
82+
loading.succeed('静态网站服务销毁中...');
83+
}
84+
else {
85+
loading.fail('静态网站服务销毁失败!');
86+
}
8087
}
81-
else {
82-
loading.fail('静态网站服务销毁失败!');
88+
catch (e) {
89+
loading.fail(e.message || '静态网站服务销毁失败!');
8390
}
8491
}));
8592
commander_1.default
8693
.command('hosting:deploy [filePath] [cloudPath]')
87-
.option('-e, --envId', '环境 Id')
94+
.option('-e, --envId [envId]', '环境 Id')
8895
.description('部署静态网站文件')
8996
.action((filePath, cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
9097
const { parent: { configFile }, envId } = options;
@@ -99,15 +106,15 @@ commander_1.default
99106
envId: assignEnvId,
100107
isDir
101108
});
102-
loading.succeed('文件部署中...');
109+
loading.succeed('文件部署成功!');
103110
}
104111
catch (error) {
105112
loading.fail('文件部署失败!');
106113
}
107114
}));
108115
commander_1.default
109116
.command('hosting:delete [cloudPath]')
110-
.option('-e, --envId', '环境 Id')
117+
.option('-e, --envId [envId]', '环境 Id')
111118
.option('-d, --dir', '删除文件夹')
112119
.description('删除静态网站文件/文件夹')
113120
.action((cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
@@ -123,15 +130,16 @@ commander_1.default
123130
envId: assignEnvId,
124131
isDir: dir
125132
});
126-
loading.succeed(`删除${fileText}成功...`);
133+
loading.succeed(`删除${fileText}成功`);
127134
}
128-
catch (error) {
129-
loading.fail(`删除${fileText}失败...`);
135+
catch (e) {
136+
loading.fail(`删除${fileText}失败!`);
137+
console.log(e.message);
130138
}
131139
}));
132140
commander_1.default
133141
.command('hosting:list')
134-
.option('-e, --envId', '环境 Id')
142+
.option('-e, --envId [envId]', '环境 Id')
135143
.description('展示文件列表')
136144
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
137145
const { parent: { configFile }, envId } = options;
@@ -156,7 +164,8 @@ commander_1.default
156164
]);
157165
utils_1.printHorizontalTable(head, tableData);
158166
}
159-
catch (error) {
160-
loading.fail('获取文件列表失败');
167+
catch (e) {
168+
loading.fail('获取文件列表失败!');
169+
console.log(e.message);
161170
}
162171
}));

lib/hosting.js

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
1515
const path_1 = __importDefault(require("path"));
1616
const manager_node_1 = __importDefault(require("@cloudbase/manager-node"));
1717
const utils_1 = require("./utils");
18-
const storage_1 = require("./storage");
1918
const error_1 = require("./error");
2019
const utils_2 = require("./utils");
2120
function getStorageService(envId) {
@@ -31,21 +30,16 @@ function getStorageService(envId) {
3130
return app.storage;
3231
});
3332
}
33+
const HostingStatusMap = {
34+
init: '初始化中',
35+
process: '处理中',
36+
online: '上线',
37+
destroying: '销毁中',
38+
offline: '下线',
39+
create_fail: '初始化失败',
40+
destroy_fail: '销毁失败'
41+
};
3442
const tcbService = new utils_1.CloudApiService('tcb');
35-
function enableHosting(options) {
36-
return __awaiter(this, void 0, void 0, function* () {
37-
const { envId } = options;
38-
const res = yield tcbService.request('CreateStaticStore', {
39-
EnvId: envId
40-
});
41-
const code = res.Result === 'succ' ? 0 : -1;
42-
return {
43-
code,
44-
requestId: res.RequestId
45-
};
46-
});
47-
}
48-
exports.enableHosting = enableHosting;
4943
function getHostingInfo(options) {
5044
return __awaiter(this, void 0, void 0, function* () {
5145
const { envId } = options;
@@ -61,34 +55,69 @@ function checkHostingStatus(envId) {
6155
return __awaiter(this, void 0, void 0, function* () {
6256
const hostings = yield getHostingInfo({ envId });
6357
if (!hostings.data || !hostings.data.length) {
64-
throw new error_1.CloudBaseError('静态托管服务未开启!', {
58+
throw new error_1.CloudBaseError('静态网站服务未开启!', {
6559
code: 'INVALID_OPERATION'
6660
});
6761
}
68-
return hostings;
62+
const website = hostings.data[0];
63+
if (website.status !== 'online') {
64+
throw new error_1.CloudBaseError(`静态网站服务【${HostingStatusMap[website.status]}】,请稍后重试!`, {
65+
code: 'INVALID_OPERATION'
66+
});
67+
}
68+
return website;
6969
});
7070
}
71-
function destroyHosting(options) {
71+
function enableHosting(options) {
7272
return __awaiter(this, void 0, void 0, function* () {
7373
const { envId } = options;
74-
const files = yield storage_1.list({
75-
envId,
76-
cloudPath: ''
77-
});
7874
const hostings = yield getHostingInfo(options);
79-
if (!hostings.data || !hostings.data.length) {
80-
throw new error_1.CloudBaseError('静态托管服务未开启!', {
75+
if (hostings.data && hostings.data.length) {
76+
const website = hostings.data[0];
77+
if (website.status !== 'destroy' || website.status !== 'destroy_fail') {
78+
throw new error_1.CloudBaseError('静态网站服务已开启!');
79+
}
80+
}
81+
const res = yield tcbService.request('CreateStaticStore', {
82+
EnvId: envId
83+
});
84+
const code = res.Result === 'succ' ? 0 : -1;
85+
return {
86+
code,
87+
requestId: res.RequestId
88+
};
89+
});
90+
}
91+
exports.enableHosting = enableHosting;
92+
function hostingList(options) {
93+
return __awaiter(this, void 0, void 0, function* () {
94+
const { envId } = options;
95+
const hosting = yield checkHostingStatus(envId);
96+
const { bucket, regoin } = hosting;
97+
const storageService = yield getStorageService(envId);
98+
const list = yield storageService.walkCloudDirCustom('', bucket, regoin);
99+
return list;
100+
});
101+
}
102+
exports.hostingList = hostingList;
103+
function destroyHosting(options) {
104+
return __awaiter(this, void 0, void 0, function* () {
105+
const { envId } = options;
106+
const files = yield hostingList(options);
107+
if (files && files.length) {
108+
throw new error_1.CloudBaseError('静态网站文件不为空,无法销毁!', {
81109
code: 'INVALID_OPERATION'
82110
});
83111
}
84-
const website = hostings.data[0];
85-
if (website.status !== 'online') {
86-
throw new error_1.CloudBaseError('静态托管服务状态异常,无法销毁!', {
112+
const hostings = yield getHostingInfo(options);
113+
if (!hostings.data || !hostings.data.length) {
114+
throw new error_1.CloudBaseError('静态网站服务未开启!', {
87115
code: 'INVALID_OPERATION'
88116
});
89117
}
90-
if (files.length) {
91-
throw new error_1.CloudBaseError('静态托管文件不为空,无法销毁!', {
118+
const website = hostings.data[0];
119+
if (website.status !== 'online' && website.status !== 'destroy_fail') {
120+
throw new error_1.CloudBaseError(`静态网站服务【${HostingStatusMap[website.status]}】,无法处理请求!`, {
92121
code: 'INVALID_OPERATION'
93122
});
94123
}
@@ -108,41 +137,30 @@ function hostingDeploy(options) {
108137
const { envId, filePath, cloudPath } = options;
109138
const resolvePath = path_1.default.resolve(filePath);
110139
utils_1.checkPathExist(resolvePath, true);
111-
const hostings = yield checkHostingStatus(envId);
112-
const { bucket, region } = hostings.data[0];
140+
const hosting = yield checkHostingStatus(envId);
141+
const { bucket, regoin } = hosting;
113142
const storageService = yield getStorageService(envId);
114143
if (utils_1.isDirectory(resolvePath)) {
115-
storageService.uploadDirectoryCustom(resolvePath, cloudPath, bucket, region);
144+
storageService.uploadDirectoryCustom(resolvePath, cloudPath, bucket, regoin);
116145
}
117146
else {
118-
storageService.uploadFileCustom(resolvePath, cloudPath, bucket, region);
147+
storageService.uploadFileCustom(resolvePath, cloudPath, bucket, regoin);
119148
}
120149
});
121150
}
122151
exports.hostingDeploy = hostingDeploy;
123152
function hostingDelete(options) {
124153
return __awaiter(this, void 0, void 0, function* () {
125154
const { envId, cloudPath, isDir } = options;
126-
const hostings = yield checkHostingStatus(envId);
127-
const { bucket, region } = hostings.data[0];
155+
const hosting = yield checkHostingStatus(envId);
156+
const { bucket, regoin } = hosting;
128157
const storageService = yield getStorageService(envId);
129158
if (isDir) {
130-
storageService.deleteDirectoryCustom(cloudPath, bucket, region);
159+
storageService.deleteDirectoryCustom(cloudPath, bucket, regoin);
131160
}
132161
else {
133-
storageService.deleteFileCustom([cloudPath], bucket, region);
162+
storageService.deleteFileCustom([cloudPath], bucket, regoin);
134163
}
135164
});
136165
}
137166
exports.hostingDelete = hostingDelete;
138-
function hostingList(options) {
139-
return __awaiter(this, void 0, void 0, function* () {
140-
const { envId } = options;
141-
const hostings = yield checkHostingStatus(envId);
142-
const { bucket, region } = hostings.data[0];
143-
const storageService = yield getStorageService(envId);
144-
const list = yield storageService.walkCloudDirCustom('', bucket, region);
145-
return list;
146-
});
147-
}
148-
exports.hostingList = hostingList;

lib/utils/cloud-api-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class CloudApiService {
128128
headers: {
129129
Host: new url_1.URL(this.url).host,
130130
'X-TC-Action': this.action,
131-
'X-TC-Region': process.env.TCB_Region || 'ap-shanghai',
131+
'X-TC-Region': process.env.TCB_REGION || 'ap-shanghai',
132132
'X-TC-Timestamp': timestamp,
133133
'X-TC-Version': this.version
134134
}

lib/utils/object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const lodash_1 = __importDefault(require("lodash"));
77
lodash_1.default.mixin({
8-
deeply: function (obj, mapper) {
8+
deep: function (obj, mapper) {
99
return mapper(lodash_1.default.mapValues(obj, function (v) {
1010
if (lodash_1.default.isPlainObject(v)) {
1111
return lodash_1.default.deep(v, mapper);

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"author": "",
2626
"license": "ISC",
2727
"dependencies": {
28-
"@cloudbase/manager-node": "^1.1.2",
28+
"@cloudbase/manager-node": "^1.2.2",
2929
"@sentry/node": "^5.7.1",
3030
"address": "^1.1.2",
3131
"archiver": "^3.1.1",

0 commit comments

Comments
 (0)