forked from serverdensity/sd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.py
executable file
·315 lines (269 loc) · 9.8 KB
/
plugins.py
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
#!/usr/bin/env python
"""
Classes for plugin download, installation, and registration.
"""
import ConfigParser
import os
import platform
import urllib, urllib2
from optparse import OptionParser
from zipfile import ZipFile
BASE_URL = 'http://plugins.serverdensity.com/'
python_version = platform.python_version_tuple()
if int(python_version[1]) >= 6:
import json
else:
import minjson
json = None
class App(object):
"""
Class for collecting arguments and options for the plugin
download and installation process.
"""
def __init__(self):
usage = 'usage: %prog [options] key'
self.parser = OptionParser(usage=usage)
self.parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
default=False, help='run in verbose mode')
self.parser.add_option('-r', '--remove', action='store_true', dest='remove',
default=False, help='remove plugin')
self.parser.add_option('-u', '--update', action='store_true', dest='update',
default=False, help='update installed plugins')
def run(self):
"""
Entry point to the plugin helper application.
"""
(options, args) = self.parser.parse_args()
if len(args) != 1:
if options.update:
updater = PluginUpdater(verbose=options.verbose)
updater.start()
return
else:
self.parser.error('incorrect number of arguments')
if options.remove:
remover = PluginRemover(key=args[0], verbose=options.verbose)
remover.start()
else:
downloader = PluginDownloader(key=args[0], verbose=options.verbose)
downloader.start()
class PluginMetadata(object):
def __init__(self, downloader=None):
self.downloader = downloader
def get(self):
raise Exception, 'sub-classes to provide implementation.'
def json(self):
metadata = self.get()
if self.downloader.verbose:
print metadata
if json:
return json.loads(metadata)
else:
return minjson.safeRead(metadata)
class FilePluginMetadata(PluginMetadata):
"""
File-based metadata provider, for testing purposes.
"""
def get(self):
path = os.path.join(os.path.dirname(__file__), 'tests/plugin.json')
if self.downloader.verbose:
print 'reading plugin data from %s' % path
f = open(path, 'r')
data = f.read()
f.close()
return data
class WebPluginMetadata(PluginMetadata):
"""
Web-based metadata provider.
"""
def __init__(self, downloader=None, agent_key=None):
super(WebPluginMetadata, self).__init__(downloader=downloader)
self.agent_key = agent_key
def get(self):
url = '%sinstall/' % BASE_URL
data = {
'installId': self.downloader.key,
'agentKey': self.agent_key
}
if self.downloader.verbose:
print 'sending %s to %s' % (data, url)
request = urllib2.urlopen(url, urllib.urlencode(data))
response = request.read()
return response
class Action(object):
def __init__(self, key=None, verbose=True):
self.key = key
self.verbose = verbose
def start(self):
raise Exception, 'sub-classes to provide implementation.'
class PluginUpdater(Action):
def __init__(self, verbose=True):
super(PluginUpdater, self).__init__(verbose=verbose)
def __get_installs(self):
url = '%supdate/' % BASE_URL
data = {
'agentKey': self.config.agent_key
}
request = urllib2.urlopen(url, urllib.urlencode(data))
response = request.read()
if json:
return json.loads(response)
else:
return minjson.safeRead(response)
def start(self):
self.config = AgentConfig(action=self)
if self.verbose:
print 'updating plugins'
installs = self.__get_installs()
for install_id in installs['installIds']:
PluginDownloader(key=install_id, verbose=self.verbose).start()
class PluginRemover(Action):
"""
Class for removing a plugin.
"""
def __init__(self, key=None, verbose=True):
super(PluginRemover, self).__init__(key=key, verbose=verbose)
def __send_removal(self):
url = '%suninstall/' % BASE_URL
data = {
'installId': self.key,
'agentKey': self.config.agent_key
}
request = urllib2.urlopen(url, urllib.urlencode(data))
response = request.read()
if json:
return json.loads(response)
else:
return minjson.safeRead(response)
def __remove_file(self, name):
name = '%s.py' % name
path = os.path.join(self.config.plugin_path, name)
if self.verbose:
print 'removing %s' % path
os.remove(path)
def start(self):
self.config = AgentConfig(action=self)
if self.verbose:
print 'removing plugin with install key:', self.key
response = self.__send_removal()
if self.verbose:
print 'retrieved remove response.'
assert 'status' in response, 'response is not valid.'
if 'status' in response and response['status'] == 'error':
raise Exception, response['msg']
self.__remove_file(response['name'])
print 'plugin removed successfully.'
class PluginDownloader(Action):
"""
Class for downloading a plugin.
"""
def __init__(self, key=None, verbose=True):
super(PluginDownloader, self).__init__(key=key, verbose=verbose)
def __prepare_plugin_directory(self):
if not os.path.exists(self.config.plugin_path):
if self.verbose:
print '%s does not exist, creating' % self.config.plugin_path
os.mkdir(self.config.plugin_path)
if self.verbose:
print '%s created' % self.config.plugin_path
elif self.verbose:
print '%s exists' % self.config.plugin_path
def __download(self):
self.url = '%sdownload/%s/%s/' % (BASE_URL, self.key, self.config.agent_key)
if self.verbose:
print 'downloading for agent %s: %s' % (self.config.agent_key, self.url)
request = urllib2.urlopen(self.url)
data = request.read()
path = os.path.join(self.config.plugin_path, '%s.zip' % self.key)
f = open(path, 'w')
f.write(data)
f.close()
z = ZipFile(path, 'r')
try:
if json:
if self.verbose:
print 'extract all: %s' % (os.path.dirname(path))
z.extractall(os.path.dirname(path))
else:
for name in z.namelist():
if self.verbose:
print 'extract loop: %s' % (os.path.join(os.path.dirname(path), name))
data = z.read(name)
f = open(os.path.join(os.path.dirname(path), name), 'w')
f.write(data)
f.close()
except Exception, ex:
print ex
z.close()
os.remove(path)
def start(self):
self.config = AgentConfig(action=self)
metadata = WebPluginMetadata(self, agent_key=self.config.agent_key).json()
if self.verbose:
print 'retrieved metadata.'
assert 'configKeys' in metadata or 'status' in metadata, 'metadata is not valid.'
if 'status' in metadata and metadata['status'] == 'error':
raise Exception, metadata['msg']
self.__prepare_plugin_directory()
self.__download()
self.config.prompt(metadata['configKeys'])
print 'plugin installed; please restart your agent'
class AgentConfig(object):
"""
Class for writing new config options to sd-agent config.
"""
def __init__(self, action=None):
self.action = action
self.path = self.__get_config_path()
assert self.path, 'no config path found.'
self.config = self.__parse()
if self.config.get('Main', 'plugin_directory'):
self.plugin_path = self.config.get('Main', 'plugin_directory')
self.agent_key = self.config.get('Main', 'agent_key')
assert self.agent_key, 'no agent key.'
def __get_config_path(self):
paths = (
'/etc/sd-agent/config.cfg',
os.path.join(os.path.dirname(__file__), 'config.cfg')
)
for path in paths:
if os.path.exists(path):
if self.action.verbose:
print 'found config at %s' % path
return path
def __parse(self):
if os.access(self.path, os.R_OK) == False:
if self.action.verbose:
print 'cannot access config'
raise Exception, 'cannot access config'
if self.action.verbose:
print 'found config, parsing'
config = ConfigParser.ConfigParser()
config.read(self.path)
if self.action.verbose:
print 'parsed config'
return config
def __write(self, values):
for key in values.keys():
self.config.set('Main', key, values[key])
try:
f = open(self.path, 'w')
self.config.write(f)
f.close()
except Exception, ex:
print ex
sys.exit(1)
def prompt(self, options):
if not options:
return
values = {}
for option in options:
if not self.config.has_option('Main', option):
values[option] = raw_input('value for %s: ' % option)
self.__write(values)
if __name__ == '__main__':
try:
app = App()
app.run()
except Exception, ex:
print 'error: %s' % ex