forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgrade229to2210.java
More file actions
194 lines (169 loc) · 7.83 KB
/
Copy pathUpgrade229to2210.java
File metadata and controls
194 lines (169 loc) · 7.83 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
// 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 java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import org.apache.log4j.Logger;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
public class Upgrade229to2210 implements DbUpgrade {
final static Logger s_logger = Logger.getLogger(Upgrade229to2210.class);
@Override
public String[] getUpgradableVersionRange() {
return new String[] { "2.2.9", "2.2.9"};
}
@Override
public String getUpgradedVersion() {
return "2.2.10";
}
@Override
public boolean supportsRollingUpgrade() {
return true;
}
@Override
public File[] getPrepareScripts() {
String script = Script.findScript("", "db/schema-229to2210.sql");
if (script == null) {
throw new CloudRuntimeException("Unable to find db/schema-229to2210.sql");
}
return new File[] { new File(script) };
}
@Override
public void performDataMigration(Connection conn) {
updateFirewallRules(conn);
updateSnapshots(conn);
}
@Override
public File[] getCleanupScripts() {
return null;
}
private void updateSnapshots(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
long currentSnapshotId = 0;
try {
pstmt = conn.prepareStatement("select id, prev_snap_id from snapshots where sechost_id is NULL and prev_snap_id is not NULL and status=\"BackedUp\" and removed is NULL order by id");
rs = pstmt.executeQuery();
while (rs.next()) {
long id = rs.getLong(1);
long preSnapId = rs.getLong(2);
currentSnapshotId = id;
pstmt = conn.prepareStatement("select sechost_id from snapshots where id=? and sechost_id is not NULL");
pstmt.setLong(1, preSnapId);
ResultSet sechost = pstmt.executeQuery();
if (sechost.next()) {
long secHostId = sechost.getLong(1);
pstmt = conn.prepareStatement("update snapshots set sechost_id=? where id=?");
pstmt.setLong(1, secHostId);
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update snapshots id=" + currentSnapshotId, e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
private void updateFirewallRules(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rs = null;
long currentRuleId = 0;
try {
// Host and Primary storage capacity types
pstmt = conn.prepareStatement("select id, ip_address_id, start_port, end_port, protocol, account_id, domain_id, network_id from firewall_rules where state != 'Revoke'");
rs = pstmt.executeQuery();
while (rs.next()) {
long id = rs.getLong(1);
long ipId = rs.getLong(2);
int startPort = rs.getInt(3);
int endPort = rs.getInt(4);
String protocol = rs.getString(5);
long accountId = rs.getLong(6);
long domainId = rs.getLong(7);
long networkId = rs.getLong(8);
currentRuleId = id;
Long firewallRuleId = null;
pstmt = conn.prepareStatement("INSERT INTO firewall_rules (ip_address_id, start_port, end_port, protocol, account_id, domain_id, network_id, purpose, state, xid, created, related) VALUES (?, ?, ?, ?, ?, ?, ?, 'Firewall', 'Active', ?, now(), ?)");
pstmt.setLong(1, ipId);
pstmt.setInt(2, startPort);
pstmt.setInt(3, endPort);
pstmt.setString(4, protocol);
pstmt.setLong(5, accountId);
pstmt.setLong(6, domainId);
pstmt.setLong(7, networkId);
pstmt.setString(8, UUID.randomUUID().toString());
pstmt.setLong(9, id);
s_logger.debug("Updating firewall rule with the statement " + pstmt);
pstmt.executeUpdate();
//get new FirewallRule update
pstmt = conn.prepareStatement("SELECT id from firewall_rules where purpose='Firewall' and start_port=? and end_port=? and protocol=? and ip_address_id=? and network_id=? and related=?");
pstmt.setInt(1, startPort);
pstmt.setInt(2, endPort);
pstmt.setString(3, protocol);
pstmt.setLong(4, ipId);
pstmt.setLong(5, networkId);
pstmt.setLong(6, id);
ResultSet rs1 = pstmt.executeQuery();
if (rs1.next()) {
firewallRuleId = rs1.getLong(1);
} else {
throw new CloudRuntimeException("Unable to find just inserted firewall rule for ptocol " + protocol + ", start_port " + startPort + " and end_port " + endPort + " and ip address id=" + ipId);
}
pstmt = conn.prepareStatement("select id from firewall_rules_cidrs where firewall_rule_id=?");
pstmt.setLong(1, id);
ResultSet rs2 = pstmt.executeQuery();
if (rs2.next()) {
pstmt = conn.prepareStatement("update firewall_rules_cidrs set firewall_rule_id=? where firewall_rule_id=?");
pstmt.setLong(1, firewallRuleId);
pstmt.setLong(2, id);
s_logger.debug("Updating existing cidrs for the rule id=" + id + " with the new Firewall rule id=" + firewallRuleId + " with statement" + pstmt);
pstmt.executeUpdate();
} else {
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 update firewall rule id=" + currentRuleId, e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
}