forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgrade410to420.java
More file actions
402 lines (367 loc) · 16.5 KB
/
Copy pathUpgrade410to420.java
File metadata and controls
402 lines (367 loc) · 16.5 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.upgrade.dao;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
import org.apache.log4j.Logger;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class Upgrade410to420 implements DbUpgrade {
final static Logger s_logger = Logger.getLogger(Upgrade410to420.class);
@Override
public String[] getUpgradableVersionRange() {
return new String[] { "4.1.0", "4.2.0" };
}
@Override
public String getUpgradedVersion() {
return "4.2.0";
}
@Override
public boolean supportsRollingUpgrade() {
return false;
}
@Override
public File[] getPrepareScripts() {
String script = Script.findScript("", "db/schema-410to420.sql");
if (script == null) {
throw new CloudRuntimeException("Unable to find db/schema-410to420.sql");
}
return new File[] { new File(script) };
}
@Override
public void performDataMigration(Connection conn) {
upgradeVmwareLabels(conn);
createPlaceHolderNics(conn);
updateRemoteAccessVpn(conn);
updateSystemVmTemplates(conn);
updateCluster_details(conn);
updatePrimaryStore(conn);
addEgressFwRulesForSRXGuestNw(conn);
upgradeEIPNetworkOfferings(conn);
}
private void updateSystemVmTemplates(Connection conn) {
PreparedStatement sql = null;
try {
sql = conn.prepareStatement("update vm_template set image_data_store_id = 1 where type = 'SYSTEM' or type = 'BUILTIN'");
sql.executeUpdate();
} catch (SQLException e) {
throw new CloudRuntimeException("Failed to upgrade vm template data store uuid: " + e.toString());
} finally {
if (sql != null) {
try {
sql.close();
} catch (SQLException e) {
}
}
}
}
private void updatePrimaryStore(Connection conn) {
PreparedStatement sql = null;
PreparedStatement sql2 = null;
try {
sql = conn.prepareStatement("update storage_pool set storage_provider_name = ? , scope = ? where pool_type = 'Filesystem' or pool_type = 'LVM'");
sql.setString(1, "ancient primary data store provider");
sql.setString(2, "HOST");
sql.executeUpdate();
sql2 = conn.prepareStatement("update storage_pool set storage_provider_name = ? , scope = ? where pool_type != 'Filesystem' and pool_type != 'LVM'");
sql2.setString(1, "ancient primary data store provider");
sql2.setString(2, "CLUSTER");
sql2.executeUpdate();
} catch (SQLException e) {
throw new CloudRuntimeException("Failed to upgrade vm template data store uuid: " + e.toString());
} finally {
if (sql != null) {
try {
sql.close();
} catch (SQLException e) {
}
}
if (sql2 != null) {
try {
sql2.close();
} catch (SQLException e) {
}
}
}
}
//update the cluster_details table with default overcommit ratios.
private void updateCluster_details(Connection conn) {
PreparedStatement pstmt = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 =null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("select id from `cloud`.`cluster`");
pstmt1=conn.prepareStatement("INSERT INTO `cloud`.`cluster_details` (cluster_id, name, value) VALUES(?, 'cpuOvercommitRatio', '1')");
pstmt2=conn.prepareStatement("INSERT INTO `cloud`.`cluster_details` (cluster_id, name, value) VALUES(?, 'memoryOvercommitRatio', '1')");
rs = pstmt.executeQuery();
while (rs.next()) {
long id = rs.getLong(1);
//update cluster_details table with the default overcommit ratios.
pstmt1.setLong(1,id);
pstmt1.execute();
pstmt2.setLong(1,id);
pstmt2.execute();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update cluster_details with default overcommit ratios.", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
@Override
public File[] getCleanupScripts() {
String script = Script.findScript("", "db/schema-410to420-cleanup.sql");
if (script == null) {
throw new CloudRuntimeException("Unable to find db/schema-410to420-cleanup.sql");
}
return new File[] { new File(script) };
}
private String getNewLabel(ResultSet rs, String oldParamValue) {
int separatorIndex;
String oldGuestLabel;
String newGuestLabel = oldParamValue;
try {
// No need to iterate because the global param setting applies to all physical networks irrespective of traffic type
if (rs.next()) {
oldGuestLabel = rs.getString("vmware_network_label");
// guestLabel is in format [[<VSWITCHNAME>],VLANID]
separatorIndex = oldGuestLabel.indexOf(",");
if(separatorIndex > -1) {
newGuestLabel += oldGuestLabel.substring(separatorIndex);
}
}
} catch (SQLException e) {
s_logger.error(new CloudRuntimeException("Failed to read vmware_network_label : " + e));
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}
return newGuestLabel;
}
private void upgradeVmwareLabels(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rsParams = null;
ResultSet rsLabel = null;
String newLabel;
String trafficType = null;
String trafficTypeVswitchParam;
String trafficTypeVswitchParamValue;
try {
// update the existing vmware traffic labels
pstmt = conn.prepareStatement("select name,value from `cloud`.`configuration` where category='Hidden' and value is not NULL and name REGEXP 'vmware\\.*\\.vswitch';");
rsParams = pstmt.executeQuery();
while (rsParams.next()) {
trafficTypeVswitchParam = rsParams.getString("name");
trafficTypeVswitchParamValue = rsParams.getString("value");
// When upgraded from 4.0 to 4.1 update physical network traffic label with trafficTypeVswitchParam
if (trafficTypeVswitchParam.equals("vmware.private.vswitch")) {
trafficType = "Management"; //TODO(sateesh): Ignore storage traffic, as required physical network already implemented, anything else tobe done?
} else if (trafficTypeVswitchParam.equals("vmware.public.vswitch")) {
trafficType = "Public";
} else if (trafficTypeVswitchParam.equals("vmware.guest.vswitch")) {
trafficType = "Guest";
}
s_logger.debug("Updating vmware label for " + trafficType + " traffic. Update SQL statement is " + pstmt);
pstmt = conn.prepareStatement("select physical_network_id, traffic_type, vmware_network_label from physical_network_traffic_types where vmware_network_label is not NULL and traffic_type='" + trafficType + "';");
rsLabel = pstmt.executeQuery();
newLabel = getNewLabel(rsLabel, trafficTypeVswitchParamValue);
pstmt = conn.prepareStatement("update physical_network_traffic_types set vmware_network_label = " + newLabel + " where traffic_type = '" + trafficType + "' and vmware_network_label is not NULL;");
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to set vmware traffic labels ", e);
} finally {
try {
if (rsParams != null) {
rsParams.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
private void createPlaceHolderNics(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("SELECT network_id, gateway, ip4_address FROM `cloud`.`nics` WHERE reserver_name IN ('DirectNetworkGuru','DirectPodBasedNetworkGuru') and vm_type='DomainRouter' AND removed IS null");
rs = pstmt.executeQuery();
while (rs.next()) {
Long networkId = rs.getLong(1);
String gateway = rs.getString(2);
String ip = rs.getString(3);
String uuid = UUID.randomUUID().toString();
//Insert placeholder nic for each Domain router nic in Shared network
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`nics` (uuid, ip4_address, gateway, network_id, state, strategy, vm_type) VALUES (?, ?, ?, ?, 'Reserved', 'PlaceHolder', 'DomainRouter')");
pstmt.setString(1, uuid);
pstmt.setString(2, ip);
pstmt.setString(3, gateway);
pstmt.setLong(4, networkId);
pstmt.executeUpdate();
s_logger.debug("Created placeholder nic for the ipAddress " + ip);
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to create placeholder nics", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
private void updateRemoteAccessVpn(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("SELECT vpn_server_addr_id FROM `cloud`.`remote_access_vpn`");
rs = pstmt.executeQuery();
long id=1;
while (rs.next()) {
String uuid = UUID.randomUUID().toString();
Long ipId = rs.getLong(1);
pstmt = conn.prepareStatement("UPDATE `cloud`.`remote_access_vpn` set uuid=?, id=? where vpn_server_addr_id=?");
pstmt.setString(1, uuid);
pstmt.setLong(2, id);
pstmt.setLong(3, ipId);
pstmt.executeUpdate();
id++;
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update id/uuid of remote_access_vpn table", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
private void addEgressFwRulesForSRXGuestNw(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
ResultSet rsId = null;
ResultSet rsNw = null;
try {
pstmt = conn.prepareStatement("select network_id FROM `cloud`.`ntwk_service_map` where service='Firewall' and provider='JuniperSRX' ");
rs = pstmt.executeQuery();
while (rs.next()) {
long netId = rs.getLong(1);
//checking for Isolated OR Virtual
pstmt = conn.prepareStatement("select account_id, domain_id FROM `cloud`.`networks` where (guest_type='Isolated' OR guest_type='Virtual') and traffic_type='Guest' and vpc_id is NULL and (state='implemented' OR state='Shutdown') and id=? ");
pstmt.setLong(1, netId);
s_logger.debug("Getting account_id, domain_id from networks table: " + pstmt);
rsNw = pstmt.executeQuery();
if(rsNw.next()) {
long accountId = rsNw.getLong(1);
long domainId = rsNw.getLong(2);
//Add new rule for the existing networks
s_logger.debug("Adding default egress firewall rule for network " + netId);
pstmt = conn.prepareStatement("INSERT INTO firewall_rules (uuid, state, protocol, purpose, account_id, domain_id, network_id, xid, created, traffic_type) VALUES (?, 'Active', 'all', 'Firewall', ?, ?, ?, ?, now(), 'Egress')");
pstmt.setString(1, UUID.randomUUID().toString());
pstmt.setLong(2, accountId);
pstmt.setLong(3, domainId);
pstmt.setLong(4, netId);
pstmt.setString(5, UUID.randomUUID().toString());
s_logger.debug("Inserting default egress firewall rule " + pstmt);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?");
pstmt.setLong(1, netId);
rsId = pstmt.executeQuery();
long firewallRuleId;
if(rsId.next()) {
firewallRuleId = rsId.getLong(1);
pstmt = conn.prepareStatement("insert into firewall_rules_cidrs (firewall_rule_id,source_cidr) values (?, '0.0.0.0/0')");
pstmt.setLong(1, firewallRuleId);
s_logger.debug("Inserting rule for cidr 0.0.0.0/0 for the new Firewall rule id=" + firewallRuleId + " with statement " + pstmt);
pstmt.executeUpdate();
}
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
private void upgradeEIPNetworkOfferings(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("select id, elastic_ip_service from `cloud`.`network_offerings` where traffic_type='Guest'");
rs = pstmt.executeQuery();
while (rs.next()) {
long id = rs.getLong(1);
// check if elastic IP service is enabled for network offering
if (rs.getLong(2) != 0) {
//update network offering with eip_associate_public_ip set to true
pstmt = conn.prepareStatement("UPDATE `cloud`.`network_offerings` set eip_associate_public_ip=? where id=?");
pstmt.setBoolean(1, true);
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to set elastic_ip_service for network offerings with EIP service enabled.", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
}