|
| 1 | +import {EC2} from 'aws-sdk'; |
| 2 | +import {exactTag, except, flatten, instanceAge, namedTag} from 'utils'; |
| 3 | + |
| 4 | +import {EC2Facade} from './ec2-facade'; |
| 5 | + |
| 6 | +const cronTagName = 'Cron'; |
| 7 | +const cronTagValue = 'Present'; |
| 8 | + |
| 9 | +class CreateTags { |
| 10 | + private project: string; |
| 11 | + private environment: string; |
| 12 | + private globalFilter: EC2.FilterList = [ |
| 13 | + {Name: 'tag:Infrastructure', Values: ['mageops']}, |
| 14 | + ]; |
| 15 | + |
| 16 | + private projectFilter: EC2.FilterList; |
| 17 | + |
| 18 | + private appNodeFilter: EC2.FilterList = [ |
| 19 | + {Name: 'tag:Role', Values: ['app']}, |
| 20 | + {Name: 'instance-state-name', Values: ['running']}, |
| 21 | + ]; |
| 22 | + |
| 23 | + private ec2 = new EC2Facade(); |
| 24 | + |
| 25 | + constructor() { |
| 26 | + const project = process.env['PROJECT']; |
| 27 | + if (!project) { |
| 28 | + throw Error('PROJECT not specified'); |
| 29 | + } |
| 30 | + this.project = project; |
| 31 | + |
| 32 | + const environment = process.env['ENVIRONMENT']; |
| 33 | + if (!environment) { |
| 34 | + throw Error('ENVIRONMENT not specified'); |
| 35 | + } |
| 36 | + this.environment = environment; |
| 37 | + console.log( |
| 38 | + `Using project: ${this.project}, environment: ${this.environment}`); |
| 39 | + |
| 40 | + this.projectFilter = [ |
| 41 | + {Name: 'tag:Project', Values: [this.project]}, |
| 42 | + {Name: 'tag:Environment', Values: [this.environment]}, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + async tagCron() { |
| 47 | + const reservations = await this.ec2.describeInstances({ |
| 48 | + Filters: [ |
| 49 | + ...this.globalFilter, |
| 50 | + ...this.projectFilter, |
| 51 | + ...this.appNodeFilter, |
| 52 | + ], |
| 53 | + }); |
| 54 | + |
| 55 | + const instances = |
| 56 | + (reservations.Reservations ?? []).map(r => r.Instances) |
| 57 | + .filter((i): i is EC2.InstanceList => !!i) |
| 58 | + .reduce(flatten, []) |
| 59 | + .sort(instanceAge); |
| 60 | + |
| 61 | + const [oldest, ...rest] = instances; |
| 62 | + |
| 63 | + if (!oldest) { |
| 64 | + console.log(`There is no instances found!`); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const updates = []; |
| 69 | + |
| 70 | + if(!oldest.Tags?.find(exactTag(cronTagName, cronTagValue))) { |
| 71 | + updates.push(this.ec2.createInstanceTags(oldest, [ |
| 72 | + {Key: cronTagName, Value: cronTagValue}, |
| 73 | + ])); |
| 74 | + } |
| 75 | + |
| 76 | + for (const instance of rest) { |
| 77 | + if(instance.Tags?.find(exactTag(cronTagName, cronTagValue))) { |
| 78 | + updates.push( |
| 79 | + this.ec2.deleteInstanceTags( |
| 80 | + instance, instance.Tags.filter(namedTag(cronTagName))), |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + await Promise.all(updates); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export async function handler() { |
| 90 | + const tagger = new CreateTags(); |
| 91 | + await tagger.tagCron(); |
| 92 | + |
| 93 | + return { |
| 94 | + statusCode: 200, |
| 95 | + body: 'OK', |
| 96 | + }; |
| 97 | +} |
0 commit comments