1 | 'use strict';
|
2 |
|
3 | const getProfile = require('./profile').getProfile;
|
4 | const _ = require('lodash');
|
5 | const { getVpcPopClient, getEcsPopClient } = require('./client');
|
6 |
|
7 | const vswitch = require('./vswitch');
|
8 | const securityGroup = require('./security-group');
|
9 | const debug = require('debug')('fun:nas');
|
10 | const { green } = require('colors');
|
11 | const { sleep } = require('./time');
|
12 | const { throwProcessedException } = require('./error-message');
|
13 |
|
14 | var requestOption = {
|
15 | method: 'POST'
|
16 | };
|
17 |
|
18 | const defaultVSwitchName = 'fc-fun-vswitch-1';
|
19 | const defaultSecurityGroupName = 'fc-fun-sg-1';
|
20 |
|
21 | async function findVpc(vpcClient, region, vpcName) {
|
22 |
|
23 | const pageSize = 50;
|
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 |
|
54 | async 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 |
|
71 | throwProcessedException(ex, 'AliyunVpcFullAccess');
|
72 | }
|
73 |
|
74 | const vpcId = createRs.VpcId;
|
75 |
|
76 | debug('create vpc rs is: %j', createRs);
|
77 |
|
78 | await waitVpcUntilAvaliable(vpcClient, region, vpcId);
|
79 |
|
80 | return vpcId;
|
81 | }
|
82 |
|
83 | async function waitVpcUntilAvaliable(vpcClient, region, vpcId) {
|
84 |
|
85 | let count = 0;
|
86 | let status;
|
87 |
|
88 | do {
|
89 | count++;
|
90 |
|
91 | var params = {
|
92 | 'RegionId': region,
|
93 | 'VpcId': vpcId
|
94 | };
|
95 |
|
96 | await sleep(800);
|
97 |
|
98 | const rs = await vpcClient.request('DescribeVpcs', params, requestOption);
|
99 |
|
100 | status = rs.Vpcs.Vpc[0].Status;
|
101 |
|
102 | debug('vpc status is: ' + status);
|
103 |
|
104 | console.log(`\t\tvpc already created, waiting for status to be 'Available', now is ${status}`);
|
105 |
|
106 | } while (count < 15 && status !== 'Available');
|
107 |
|
108 | if (status !== 'Available') { throw new Error(`Timeout while waiting for vpc ${vpcId} status to be 'Available'`); }
|
109 |
|
110 | }
|
111 |
|
112 | async function createDefaultVSwitchIfNotExist(vpcClient, region, vpcId, vswitchIds) {
|
113 | let vswitchId = await vswitch.findVswitchExistByName(vpcClient, region, vswitchIds, defaultVSwitchName);
|
114 |
|
115 | if (!vswitchId) {
|
116 | console.log('\t\tcould not find default vswitch, ready to generate one');
|
117 | vswitchId = await vswitch.createDefaultVSwitch(vpcClient, region, vpcId, defaultVSwitchName);
|
118 | console.log(green('\t\tdefault vswitch has been generated, vswitchId is: ' + vswitchId));
|
119 | } else {
|
120 | console.log(green('\t\tvswitch already generated, vswitchId is: ' + vswitchId));
|
121 | }
|
122 |
|
123 | return vswitchId;
|
124 | }
|
125 |
|
126 | async function createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId) {
|
127 |
|
128 | let defaultSecurityGroup = await securityGroup.describeSecurityGroups(ecsClient, region, vpcId, defaultSecurityGroupName);
|
129 | debug('default security grpup: %j', defaultSecurityGroup);
|
130 |
|
131 |
|
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 | }
|
155 |
|
156 | async function createDefaultVpcIfNotExist() {
|
157 |
|
158 | const profile = await getProfile();
|
159 | const region = profile.defaultRegion;
|
160 |
|
161 | const vpcClient = await getVpcPopClient();
|
162 |
|
163 | const ecsClient = await getEcsPopClient();
|
164 |
|
165 | const defaultVpcName = 'fc-fun-vpc';
|
166 |
|
167 | let vswitchIds;
|
168 | let vpcId;
|
169 |
|
170 | const funDefaultVpc = await findVpc(vpcClient, region, defaultVpcName);
|
171 |
|
172 | if (funDefaultVpc) {
|
173 | vswitchIds = funDefaultVpc.VSwitchIds.VSwitchId;
|
174 | vpcId = funDefaultVpc.VpcId;
|
175 |
|
176 | console.log(green('\t\tvpc already generated, vpcId is: ' + vpcId));
|
177 | } else {
|
178 | console.log('\t\tcould not find default vpc, ready to generate one');
|
179 |
|
180 | vpcId = await createVpc(vpcClient, region, defaultVpcName);
|
181 |
|
182 | console.log(green('\t\tdefault vpc has been generated, vpcId is: ' + vpcId));
|
183 |
|
184 | }
|
185 |
|
186 | debug('vpcId is %s', vpcId);
|
187 |
|
188 | const vswitchId = await createDefaultVSwitchIfNotExist(vpcClient, region, vpcId, vswitchIds);
|
189 |
|
190 | vswitchIds = [ vswitchId ];
|
191 |
|
192 |
|
193 |
|
194 | const securityGroupId = await createDefaultSecurityGroupIfNotExist(ecsClient, region, vpcId);
|
195 |
|
196 | return {
|
197 | vpcId,
|
198 | vswitchIds,
|
199 | securityGroupId
|
200 | };
|
201 | }
|
202 |
|
203 | module.exports = {
|
204 | createDefaultVpcIfNotExist,
|
205 | findVpc,
|
206 | createVpc
|
207 | }; |
\ | No newline at end of file |