@cloudbase/cli 2.0.12 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/.history/.gitignore_20220624162613 +84 -0
  2. package/.history/.gitignore_20220627114054 +86 -0
  3. package/.history/package_20220711204706.json +88 -0
  4. package/.history/package_20220711204818.json +88 -0
  5. package/.history/package_20220712151717.json +88 -0
  6. package/.history/package_20220712212537.json +88 -0
  7. package/.history/package_20220722205058.json +88 -0
  8. package/.history/package_20220726110155.json +88 -0
  9. package/.history/package_20220726110201.json +88 -0
  10. package/.history/package_20220728110905.json +88 -0
  11. package/.history/package_20220728110936.json +88 -0
  12. package/.history/package_20220728111531.json +89 -0
  13. package/.history/package_20220728111810.json +89 -0
  14. package/.history/src/commands/lowcode/app_20220728111717.ts +52 -0
  15. package/{src/commands/lowcode/app.ts → .history/src/commands/lowcode/app_20220728112031.ts} +2 -2
  16. package/{src/index.ts → .history/src/index_20220624162613.ts} +0 -0
  17. package/.history/src/index_20220627113609.ts +15 -0
  18. package/.history/src/index_20220627114102.ts +13 -0
  19. package/.vscode/launch.json +10 -0
  20. package/bin/cloudbase.js +0 -0
  21. package/bin/tcb.js +5 -5
  22. package/lib/commands/account/login.js +3 -6
  23. package/lib/commands/env/index.js +0 -1
  24. package/lib/commands/lowcode/app.js +1 -1
  25. package/lib/commands/run/index.js +0 -1
  26. package/lib/commands/run/service/deploy.js +6 -1
  27. package/lib/commands/run/service/index.js +0 -1
  28. package/lib/commands/run/service/update.js +4 -0
  29. package/lib/constant.js +15 -1
  30. package/lib/env/index.js +1 -11
  31. package/lib/run/index.js +0 -1
  32. package/lib/run/service/common.js +20 -7
  33. package/lib/run/service/config.js +2 -3
  34. package/lib/run/service/deployPackage.js +2 -9
  35. package/lib/run/service/index.js +0 -1
  36. package/lib/run/service/update.js +1 -1
  37. package/lib/utils/debug-logger.js +30 -0
  38. package/lib/utils/net/cloud-api-request.js +7 -1
  39. package/lib/utils/net/http-request.js +16 -5
  40. package/package.json +2 -2
  41. package/types/commands/env/index.d.ts +0 -1
  42. package/types/commands/run/index.d.ts +0 -1
  43. package/types/commands/run/service/index.d.ts +0 -1
  44. package/types/env/index.d.ts +0 -6
  45. package/types/run/index.d.ts +0 -1
  46. package/types/run/service/common.d.ts +8 -1
  47. package/types/run/service/index.d.ts +0 -1
  48. package/types/types.d.ts +4 -0
  49. package/types/utils/debug-logger.d.ts +1 -0
  50. package/types/utils/net/http-request.d.ts +6 -2
  51. package/lib/commands/env/create.js +0 -115
  52. package/lib/commands/run/create.js +0 -162
  53. package/lib/commands/run/service/create.js +0 -144
  54. package/lib/run/create.js +0 -19
  55. package/lib/run/service/create.js +0 -67
  56. package/src/auth/index.ts +0 -2
  57. package/src/auth/login.ts +0 -91
  58. package/src/auth/logout.ts +0 -7
  59. package/src/commands/account/index.ts +0 -2
  60. package/src/commands/account/login.ts +0 -192
  61. package/src/commands/account/logout.ts +0 -24
  62. package/src/commands/common.ts +0 -246
  63. package/src/commands/env/base.ts +0 -90
  64. package/src/commands/env/create.ts +0 -92
  65. package/src/commands/env/domain.ts +0 -186
  66. package/src/commands/env/index.ts +0 -4
  67. package/src/commands/env/login.ts +0 -235
  68. package/src/commands/framework/index.ts +0 -124
  69. package/src/commands/functions/alias/getRoute.ts +0 -76
  70. package/src/commands/functions/alias/index.ts +0 -2
  71. package/src/commands/functions/alias/setRoute.ts +0 -82
  72. package/src/commands/functions/code-download.ts +0 -100
  73. package/src/commands/functions/code-update.ts +0 -62
  74. package/src/commands/functions/concurrency/delete.ts +0 -45
  75. package/src/commands/functions/concurrency/index.ts +0 -3
  76. package/src/commands/functions/concurrency/list.ts +0 -58
  77. package/src/commands/functions/concurrency/set.ts +0 -47
  78. package/src/commands/functions/config-update.ts +0 -76
  79. package/src/commands/functions/copy.ts +0 -62
  80. package/src/commands/functions/delete.ts +0 -79
  81. package/src/commands/functions/deploy.ts +0 -293
  82. package/src/commands/functions/detail.ts +0 -138
  83. package/src/commands/functions/index.ts +0 -16
  84. package/src/commands/functions/invoke.ts +0 -121
  85. package/src/commands/functions/layer/bind.ts +0 -182
  86. package/src/commands/functions/layer/common.ts +0 -8
  87. package/src/commands/functions/layer/create.ts +0 -49
  88. package/src/commands/functions/layer/delete.ts +0 -73
  89. package/src/commands/functions/layer/download.ts +0 -92
  90. package/src/commands/functions/layer/index.ts +0 -7
  91. package/src/commands/functions/layer/list.ts +0 -94
  92. package/src/commands/functions/layer/sort.ts +0 -76
  93. package/src/commands/functions/list.ts +0 -68
  94. package/src/commands/functions/log.ts +0 -148
  95. package/src/commands/functions/run.ts +0 -249
  96. package/src/commands/functions/trigger-create.ts +0 -79
  97. package/src/commands/functions/trigger-delete.ts +0 -105
  98. package/src/commands/functions/version/index.ts +0 -2
  99. package/src/commands/functions/version/list.ts +0 -73
  100. package/src/commands/functions/version/publish.ts +0 -43
  101. package/src/commands/gateway/create.ts +0 -109
  102. package/src/commands/gateway/delete.ts +0 -81
  103. package/src/commands/gateway/domain.ts +0 -159
  104. package/src/commands/gateway/index.ts +0 -5
  105. package/src/commands/gateway/list.ts +0 -76
  106. package/src/commands/gateway/switch.ts +0 -107
  107. package/src/commands/helpers/index.ts +0 -3
  108. package/src/commands/helpers/init.ts +0 -431
  109. package/src/commands/helpers/new.ts +0 -117
  110. package/src/commands/helpers/open.ts +0 -67
  111. package/src/commands/hosting/hosting.ts +0 -360
  112. package/src/commands/hosting/index.ts +0 -1
  113. package/src/commands/index.ts +0 -13
  114. package/src/commands/lowcode/comps.ts +0 -325
  115. package/src/commands/lowcode/index.ts +0 -2
  116. package/src/commands/lowcode/utils.ts +0 -25
  117. package/src/commands/run/create.ts +0 -153
  118. package/src/commands/run/delete.ts +0 -77
  119. package/src/commands/run/image/common.ts +0 -8
  120. package/src/commands/run/image/delete.ts +0 -68
  121. package/src/commands/run/image/download.ts +0 -68
  122. package/src/commands/run/image/index.ts +0 -5
  123. package/src/commands/run/image/list.ts +0 -84
  124. package/src/commands/run/image/upload.ts +0 -76
  125. package/src/commands/run/index.ts +0 -7
  126. package/src/commands/run/list.ts +0 -83
  127. package/src/commands/run/service/config.ts +0 -81
  128. package/src/commands/run/service/create.ts +0 -122
  129. package/src/commands/run/service/deploy.ts +0 -125
  130. package/src/commands/run/service/index.ts +0 -5
  131. package/src/commands/run/service/list.ts +0 -94
  132. package/src/commands/run/service/update.ts +0 -108
  133. package/src/commands/run/standalonegateway/common.ts +0 -8
  134. package/src/commands/run/standalonegateway/create.ts +0 -85
  135. package/src/commands/run/standalonegateway/destroy.ts +0 -59
  136. package/src/commands/run/standalonegateway/index.ts +0 -5
  137. package/src/commands/run/standalonegateway/list.ts +0 -53
  138. package/src/commands/run/standalonegateway/package.ts +0 -62
  139. package/src/commands/run/standalonegateway/turn.ts +0 -63
  140. package/src/commands/run/version/common.ts +0 -8
  141. package/src/commands/run/version/create.ts +0 -369
  142. package/src/commands/run/version/delete.ts +0 -64
  143. package/src/commands/run/version/index.ts +0 -5
  144. package/src/commands/run/version/list.ts +0 -91
  145. package/src/commands/run/version/modify.ts +0 -137
  146. package/src/commands/run/version/update.ts +0 -398
  147. package/src/commands/smart.ts +0 -132
  148. package/src/commands/storage/index.ts +0 -1
  149. package/src/commands/storage/storage.ts +0 -464
  150. package/src/commands/third/index.ts +0 -1
  151. package/src/commands/third/thirdAttach.ts +0 -49
  152. package/src/completion/index.ts +0 -13
  153. package/src/constant.ts +0 -154
  154. package/src/decorators/captureError.ts +0 -25
  155. package/src/decorators/constants.ts +0 -12
  156. package/src/decorators/deprecate.ts +0 -25
  157. package/src/decorators/guard.ts +0 -42
  158. package/src/decorators/index.ts +0 -7
  159. package/src/decorators/injectParams.ts +0 -54
  160. package/src/decorators/params/common.ts +0 -28
  161. package/src/decorators/params/index.ts +0 -35
  162. package/src/env/domain.ts +0 -33
  163. package/src/env/index.ts +0 -63
  164. package/src/env/login.ts +0 -80
  165. package/src/error.ts +0 -36
  166. package/src/function/alias.ts +0 -43
  167. package/src/function/base.ts +0 -253
  168. package/src/function/code.ts +0 -55
  169. package/src/function/concurrency.ts +0 -57
  170. package/src/function/create.ts +0 -78
  171. package/src/function/delete.ts +0 -42
  172. package/src/function/index.ts +0 -11
  173. package/src/function/layer/attach.ts +0 -68
  174. package/src/function/layer/create.ts +0 -63
  175. package/src/function/layer/delete.ts +0 -21
  176. package/src/function/layer/download.ts +0 -54
  177. package/src/function/layer/index.ts +0 -7
  178. package/src/function/layer/list.ts +0 -32
  179. package/src/function/layer/sort.ts +0 -24
  180. package/src/function/trigger.ts +0 -97
  181. package/src/function/update.ts +0 -35
  182. package/src/function/version.ts +0 -39
  183. package/src/function/vpc.ts +0 -22
  184. package/src/gateway/index.ts +0 -137
  185. package/src/help.ts +0 -50
  186. package/src/hosting.ts +0 -212
  187. package/src/logger.ts +0 -17
  188. package/src/run/create.ts +0 -23
  189. package/src/run/delete.ts +0 -15
  190. package/src/run/image/build.ts +0 -36
  191. package/src/run/image/delete.ts +0 -14
  192. package/src/run/image/index.ts +0 -4
  193. package/src/run/image/info.ts +0 -27
  194. package/src/run/image/list.ts +0 -23
  195. package/src/run/index.ts +0 -8
  196. package/src/run/list.ts +0 -30
  197. package/src/run/repo.ts +0 -25
  198. package/src/run/service/common.ts +0 -311
  199. package/src/run/service/config.ts +0 -77
  200. package/src/run/service/create.ts +0 -52
  201. package/src/run/service/deployPackage.ts +0 -65
  202. package/src/run/service/index.ts +0 -7
  203. package/src/run/service/list.ts +0 -29
  204. package/src/run/service/showLogs.ts +0 -98
  205. package/src/run/service/update.ts +0 -82
  206. package/src/run/standalonegateway/create.ts +0 -24
  207. package/src/run/standalonegateway/destroy.ts +0 -19
  208. package/src/run/standalonegateway/index.ts +0 -5
  209. package/src/run/standalonegateway/list.ts +0 -74
  210. package/src/run/standalonegateway/package/index.ts +0 -1
  211. package/src/run/standalonegateway/package/list.ts +0 -24
  212. package/src/run/standalonegateway/turn/index.ts +0 -2
  213. package/src/run/standalonegateway/turn/off.ts +0 -19
  214. package/src/run/standalonegateway/turn/on.ts +0 -19
  215. package/src/run/version/create.ts +0 -68
  216. package/src/run/version/delete.ts +0 -16
  217. package/src/run/version/index.ts +0 -6
  218. package/src/run/version/list.ts +0 -17
  219. package/src/run/version/modify.ts +0 -17
  220. package/src/run/version/repo.ts +0 -28
  221. package/src/run/version/update.ts +0 -59
  222. package/src/storage.ts +0 -114
  223. package/src/third/index.ts +0 -13
  224. package/src/types.ts +0 -672
  225. package/src/utils/auth.ts +0 -15
  226. package/src/utils/checkTcbrEnv.ts +0 -69
  227. package/src/utils/cli-table.ts +0 -23
  228. package/src/utils/commonParamsCheck.ts +0 -65
  229. package/src/utils/config.ts +0 -39
  230. package/src/utils/env.ts +0 -244
  231. package/src/utils/fs/del.ts +0 -5
  232. package/src/utils/fs/index.ts +0 -71
  233. package/src/utils/function-packer.ts +0 -97
  234. package/src/utils/index.ts +0 -25
  235. package/src/utils/log.ts +0 -82
  236. package/src/utils/net/cloud-api-request.ts +0 -62
  237. package/src/utils/net/credential.ts +0 -53
  238. package/src/utils/net/http-request.ts +0 -84
  239. package/src/utils/net/index.ts +0 -5
  240. package/src/utils/net/manager-service.ts +0 -36
  241. package/src/utils/net/proxy.ts +0 -6
  242. package/src/utils/notice.ts +0 -28
  243. package/src/utils/output/highlight.ts +0 -5
  244. package/src/utils/output/index.ts +0 -3
  245. package/src/utils/output/link.ts +0 -10
  246. package/src/utils/output/loading.ts +0 -82
  247. package/src/utils/parallel.ts +0 -82
  248. package/src/utils/platform/index.ts +0 -3
  249. package/src/utils/platform/mac.ts +0 -21
  250. package/src/utils/platform/os.ts +0 -64
  251. package/src/utils/platform/port.ts +0 -10
  252. package/src/utils/progress-bar.ts +0 -38
  253. package/src/utils/prompt/index.ts +0 -1
  254. package/src/utils/prompt/select.ts +0 -60
  255. package/src/utils/reporter/agree.ts +0 -20
  256. package/src/utils/reporter/download.ts +0 -26
  257. package/src/utils/reporter/index.ts +0 -3
  258. package/src/utils/reporter/usage.ts +0 -20
  259. package/src/utils/store/auth.ts +0 -49
  260. package/src/utils/store/common.ts +0 -8
  261. package/src/utils/store/db.ts +0 -68
  262. package/src/utils/store/index.ts +0 -5
  263. package/src/utils/store/usage.ts +0 -12
  264. package/src/utils/tcbrApi/callTcbrApi.ts +0 -28
  265. package/src/utils/tcbrApi/index.ts +0 -1
  266. package/src/utils/template.ts +0 -170
  267. package/src/utils/tools/encoding.ts +0 -8
  268. package/src/utils/tools/index.ts +0 -4
  269. package/src/utils/tools/object.ts +0 -33
  270. package/src/utils/tools/time.ts +0 -38
  271. package/src/utils/tools/uid.ts +0 -19
  272. package/src/utils/validator.ts +0 -65
  273. package/types/commands/env/create.d.ts +0 -12
  274. package/types/commands/run/create.d.ts +0 -13
  275. package/types/commands/run/service/create.d.ts +0 -13
  276. package/types/run/create.d.ts +0 -2
  277. package/types/run/service/create.d.ts +0 -7
@@ -1,311 +0,0 @@
1
- import { CloudApiService, parseOptionalParams, parseInputParam, callTcbrApi, genClickableLink } from '../../utils'
2
- import { ITcbrServiceOptions, IDescribeWxCloudBaseRunReleaseOrder, ITcbrServiceRequiredOptions, DEPLOY_TYPE, IAuthorizedTcrInstance } from '../../types'
3
- import { CloudBaseError } from '@cloudbase/toolbox'
4
- import { packageDeploy } from './index'
5
- import { listImage } from '..'
6
- import { DEFAULT_CPU_MEM_SET } from '../../constant'
7
-
8
- const tcbService = CloudApiService.getInstance('tcb')
9
- const tcrCloudApiService = new CloudApiService('tcr', {}, '2019-09-24')
10
-
11
- export async function describeWxCloudBaseRunReleaseOrder(options: IDescribeWxCloudBaseRunReleaseOrder) {
12
- const res = await tcbService.request('DescribeWxCloudBaseRunReleaseOrder', {
13
- EnvId: options.envId,
14
- ServerName: options.serviceName
15
- })
16
- return res
17
- }
18
-
19
- export const convertNumber = (item) => {
20
- if (isNaN(Number(item))) {
21
- throw new CloudBaseError(`${item} 必须为数字`)
22
- }
23
- return Number(item)
24
- }
25
-
26
- export const extractPolicyDetails = (policyDetails: string) => {
27
- return policyDetails.split('&').map(condition => {
28
- let [type, threshold] = [condition.split('=')[0], Number(condition.split('=')[1])]
29
- if (isNaN(threshold) || !['cpu', 'mem'].includes(type)) {
30
- throw new CloudBaseError('请输入合法的缩扩容配置')
31
- }
32
- return {
33
- PolicyType: type,
34
- PolicyThreshold: threshold
35
- }
36
- })
37
- }
38
-
39
- export const parseEnvParams = (envParams: string) => {
40
- return envParams.split('&').reduce((acc, cur) => {
41
- const [key, value] = cur.split('=')
42
- acc[key] = value
43
- return acc
44
- }, {})
45
- }
46
-
47
- /**
48
- * @description 合并当前与已有的环境变量,当前传入的环境变量 key 去重并取位置靠后的,如与原有环境变量 key 相同,则替换
49
- * @param curEnvParams 当前输入的环境变量
50
- * @param preEnvParams 已有的环境变量
51
- * @returns
52
- */
53
- export const mergeEnvParams = (curEnvParams: string, preEnvParams: string) => {
54
- const curEnv = parseEnvParams(curEnvParams)
55
- const preEnv = preEnvParams ? JSON.parse(preEnvParams) : {}
56
- const curEnvKeys = Object.keys(curEnv)
57
- Object.entries(preEnv).forEach(([key, value]) => {
58
- if (!curEnvKeys.includes(key)) {
59
- curEnv[key] = value
60
- }
61
- })
62
- return JSON.stringify(curEnv)
63
- }
64
-
65
- function checkRequiredParams(options: ITcbrServiceRequiredOptions) {
66
- if (!options.envId) {
67
- throw new CloudBaseError('必须使用 -e 或 --envId 指定环境ID')
68
- }
69
-
70
- if (!options.serviceName) {
71
- throw new CloudBaseError('必须使用 -s 或 --serviceName 指定服务名')
72
- }
73
-
74
- if (!options.containerPort) {
75
- throw new CloudBaseError('必须使用 --containerPort 指定监听端口号')
76
- }
77
-
78
- if (!options.isCreated && !options.path && !options.custom_image) {
79
- throw new CloudBaseError('请使用 --path 指定代码根目录或 --custom_image 指定 TCR 镜像 URL')
80
- }
81
-
82
- if (options.isCreated && !options.path && !options.custom_image && !options.library_image && !options.image) {
83
- throw new CloudBaseError('请使用 --path 指定代码根目录或 --custom_image 指定 TCR 镜像 URL 或 --library_image 指定线上镜像 tag ')
84
- }
85
- }
86
-
87
- /**
88
- *
89
- * @param options 原始用户输入
90
- * @param isCreated 服务是否已被创建(未创建的服务不能使用镜像 tag )
91
- * @param defaultOverride 是否默认用上次的服务配置替代缺省值
92
- * @param previousServerConfig 上次的服务配置
93
- * @returns
94
- */
95
- export async function tcbrServiceOptions(options: ITcbrServiceOptions, isCreated: boolean, defaultOverride?: boolean, previousServerConfig?) {
96
- let {
97
- noConfirm: _noConfirm = false,
98
- override: _override = (defaultOverride || false),
99
- envId,
100
- serviceName,
101
- path,
102
- cpu,
103
- mem,
104
- minNum,
105
- maxNum,
106
- policyDetails,
107
- customLogs,
108
- envParams,
109
- containerPort,
110
- remark,
111
- targetDir,
112
- dockerfile,
113
- image,
114
- custom_image,
115
- library_image,
116
- json: _json = false
117
- } = options
118
-
119
- // 检查必选参数是否填写
120
- checkRequiredParams({
121
- envId,
122
- serviceName,
123
- containerPort,
124
- isCreated,
125
- path,
126
- custom_image,
127
- library_image,
128
- image
129
- })
130
- containerPort = Number(containerPort)
131
-
132
- // 处理用户传入参数
133
- const deployByImage = Boolean(custom_image || library_image || image)
134
- const DeployInfo: any = {
135
- DeployType: deployByImage ? DEPLOY_TYPE.IMAGE : DEPLOY_TYPE.PACKAGE,
136
- DeployRemark: remark || '',
137
- ReleaseType: 'FULL'
138
- }
139
-
140
- // 可选参数进行校验和转换
141
- let {
142
- cpuConverted,
143
- memConverted,
144
- maxNumConverted,
145
- minNumConverted
146
- } = parseOptionalParams({
147
- cpu,
148
- mem,
149
- maxNum,
150
- minNum
151
- })
152
-
153
- const defaultLogType = 'none'
154
- const newServiceOptions = {
155
- ServerName: serviceName,
156
- EnvId: envId,
157
- ServerConfig: {
158
- EnvId: envId,
159
- MaxNum: parseInputParam(maxNumConverted, _override, convertNumber, previousServerConfig?.MaxNum, 50),
160
- MinNum: parseInputParam(minNumConverted, _override, convertNumber, previousServerConfig?.MinNum, 0),
161
- BuildDir: parseInputParam(targetDir, _override, null, previousServerConfig?.BuildDir, '.'),
162
- Cpu: parseInputParam(cpuConverted, _override, null, previousServerConfig?.Cpu, 0.5),
163
- Mem: parseInputParam(memConverted, _override, null, previousServerConfig?.Mem, 1),
164
- OpenAccessTypes: ['PUBLIC'],
165
- ServerName: serviceName,
166
- InitialDelaySeconds: 2,
167
- CustomLogs: parseInputParam(customLogs, _override, null, previousServerConfig?.CustomLogs, 'stdout'),
168
- CreateTime: previousServerConfig?.CreateTime || (new Date()).toLocaleString().replace(/\//g, '-'),
169
- PolicyDetails: parseInputParam(policyDetails, _override, extractPolicyDetails, previousServerConfig?.PolicyDetails, DEFAULT_CPU_MEM_SET),
170
- EnvParams: parseInputParam(envParams, _override, mergeEnvParams, previousServerConfig?.EnvParams, '', previousServerConfig?.EnvParams),
171
- Port: containerPort,
172
- HasDockerfile: true,
173
- Dockerfile: dockerfile || 'Dockerfile',
174
- LogType: defaultLogType
175
- },
176
- DeployInfo: {
177
- ...DeployInfo
178
- }
179
- }
180
-
181
- if (DeployInfo.DeployType === DEPLOY_TYPE.PACKAGE) {
182
- // 本地上传
183
- const { PackageName, PackageVersion } = await packageDeploy({
184
- envId,
185
- serviceName,
186
- filePath: path,
187
- })
188
- DeployInfo.PackageName = PackageName
189
- DeployInfo.PackageVersion = PackageVersion
190
- } else if (DeployInfo.DeployType === DEPLOY_TYPE.IMAGE) {
191
- // 传入 tcr 镜像实例
192
- if (custom_image) {
193
- const authorizedTcrInstances = await getAuthorizedTcrInstance(envId)
194
- if (!authorizedTcrInstances) {
195
- const link = `https://console.cloud.tencent.com/tcbr/env?/tcbr/env=&envId=${envId}`
196
- throw new CloudBaseError(`您还未授权 tcr 实例,请先到控制台授权:${genClickableLink(link)}`)
197
- }
198
- // 检查传入的 URL 是否合法,不合法 throw error 阻止执行
199
- await validateTcrImageURL(authorizedTcrInstances, custom_image)
200
-
201
- DeployInfo.ImageUrl = custom_image
202
- } else {
203
- // 拉取镜像
204
- const imageList = await listImage({
205
- envId,
206
- serviceName
207
- })
208
- if (library_image) {
209
- const imageInfo = imageList.find(({ Tag }) => Tag === library_image)
210
- if (!imageInfo) {
211
- throw new CloudBaseError(`镜像 ${library_image} 不存在`)
212
- }
213
- DeployInfo.ImageUrl = imageInfo.ImageUrl
214
- } else {
215
- // 暂时不支持 image 镜像
216
- throw new CloudBaseError('暂不支持 --image 参数,请使用 --custom_image 指定 tcr 镜像 URL 或 --library_image 指定线上镜像 tag')
217
- }
218
- }
219
- }
220
-
221
- newServiceOptions.DeployInfo = {
222
- ...DeployInfo
223
- }
224
- return newServiceOptions
225
- }
226
-
227
- // 获取授权 tcr 实例
228
- export async function getAuthorizedTcrInstance(envId: string): Promise<IAuthorizedTcrInstance[] | null> {
229
- let { data: { TcrInstances: authorizedTcrInstances } } = await callTcbrApi('DescribeTcrInstances', {
230
- EnvId: envId
231
- })
232
- return authorizedTcrInstances
233
- }
234
-
235
- /**
236
- *
237
- * @description 校验输入的 tcr 镜像 URL 是否合法(域名/仓库名:镜像tag)
238
- * @param authorizedTcrInstances 已授权的 tcr 实例列表
239
- * @param imageUrl 输入的镜像 URL 地址
240
- */
241
- export async function validateTcrImageURL(authorizedTcrInstances: IAuthorizedTcrInstance[] | null, imageUrl: string) {
242
- const errMsg = '镜像URL解析失败,请检查输入的镜像URL是否正确'
243
- try {
244
- const host = imageUrl.split('/')[0]
245
- const namespace = imageUrl.split('/')[1]
246
- const name = `${namespace}/${imageUrl.split('/')[2].split(':')[0]}`
247
- const tag = imageUrl.split('/')[2].split(':')[1]
248
- // 获取授权实例,校验域名
249
- const filteredInstances = authorizedTcrInstances?.filter(({ Domain }) => host === Domain)
250
-
251
- if (!filteredInstances?.length) {
252
- throw new CloudBaseError(errMsg)
253
- }
254
- // 获取实例中仓库,校验仓库名
255
- const reposUnderSpecifiedRegistry = []
256
- for (const registry of filteredInstances) {
257
- const repos = []
258
- let { Id: registryId, Domain: domain } = registry
259
- const limit = 100
260
- let curIndex = 1
261
- let totalCount = 0
262
- do {
263
- const rsp = await tcrCloudApiService.request('DescribeRepositories', {
264
- RegistryId: registryId,
265
- Offset: curIndex,
266
- Limit: limit
267
- })
268
- repos.push(...rsp.RepositoryList)
269
- curIndex += 1
270
- totalCount = rsp.TotalCount
271
- } while (repos.length < totalCount)
272
- reposUnderSpecifiedRegistry.push({ registryId, domain, repos })
273
- }
274
-
275
- const filteredRepos = []
276
- for (const repo of reposUnderSpecifiedRegistry) {
277
- const { registryId, repos } = repo
278
- filteredRepos.push(...repos.filter(({ Name }) => Name === name))
279
- if (!filteredRepos?.length) {
280
- throw new CloudBaseError(errMsg)
281
- }
282
- filteredRepos.forEach(item => { item.registryId = registryId }) // 手动插入实例id,获取镜像接口用
283
- }
284
- // 获取仓库内镜像,校验tag
285
- for (const repoItem of filteredRepos) {
286
- const { Name, Namespace, registryId } = repoItem
287
- const images = []
288
- const limit = 100
289
- let curIndex = 1
290
- let totalCount = 0
291
- do {
292
- const rsp = await tcrCloudApiService.request('DescribeImages', {
293
- RegistryId: registryId,
294
- NamespaceName: Namespace,
295
- RepositoryName: Name.split(`${Namespace}/`)[1],
296
- Offset: curIndex,
297
- Limit: limit
298
- })
299
- images.push(...rsp.ImageInfoList)
300
- curIndex += 1
301
- totalCount = rsp.TotalCount
302
- } while (images.length < totalCount)
303
-
304
- if (!images.some(({ ImageVersion }) => ImageVersion === tag)) {
305
- throw new CloudBaseError(errMsg)
306
- }
307
- }
308
- } catch (e) {
309
- throw new CloudBaseError(errMsg)
310
- }
311
- }
@@ -1,77 +0,0 @@
1
- import { describeCloudRunServerDetail } from './create'
2
- import { ITcbrServiceConfigOptions, IDescribeCloudRunServerDetail } from '../../types'
3
- import { CloudBaseError } from '@cloudbase/toolbox'
4
- import { mergeEnvParams, extractPolicyDetails } from './common'
5
- import { callTcbrApi, parseOptionalParams } from '../../utils'
6
-
7
- export async function tcbrServiceConfigOptions(options: ITcbrServiceConfigOptions) {
8
- let {
9
- serviceName,
10
- envId,
11
- cpu,
12
- mem,
13
- minNum,
14
- maxNum,
15
- policyDetails,
16
- customLogs,
17
- envParams,
18
- } = options
19
-
20
- if (!envId) {
21
- throw new CloudBaseError('必须使用 -e 或 --envId 指定环境ID')
22
- }
23
-
24
- if (!serviceName) {
25
- throw new CloudBaseError('必须使用 -s 或 --serviceName 指定服务名')
26
- }
27
-
28
- let {
29
- cpuConverted,
30
- memConverted,
31
- maxNumConverted,
32
- minNumConverted
33
- } = parseOptionalParams({
34
- cpu,
35
- mem,
36
- maxNum,
37
- minNum
38
- })
39
-
40
- const serviceInfo = await describeCloudRunServerDetail({
41
- envId,
42
- serviceName
43
- })
44
-
45
- if(serviceInfo instanceof Error && serviceInfo['code'] === 'InvalidParameter') {
46
- throw new CloudBaseError('服务不存在,请检查服务名是否正确或用 tcb run service:create 创建服务')
47
- }
48
-
49
- const { ServerConfig: previousServerConfig } = serviceInfo.data as IDescribeCloudRunServerDetail
50
-
51
- const newServiceOptions = {
52
- EnvId: envId,
53
- ServerName: serviceName,
54
- OpenAccessTypes: previousServerConfig.OpenAccessTypes,
55
- Cpu: cpuConverted || previousServerConfig.Cpu,
56
- Mem: memConverted || previousServerConfig.Mem,
57
- MinNum: minNumConverted || previousServerConfig.MinNum,
58
- MaxNum: maxNumConverted || previousServerConfig.MaxNum,
59
- PolicyDetails: policyDetails ? extractPolicyDetails(policyDetails) : previousServerConfig.PolicyDetails,
60
- CustomLogs: customLogs || previousServerConfig.CustomLogs,
61
- EnvParams: envParams ? mergeEnvParams(envParams, previousServerConfig?.EnvParams) : previousServerConfig.EnvParams,
62
- InitialDelaySeconds: 2,
63
- CreateTime: previousServerConfig.CreateTime,
64
- Port: previousServerConfig.Port,
65
- HasDockerfile: true,
66
- Dockerfile: previousServerConfig.Dockerfile,
67
- BuildDir: previousServerConfig.BuildDir,
68
- }
69
- return newServiceOptions
70
- }
71
-
72
- export async function updateCloudRunServerConfig(options) {
73
- return await callTcbrApi('UpdateCloudRunServerConfig', {
74
- EnvId: options.envId,
75
- ServerBaseConfig: options.ServerBaseConfig,
76
- })
77
- }
@@ -1,52 +0,0 @@
1
- import { callTcbrApi } from '../../utils'
2
- import { getLogs, tcbrServiceOptions } from '.'
3
- import inquirer from 'inquirer'
4
- import { ITcbrServiceOptions } from '../../types'
5
- import { logger } from '@cloudbase/toolbox'
6
-
7
- export const describeCloudRunServerDetail = async (options: { envId: string; serviceName: string }) => {
8
- return await callTcbrApi('DescribeCloudRunServerDetail', {
9
- EnvId: options.envId,
10
- ServerName: options.serviceName,
11
- })
12
- }
13
-
14
- export async function createCloudRunServer(serviceConfigOptions: Record<string, any>) {
15
- return callTcbrApi('CreateCloudRunServer', serviceConfigOptions)
16
- }
17
-
18
- export async function createTcbrService(options: ITcbrServiceOptions) {
19
- const newServiceOptions = await tcbrServiceOptions(options, false, false)
20
-
21
- if (!options.noConfirm) {
22
- const { confirm } = await inquirer.prompt([
23
- {
24
- type: 'confirm',
25
- name: 'confirm',
26
- message: '确定要创建服务吗?',
27
- }
28
- ])
29
- if (!confirm) {
30
- return
31
- }
32
- }
33
-
34
- const createRes: any = await createCloudRunServer(newServiceOptions)
35
- const taskId = createRes.data?.TaskId
36
-
37
- if (options.json) {
38
- console.log(JSON.stringify(createRes, null, 2))
39
- }
40
-
41
- // `--verbose` 也用于全局指定参数,因此不能从 options 中获取到
42
- if (process.argv.includes('--verbose')) {
43
- await getLogs({
44
- envId: options.envId,
45
- taskId,
46
- serviceName: options.serviceName
47
- })
48
- console.log(`本次任务的 TaskID: ${taskId}`)
49
- } else {
50
- logger.success('创建服务成功, 本次任务的 TaskID: ' + taskId)
51
- }
52
- }
@@ -1,65 +0,0 @@
1
- import path from 'path'
2
- import * as fs from 'fs'
3
- import { fetch } from '../../utils'
4
- import { createBuild } from '..'
5
- import { CloudBaseError, execWithLoading, loadingFactory, zipDir } from '@cloudbase/toolbox'
6
-
7
- interface IPackageDeploy {
8
- envId: string,
9
- serviceName: string,
10
- filePath: string,
11
- fileToIgnore?: string | string[]
12
- }
13
-
14
- export async function packageDeploy(options: IPackageDeploy) {
15
- let {
16
- envId,
17
- serviceName,
18
- filePath,
19
- fileToIgnore
20
- } = options
21
- let { PackageName, PackageVersion, UploadUrl } = await createBuild({
22
- envId,
23
- serviceName
24
- })
25
-
26
- const loading = loadingFactory()
27
- const zipFile = `.tcbr_${serviceName}_${Date.now()}.zip`
28
- const dstPath = path.join(process.cwd(), zipFile)
29
-
30
- try {
31
- if(fs.statSync(filePath).isDirectory()) {
32
- loading.start('正在压缩文件…')
33
- await zipDir(filePath, dstPath, fileToIgnore)
34
- loading.succeed('压缩文件完成')
35
- }
36
- } catch (error) {
37
- if(error.code === 'ENOENT') {
38
- throw new CloudBaseError('找不到指定文件夹,请检查文件路径是否正确')
39
- } else {
40
- throw new CloudBaseError(error.message)
41
- }
42
- }
43
-
44
- try {
45
- return await execWithLoading(
46
- async () => {
47
- await fetch(UploadUrl, {
48
- method: 'PUT',
49
- body: fs.createReadStream(zipFile),
50
- headers: {
51
- 'content-type': 'application/zip'
52
- }
53
- })
54
- return { PackageName, PackageVersion }
55
- },
56
- {
57
- startTip: '\n正在上传代码包...',
58
- failTip: '上传代码包失败,请稍后重试'
59
- }
60
- )
61
- } finally {
62
- await fs.promises.unlink(dstPath)
63
- }
64
-
65
- }
@@ -1,7 +0,0 @@
1
- export * from './list'
2
- export * from './create'
3
- export * from './showLogs'
4
- export * from './deployPackage'
5
- export * from './common'
6
- export * from './update'
7
- export * from './config'
@@ -1,29 +0,0 @@
1
- import { IServerInfo } from '../../types'
2
- import { callTcbrApi, CloudApiService } from '../../utils'
3
-
4
- const tcbService = CloudApiService.getInstance('tcb')
5
-
6
- export const listService = async (options): Promise<IServerInfo[]> => {
7
- // 没有 CreateTime 因此需要调用两个接口
8
- const { data: { ServerList: serverList } } = await callTcbrApi('DescribeCloudRunServers', {
9
- EnvId: options.envId,
10
- })
11
-
12
- const { CloudBaseRunServerSet: serverSet} = await tcbService.request('DescribeCloudBaseRunServers', {
13
- EnvId: options.envId,
14
- Offset: 0,
15
- Limit: options.limit || 100
16
- })
17
-
18
- // 按照 ServerName 拼接结果
19
- if(!serverList.length) return []
20
- const serverInfo = serverList.map(serverItem => {
21
- return {
22
- ...serverItem,
23
- CreatedTime: serverSet.find( ( item ) => item.ServerName === serverItem.ServerName).CreatedTime
24
- }
25
- })
26
-
27
- return serverInfo
28
-
29
- }
@@ -1,98 +0,0 @@
1
- import { callTcbrApi } from '../../utils'
2
- import { EnumDeployStatus } from '../../constant'
3
- import chalk from 'chalk'
4
-
5
- export async function getBuildStatus(envId: string, serviceName: string) {
6
- const { data: deployRes } = await callTcbrApi('DescribeCloudRunDeployRecord', {
7
- EnvId: envId,
8
- ServerName: serviceName,
9
- })
10
- if (deployRes?.DeployRecords) {
11
- if (deployRes?.DeployRecords[0].Status === EnumDeployStatus.Deploying) {
12
- return Promise.resolve('pending')
13
- } else {
14
- return Promise.resolve('completed')
15
- }
16
- } else {
17
- return Promise.resolve('pending')
18
- }
19
- }
20
-
21
-
22
- async function getBuildId(envId: string, serviceName: string): Promise<number> {
23
- const { data: deployRes } = await callTcbrApi('DescribeCloudRunDeployRecord', {
24
- EnvId: envId,
25
- ServerName: serviceName,
26
- })
27
- if (deployRes?.DeployRecords) {
28
- if (deployRes.DeployRecords[0].Status !== 'deploying') {
29
- return Promise.resolve(deployRes.DeployRecords[0].BuildId)
30
- }
31
- }
32
- }
33
-
34
- async function getRunId(envId: string, serviceName: string) {
35
- return new Promise<string>((resolve) => {
36
- const timer = setInterval(async () => {
37
- const { data: deployRes } = await callTcbrApi('DescribeCloudRunDeployRecord', {
38
- EnvId: envId,
39
- ServerName: serviceName,
40
- })
41
- if (deployRes?.DeployRecords) {
42
- clearInterval(timer)
43
- resolve(deployRes.DeployRecords[0].RunId)
44
- }
45
- }, 3000)
46
- })
47
- }
48
-
49
-
50
- // 多次获取 processLog
51
- async function showProcessLogs(envId: string, runId: string, serviceName: string) {
52
- return new Promise<void>(resolve => {
53
- const timer = setInterval(async () => {
54
- if (await getBuildStatus(envId, serviceName) === 'completed') {
55
- clearInterval(timer)
56
- resolve()
57
- } else {
58
- const { data: processLogs } = await callTcbrApi('DescribeCloudRunProcessLog', {
59
- EnvId: envId,
60
- RunId: runId,
61
- })
62
- if (processLogs?.Logs) {
63
- console.log(processLogs?.Logs.join('\n'))
64
- }
65
- }
66
- }, 5000)
67
- })
68
- }
69
-
70
- // buildLog 仅在完成后获取一次(未完成 BuildId 为0)
71
- async function showBuildLogs(envId: string, serviceName: string, serverVersion = '', offset = 0) {
72
- const buildId = await getBuildId(envId, serviceName)
73
-
74
- const { data } = await callTcbrApi('DescribeCloudRunBuildLog', {
75
- EnvId: envId,
76
- BuildId: buildId,
77
- ServerName: serviceName,
78
- ServerVersion: serverVersion || '',
79
- Offset: offset || 0,
80
- })
81
-
82
- if (data?.Log?.Text) {
83
- console.log(data?.Log?.Text)
84
- }
85
- return Promise.resolve()
86
- }
87
-
88
- export async function getLogs(options) {
89
-
90
- const runId = await getRunId(options.envId, options.serviceName)
91
-
92
- console.log(chalk.blue('============ 日志开始 ==============='))
93
- await showProcessLogs(options.envId, runId, options.serviceName)
94
- if (await getBuildStatus(options.envId, options.serviceName) === 'completed') {
95
- await showBuildLogs(options.envId, options.serviceName)
96
- }
97
- console.log(chalk.blue('============ 日志结束 ==============='))
98
- }