1
2
3
4
5
6
7
8
9
10
11 import logging
12 from urllib2 import URLError
13
14 from Products import Zuul
15 from Products.ZenMessaging.audit import audit
16 from Products.Zuul.routers import TreeRouter
17 from Products.ZenUtils.Ext import DirectResponse
18 from Products.Zuul.form.interfaces import IFormBuilder
19 from Products.Zuul.interfaces import IInfo, ITreeNode
20 from Products.Zuul.marshalling import Marshaller
21
22 log = logging.getLogger('zen.ApplicationRouter')
23
24 _monkeys = ['ccbacked', 'leaf', 'name', 'text', 'devcount', 'path',
25 'type', 'id', 'uid']
26 _appkeys = ['hostId', 'description', 'text', 'children', 'uid',
27 'qtip', 'uptime', 'leaf', 'name', 'isRestarting',
28 'id', 'state', 'autostart', 'type']
29 _monitorprefix = '.zport.dmd.Monitors.Performance.'
30
31
33 """
34 """
35
37 return Zuul.getFacade('applications', self.context)
38
40 return Zuul.getFacade('monitors', self.context)
41
43 """
44 Returns the tree structure of the application and collector
45 hierarchy.
46
47 @type id: string
48 @param id: Id of the root node of the tree to be returned
49 @rtype: [dictionary]
50 @return: Object representing the tree
51 """
52 try:
53 if not hasattr(id, '__iter__'):
54 return self._getOneTree(id)
55
56 trees = {i: self._getOneTree(i) for i in id}
57 treeKeys = self._getParentTreeKeys(trees)
58
59
60 for key in treeKeys:
61 children = trees[key]['children']
62 for i in range(len(children)):
63 currentChild = children[i]
64 if trees.has_key(currentChild['id']):
65 children[i] = trees[currentChild['id']]
66
67 return trees['root']
68
69 except URLError as e:
70 log.exception(e)
71 return DirectResponse.fail(
72 "Error fetching daemons list: " + str(e.reason)
73 )
74
77
79 if id.startswith(_monitorprefix):
80 return self._getMonitorTree(id)
81
82 appfacade = self._getFacade()
83 monitorfacade = Zuul.getFacade("monitors", self.context)
84
85 roots = []
86 monitors = [ITreeNode(m) for m in monitorfacade.query()]
87 for monitor in monitors:
88 monitordict = Marshaller(monitor).marshal(_monkeys)
89 if not appfacade.queryMonitorDaemons(monitor.name):
90 monitordict['children'] = []
91 roots.append(monitordict)
92 apps = [
93 IInfo(a) for a in appfacade.queryMasterDaemons()
94 ]
95 roots.extend([Marshaller(app).marshal(_appkeys) for app in apps])
96 return {'id': 'root', 'children': roots}
97
99 appfacade = self._getFacade()
100 monitorfacade = Zuul.getFacade("monitors", self.context)
101 m = monitorfacade.get(id[len(_monitorprefix):])
102 monitor = ITreeNode(m)
103 apps = appfacade.queryMonitorDaemons(monitor.name)
104 for app in apps:
105 monitor.addChild(IInfo(app))
106 return Zuul.marshal(monitor)
107
109 """
110 Returns the tree structure of the application and collector
111 hierarchy.
112
113 @type id: string
114 @param id: Id of the root node of the tree to be returned
115 @rtype: [dictionary]
116 @return: Object representing the tree
117 """
118 try:
119 appfacade = self._getFacade()
120 monitorfacade = Zuul.getFacade("monitors", self.context)
121 nodes = [ITreeNode(m) for m in monitorfacade.query()]
122 for monitor in nodes:
123 apps = appfacade.queryMonitorDaemons(monitor.name)
124 for app in apps:
125 monitor.addChild(IInfo(app))
126 apps = appfacade.queryMasterDaemons()
127 for app in apps:
128 nodes.append(IInfo(app))
129 return Zuul.marshal(nodes)
130 except URLError as e:
131 log.exception(e)
132 return DirectResponse.fail(
133 "Error fetching daemons list: " + str(e.reason)
134 )
135
152
154 """
155 Will issue the command to start the selected ids
156 @type uids: Array[Strings]
157 @param uids: List of valid daemon ids that will need to started
158 @rtype: DirectResposne
159 @return: DirectReponse of success if no errors are encountered
160 """
161
162 if not Zuul.checkPermission('Manage DMD'):
163 return DirectResponse.fail("You don't have permission to start a daemon", sticky=False)
164
165 facade = self._getFacade()
166 for uid in uids:
167 facade.start(uid)
168 audit('UI.Applications.Start', uid)
169 if len(uids) > 1:
170 return DirectResponse.succeed("Started %s daemons" % len(uids))
171 return DirectResponse.succeed()
172
173 - def stop(self, uids):
174 """
175 Will issue the command to stop the selected ids
176 @type uids: Array[Strings]
177 @param uids: List of valid daemon ids that will need to stopped
178 @rtype: DirectResposne
179 @return: DirectReponse of success if no errors are encountered
180 """
181
182 if not Zuul.checkPermission('Manage DMD'):
183 return DirectResponse.fail("You don't have permission to stop a daemon", sticky=False)
184
185 facade = self._getFacade()
186 for uid in uids:
187 facade.stop(uid)
188 audit('UI.Applications.Stop', uid)
189 if len(uids) > 1:
190 return DirectResponse.succeed("Stopped %s daemons" % len(uids))
191 return DirectResponse.succeed()
192
194 """
195 Will issue the command to restart the selected ids
196 @type uids: Array[Strings]
197 @param uids: List of valid daemon ids that will need to restarted
198 @rtype: DirectResposne
199 @return: DirectReponse of success if no errors are encountered
200 """
201
202 if not Zuul.checkPermission('Manage DMD'):
203 return DirectResponse.fail("You don't have permission to restart a daemon", sticky=False)
204
205 facade = self._getFacade()
206 for uid in uids:
207 facade.restart(uid)
208 audit('UI.Applications.Restart', uid)
209 if len(uids) > 1:
210 return DirectResponse.succeed("Restarted %s daemons" % len(uids))
211 return DirectResponse.succeed()
212
214 """
215 Enables or disables autostart on all applications passed into uids.
216 If it is already in that state it is a noop.
217 @type uids: Array[Strings]
218 @param uids: List of valid daemon ids that will need to enabled
219 @type enabled: boolean
220 @param uids: true for enabled or false for disabled
221 @rtype: DirectResposne
222 @return: DirectReponse of success if no errors are encountered
223 """
224
225 if not Zuul.checkPermission('Manage DMD'):
226 return DirectResponse.fail("You don't have permission to set autostart", sticky=False)
227
228 facade = self._getFacade()
229 applications = facade.query()
230 for app in applications:
231 if app.id in uids:
232 app.autostart = enabled
233 audit('UI.Applications.AutoStart', app.id, {'autostart': enabled})
234 return DirectResponse.succeed()
235
237 """
238 Returns the serialized info object for the given id
239 @type: id: String
240 @param id: Valid id of a application
241 @rtype: DirectResponse
242 @return: DirectResponse with data of the application
243 """
244 facade = self._getFacade()
245 app = facade.get(id)
246 data = Zuul.marshal(IInfo(app))
247 return DirectResponse.succeed(data=data)
248
250 """
251 Returns a list of resource pool identifiers.
252 @rtype: DirectResponse
253 @return: B{Properties}:
254 - data: ([String]) List of resource pool identifiers
255 """
256 pools = self._monitorFacade().queryPools()
257 ids = (dict(name=p.id) for p in pools)
258 return DirectResponse.succeed(data=Zuul.marshal(ids))
259
261 """
262 Returns all the configuration files for an application
263 """
264 facade = self._getFacade()
265 info = IInfo(facade.get(id))
266 files = info.configFiles
267 return DirectResponse.succeed(data=Zuul.marshal(files))
268
270 """
271 Updates the configuration files for an application specified by id.
272 The configFiles parameters is an array of dictionaries of the form:
273 {
274 filename: "blah",
275 content: "line 1\nline 2\n..."
276 }
277 The filename parameter serves as the "id" of each configFile
278 passed in.
279 """
280
281 if not Zuul.checkPermission('Manage DMD'):
282 return DirectResponse.fail("You don't have permission to set update config files", sticky=False)
283
284 facade = self._getFacade()
285 deployedApp = facade.get(id)
286 newConfigs = []
287 for deployedAppConfig in deployedApp.configurations:
288 if deployedAppConfig.filename in [ cf['filename'] for cf in configFiles ]:
289 audit('UI.Applications.UpdateConfigFiles',
290 service=id,
291 servicename=deployedApp.name,
292 filename=deployedAppConfig.filename)
293 deployedAppConfig.content = next((cf['content'] for cf in configFiles if cf['filename'] == deployedAppConfig.filename))
294 newConfigs.append(deployedAppConfig)
295 deployedApp.configurations = newConfigs
296 return DirectResponse.succeed()
297