forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgradeManagerImpl.java
More file actions
189 lines (158 loc) · 6.66 KB
/
UpgradeManagerImpl.java
File metadata and controls
189 lines (158 loc) · 6.66 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
// 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.maint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.maint.dao.AgentUpgradeDao;
import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.component.ManagerBase;
/**
*
* UpgradeManagerImpl implements the upgrade process. It's functionalities
* include
* 1. Identifying agents that require an upgrade before it can connect.
* 2. Spread out a release update to agents so that the entire system
* does not come down at the same time.
*/
@Component
@Local(UpgradeManager.class)
public class UpgradeManagerImpl extends ManagerBase implements UpgradeManager {
private final static Logger s_logger = Logger.getLogger(UpgradeManagerImpl.class);
private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
String _minimalVersion;
String _recommendedVersion;
// String _upgradeUrl;
String _agentPath;
long _checkInterval;
@Inject AgentUpgradeDao _upgradeDao;
@Inject ConfigurationDao _configDao;
@Override
public State registerForUpgrade(long hostId, String version) {
State state = State.UpToDate;
s_logger.debug("Minimal version is " + _minimalVersion + "; version is " + version + "; recommended version is " + _recommendedVersion);
if (Version.compare(version, _minimalVersion) < 0) {
state = State.RequiresUpdate;
} else if (Version.compare(version, _recommendedVersion) < 0) {
state = State.WaitingForUpdate;
} else {
state = State.UpToDate;
}
/*
if (state != State.UpToDate) {
AgentUpgradeVO vo = _upgradeDao.findById(hostId);
if (vo == null) {
vo = new AgentUpgradeVO(hostId, version, state);
_upgradeDao.persist(vo);
}
}
*/
return state;
}
public String deployNewAgent(String url) {
s_logger.info("Updating agent with binary from " + url);
final HttpClient client = new HttpClient(s_httpClientManager);
final GetMethod method = new GetMethod(url);
int response;
File file = null;
try {
response = client.executeMethod(method);
if (response != HttpURLConnection.HTTP_OK) {
s_logger.warn("Retrieving the agent gives response code: " + response);
return "Retrieving the file from " + url + " got response code: " + response;
}
final InputStream is = method.getResponseBodyAsStream();
file = File.createTempFile("agent-", "-" + Long.toString(new Date().getTime()));
file.deleteOnExit();
s_logger.debug("Retrieving new agent into " + file.getAbsolutePath());
final FileOutputStream fos = new FileOutputStream(file);
final ByteBuffer buffer = ByteBuffer.allocate(2048);
final ReadableByteChannel in = Channels.newChannel(is);
final WritableByteChannel out = fos.getChannel();
while (in.read(buffer) != -1) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
in.close();
out.close();
s_logger.debug("New Agent zip file is now retrieved");
} catch (final HttpException e) {
return "Unable to retrieve the file from " + url;
} catch (final IOException e) {
return "Unable to retrieve the file from " + url;
} finally {
method.releaseConnection();
}
file.delete();
return "File will be deployed.";
}
// @Override
// public String getAgentUrl() {
// return _upgradeUrl;
// }
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
final Map<String, String> configs = _configDao.getConfiguration("UpgradeManager", params);
File agentUpgradeFile = PropertiesUtil.findConfigFile("agent-update.properties");
Properties agentUpgradeProps = new Properties();
try {
if (agentUpgradeFile != null) {
agentUpgradeProps.load(new FileInputStream(agentUpgradeFile));
}
_minimalVersion = agentUpgradeProps.getProperty("agent.minimal.version");
_recommendedVersion = agentUpgradeProps.getProperty("agent.recommended.version");
if (_minimalVersion == null) {
_minimalVersion = "0.0.0.0";
}
if (_recommendedVersion == null) {
_recommendedVersion = _minimalVersion;
}
//_upgradeUrl = configs.get("upgrade.url");
// if (_upgradeUrl == null) {
// s_logger.debug("There is no upgrade url found in configuration table");
// // _upgradeUrl = "http://updates.vmops.com/releases/rss.xml";
// }
return true;
} catch (IOException ex) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Error reading agent-update.properties: " + ex);
}
}
return false;
}
}