Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 2x 4x 3x 3x 3x 3x 2x 2x 2x 1x 4x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 2x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 8x 8x 8x 1x 1x 8x 8x 8x 8x 9x 6x 6x 9x 6x 3x 3x 3x 3x 3x 8x 8x 6x 6x 8x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 9x 9x 9x 6x 6x 6x 3x 3x 1x | "use strict";
const debugLib = require("debug");
const info = debugLib("abc4:info");
const debug = debugLib("abc4:aws");
const sdklog = debugLib("abc4:aws-sdk");
const AWS = require("aws-sdk");
AWS.config.update({ logger: { log: sdklog } });
async function waitForWrapper(service, event, param) {
debug(`waiting for ${event}`);
let rt = await service.waitFor(event, param).promise();
let NextToken = rt.NextToken;
while (NextToken) {
debug("NextToken found, try again");
rt = await service.waitFor(event, Object.assign(param, { NextToken })).promise();
NextToken = rt.NextToken;
}
debug(`${event} done`);
return rt;
}
async function getVPCSetting(opt, ec2, clusterID) {
//create new VPC
Iif (!opt.hasOwnProperty("SubnetId") || !opt.hasOwnProperty("SecurityGroupIds")) {
return setupVPC(ec2, clusterID);
}
info("use specified subnet and security group");
Iif (typeof opt.SubnetId !== "string") {
return Promise.reject(new Error("SubnetId must be string"));
}
if (Array.isArray(opt.SecurityGroupIds)) {
Iif (opt.SecurityGroupIds.some((e)=>{
return typeof e !== "string";
})) {
return Promise.reject(new Error("all SecurityGroupIds member must be string"));
}
}
const sg = typeof opt.SecurityGroupIds === "string" ? [opt.SecurityGroupIds] : opt.SecurityGroupIds;
debug("SubnetID:", opt.SubnetId);
debug("SecurityGroupIds:", sg);
return [opt.SubnetId, sg];
}
function getUserName(os) {
Iif (os.startsWith("centos")) {
return "centos";
}
Eif (os.startsWith("ubuntu")) {
return "ubuntu";
}
return "ec2-user";
}
function getGroupName(os) {
//fall-back to username
return getUserName(os);
}
async function setupKeyPair(ec2, KeyName, PublicKeyMaterial) {
if (PublicKeyMaterial) {
info("import public key");
await ec2.importKeyPair({ KeyName, PublicKeyMaterial }).promise();
debug("import public key done");
return Promise.resolve();
}
info("create key pair");
const keyPair = await ec2.createKeyPair({ KeyName }).promise();
debug("create key pair done");
return keyPair.KeyMaterial;
}
/**
* Create IAM role and instanceProfile.
* @param {Object} iam - AWS.IAM instance.
* @param {string} clusterID - Id string returned by AWScreate.
* @returns {string} - Arn of created instanceProfile.
*/
async function setupIAMRole(iam, RoleName) {
info("create IAM role");
const AssumeRolePolicyDocument = JSON.stringify({
Version: "2012-10-17",
Statement: {
Effect: "Allow",
Principal: { Service: "ec2.amazonaws.com" },
Action: "sts:AssumeRole"
}
});
await iam.createRole({ RoleName, AssumeRolePolicyDocument }).promise();
debug("createRole done");
await iam.attachRolePolicy({ RoleName, PolicyArn: "arn:aws:iam::aws:policy/AmazonEC2FullAccess" }).promise();
await iam.attachRolePolicy({ RoleName, PolicyArn: "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM" }).promise();
debug("attachRolePolicy done");
const rt = await iam.createInstanceProfile({ InstanceProfileName: RoleName }).promise();
debug("createInstanceProfile done");
await iam.addRoleToInstanceProfile({ InstanceProfileName: RoleName, RoleName }).promise();
info("create IAM role done");
return rt.Arn;
}
/**
* Create VPC and security group which allow inbound ssh access.
* @param {Object} ec2 - AWS.EC2 instance.
* @param {string} clusterID - Id string returned by AWScreate.
* @returns {string, string} SubnetId, groupId - subnet and security group id respectively.
*/
async function setupVPC(ec2, clusterID) {
info("create VPC");
const { Vpc } = await ec2.createVpc({ CidrBlock: "10.0.0.0/16" }).promise();
const VpcId = Vpc.VpcId;
await ec2.modifyVpcAttribute({ EnableDnsHostnames: { Value: true }, VpcId }).promise();
await ec2.createTags({
Resources: [VpcId],
Tags: [{ Key: "abc4ClusterId", Value: clusterID }]
}).promise();
debug("create VPC done", VpcId);
const { Subnet } = await ec2.createSubnet({ CidrBlock: "10.0.0.0/16", VpcId }).promise();
const SubnetId = Subnet.SubnetId;
await ec2.createTags({
Resources: [SubnetId],
Tags: [{ Key: "abc4ClusterId", Value: clusterID }]
}).promise();
await ec2.modifySubnetAttribute({
MapPublicIpOnLaunch: { Value: true },
SubnetId
}).promise();
await ec2.modifySubnetAttribute({
AssignIpv6AddressOnCreation: { Value: false },
SubnetId
}).promise();
const { InternetGateway } = await ec2.createInternetGateway().promise();
const InternetGatewayId = InternetGateway.InternetGatewayId;
await ec2.createTags({
Resources: [InternetGatewayId],
Tags: [{ Key: "abc4ClusterId", Value: clusterID }]
}).promise();
debug("create IGW done", InternetGateway);
await ec2.attachInternetGateway({ InternetGatewayId, VpcId }).promise();
debug("attach IGW done", InternetGateway);
const { RouteTables } = await ec2.describeRouteTables({
Filters: [{ Name: "vpc-id", Values: [VpcId] }, { Name: "association.main", Values: ["true"] }]
}).promise();
const { RouteTableId } = RouteTables[0];
debug("main route table id=", RouteTableId);
await ec2.createTags({
Resources: [RouteTableId],
Tags: [{ Key: "abc4ClusterId", Value: clusterID }]
}).promise();
await ec2.createRoute({
DestinationCidrBlock: "0.0.0.0/0",
GatewayId: InternetGatewayId,
RouteTableId
}).promise();
debug("add default route to IGW done", RouteTableId);
debug("create Subnet done", SubnetId);
const { SecurityGroups } = await ec2.describeSecurityGroups({ Filters: [{ Name: "vpc-id", Values: [VpcId] }] }).promise();
const defaultGroupId = SecurityGroups[0].GroupId;
const { GroupId } = await ec2.createSecurityGroup({ VpcId, Description: "allow ssh access", GroupName: `${clusterID}-head` }).promise();
await ec2.createTags({
Resources: [GroupId],
Tags: [{ Key: "abc4ClusterId", Value: clusterID }]
}).promise();
await ec2.authorizeSecurityGroupIngress({
GroupId,
IpPermissions: [
{
UserIdGroupPairs: [
{
Description: "default",
GroupId: defaultGroupId
}
],
IpProtocol: "-1"
},
{
IpRanges: [
{
CidrIp: "0.0.0.0/0",
Description: "from all over the world"
}
],
FromPort: 22,
ToPort: 22,
IpProtocol: "tcp"
}
]
}).promise();
await ec2.authorizeSecurityGroupIngress({
GroupId: defaultGroupId,
IpPermissions: [
{
UserIdGroupPairs: [
{
Description: "head node",
GroupId
}
],
IpProtocol: "-1"
}
]
}).promise();
debug("create Security Group done", GroupId);
return [SubnetId, [GroupId]];
}
/**
* Cleanup all resources except for instances.
* @param {Object} ec2 - AWS.EC2 instance.
* @param {Object} iam - AWS.IAM instance.
* @param {string} clusterID - Id string returned by AWScreate.
*/
async function cleanUp(ec2, iam, clusterID) {
const Filters = [{ Name: "tag:abc4ClusterId", Values: [clusterID] }];
const p = [];
const p2 = [];
const { SecurityGroups } = await ec2.describeSecurityGroups({ Filters }).promise();
for (const sg of SecurityGroups) {
info("remove security group", sg.GroupId);
const defaultGroupId = sg.IpPermissions.map((e)=>{
return e.UserIdGroupPairs;
})[0][0].GroupId;
p2.push(
ec2.revokeSecurityGroupIngress({
GroupId: defaultGroupId,
IpPermissions: [
{
UserIdGroupPairs: [
{
GroupId: sg.GroupId
}
],
IpProtocol: "-1"
}
]
})
.promise()
.then(()=>{
return ec2.deleteSecurityGroup({ GroupId: sg.GroupId }).promise();
})
);
}
const { InternetGateways } = await ec2.describeInternetGateways({ Filters }).promise();
for (const e of InternetGateways) {
info("remove Internet Gateway", e.InternetGatewayId);
const pDetach = [];
for (const attachement of e.Attachments) {
pDetach.push(ec2.detachInternetGateway({ InternetGatewayId: e.InternetGatewayId, VpcId: attachement.VpcId }).promise());
}
p2.push(Promise.all(pDetach)
.then(()=>{
return ec2.deleteInternetGateway({ InternetGatewayId: e.InternetGatewayId }).promise();
}));
}
const { Subnets } = await ec2.describeSubnets({ Filters }).promise();
for (const e of Subnets) {
info("remove Subnet", e.SubnetId);
p2.push(ec2.deleteSubnet({ SubnetId: e.SubnetId }).promise());
}
await Promise.all(p2);
const { Vpcs } = await ec2.describeVpcs({ Filters }).promise();
for (const e of Vpcs) {
info("remove Vpc", e.VpcId);
p.push(ec2.deleteVpc({ VpcId: e.VpcId }).promise());
}
p.push(cleanUpKeyPair(ec2, clusterID));
p.push(cleanUpIAMRole(iam, clusterID));
await Promise.all(p);
info("cleanUp done");
}
async function cleanUpKeyPair(ec2, clusterID) {
const rt = await ec2.describeKeyPairs({ KeyNames: [clusterID] })
.promise()
.catch((err)=>{
Eif (err.code === "InvalidKeyPair.NotFound") {
info("key pair not found");
} else {
debug(err);
throw err;
}
});
if (typeof rt === "undefined") {
return;
}
const p = [];
for (const e of rt.KeyPairs) {
const KeyName = e.KeyName;
p.push(ec2.deleteKeyPair({ KeyName }).promise());
}
await Promise.all(p);
}
async function cleanUpIAMRole(iam, clusterID) {
info("remove IAM role");
const rt = await iam.listInstanceProfilesForRole({ RoleName: clusterID })
.promise()
.catch((err)=>{
Eif (err.code === "NoSuchEntity") {
info("instance profile not found");
} else {
debug(err);
throw err;
}
});
if (typeof rt === "undefined") {
return;
}
await Promise.all(rt.InstanceProfiles.map(async(e)=>{
const InstanceProfileName = e.InstanceProfileName;
await iam.removeRoleFromInstanceProfile({ InstanceProfileName, RoleName: clusterID }).promise();
await iam.deleteInstanceProfile({ InstanceProfileName }).promise();
return Promise.all(e.Roles.map(async(role)=>{
const RoleName = role.RoleName;
await iam.detachRolePolicy({ RoleName, PolicyArn: "arn:aws:iam::aws:policy/AmazonEC2FullAccess" }).promise();
await iam.detachRolePolicy({ RoleName, PolicyArn: "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM" }).promise();
return iam.deleteRole({ RoleName }).promise();
}));
}));
}
function createConfig(region, accessKeyId, secretAccessKey) {
const awsConfig = {};
Eif (typeof region === "string") {
awsConfig.region = region;
}
Iif (typeof accessKeyId === "string") {
awsConfig.accessKeyId = accessKeyId;
}
Iif (typeof secretAccessKey === "string") {
awsConfig.secretAccessKey = secretAccessKey;
}
return awsConfig;
}
function createEC2Object(region, accessKeyId, secretAccessKey) {
const awsConfig = createConfig(region, accessKeyId, secretAccessKey);
awsConfig.maxRetries = 100;
return new AWS.EC2(awsConfig);
}
function createIAMObject(region, accessKeyId, secretAccessKey) {
const awsConfig = createConfig(region, accessKeyId, secretAccessKey);
return new AWS.IAM(awsConfig);
}
module.exports = {
waitForWrapper,
setupKeyPair,
setupIAMRole,
setupVPC,
cleanUp,
cleanUpIAMRole,
cleanUpKeyPair,
createEC2Object,
createIAMObject,
getUserName,
getGroupName,
getVPCSetting
};
|