Package Products :: Package ZenModel :: Module zenbuild |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.zenbuild

      1  #!/usr/bin/env python
      2  ##############################################################################
      3  #
      4  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      5  #
      6  # This content is made available according to terms specified in
      7  # License.zenoss under the directory where your Zenoss product is installed.
      8  #
      9  ##############################################################################
     10
     11
     12  __doc__="""zenbuild
     13
     14  Build the zentinel portal object and the dmd database
     15
     16  $Id: DmdBuilder.py,v 1.11 2004/04/06 22:33:07 edahl Exp $"""
     17
     18  __version__ = "$Revision: 1.11 $"[11:-2]
     19
     20  import os
     21  import Globals
     22  import transaction
     23  import subprocess
     24  import sys
     25
     26  from Products.ZenUtils.Utils import zenPath
     27
     28  from Products.ZenUtils import Security
     29  from Products.ZenUtils.CmdBase import CmdBase
     30  from Products.PluggableAuthService import plugins
     31
     32 -class zenbuild(CmdBase):
     33
     34      sitename = "zport"
     35
     36 -    def connect(self):
     37          zopeconf = zenPath("etc","zope.conf")
     38          import Zope2
     39          Zope2.configure(zopeconf)
     40          self.app = Zope2.app()
     41
     42 -    def buildOptions(self):
     43          CmdBase.buildOptions(self)
     44          self.parser.add_option('--xml', dest="fromXml",
     45                  action='store_true', default=False,
     46                  help="Load data from XML files instead of SQL")
     47          self.parser.add_option('-s','--evthost', dest="evthost",
     48                  default="127.0.0.1", help="events database hostname")
     49          self.parser.add_option('-u','--evtuser', dest="evtuser", default="root",
     50                  help="username used to connect to the events database")
     51          self.parser.add_option('-p','--evtpass', dest="evtpass", default="",
     52                  help="password used to connect to the events database")
     53          self.parser.add_option('-d','--evtdb', dest="evtdb", default="events",
     54                  help="name of events database")
     55          self.parser.add_option('-t','--evtport', dest="evtport",
     56                  type='int', default=3306,
     57                  help="port used to connect to the events database")
     58          self.parser.add_option('--smtphost', dest="smtphost", default="localhost",
     59                  help="smtp host")
     60          self.parser.add_option('--smtpport', dest="smtpport", default=25,
     61                  type='int', help="smtp port")
     62
     63          self.parser.add_option('--pagecommand', dest="pagecommand", default="$ZENHOME/bin/zensnpp localhost 444 $RECIPIENT",
     64                  help="page command")
     65          # amqp stuff
     66          self.parser.add_option('--amqphost', dest="amqphost", default="localhost",
     67                                 help="AMQP Host Location")
     68          self.parser.add_option('--amqpport', dest="amqpport", default=5672,
     69                                 type='int', help="AMQP Server Port")
     70          self.parser.add_option('--amqpvhost', dest="amqpvhost", default="/zenoss",
     71                                 help="Default Virtual Host")
     72          self.parser.add_option('--amqpuser', dest="amqpuser", default="zenoss",
     73                                 help="AMQP User Name")
     74          self.parser.add_option('--amqppassword', dest="amqppassword", default="zenoss",
     75                                 help="AMQP Password")
     76
     77          from zope.component import getUtility
     78          from Products.ZenUtils.ZodbFactory import IZodbFactoryLookup
     79          connectionFactory = getUtility(IZodbFactoryLookup).get()
     80          connectionFactory.buildOptions(self.parser)
     81          self.connectionFactory = connectionFactory
     82
     83
     84 -    def zodbConnect(self):
     85          """
     86          Used to connect to ZODB (without going through the entire ZOPE
     87          initialization process. This allows us to get a lightweight
     88          connection to the database to test to see if the database is already
     89          initialized.
     90          """
     91          from zope.component import getUtility
     92          from Products.ZenUtils.ZodbFactory import IZodbFactoryLookup
     93          connectionFactory = getUtility(IZodbFactoryLookup).get()
     94          self.db, self.storage = connectionFactory.getConnection(**self.options.__dict__)
     95
     96 -    def build(self):
     97          self.db = None
     98          self.storage = None
     99
    100          conn = None
    101          try:
    102              self.zodbConnect()
    103              conn = self.db.open()
    104              root = conn.root()
    105              app = root.get('Application')
    106              if app and getattr(app, self.sitename, None) is not None:
    107                  print "zport portal object exists; exiting."
    108                  return
    109          except self.connectionFactory.exceptions.OperationalError as e:
    110              print "zenbuild: Database does not exists."
    111              sys.exit(1)
    112          finally:
    113              if conn:
    114                  conn.close()
    115              if self.db:
    116                  self.db.close()
    117                  self.db = None
    118              if self.storage:
    119                  self.storage.close()
    120                  self.storage = None
    121
    122          # TODO: remove the port condition, in here for now because there is no SQL dump of postgresql
    123          if self.options.fromXml:
    124              self.connect()
    125              from Products.ZenModel.ZentinelPortal import manage_addZentinelPortal
    126              manage_addZentinelPortal(self.app, self.sitename)
    127              site = self.app._getOb(self.sitename)
    128
    129              # build index_html
    130              if self.app.hasObject('index_html'):
    131                  self.app._delObject('index_html')
    132              from Products.PythonScripts.PythonScript import manage_addPythonScript
    133              manage_addPythonScript(self.app, 'index_html')
    134              newIndexHtml = self.app._getOb('index_html')
    135              text = 'container.REQUEST.RESPONSE.redirect("/zport/dmd/")\n'
    136              newIndexHtml.ZPythonScript_edit('', text)
    137
    138              # build standard_error_message
    139              if self.app.hasObject('standard_error_message'):
    140                  self.app._delObject('standard_error_message')
    141              file = open(zenPath('Products/ZenModel/dtml/standard_error_message.dtml'))
    142              try:
    143                  text = file.read()
    144              finally:
    145                  file.close()
    146              import OFS.DTMLMethod
    147              OFS.DTMLMethod.addDTMLMethod(self.app, id='standard_error_message',
    148                                              file=text)
    149
    150              # Convert the acl_users folder at the root to a PAS folder and update
    151              # the login form to use the Zenoss login form
    152              Security.replaceACLWithPAS(self.app, deleteBackup=True)
    153
    154              # Add groupManager to zport.acl
    155              acl = site.acl_users
    156              if not hasattr(acl, 'groupManager'):
    157                  plugins.ZODBGroupManager.addZODBGroupManager(acl, 'groupManager')
    158              acl.groupManager.manage_activateInterfaces(['IGroupsPlugin',])
    159
    160              trans = transaction.get()
    161              trans.note("Initial ZentinelPortal load by zenbuild.py")
    162              trans.commit()
    163              print "ZentinelPortal loaded at %s" % self.sitename
    164
    165              # build dmd
    166              from Products.ZenModel.DmdBuilder import DmdBuilder
    167              dmdBuilder = DmdBuilder(site,
    168                                      self.options.evthost,
    169                                      self.options.evtuser,
    170                                      self.options.evtpass,
    171                                      self.options.evtdb,
    172                                      self.options.evtport,
    173                                      self.options.smtphost,
    174                                      self.options.smtpport,
    175                                      self.options.pagecommand)
    176              dmdBuilder.build()
    177              transaction.commit()
    178
    179              # Load XML Data
    180              from Products.ZenModel.XmlDataLoader import XmlDataLoader
    181              dl = XmlDataLoader(noopts=True, app=self.app)
    182              dl.loadDatabase()
    183
    184          else:
    185
    186              cmd = "gunzip -c  %s | %s --usedb=zodb" % (
    187                   zenPath("Products/ZenModel/data/zodb.sql.gz"),
    188                   zenPath("Products/ZenUtils/ZenDB.py"),
    189              )
    190              returncode = os.system(cmd)
    191              if returncode:
    192                  print >> sys.stderr, "There was a problem creating the database from the sql dump."
    193                  sys.exit(1)
    194
    195              # Relstorage may have already loaded items into the cache in the
    196              # initial connection to the database. We have to expire everything
    197              # in the cache in order to prevent errors with overlapping
    198              # transactions from the model which was just imported above.
    199              if self.options.zodb_cacheservers:
    200                  self.flush_memcached(self.options.zodb_cacheservers.split())
    201
    202              self.connect()
    203
    204              # Set all the attributes
    205              site = getattr(self.app, self.sitename, None)
    206              site.dmd.smtpHost = self.options.smtphost
    207              site.dmd.smtpPort = self.options.smtpport
    208              site.dmd.pageCommand = self.options.pagecommand
    209              site.dmd.uuid = None
    210              site.dmd._rq = False
    211              for evmgr in (site.dmd.ZenEventManager, site.dmd.ZenEventHistory):
    212                  evmgr.username = self.options.evtuser
    213                  evmgr.password = self.options.evtpass
    214                  evmgr.database = self.options.evtdb
    215                  evmgr.host = self.options.evthost
    216                  evmgr.port = self.options.evtport
    217              transaction.commit()
    218
    219          # Load reports
    220          from Products.ZenReports.ReportLoader import ReportLoader
    221          rl = ReportLoader(noopts=True, app=self.app)
    222          rl.loadDatabase()
    223
    224 -    def flush_memcached(self, cacheservers):
    225          import memcache
    226          mc = memcache.Client(cacheservers, debug=0)
    227          mc.flush_all()
    228          mc.disconnect_all()
    229
    230  if __name__ == "__main__":
    231      zb = zenbuild(args=sys.argv)
    232      zb.build()
    233