The Assimilation Project  based on Assimilation version 1.1.7.1474836767
cmainit.py
Go to the documentation of this file.
1 
2 #!/usr/bin/env python
3 # vim: smartindent tabstop=4 shiftwidth=4 expandtab number
4 #
5 # This file is part of the Assimilation Project.
6 #
7 # Copyright (C) 2011, 2012 - Alan Robertson <alanr@unix.sh>
8 #
9 # The Assimilation software is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # The Assimilation software is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
21 #
22 #
23 '''
24 This module provides a class which initializes the CMA.
25 '''
26 
27 import sys, time
28 import logging, logging.handlers
29 import py2neo
30 from py2neo import neo4j
31 from store import Store
32 from cmadb import CMAdb, Neo4jCreds
33 from consts import CMAconsts
34 from graphnodes import GraphNode
35 
36 # R0903: too few public methods
37 # pylint: disable=R0903
38 class CMAinit(object):
39  '''
40  The CMAinit class
41  '''
42  #cmainit.py:43: [R0913:CMAinit.__init__] Too many arguments (9/7)
43  #cmainit.py:43: [R0914:CMAinit.__init__] Too many local variables (17/15)
44  # pylint: disable=R0914,R0913
45  def __init__(self, io, host='localhost', port=7474, cleanoutdb=False, debug=False
46  , retries=300, readonly=False, encryption_required=False, use_network=True
47  , neologin=None, neopass=None):
48  'Initialize and construct a global database instance'
49  #print >> sys.stderr, 'CALLING NEW initglobal'
50  CMAdb.log = logging.getLogger('cma')
51  CMAdb.debug = debug
52  CMAdb.io = io
53  from hbring import HbRing
54  try:
55  syslog = logging.handlers.SysLogHandler(address='/dev/log'
56  , facility=logging.handlers.SysLogHandler.LOG_DAEMON)
57  except EnvironmentError:
58  # Docker doesn't really get along with logging - sigh...
59  # And pylint doesn't like us assigning syslog a different type...
60  # pylint: disable=R0204
61  syslog = logging.StreamHandler()
62  syslog.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
63  CMAdb.log.addHandler(syslog)
64  CMAdb.log.setLevel(logging.DEBUG)
65  hostport = '%s:%s' % (host, port)
66  url = ('https://%s/db/data/' % (hostport))
67  url = ('http://%s/db/data/' % (hostport))
68  Neo4jCreds(neologin=neologin, neopass=neopass).authenticate(hostport)
69  neodb = neo4j.Graph(url)
70  self.db = neodb
71  if cleanoutdb:
72  CMAdb.log.info('Re-initializing the NEO4j database')
73  self.delete_all()
74  self.db = neodb
75  CMAdb.use_network = use_network
76  trycount=0
77  while True:
78  try:
79  CMAdb.cdb = CMAdb(db=neodb)
80  # Neo4j started. All is well with the world.
81  break
82  except (RuntimeError, IOError, py2neo.GraphError) as exc:
83  print >> sys.stderr, 'TRYING AGAIN [%s]...[%s]' % (url, str(exc))
84  trycount += 1
85  if trycount > retries:
86  print >> sys.stderr, ('Neo4j still not started - giving up.')
87  CMAdb.log.critical('Neo4j still not started - giving up.')
88  raise RuntimeError('Neo4j not running - giving up [%s]' % str(exc))
89  if (trycount % 60) == 1:
90  print >> sys.stderr, ('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc)))
91  CMAdb.log.warning('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc)))
92  # Let's try again in a second...
93  time.sleep(1)
94  Store.debug = debug
95  Store.log = CMAdb.log
96  CMAdb.store = Store(neodb, CMAconsts.uniqueindexes, CMAconsts.classkeymap
97  , readonly=readonly)
98 
99  if not readonly:
100  for classname in GraphNode.classmap:
101  GraphNode.initclasstypeobj(CMAdb.store, classname)
102  from transaction import Transaction
103  CMAdb.transaction = Transaction(encryption_required=encryption_required)
104  #print >> sys.stderr, 'CMAdb:', CMAdb
105  #print >> sys.stderr, 'CMAdb.store(cmadb.py):', CMAdb.store
106  CMAdb.TheOneRing = CMAdb.store.load_or_create(HbRing, name='The_One_Ring'
107  , ringtype=HbRing.THEONERING)
108  CMAdb.transaction.commit_trans(io)
109  #print >> sys.stderr, 'COMMITTING Store'
110  #print >> sys.stderr, 'Transaction Commit results:', CMAdb.store.commit()
111  CMAdb.store.commit()
112  #print >> sys.stderr, 'Store COMMITTED'
113  else:
114  CMAdb.transaction = None
115 
116  @staticmethod
117  def uninit():
118  "Undo initialization to make sure we aren't hanging onto any objects"
119  CMAdb.cdb = None
120  CMAdb.transaction = None
121  CMAdb.TheOneRing = None
122  CMAdb.store = None
123  CMAdb.io = None
124 
125 
126  def delete_all(self):
127  'Empty everything out of our database - start over!'
128  dbvers = self.db.neo4j_version
129  if dbvers[0] >= 2:
130  qstring = 'match (n) optional match (n)-[r]-() delete n,r'
131  else:
132  qstring = 'start n=node(*) match n-[r?]-() delete n,r'
133 
134  result = self.db.cypher.execute(qstring)
135  if CMAdb.debug:
136  CMAdb.log.debug('Cypher query to delete all relationships'
137  ' and nonzero nodes executing: %s' % qstring)
138  CMAdb.log.debug('Execution results: %s' % str(result))
139  indexes = self.db.legacy.get_indexes(neo4j.Node)
140  for index in indexes.keys():
141  if CMAdb.debug:
142  CMAdb.log.debug('Deleting index %s' % str(index))
143  self.db.legacy.delete_index(neo4j.Node, index)
144 
145 
146 
147 if __name__ == '__main__':
148  print >> sys.stderr, 'Starting'
149  CMAinit(None, cleanoutdb=True, debug=True)
150  if CMAdb.store.transaction_pending:
151  print >> sys.stderr, 'Transaction pending in:', CMAdb.store
152  print >> sys.stderr, 'Results:', CMAdb.store.commit()
153  print >> sys.stderr, 'Init done'
def __init__(self, io, host='localhost', port=7474, cleanoutdb=False, debug=False, retries=300, readonly=False, encryption_required=False, use_network=True, neologin=None, neopass=None)
Definition: cmainit.py:47
def delete_all(self)
Definition: cmainit.py:126