-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deployment action calling aliyun ok
Signed-off-by: seven <[email protected]>
- Loading branch information
Showing
11 changed files
with
513 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { deployStack } from '../stack'; | ||
import { printer } from '../common'; | ||
import { parseYaml } from '../iac'; | ||
import { constructActionContext } from '../common/actionContext'; | ||
|
||
export const deploy = (location?: string) => { | ||
export const deploy = async (stackName: string, options: { location: string }) => { | ||
const context = constructActionContext({ location: options.location }); | ||
printer.info(`Deploying stack context: ${JSON.stringify(context)}...`); | ||
printer.info('Validating yaml...'); | ||
const iac = parseYaml(location); | ||
const iac = parseYaml(context.iacLocation); | ||
printer.success('Yaml is valid! 🎉'); | ||
|
||
printer.info('Deploying stack...'); | ||
deployStack(iac); | ||
await deployStack(stackName, iac, context); | ||
|
||
printer.success('Stack deployed! 🎉'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { parseYaml } from '../iac'; | ||
import { printer } from '../common'; | ||
import { constructActionContext } from '../common/actionContext'; | ||
|
||
export const validate = (location?: string) => { | ||
parseYaml(location); | ||
const context = constructActionContext({ location }); | ||
parseYaml(context.iacLocation); | ||
printer.success('Yaml is valid! 🎉'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ActionContext } from '../types'; | ||
import path from 'node:path'; | ||
|
||
export const constructActionContext = (config?: { | ||
region?: string; | ||
account?: string; | ||
accessKeyId?: string; | ||
accessKeySecret?: string; | ||
securityToken?: string; | ||
location?: string; | ||
}): ActionContext => { | ||
return { | ||
region: | ||
config?.region ?? process.env.ROS_REGION_ID ?? process.env.ALIYUN_REGION ?? 'cn-hangzhou', | ||
accessKeyId: config?.accessKeyId ?? (process.env.ALIYUN_ACCESS_KEY_ID as string), | ||
accessKeySecret: config?.accessKeySecret ?? (process.env.ALIYUN_ACCESS_KEY_SECRET as string), | ||
securityToken: config?.securityToken ?? process.env.ALIYUN_SECURITY_TOKEN, | ||
iacLocation: (() => { | ||
const projectRoot = path.resolve(process.cwd()); | ||
return config?.location | ||
? path.resolve(projectRoot, config?.location) | ||
: path.resolve(projectRoot, 'serverless-insight.yml'); | ||
})(), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Util from '@alicloud/tea-util'; | ||
import ROS20190910, { | ||
CreateStackRequest, | ||
CreateStackRequestParameters, | ||
CreateStackRequestTags, | ||
ListStacksRequest, | ||
} from '@alicloud/ros20190910'; | ||
import { Config } from '@alicloud/openapi-client'; | ||
import { ActionContext } from '../types'; | ||
import { printer } from './printer'; | ||
|
||
const client = new ROS20190910( | ||
new Config({ | ||
accessKeyId: process.env.ALIYUN_ACCESS_KEY_ID, | ||
accessKeySecret: process.env.ALIYUN_ACCESS_KEY_SECRET, | ||
regionId: process.env.ALIYUN_REGION, | ||
disableRollback: false, | ||
}), | ||
); | ||
|
||
const createStack = async (stackName: string, templateBody: unknown, context: ActionContext) => { | ||
const parameters = context.parameters?.map( | ||
(parameter) => | ||
new CreateStackRequestParameters({ | ||
parameterKey: Util.assertAsString(parameter.key), | ||
parameterValue: Util.assertAsString(parameter.value), | ||
}), | ||
); | ||
|
||
const createStackRequest = new CreateStackRequest({ | ||
regionId: context.region, | ||
stackName, | ||
templateBody: JSON.stringify(templateBody), | ||
parameters, | ||
tags: context.tags?.map((tag) => new CreateStackRequestTags(tag)), | ||
}); | ||
|
||
console.log('createStackRequest:', createStackRequest); | ||
|
||
const response = await client.createStack(createStackRequest); | ||
console.log(`创建中,资源栈ID:${response.body?.stackId}`); | ||
return response.body?.stackId; | ||
}; | ||
|
||
const updateStack = async (stackName: string, templateBody: unknown, context: ActionContext) => { | ||
const parameters = context.parameters?.map( | ||
(parameter) => | ||
new CreateStackRequestParameters({ | ||
parameterKey: Util.assertAsString(parameter.key), | ||
parameterValue: Util.assertAsString(parameter.value), | ||
}), | ||
); | ||
|
||
const createStackRequest = new CreateStackRequest({ | ||
stackName, | ||
templateBody, | ||
parameters, | ||
}); | ||
|
||
const response = await client.updateStack(createStackRequest); | ||
console.log(`更新中,资源栈ID:${response.body?.stackId}`); | ||
return response.body?.stackId; | ||
}; | ||
|
||
const getStackByName = async (stackName: string, region: string) => { | ||
const result = await client.listStacks( | ||
new ListStacksRequest({ | ||
regionId: region, | ||
pageSize: 10, | ||
pageNumber: 1, | ||
stackName: [stackName], | ||
}), | ||
); | ||
|
||
if (result.statusCode === 200) { | ||
return result.body?.stacks?.[0]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
export const rosStackDeploy = async ( | ||
stackName: string, | ||
templateBody: unknown, | ||
context: ActionContext, | ||
) => { | ||
const stackInfo = await getStackByName(stackName, context.region); | ||
if (stackInfo) { | ||
const { Status: stackStatus } = stackInfo; | ||
if (stackStatus?.indexOf('IN_PROGRESS') >= 0) { | ||
printer.error(`fail to update stack, because stack status is ${stackStatus}`); | ||
throw new Error(`fail to update stack, because stack status is ${stackStatus}`); | ||
} | ||
|
||
printer.info(`Update stack: ${stackName} deploying... `); | ||
return await updateStack(stackName, templateBody, context); | ||
} else { | ||
// create stack | ||
printer.info(`Create stack: ${stackName} deploying... `); | ||
return await createStack(stackName, templateBody, context); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters