UNPKG

7.26 kBJavaScriptView Raw
1'use strict';
2var __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};
10const getProfile = require('./profile').getProfile;
11const vswitch = require('./vswitch');
12const securityGroup = require('./security-group');
13const debug = require('debug')('fun:nas');
14const { green } = require('colors');
15const { sleep } = require('./time');
16const { getVpcPopClient, getEcsPopClient } = require('./client');
17const _ = require('lodash');
18var requestOption = {
19 method: 'POST'
20};
21const defaultVSwitchName = 'fc-fun-vswitch-1';
22const defaultSecurityGroupName = 'fc-fun-sg-1';
23function findVpc(vpcClient, region, vpcName) {
24 return __awaiter(this, void 0, void 0, function* () {
25 const pageSize = 50; // max value is 50. see https://help.aliyun.com/document_detail/104577.html
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}
47function 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}
69function 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}
93function 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) { // create vswitch
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}
107function createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId) {
108 return __awaiter(this, void 0, void 0, function* () {
109 // check fun default security group exist?
110 let defaultSecurityGroup = yield securityGroup.describeSecurityGroups(ecsClient, region, vpcId, defaultSecurityGroupName);
111 debug('default security grpup: %j', defaultSecurityGroup);
112 // create security group
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}
127function 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) { // update
138 vswitchIds = funDefaultVpc.VSwitchIds.VSwitchId;
139 vpcId = funDefaultVpc.VpcId;
140 console.log(green('\t\tvpc already generated, vpcId is: ' + vpcId));
141 }
142 else { // create
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 // create security
151 const securityGroupId = yield createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId);
152 return {
153 vpcId,
154 vswitchIds,
155 securityGroupId
156 };
157 });
158}
159module.exports = {
160 createDefaultVpcIfNotExist,
161 findVpc,
162 createVpc
163};