-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEC2Manager.py
296 lines (252 loc) · 11.6 KB
/
EC2Manager.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
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2009, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################
import re
import pickle
import logging
log = logging.getLogger('ec2')
from Globals import InitializeClass
from Products.ZenModel.Device import Device, manage_createDevice
from Products.ZenModel.Exceptions import DeviceExistsError
from Products.ZenModel.ZenossSecurity import ZEN_VIEW
from Products.ZenRelations.RelSchema import ToOne, ToManyCont
from ZenPacks.zenoss.ZenAWS.EC2Instance import EC2Instance
from ZenPacks.zenoss.ZenAWS.EC2InstanceType import EC2InstanceType
DISCOVER = 10
LINK = 20
COMPLETED = 30
isWindowsPlatform = re.compile(r"\bwindows\b", re.IGNORECASE).search
class EC2Manager(Device):
"""
A DMD Device that represents a group of VMware hosts
that can run virtual devices.
"""
meta_type = portal_type = "EC2Manager"
instid = ''
access_id = ''
devicePath = ''
devicePathForWindows = ''
_my_pickle_data = ''
_properties = Device._properties + (
{'id':'access_id', 'type':'string', 'mode':'w'},
{'id':'devicePath', 'type':'string', 'mode':'w'},
{'id':'devicePathForWindows', 'type':'string', 'mode':'w'},
)
_relations = Device._relations + (
('instances', ToManyCont(ToOne,
"ZenPacks.zenoss.ZenAWS.EC2Instance", "manager")),
('instanceTypes', ToManyCont(ToOne,
"ZenPacks.zenoss.ZenAWS.EC2InstanceType", "manager")),
)
factory_type_information = (
{
'immediate_view' : 'devicedetail',
'actions' :
(
{ 'id' : 'editEC2Manager'
, 'name' : 'Configure EC2'
, 'action' : 'editEC2Manager'
, 'permissions' : ("Change Device",)
},
{ 'id' : 'instances'
, 'name' : 'Instances'
, 'action' : 'viewInstances'
, 'permissions' : (ZEN_VIEW, )
},
{ 'id' : 'instanceTypes'
, 'name' : 'Instance Types'
, 'action' : 'viewInstanceTypes'
, 'permissions' : (ZEN_VIEW, )
},
{ 'id' : 'events'
, 'name' : 'Events'
, 'action' : 'viewEvents'
, 'permissions' : (ZEN_VIEW, )
},
{ 'id' : 'perfServer'
, 'name' : 'Graphs'
, 'action' : 'viewDevicePerformance'
, 'permissions' : (ZEN_VIEW, )
}
)
},
)
def __init__(self, id, buildRelations=True):
super(EC2Manager, self).__init__(id, buildRelations)
def setInstances(self, instPickle):
"""
This method is called by the modeler.
"""
self._my_pickle_data = instPickle
instances = pickle.loads(instPickle)
self._setInstances(instances)
def _setInstances(self, instances):
"""
Use this method if tests are ever written.
"""
instids = self.instances.objectIdsAll()
for instdict in instances:
instid = str(instdict['id'])
deviceId = "aws-" + instid
if instid in instids: instids.remove(instid)
inst = self._createOrUpdateInstance(instid, instdict)
itype = self._createOrUpdateInstanceTypes(inst, instdict)
if inst.instanceType() != itype:
inst.instanceType.removeRelation()
inst.instanceType.addRelation(itype)
if inst.state == 'running':
devicePath = self._getDevicePath(instdict)
if inst._discoveryState == DISCOVER and inst.dns_name:
if devicePath:
# see if device already exists
if self.findDeviceByIdExact(deviceId):
# device was found, just link
rdev = self._linkInstancesToDevices(inst, deviceId)
if rdev:
log.debug('linked inst %s to existing device %s',
inst.id, rdev.id)
inst._discoveryState = COMPLETED
else:
log.debug('failed to link %s to an existing device %s',
inst.id, deviceId)
else:
# device was not found, let zendisc create one
collector = self.getPerformanceServer()
inst._discoveryJobId = collector._executeZenDiscCommand(
inst.dns_name,
devicePath,
self.getPerformanceServerName(),
background=True).id
log.debug('started auto-discovery for %s', inst.id)
inst._discoveryState = LINK
else:
# don't try to link device, if no device classes were set
self._discoveryState = COMPLETED
log.debug('skipped auto-discovery for %s: devicePath not set',
inst.id)
elif inst._discoveryState == LINK:
rdev = self._linkInstancesToDevices(inst, deviceId)
if rdev:
log.debug('linked inst %s to device %s',
inst.id, rdev.id)
inst._discoveryState = COMPLETED
else:
if inst.isDiscoveryPending():
log.debug('failed to link %s to a device, device creation is pending', inst.id)
else:
collector = self.getPerformanceServer()
inst._discoveryJobId = \
collector._executeZenDiscCommand(
inst.dns_name,
devicePath,
self.getPerformanceServerName(),
background=True).id
log.debug('restarted auto-discovery for %s', inst.id)
elif inst._discoveryState == COMPLETED and devicePath and \
self.findDeviceByIdExact(deviceId) is None:
collector = self.getPerformanceServer()
inst._discoveryJobId = collector._executeZenDiscCommand(
inst.dns_name,
devicePath,
self.getPerformanceServerName(),
background=True).id
log.debug('started auto-discovery for %s. ' +
'Device was deleted or got new mapping', inst.id)
inst._discoveryState = LINK
elif inst.state == 'stopped' and inst._discoveryState != LINK:
rdev = self.findDeviceByIdExact(deviceId)
if rdev:
inst._deviceProdStatePreStop = rdev.productionState
rdev.setProdState(-1)
log.debug('pausing device %s after it was stopped',
rdev.id)
inst._discoveryState = LINK
for instid in instids:
deviceId = "aws-" + instid
rdev = self.findDeviceByIdExact(deviceId)
if rdev:
log.debug('deleting device %s after inst %s was terminated',
rdev.id, instid)
rdev.deleteDevice()
self.instances._delObject(instid)
def getInstances(self):
"""
return the last pickle sent to see if anything actually changed.
"""
return [] # return None for now until we debug the optimization below
#if [ inst for inst in self.instances() \
# if inst._discoveryState == COMPLETED ]:
# return self._my_pickle_data
def _linkInstancesToDevices(self, inst, deviceId):
if not inst.id.startswith('i-'): return
try:
import socket
ipaddr = socket.gethostbyname(inst.dns_name)
except socket.gaierror, e:
# if we don't have an ip we need to bail
return
rdev = self.findDeviceByIdExact(deviceId)
if rdev:
rdev.title = inst.dns_name
rdev.manageIp = ipaddr
rdev.setProdState(inst._deviceProdStatePreStop)
inst.deviceId = rdev.id
else:
rdev = self.findDeviceByIdOrIp(ipaddr)
if ipaddr and rdev and rdev.manageIp == ipaddr:
rlink = "<a href='%s'>%s</a>" % (inst.getPrimaryUrlPath(),
inst.id)
rdev.setZenProperty('zLinks', rlink)
rdev.renameDevice(deviceId)
rdev.setLocation("/"+str(inst.placement.replace('-','/')))
rdev.title = inst.dns_name
inst.deviceId = rdev.id
return rdev
def _createOrUpdateInstance(self, instid, properties):
inst = self.instances._getOb(instid, None)
if inst is None:
inst = EC2Instance(instid)
self.instances._setObject(instid, inst)
inst = self.instances._getOb(instid)
inst.updateFromDict(properties)
inst.index_object() # reindex because updateFromDict will have set state
return inst
def _createOrUpdateInstanceTypes(self, instance, instdict):
itypename = str(instdict['instance_type'])
itype = self.instanceTypes._getOb(itypename, None)
if itype is None:
itype = EC2InstanceType(itypename)
self.instanceTypes._setObject(itypename, itype)
itype = self.instanceTypes._getOb(itypename)
return itype
def manage_editEC2Manager(self, access_id, secret, devicePath="", devicePathForWindows="", **kwargs):
"""edit a ec2manager"""
self.access_id = access_id
self.devicePath = devicePath
self.devicePathForWindows = devicePathForWindows
if secret and secret != self.zEC2Secret:
self.setZenProperty('zEC2Secret', secret)
return super(EC2Manager, self).manage_editDevice(
REQUEST=self.REQUEST, **kwargs)
def manage_resetInstancesState(self):
instids = self.instances.objectIdsAll()
for instid in instids:
inst = self.instances._getOb(instid, None)
if inst:
inst._discoveryState = DISCOVER
inst.index_object()
def _getDevicePath(self, instdict):
# determine platform
devicePath = None
if instdict['platform'] and isWindowsPlatform(instdict['platform']):
# @TODO use products key to determine SQL Server, etc
devicePath = self.devicePathForWindows
else:
devicePath = self.devicePath
return devicePath
InitializeClass(EC2Manager)