UNPKG

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