|
| 1 | +import Util from '@alicloud/tea-util'; |
| 2 | +import ROS20190910, { |
| 3 | + CreateStackRequest, |
| 4 | + CreateStackRequestParameters, |
| 5 | + CreateStackRequestTags, |
| 6 | + ListStacksRequest, |
| 7 | +} from '@alicloud/ros20190910'; |
| 8 | +import { Config } from '@alicloud/openapi-client'; |
| 9 | +import { ActionContext } from '../types'; |
| 10 | +import { printer } from './printer'; |
| 11 | + |
| 12 | +const client = new ROS20190910( |
| 13 | + new Config({ |
| 14 | + accessKeyId: process.env.ALIYUN_ACCESS_KEY_ID, |
| 15 | + accessKeySecret: process.env.ALIYUN_ACCESS_KEY_SECRET, |
| 16 | + regionId: process.env.ALIYUN_REGION, |
| 17 | + disableRollback: false, |
| 18 | + }), |
| 19 | +); |
| 20 | + |
| 21 | +const createStack = async (stackName: string, templateBody: unknown, context: ActionContext) => { |
| 22 | + const parameters = context.parameters?.map( |
| 23 | + (parameter) => |
| 24 | + new CreateStackRequestParameters({ |
| 25 | + parameterKey: Util.assertAsString(parameter.key), |
| 26 | + parameterValue: Util.assertAsString(parameter.value), |
| 27 | + }), |
| 28 | + ); |
| 29 | + |
| 30 | + const createStackRequest = new CreateStackRequest({ |
| 31 | + regionId: context.region, |
| 32 | + stackName, |
| 33 | + templateBody: JSON.stringify(templateBody), |
| 34 | + parameters, |
| 35 | + tags: context.tags?.map((tag) => new CreateStackRequestTags(tag)), |
| 36 | + }); |
| 37 | + |
| 38 | + console.log('createStackRequest:', createStackRequest); |
| 39 | + |
| 40 | + const response = await client.createStack(createStackRequest); |
| 41 | + console.log(`创建中,资源栈ID:${response.body?.stackId}`); |
| 42 | + return response.body?.stackId; |
| 43 | +}; |
| 44 | + |
| 45 | +const updateStack = async (stackName: string, templateBody: unknown, context: ActionContext) => { |
| 46 | + const parameters = context.parameters?.map( |
| 47 | + (parameter) => |
| 48 | + new CreateStackRequestParameters({ |
| 49 | + parameterKey: Util.assertAsString(parameter.key), |
| 50 | + parameterValue: Util.assertAsString(parameter.value), |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + const createStackRequest = new CreateStackRequest({ |
| 55 | + stackName, |
| 56 | + templateBody, |
| 57 | + parameters, |
| 58 | + }); |
| 59 | + |
| 60 | + const response = await client.updateStack(createStackRequest); |
| 61 | + console.log(`更新中,资源栈ID:${response.body?.stackId}`); |
| 62 | + return response.body?.stackId; |
| 63 | +}; |
| 64 | + |
| 65 | +const getStackByName = async (stackName: string, region: string) => { |
| 66 | + const result = await client.listStacks( |
| 67 | + new ListStacksRequest({ |
| 68 | + regionId: region, |
| 69 | + pageSize: 10, |
| 70 | + pageNumber: 1, |
| 71 | + stackName: [stackName], |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + if (result.statusCode === 200) { |
| 76 | + return result.body?.stacks?.[0]; |
| 77 | + } else { |
| 78 | + return null; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +export const rosStackDeploy = async ( |
| 83 | + stackName: string, |
| 84 | + templateBody: unknown, |
| 85 | + context: ActionContext, |
| 86 | +) => { |
| 87 | + const stackInfo = await getStackByName(stackName, context.region); |
| 88 | + if (stackInfo) { |
| 89 | + const { Status: stackStatus } = stackInfo; |
| 90 | + if (stackStatus?.indexOf('IN_PROGRESS') >= 0) { |
| 91 | + printer.error(`fail to update stack, because stack status is ${stackStatus}`); |
| 92 | + throw new Error(`fail to update stack, because stack status is ${stackStatus}`); |
| 93 | + } |
| 94 | + |
| 95 | + printer.info(`Update stack: ${stackName} deploying... `); |
| 96 | + return await updateStack(stackName, templateBody, context); |
| 97 | + } else { |
| 98 | + // create stack |
| 99 | + printer.info(`Create stack: ${stackName} deploying... `); |
| 100 | + return await createStack(stackName, templateBody, context); |
| 101 | + } |
| 102 | +}; |
0 commit comments