Fixes and refactoring to base stack handling
parent
3b55532c6c
commit
128a980f61
|
|
@ -1024,52 +1024,60 @@ class AWSBuildEnvironment {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setupBaseStack(CF) {
|
setupBaseStack(CF) {
|
||||||
var _a, _b, _c, _d, _e, _f;
|
var _a, _b, _c, _d, _e;
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
|
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
|
||||||
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
|
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
|
||||||
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
|
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
|
||||||
|
// Cloud Formation Input
|
||||||
const describeStackInput = {
|
const describeStackInput = {
|
||||||
StackName: baseStackName,
|
StackName: baseStackName,
|
||||||
};
|
};
|
||||||
const stacks = (_a = (yield CF.listStacks().promise()).StackSummaries) === null || _a === void 0 ? void 0 : _a.map((x) => x.StackName);
|
const parameters = [
|
||||||
const stackExists = (stacks === null || stacks === void 0 ? void 0 : stacks.includes(baseStackName)) || false;
|
|
||||||
if (!stackExists) {
|
|
||||||
core.info('stack does not exist');
|
|
||||||
yield CF.createStack({
|
|
||||||
StackName: baseStackName,
|
|
||||||
TemplateBody: baseStack,
|
|
||||||
Capabilities: ['CAPABILITY_IAM'],
|
|
||||||
Parameters: [
|
|
||||||
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
|
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
|
||||||
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
|
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
|
||||||
{ ParameterKey: 'Version', ParameterValue: `hash` },
|
{ ParameterKey: 'Version', ParameterValue: `hash` },
|
||||||
],
|
];
|
||||||
}).promise();
|
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(createStackInput).promise();
|
||||||
core.info('created stack');
|
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];
|
let stack = (_b = CFState.Stacks) === null || _b === void 0 ? void 0 : _b[0];
|
||||||
if (!stack) {
|
if (!stack) {
|
||||||
throw new Error(`Base stack doesn't exist, even after creation, stackExists check: ${stackExists}`);
|
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') {
|
if (stack.StackStatus === 'CREATE_IN_PROGRESS') {
|
||||||
yield CF.waitFor('stackCreateComplete', describeStackInput).promise();
|
yield CF.waitFor('stackCreateComplete', describeStackInput).promise();
|
||||||
}
|
}
|
||||||
if (stackExists) {
|
if (stackExists) {
|
||||||
core.info('stack exists');
|
core.info(`stack exists, stack version is ${stackVersion}, local version is ${hash}`);
|
||||||
if (stack.StackName === baseStackName &&
|
if (hash !== stackVersion) {
|
||||||
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)) {
|
core.info(`Updating`);
|
||||||
const updateInput = {
|
|
||||||
StackName: baseStackName,
|
|
||||||
TemplateBody: baseStack,
|
|
||||||
};
|
|
||||||
yield CF.updateStack(updateInput).promise();
|
yield CF.updateStack(updateInput).promise();
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (!stack) {
|
||||||
throw new Error(`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`);
|
throw new Error(`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -235,55 +235,59 @@ class AWSBuildEnvironment implements RemoteBuilderProviderInterface {
|
||||||
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
|
const baseStackName = process.env.baseStackName || 'game-ci-base-stack-01';
|
||||||
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
|
const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8');
|
||||||
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
|
const hash = crypto.createHash('md5').update(baseStack).digest('hex');
|
||||||
|
|
||||||
|
// Cloud Formation Input
|
||||||
const describeStackInput: SDK.CloudFormation.DescribeStacksInput = {
|
const describeStackInput: SDK.CloudFormation.DescribeStacksInput = {
|
||||||
StackName: baseStackName,
|
StackName: baseStackName,
|
||||||
};
|
};
|
||||||
const stacks = (await CF.listStacks().promise()).StackSummaries?.map((x) => x.StackName);
|
const parameters: SDK.CloudFormation.Parameter[] = [
|
||||||
const stackExists: Boolean = stacks?.includes(baseStackName) || false;
|
|
||||||
|
|
||||||
if (!stackExists) {
|
|
||||||
core.info('stack does not exist');
|
|
||||||
await CF.createStack({
|
|
||||||
StackName: baseStackName,
|
|
||||||
TemplateBody: baseStack,
|
|
||||||
Capabilities: ['CAPABILITY_IAM'],
|
|
||||||
Parameters: [
|
|
||||||
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
|
{ ParameterKey: 'EnvironmentName', ParameterValue: 'development' },
|
||||||
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
|
{ ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` },
|
||||||
{ ParameterKey: 'Version', ParameterValue: `hash` },
|
{ ParameterKey: 'Version', ParameterValue: `hash` },
|
||||||
],
|
];
|
||||||
}).promise();
|
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(createStackInput).promise();
|
||||||
core.info('created stack');
|
core.info('created stack');
|
||||||
}
|
}
|
||||||
const CFState = await CF.describeStacks(describeStackInput).promise();
|
const CFState = await describeStack();
|
||||||
let stack = CFState.Stacks?.[0];
|
let stack = CFState.Stacks?.[0];
|
||||||
if (!stack) {
|
if (!stack) {
|
||||||
throw new Error(`Base stack doesn't exist, even after creation, stackExists check: ${stackExists}`);
|
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') {
|
if (stack.StackStatus === 'CREATE_IN_PROGRESS') {
|
||||||
await CF.waitFor('stackCreateComplete', describeStackInput).promise();
|
await CF.waitFor('stackCreateComplete', describeStackInput).promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stackExists) {
|
if (stackExists) {
|
||||||
core.info('stack exists');
|
core.info(`stack exists, stack version is ${stackVersion}, local version is ${hash}`);
|
||||||
if (
|
if (hash !== stackVersion) {
|
||||||
stack.StackName === baseStackName &&
|
core.info(`Updating`);
|
||||||
hash !== stack.Parameters?.find((x) => x.ParameterKey === 'Version')?.ParameterValue
|
|
||||||
) {
|
|
||||||
const updateInput: SDK.CloudFormation.UpdateStackInput = {
|
|
||||||
StackName: baseStackName,
|
|
||||||
TemplateBody: baseStack,
|
|
||||||
};
|
|
||||||
await CF.updateStack(updateInput).promise();
|
await CF.updateStack(updateInput).promise();
|
||||||
} else {
|
} else {
|
||||||
core.info(
|
core.info(`No update required`);
|
||||||
`Skipping any update for base stack ${stack.StackName} ${stack.Parameters?.find(
|
|
||||||
(x) => x.ParameterKey === 'Version',
|
|
||||||
)}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
stack = (await CF.describeStacks(describeStackInput).promise()).Stacks?.[0];
|
stack = (await describeStack()).Stacks?.[0];
|
||||||
if (!stack) {
|
if (!stack) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`,
|
`Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue