Package Products :: Package Zuul :: Package routers :: Module application
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.application

  1  ############################################################################## 
  2  # 
  3  # Copyright (C) Zenoss, Inc. 2013, all rights reserved. 
  4  # 
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  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   
24 -class ApplicationRouter(TreeRouter):
25 """ 26 """ 27
28 - def _getFacade(self):
29 return Zuul.getFacade('applications', self.context)
30
31 - def _monitorFacade(self):
32 return Zuul.getFacade('monitors', self.context)
33
34 - def getTree(self, id):
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
62 - def getForm(self, uid):
63 """ 64 Given an object identifier, this returns all of the editable fields 65 on that object as well as their ExtJs xtype that one would 66 use on a client side form. 67 68 @type uid: string 69 @param uid: Unique identifier of an object 70 @rtype: DirectResponse 71 @return: B{Properties} 72 - form: (dictionary) form fields for the object 73 """ 74 app = self._getFacade().get(uid) 75 form = IFormBuilder(IInfo(app)).render(fieldsets=False) 76 form = Zuul.marshal(form) 77 return DirectResponse(form=form)
78
79 - def start(self, uids):
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
119 - def restart(self, uids):
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
139 - def setAutoStart(self, uids, enabled):
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
162 - def getInfo(self, id):
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
175 - def getAllResourcePools(self, query=None):
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
186 - def getApplicationConfigFiles(self, id):
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
195 - def updateConfigFiles(self, id, configFiles):
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