1 | 'use strict';
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | const getProfile = require('./profile').getProfile;
|
11 | const vswitch = require('./vswitch');
|
12 | const securityGroup = require('./security-group');
|
13 | const debug = require('debug')('fun:nas');
|
14 | const { green } = require('colors');
|
15 | const { sleep } = require('./time');
|
16 | const { getVpcPopClient, getEcsPopClient } = require('./client');
|
17 | const _ = require('lodash');
|
18 | var requestOption = {
|
19 | method: 'POST'
|
20 | };
|
21 | const defaultVSwitchName = 'fc-fun-vswitch-1';
|
22 | const defaultSecurityGroupName = 'fc-fun-sg-1';
|
23 | function findVpc(vpcClient, region, vpcName) {
|
24 | return __awaiter(this, void 0, void 0, function* () {
|
25 | const pageSize = 50;
|
26 | let requestPageNumber = 0;
|
27 | let totalCount;
|
28 | let pageNumber;
|
29 | let vpc;
|
30 | do {
|
31 | var params = {
|
32 | 'RegionId': region,
|
33 | 'PageSize': pageSize,
|
34 | 'PageNumber': ++requestPageNumber
|
35 | };
|
36 | const rs = yield vpcClient.request('DescribeVpcs', params, requestOption);
|
37 | totalCount = rs.TotalCount;
|
38 | pageNumber = rs.PageNumber;
|
39 | const vpcs = rs.Vpcs.Vpc;
|
40 | debug('find vpc rs: %s', rs);
|
41 | vpc = _.find(vpcs, { VpcName: vpcName });
|
42 | debug('find default vpc: %s', vpc);
|
43 | } while (!vpc && totalCount && pageNumber && pageNumber * pageSize < totalCount);
|
44 | return vpc;
|
45 | });
|
46 | }
|
47 | function createVpc(vpcClient, region, vpcName) {
|
48 | return __awaiter(this, void 0, void 0, function* () {
|
49 | var createParams = {
|
50 | 'RegionId': region,
|
51 | 'CidrBlock': '10.0.0.0/8',
|
52 | 'EnableIpv6': false,
|
53 | 'VpcName': vpcName,
|
54 | 'Description': 'default vpc created by fc fun'
|
55 | };
|
56 | var createRs;
|
57 | try {
|
58 | createRs = yield vpcClient.request('CreateVpc', createParams, requestOption);
|
59 | }
|
60 | catch (ex) {
|
61 | throw ex;
|
62 | }
|
63 | const vpcId = createRs.VpcId;
|
64 | debug('create vpc rs is: %j', createRs);
|
65 | yield waitVpcUntilAvaliable(vpcClient, region, vpcId);
|
66 | return vpcId;
|
67 | });
|
68 | }
|
69 | function waitVpcUntilAvaliable(vpcClient, region, vpcId) {
|
70 | return __awaiter(this, void 0, void 0, function* () {
|
71 | let count = 0;
|
72 | let status;
|
73 | do {
|
74 | count++;
|
75 | var params = {
|
76 | 'RegionId': region,
|
77 | 'VpcId': vpcId
|
78 | };
|
79 | yield sleep(800);
|
80 | const rs = yield vpcClient.request('DescribeVpcs', params, requestOption);
|
81 | const vpcs = rs.Vpcs.Vpc;
|
82 | if (vpcs && vpcs.length) {
|
83 | status = vpcs[0].Status;
|
84 | debug('vpc status is: ' + status);
|
85 | console.log(`\t\tVPC already created, waiting for status to be 'Available', the status is ${status} currently`);
|
86 | }
|
87 | } while (count < 15 && status !== 'Available');
|
88 | if (status !== 'Available') {
|
89 | throw new Error(`Timeout while waiting for vpc ${vpcId} status to be 'Available'`);
|
90 | }
|
91 | });
|
92 | }
|
93 | function createDefaultVSwitchIfNotExist(vpcClient, region, vpcId, vswitchIds) {
|
94 | return __awaiter(this, void 0, void 0, function* () {
|
95 | let vswitchId = yield vswitch.findVswitchExistByName(vpcClient, region, vswitchIds, defaultVSwitchName);
|
96 | if (!vswitchId) {
|
97 | console.log('\t\tcould not find default vswitch, ready to generate one');
|
98 | vswitchId = yield vswitch.createDefaultVSwitch(vpcClient, region, vpcId, defaultVSwitchName);
|
99 | console.log(green('\t\tdefault vswitch has been generated, vswitchId is: ' + vswitchId));
|
100 | }
|
101 | else {
|
102 | console.log(green('\t\tvswitch already generated, vswitchId is: ' + vswitchId));
|
103 | }
|
104 | return vswitchId;
|
105 | });
|
106 | }
|
107 | function createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId) {
|
108 | return __awaiter(this, void 0, void 0, function* () {
|
109 |
|
110 | let defaultSecurityGroup = yield securityGroup.describeSecurityGroups(ecsClient, region, vpcId, defaultSecurityGroupName);
|
111 | debug('default security grpup: %j', defaultSecurityGroup);
|
112 |
|
113 | if (_.isEmpty(defaultSecurityGroup)) {
|
114 | console.log('\t\tcould not find default security group, ready to generate one');
|
115 | const securityGroupId = yield securityGroup.createSecurityGroup(ecsClient, region, vpcId, defaultSecurityGroupName);
|
116 | console.log('\t\t\tsetting default security group rules');
|
117 | yield securityGroup.authDefaultSecurityGroupRules(ecsClient, region, securityGroupId);
|
118 | console.log(green('\t\t\tdefault security group rules has been generated'));
|
119 | console.log(green('\t\tdefault security group has been generated, security group is: ' + securityGroupId));
|
120 | return securityGroupId;
|
121 | }
|
122 | const securityGroupId = defaultSecurityGroup[0].SecurityGroupId;
|
123 | console.log(green('\t\tsecurity group already generated, security group is: ' + securityGroupId));
|
124 | return securityGroupId;
|
125 | });
|
126 | }
|
127 | function createDefaultVpcIfNotExist() {
|
128 | return __awaiter(this, void 0, void 0, function* () {
|
129 | const profile = yield getProfile();
|
130 | const region = profile.defaultRegion;
|
131 | const vpcClient = yield getVpcPopClient();
|
132 | const ecsClient = yield getEcsPopClient();
|
133 | const defaultVpcName = 'fc-fun-vpc';
|
134 | let vswitchIds;
|
135 | let vpcId;
|
136 | const funDefaultVpc = yield findVpc(vpcClient, region, defaultVpcName);
|
137 | if (funDefaultVpc) {
|
138 | vswitchIds = funDefaultVpc.VSwitchIds.VSwitchId;
|
139 | vpcId = funDefaultVpc.VpcId;
|
140 | console.log(green('\t\tvpc already generated, vpcId is: ' + vpcId));
|
141 | }
|
142 | else {
|
143 | console.log('\t\tcould not find default vpc, ready to generate one');
|
144 | vpcId = yield createVpc(vpcClient, region, defaultVpcName);
|
145 | console.log(green('\t\tdefault vpc has been generated, vpcId is: ' + vpcId));
|
146 | }
|
147 | debug('vpcId is %s', vpcId);
|
148 | const vswitchId = yield createDefaultVSwitchIfNotExist(vpcClient, region, vpcId, vswitchIds);
|
149 | vswitchIds = [vswitchId];
|
150 |
|
151 | const securityGroupId = yield createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId);
|
152 | return {
|
153 | vpcId,
|
154 | vswitchIds,
|
155 | securityGroupId
|
156 | };
|
157 | });
|
158 | }
|
159 | module.exports = {
|
160 | createDefaultVpcIfNotExist,
|
161 | findVpc,
|
162 | createVpc
|
163 | };
|