UNPKG

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