Fixes and refactoring to base stack handling

pull/289/head
Frostebite 2021-08-13 19:52:37 +01:00
parent 3b55532c6c
commit 128a980f61
3 changed files with 60 additions and 48 deletions

50
dist/index.js vendored
View File

@ -1024,52 +1024,60 @@ class AWSBuildEnvironment {
});
}
setupBaseStack(CF) {
var _a, _b, _c, _d, _e, _f;
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
// Cloud Formation Input
const describeStackInput = {
StackName: baseStackName,
};
const parameters = [
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
{ ParameterKey: 'Version', ParameterValue: `hash` },
];
const updateInput = {
StackName: baseStackName,
TemplateBody: baseStack,
Parameters: parameters,
};
const createStackInput = {
StackName: baseStackName,
TemplateBody: baseStack,
Capabilities: ['CAPABILITY_IAM'],
Parameters: parameters,
};
const stacks = (_a = (yield CF.listStacks().promise()).StackSummaries) === null || _a === void 0 ? void 0 : _a.map((x) => x.StackName);
const stackExists = (stacks === null || stacks === void 0 ? void 0 : stacks.includes(baseStackName)) || false;
const describeStack = () => __awaiter(this, void 0, void 0, function* () {
return yield CF.describeStacks(describeStackInput).promise();
});
if (!stackExists) {
core.info('stack does not exist');
yield CF.createStack({
StackName: baseStackName,
TemplateBody: baseStack,
Capabilities: ['CAPABILITY_IAM'],
Parameters: [
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
{ ParameterKey: 'Version', ParameterValue: `hash` },
],
}).promise();
yield CF.createStack(createStackInput).promise();
core.info('created stack');
}
const CFState = yield CF.describeStacks(describeStackInput).promise();
const CFState = yield describeStack();
let stack = (_b = CFState.Stacks) === null || _b === void 0 ? void 0 : _b[0];
if (!stack) {
throw new Error(`Base stack doesn't exist, even after creation, stackExists check: ${stackExists}`);
}
const stackVersion = (_d = (_c = stack.Parameters) === null || _c === void 0 ? void 0 : _c.find((x) => x.ParameterKey === 'Version')) === null || _d === void 0 ? void 0 : _d.ParameterValue;
if (stack.StackStatus === 'CREATE_IN_PROGRESS') {
yield CF.waitFor('stackCreateComplete', describeStackInput).promise();
}
if (stackExists) {
core.info('stack exists');
if (stack.StackName === baseStackName &&
hash !== ((_d = (_c = stack.Parameters) === null || _c === void 0 ? void 0 : _c.find((x) => x.ParameterKey === 'Version')) === null || _d === void 0 ? void 0 : _d.ParameterValue)) {
const updateInput = {
StackName: baseStackName,
TemplateBody: baseStack,
};
core.info(`stack exists, stack version is ${stackVersion}, local version is ${hash}`);
if (hash !== stackVersion) {
core.info(`Updating`);
yield CF.updateStack(updateInput).promise();
}
else {
core.info(`Skipping any update for base stack ${stack.StackName} ${(_e = stack.Parameters) === null || _e === void 0 ? void 0 : _e.find((x) => x.ParameterKey === 'Version')}`);
core.info(`No update required`);
}
stack = (_f = (yield CF.describeStacks(describeStackInput).promise()).Stacks) === null || _f === void 0 ? void 0 : _f[0];
stack = (_e = (yield describeStack()).Stacks) === null || _e === void 0 ? void 0 : _e[0];
if (!stack) {
throw new Error(`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`);
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -235,55 +235,59 @@ class AWSBuildEnvironment implements RemoteBuilderProviderInterface {
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
// Cloud Formation Input
const describeStackInput: SDK.CloudFormation.DescribeStacksInput = {
StackName: baseStackName,
};
const parameters: SDK.CloudFormation.Parameter[] = [
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
{ ParameterKey: 'Version', ParameterValue: `hash` },
];
const updateInput: SDK.CloudFormation.UpdateStackInput = {
StackName: baseStackName,
TemplateBody: baseStack,
Parameters: parameters,
};
const createStackInput: SDK.CloudFormation.CreateStackInput = {
StackName: baseStackName,
TemplateBody: baseStack,
Capabilities: ['CAPABILITY_IAM'],
Parameters: parameters,
};
const stacks = (await CF.listStacks().promise()).StackSummaries?.map((x) => x.StackName);
const stackExists: Boolean = stacks?.includes(baseStackName) || false;
const describeStack = async () => {
return await CF.describeStacks(describeStackInput).promise();
};
if (!stackExists) {
core.info('stack does not exist');
await CF.createStack({
StackName: baseStackName,
TemplateBody: baseStack,
Capabilities: ['CAPABILITY_IAM'],
Parameters: [
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
{ ParameterKey: 'Version', ParameterValue: `hash` },
],
}).promise();
await CF.createStack(createStackInput).promise();
core.info('created stack');
}
const CFState = await CF.describeStacks(describeStackInput).promise();
const CFState = await describeStack();
let stack = CFState.Stacks?.[0];
if (!stack) {
throw new Error(`Base stack doesn't exist, even after creation, stackExists check: ${stackExists}`);
}
const stackVersion = stack.Parameters?.find((x) => x.ParameterKey === 'Version')?.ParameterValue;
if (stack.StackStatus === 'CREATE_IN_PROGRESS') {
await CF.waitFor('stackCreateComplete', describeStackInput).promise();
}
if (stackExists) {
core.info('stack exists');
if (
stack.StackName === baseStackName &&
hash !== stack.Parameters?.find((x) => x.ParameterKey === 'Version')?.ParameterValue
) {
const updateInput: SDK.CloudFormation.UpdateStackInput = {
StackName: baseStackName,
TemplateBody: baseStack,
};
core.info(`stack exists, stack version is ${stackVersion}, local version is ${hash}`);
if (hash !== stackVersion) {
core.info(`Updating`);
await CF.updateStack(updateInput).promise();
} else {
core.info(
`Skipping any update for base stack ${stack.StackName} ${stack.Parameters?.find(
(x) => x.ParameterKey === 'Version',
)}`,
);
core.info(`No update required`);
}
stack = (await CF.describeStacks(describeStackInput).promise()).Stacks?.[0];
stack = (await describeStack()).Stacks?.[0];
if (!stack) {
throw new Error(
`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`,