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