Skip to content

Commit 110366f

Browse files
authored
feat: si-bootstrap stack created for holding the common resources (#24)
feat|fix|refactor: summary title of the PR - create new stack: `serverlessInsight-bootstrap-${iamInfo?.accountId}-${context.region}` to holding common resources like artifacts bucket - fix the deploy dependencies issue Refs: #5 --------- Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent cdb4517 commit 110366f

File tree

11 files changed

+141
-57
lines changed

11 files changed

+141
-57
lines changed

package-lock.json

Lines changed: 25 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"function"
5050
],
5151
"dependencies": {
52+
"@alicloud/ims20190815": "^2.1.4",
5253
"@alicloud/openapi-client": "^0.4.12",
5354
"@alicloud/ros-cdk-apigateway": "^1.4.0",
5455
"@alicloud/ros-cdk-core": "^1.4.0",

src/commands/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#! /usr/bin/env node
22

33
import { Command } from 'commander';
4-
import { lang } from '../lang';
5-
import { logger, getVersion } from '../common';
4+
import { constructActionContext, getVersion, logger } from '../common';
65
import { validate } from './validate';
76
import { deploy } from './deploy';
87
import { template } from './template';
98
import { destroyStack } from './destroy';
9+
import { getIamInfo } from '../common';
1010

1111
const program = new Command();
1212

@@ -15,13 +15,10 @@ program.name('si').description('CLI for ServerlessInsight').version(getVersion()
1515
program
1616
.command('show')
1717
.description('show string')
18-
.argument('<string>', 'string to split')
19-
.option('--first', 'display just the first substring')
20-
.option('-s, --separator <char>', 'separator character', ',')
21-
.action((str, options) => {
22-
const limit = options.first ? 1 : undefined;
23-
logger.debug({ limit, first: options.first, separator: options.separator }, 'log command info');
24-
console.log(`${str} ${options.first} ${options.separator} ${lang.__('hello')}`);
18+
.action(async (options) => {
19+
const context = constructActionContext({ ...options });
20+
const result = await getIamInfo(context);
21+
console.log('result:', JSON.stringify(result));
2522
});
2623

2724
program

src/common/imsClient.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Ims20190815, * as ims20190815 from '@alicloud/ims20190815';
2+
import * as openApi from '@alicloud/openapi-client';
3+
import { ActionContext } from '../types';
4+
5+
export const getIamInfo = async (context: ActionContext) => {
6+
const imsClient = new Ims20190815(
7+
new openApi.Config({
8+
accessKeyId: context.accessKeyId,
9+
accessKeySecret: context.accessKeySecret,
10+
}),
11+
);
12+
const { body } = await imsClient.getUser(
13+
new ims20190815.GetUserRequest({ userAccessKeyId: context.accessKeyId }),
14+
);
15+
return body?.user
16+
? {
17+
...body.user,
18+
accountId: body.user.userPrincipalName?.match(/@(\d+)\.onaliyun\.com/)?.pop(),
19+
}
20+
: undefined;
21+
};

src/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './rosClient';
55
export * from './actionContext';
66
export * from './iacHelper';
77
export * from './constants';
8+
export * from './imsClient';

src/stack/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'node:fs';
33

44
import { ActionContext, ServerlessIac } from '../types';
55
import { logger, ProviderEnum, publishAssets, rosStackDeploy } from '../common';
6-
import { RosStack } from './rosStack';
6+
import { prepareBootstrapStack, RosStack } from './rosStack';
77
import { RfsStack } from './rfsStack';
88
import { get } from 'lodash';
99

@@ -48,6 +48,7 @@ export const deployStack = async (
4848
context: ActionContext,
4949
) => {
5050
const { template, assets } = generateRosStackTemplate(stackName, iac, context);
51+
await prepareBootstrapStack(context);
5152
logger.info(`Deploying stack, publishing assets...`);
5253
await publishAssets(assets, context);
5354
logger.info(`Assets published! 🎉`);

src/stack/rosStack/bootstrap.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getIamInfo, rosStackDeploy } from '../../common';
2+
import { ActionContext } from '../../types';
3+
4+
const getBootstrapTemplate = async (context: ActionContext) => {
5+
const iamInfo = await getIamInfo(context);
6+
const stackName = `serverlessInsight-bootstrap-${iamInfo?.accountId}-${context.region}`;
7+
8+
const template = {
9+
Description: 'ServerlessInsight Bootstrap Stack',
10+
Metadata: {
11+
'ALIYUN::ROS::Interface': {
12+
TemplateTags: ['Bootstrap stack created by ServerlessInsight'],
13+
},
14+
},
15+
ROSTemplateFormatVersion: '2015-09-01',
16+
Resources: {
17+
ServerlessInsight_artifacts_bucket: {
18+
Type: 'ALIYUN::OSS::Bucket',
19+
Properties: {
20+
BucketName: {
21+
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
22+
},
23+
AccessControl: 'private',
24+
DeletionForce: false,
25+
EnableOssHdfsService: false,
26+
RedundancyType: 'LRS',
27+
ServerSideEncryptionConfiguration: {
28+
SSEAlgorithm: 'KMS',
29+
},
30+
},
31+
},
32+
},
33+
};
34+
return { stackName, template };
35+
};
36+
37+
export const prepareBootstrapStack = async (context: ActionContext) => {
38+
const { stackName, template } = await getBootstrapTemplate(context);
39+
await rosStackDeploy(stackName, template, context);
40+
};

src/stack/rosStack/function.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
resolveCode,
88
} from '../../common';
99
import * as fc from '@alicloud/ros-cdk-fc3';
10-
import * as oss from '@alicloud/ros-cdk-oss';
1110
import { isEmpty } from 'lodash';
1211
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
1312
import * as ros from '@alicloud/ros-cdk-core';
@@ -29,31 +28,24 @@ export const resolveFunctions = (
2928
return { fcName, ...getFileSource(fcName, code) };
3029
});
3130

32-
let destinationBucket: oss.Bucket;
33-
let artifactsDeployment: ossDeployment.BucketDeployment;
31+
const destinationBucketName = ros.Fn.sub(
32+
'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
33+
);
34+
const ossDeploymentId = `${service}_artifacts_code_deployment`;
35+
3436
if (!isEmpty(fileSources)) {
35-
// creat oss to store code
36-
destinationBucket = new oss.Bucket(
37-
scope,
38-
replaceReference(`${service}_artifacts_bucket`, context),
39-
{
40-
bucketName: `${service}-artifacts-bucket`,
41-
serverSideEncryptionConfiguration: { sseAlgorithm: 'KMS' },
42-
},
43-
true,
44-
);
45-
artifactsDeployment = new ossDeployment.BucketDeployment(
37+
new ossDeployment.BucketDeployment(
4638
scope,
47-
`${service}_artifacts_code_deployment`,
39+
ossDeploymentId,
4840
{
4941
sources: fileSources!.map(({ source }) => source),
50-
destinationBucket,
42+
destinationBucket: destinationBucketName,
5143
timeout: 3000,
5244
logMonitoring: false,
45+
retainOnCreate: false,
5346
},
5447
true,
5548
);
56-
artifactsDeployment.addDependency(destinationBucket);
5749
}
5850
functions?.forEach((fnc) => {
5951
const storeInBucket = readCodeSize(fnc.code) > CODE_ZIP_SIZE_LIMIT;
@@ -62,13 +54,13 @@ export const resolveFunctions = (
6254
};
6355
if (storeInBucket) {
6456
code = {
65-
ossBucketName: destinationBucket.attrName,
57+
ossBucketName: destinationBucketName,
6658
ossObjectName: fileSources?.find(
6759
({ fcName }) => fcName === replaceReference(fnc.name, context),
6860
)?.objectKey,
6961
};
7062
}
71-
new fc.RosFunction(
63+
const fcn = new fc.RosFunction(
7264
scope,
7365
fnc.key,
7466
{
@@ -82,5 +74,8 @@ export const resolveFunctions = (
8274
},
8375
true,
8476
);
77+
if (storeInBucket) {
78+
fcn.addRosDependency(`${service}_artifacts_code_deployment`);
79+
}
8580
});
8681
};

src/stack/rosStack/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { resloveVars } from './vars';
88
import { resolveDatabases } from './database';
99
import { resolveEvents } from './event';
1010

11+
export * from './bootstrap';
12+
1113
export class RosStack extends ros.Stack {
1214
private readonly service: string;
1315

tests/fixtures/deployFixture.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ export const largeCodeRos = {
642642
Properties: {
643643
Code: {
644644
OssBucketName: {
645-
'Fn::GetAtt': ['my-demo-service_artifacts_bucket', 'Name'],
645+
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
646646
},
647647
OssObjectName: 'hello_fn/43cb4c356149762dbe507fc1baede172-large-artifact.zip',
648648
},
@@ -656,6 +656,7 @@ export const largeCodeRos = {
656656
Timeout: 10,
657657
},
658658
Type: 'ALIYUN::FC3::Function',
659+
DependsOn: ['my-demo-service_artifacts_code_deployment'],
659660
},
660661
'my-demo-service_apigroup': {
661662
Properties: {
@@ -669,25 +670,11 @@ export const largeCodeRos = {
669670
},
670671
Type: 'ALIYUN::ApiGateway::Group',
671672
},
672-
'my-demo-service_artifacts_bucket': {
673-
Properties: {
674-
AccessControl: 'private',
675-
BucketName: 'my-demo-service-artifacts-bucket',
676-
DeletionForce: false,
677-
EnableOssHdfsService: false,
678-
RedundancyType: 'LRS',
679-
ServerSideEncryptionConfiguration: {
680-
SSEAlgorithm: 'KMS',
681-
},
682-
},
683-
Type: 'ALIYUN::OSS::Bucket',
684-
},
685673
'my-demo-service_artifacts_code_deployment': {
686-
DependsOn: ['my-demo-service_artifacts_bucket'],
687674
Properties: {
688675
Parameters: {
689676
destinationBucket: {
690-
'Fn::GetAtt': ['my-demo-service_artifacts_bucket', 'Name'],
677+
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
691678
},
692679
retainOnCreate: false,
693680
sources: [

0 commit comments

Comments
 (0)