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
21 log = logging.getLogger('zen.ApplicationRouter')
22
23
25 """
26 """
27
29 return Zuul.getFacade('applications', self.context)
30
32 return Zuul.getFacade('monitors', self.context)
33
35 """
36 Returns the tree structure of the application and collector
37 hierarchy.
38
39 @type id: string
40 @param id: Id of the root node of the tree to be returned
41 @rtype: [dictionary]
42 @return: Object representing the tree
43 """
44 try:
45 appfacade = self._getFacade()
46 monitorfacade = Zuul.getFacade("monitors", self.context)
47 nodes = [ITreeNode(m) for m in monitorfacade.query()]
48 for monitor in nodes:
49 apps = appfacade.queryMonitorDaemons(monitor.name)
50 for app in apps:
51 monitor.addChild(IInfo(app))
52 apps = appfacade.queryMasterDaemons()
53 for app in apps:
54 nodes.append(IInfo(app))
55 return Zuul.marshal(nodes)
56 except URLError as e:
57 log.exception(e)
58 return DirectResponse.fail(
59 "Error fetching daemons list: " + str(e.reason)
60 )
61
78
80 """
81 Will issue the command to start the selected ids
82 @type uids: Array[Strings]
83 @param uids: List of valid daemon ids that will need to started
84 @rtype: DirectResposne
85 @return: DirectReponse of success if no errors are encountered
86 """
87
88 if not Zuul.checkPermission('Manage DMD'):
89 return DirectResponse.fail("You don't have permission to start a daemon", sticky=False)
90
91 facade = self._getFacade()
92 for uid in uids:
93 facade.start(uid)
94 audit('UI.Applications.Start', id)
95 if len(uids) > 1:
96 return DirectResponse.succeed("Started %s daemons" % len(uids))
97 return DirectResponse.succeed()
98
99 - def stop(self, uids):
100 """
101 Will issue the command to stop the selected ids
102 @type uids: Array[Strings]
103 @param uids: List of valid daemon ids that will need to stopped
104 @rtype: DirectResposne
105 @return: DirectReponse of success if no errors are encountered
106 """
107
108 if not Zuul.checkPermission('Manage DMD'):
109 return DirectResponse.fail("You don't have permission to stop a daemon", sticky=False)
110
111 facade = self._getFacade()
112 for uid in uids:
113 facade.stop(uid)
114 audit('UI.Applications.Stop', id)
115 if len(uids) > 1:
116 return DirectResponse.succeed("Stopped %s daemons" % len(uids))
117 return DirectResponse.succeed()
118
120 """
121 Will issue the command to restart the selected ids
122 @type uids: Array[Strings]
123 @param uids: List of valid daemon ids that will need to restarted
124 @rtype: DirectResposne
125 @return: DirectReponse of success if no errors are encountered
126 """
127
128 if not Zuul.checkPermission('Manage DMD'):
129 return DirectResponse.fail("You don't have permission to restart a daemon", sticky=False)
130
131 facade = self._getFacade()
132 for uid in uids:
133 facade.restart(uid)
134 audit('UI.Applications.Restart', id)
135 if len(uids) > 1:
136 return DirectResponse.succeed("Restarted %s daemons" % len(uids))
137 return DirectResponse.succeed()
138
140 """
141 Enables or disables autostart on all applications passed into uids.
142 If it is already in that state it is a noop.
143 @type uids: Array[Strings]
144 @param uids: List of valid daemon ids that will need to enabled
145 @type enabled: boolean
146 @param uids: true for enabled or false for disabled
147 @rtype: DirectResposne
148 @return: DirectReponse of success if no errors are encountered
149 """
150
151 if not Zuul.checkPermission('Manage DMD'):
152 return DirectResponse.fail("You don't have permission to set autostart", sticky=False)
153
154 facade = self._getFacade()
155 applications = facade.query()
156 for app in applications:
157 if app.id in uids:
158 app.autostart = enabled
159 audit('UI.Applications.AutoStart', id, {'autostart': enabled})
160 return DirectResponse.succeed()
161
163 """
164 Returns the serialized info object for the given id
165 @type: id: String
166 @param id: Valid id of a application
167 @rtype: DirectResponse
168 @return: DirectResponse with data of the application
169 """
170 facade = self._getFacade()
171 app = facade.get(id)
172 data = Zuul.marshal(IInfo(app))
173 return DirectResponse.succeed(data=data)
174
176 """
177 Returns a list of resource pool identifiers.
178 @rtype: DirectResponse
179 @return: B{Properties}:
180 - data: ([String]) List of resource pool identifiers
181 """
182 pools = self._monitorFacade().queryPools()
183 ids = (dict(name=p.id) for p in pools)
184 return DirectResponse.succeed(data=Zuul.marshal(ids))
185
187 """
188 Returns all the configuration files for an application
189 """
190 facade = self._getFacade()
191 info = IInfo(facade.get(id))
192 files = info.configFiles
193 return DirectResponse.succeed(data=Zuul.marshal(files))
194
196 """
197 Updates the configuration files for an application specified by id.
198 The configFiles parameters is an array of dictionaries of the form:
199 {
200 filename: "blah",
201 content: "line 1\nline 2\n..."
202 }
203 The filename parameter serves as the "id" of each configFile
204 passed in.
205 """
206
207 if not Zuul.checkPermission('Manage DMD'):
208 return DirectResponse.fail("You don't have permission to set update config files", sticky=False)
209
210 facade = self._getFacade()
211 deployedApp = facade.get(id)
212 newConfigs = []
213 for deployedAppConfig in deployedApp.configurations:
214 if deployedAppConfig.filename in [ cf['filename'] for cf in configFiles ]:
215 deployedAppConfig.content = next((cf['content'] for cf in configFiles if cf['filename'] == deployedAppConfig.filename))
216 newConfigs.append(deployedAppConfig)
217 deployedApp.configurations = newConfigs
218 return DirectResponse.succeed()
219