ifeq (,$(SSHURL))
@echo "sync: I need more info from the command line, e.g."
@echo " make sync PLC=boot.planetlab.eu"
- @echo " make sync PLCHOSTLXC=.. GUESTNAME=.."
+ @echo " make sync PLCHOSTLXC=.. GUESTHOSTNAME=.. GUESTNAME=.."
@exit 1
else
+$(RSYNC) plcsh PLC planetlab5.sql migrations aspects $(SSHURL)/usr/share/plc_api/
+$(RSYNC) db-config.d/ $(SSHURL)/etc/planetlab/db-config.d/
+$(RSYNC) plc.d/ $(SSHURL)/etc/plc.d/
+ +$(RSYNC) apache/plc.wsgi $(SSHURL)/usr/share/plc_api/apache/
$(SSHCOMMAND) /etc/plc.d/httpd stop
$(SSHCOMMAND) /etc/plc.d/httpd start
endif
# Load configuration
self.config = Config(config)
+# print("config has keys {}"
+# .format(vars(self.config).keys()))
# Initialize database connection
if self.config.PLC_DB_TYPE == "postgresql":
# by the Factory, you need to restart your python instance / web server
# as the cached information then becomes wrong
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.TagTypes import TagTypes, TagType
from PLC.Roles import Roles, Role
for rolename in roles_to_delete:
tag_type.remove_role(self.hash_name_to_role[rolename])
except:
- # this goes in boot.log ...
- print >> log, "WARNING, Could not enforce tag type, tagname=%s\n"%tagname
- traceback.print_exc(file=log)
+ logger.exception("WARNING, Could not enforce tag type, tagname={}\n"
+ .format(tagname))
+
else:
# not found: create it
tag_type.add_role(role_obj)
except:
# xxx todo find a more appropriate way of notifying this
- print >> log, "Accessor.locate_or_create_tag: Could not add role %r to tag_type %s"%(role,tagname)
+ logger.exception("Accessor.locate_or_create_tag: "
+ "Could not add role {} to tag_type {}"
+ .format(role,tagname))
self.set_cache(tagname,tag_type)
return tag_type
#
from PLC.Faults import *
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Messages import Message, Messages
from PLC.Persons import Person, Persons
from PLC.Sites import Site, Sites
fault = None):
messages = Messages(method.api, [message_id], enabled = True)
if not messages:
- print >> log, "No such message template '%s'" % message_id
+ logger.error("No such message template '%s'" % message_id)
return 1
message = messages[0]
import sys
import syslog
-class unbuffered:
- """
- Write to /var/log/httpd/error_log. See
-
- http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp
- """
-
- def write(self, data):
- sys.stderr.write(data)
- sys.stderr.flush()
-
-log = unbuffered()
+from PLC.Logger import logger
def profile(callable):
"""
end = time.time()
args = map(str, args)
args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()]
- print >> log, "%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start)
+ logger.info("%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start))
return result
return wrapper
from PLC.Faults import *
from PLC.Parameter import Parameter, Mixed, python_type
+from PLC.Logger import logger
class Filter(Parameter, dict):
"""
if clips:
clip_part += " " + " ".join(clips)
if Filter.debug:
- print >> log, 'Filter.sql: where_part=',where_part,'clip_part',clip_part
- return (where_part,clip_part)
+ logger.debug('Filter.sql: where_part={} - clip_part={}'
+ .format(where_part, clip_part))
+ return where_part, clip_part
import ldap
import traceback
-from PLC.Debug import profile, log
+from PLC.Debug import profile
from PLC.Faults import *
--- /dev/null
+import logging
+import logging.config
+
+# we essentially need one all-purpose logger
+# that goes into /var/log/plcapi.log
+
+plcapi_logging_config = {
+ 'version' : 1,
+ 'disable_existing_loggers' : True,
+ 'formatters': {
+ 'standard': {
+ 'format': '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s',
+ 'datefmt': '%m-%d %H:%M:%S'
+ },
+ 'shorter': {
+ 'format': '%(asctime)s %(levelname)s %(message)s',
+ 'datefmt': '%d %H:%M:%S'
+ },
+ },
+ 'handlers': {
+ 'plcapi': {
+ 'level': 'INFO',
+ 'class': 'logging.FileHandler',
+ 'formatter': 'standard',
+ 'filename' : '/var/log/plcapi.log',
+ },
+ },
+ 'loggers': {
+ 'plcapi': {
+ 'handlers': ['plcapi'],
+ 'level': 'INFO',
+ 'propagate': False,
+ },
+ },
+}
+
+logging.config.dictConfig(plcapi_logging_config)
+
+# general case:
+# from PLC.Logger import logger
+logger = logging.getLogger('plcapi')
+
+#################### test
+if __name__ == '__main__':
+ logger.info("in plcapi")
from PLC.Faults import *
from PLC.Parameter import Parameter, Mixed, python_type, xmlrpc_type
from PLC.Auth import Auth
-from PLC.Debug import profile, log
+from PLC.Debug import profile
from PLC.Events import Event, Events
from PLC.Nodes import Node, Nodes
from PLC.Persons import Person, Persons
from PLC.Namespace import email_to_hrn
from PLC.TagTypes import TagTypes
-from PLC.Debug import log
+from PLC.Logger import logger
class AddPersonToSite(Method):
"""
try:
had_no_site= (len (person['site_ids']) == 0)
if had_no_site:
- login_base=site['login_base']
+ login_base = site['login_base']
root_auth = self.api.config.PLC_HRN_ROOT
- hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
- tagname='hrn'
+ hrn = email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
+ tagname = 'hrn'
tag_type = TagTypes(self.api,{'tagname':tagname})[0]
- person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
+ person_tags = PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
if not person_tags:
person_tag = PersonTag(self.api)
person_tag['person_id'] = person['person_id']
person_tag = person_tags[0]
person_tag['value'] = hrn
person_tag.sync()
- except Exception,e:
- print >> log, "BEG Warning, cannot maintain person's hrn, %s"%e
- import traceback
- traceback.print_exc(file=log)
- print >> log, "END Warning, cannot maintain person's hrn, %s"%e
-
+ except Exception as e:
+ logger.exception("ERROR cannot maintain person's hrn, {}"
+ .format(person_id_or_email))
return 1
from PLC.Methods.AddSliceTag import AddSliceTag
from PLC.Methods.UpdateSliceTag import UpdateSliceTag
-from PLC.Debug import log
+from PLC.Logger import logger
can_update = ['name', 'instantiation', 'url', 'description', 'max_nodes']
for value in values:
AddSliceTag(self.api).__call__(auth,slice['slice_id'],'vsys',value)
except:
- print >> log, "Could not set vsys tags as configured in PLC_VSYS_DEFAULTS"
- import traceback
- traceback.print_exc(file=log)
+ logger.exception("Could not set vsys tags as configured in PLC_VSYS_DEFAULTS")
self.event_objects = {'Slice': [slice['slice_id']]}
self.message = "Slice %d created" % slice['slice_id']
from PLC.InterfaceTags import InterfaceTag, InterfaceTags
from PLC.NodeTags import NodeTag, NodeTags
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Accessors.Accessors_standard import * # import node accessors
def cleantrash (self):
for file in self.trash:
if self.DEBUG:
- print >> log, 'DEBUG -- preserving',file
+ logger.debug('DEBUG -- preserving trash file {}'.format(file))
else:
os.unlink(file)
# regular node, make build's arguments
# and build the full command line to be called
if node_type not in [ 'regular', 'reservable' ]:
- print >> log, "GetBootMedium.build_command: unexpected node_type {}".format(node_type)
+ logger.error("GetBootMedium.build_command: unexpected node_type {}".format(node_type))
return command, None
build_sh_options=""
build_sh_options,
log_file)
- print >> log, "The build command line is {}".format(command)
+ logger.info("The build command line is {}".format(command))
return command, log_file
raise PLCInvalidArgument("No such node {}".format(node_id_or_hostname))
node = nodes[0]
- print >> log, "GetBootMedium: {} requested on node {}. Node type is: {}"\
- .format(action, node['node_id'], node['node_type'])
+ logger.info("GetBootMedium: {} requested on node {}. Node type is: {}"\
+ .format(action, node['node_id'], node['node_type']))
# check the required action against the node type
node_type = node['node_type']
result = file(node_image).read()
self.trash.append(node_image)
self.cleantrash()
- print >> log, "GetBootMedium - done with build.sh"
+ logger.info("GetBootMedium - done with build.sh")
encoded_result = base64.b64encode(result)
- print >> log, "GetBootMedium - done with base64 encoding - lengths: raw={} - b64={}"\
- .format(len(result), len(encoded_result))
+ logger.info("GetBootMedium - done with base64 encoding - lengths: raw={} - b64={}"
+ .format(len(result), len(encoded_result)))
return encoded_result
except:
self.cleantrash()
import traceback
+from PLC.Logger import logger
from PLC.Method import Method
from PLC.Auth import Auth
from PLC.Faults import *
for assign in assigns:
(left,right)=[x.strip() for x in assign.split(':')]
if right not in GetNodeFlavour.known_virts:
- print >> log, "GetNodeFlavour, unknown 'virt' %s - ignored" % right
+ logger.error("GetNodeFlavour, unknown 'virt' %s - ignored" % right)
continue
for fcdistro in [ x.strip() for x in left.split(',')]:
map[fcdistro]=right
except:
- print >> log, "GetNodeFlavour, issue with parsing PLC_FLAVOUR_VIRT_MAP=%s - returning '%s'"%\
- (self.api.config.PLC_FLAVOUR_VIRT_MAP,GetNodeFlavour.default_virt)
- traceback.print_exc(file=log)
+ logger.exception("GetNodeFlavour, issue with parsing PLC_FLAVOUR_VIRT_MAP=%s - returning '%s'"%\
+ (self.api.config.PLC_FLAVOUR_VIRT_MAP, GetNodeFlavour.default_virt))
return GetNodeFlavour.default_virt
# print 'virt_from_virt_map, using map',map
if node_fcdistro in map: return map[node_fcdistro]
from PLC.Persons import Person, Persons
from PLC.Sites import Site, Sites
from PLC.Auth import Auth
+from PLC.Logger import logger
hidden_fields = ['password', 'verification_key', 'verification_expires']
returns = [return_fields]
def call(self, auth, person_filter = None, return_fields = None):
+
+ logger.info("incoming GetPersons, filter={}, return fields={}"
+ .format(person_filter, return_fields))
+
# If we are not admin, make sure to only return viewable accounts
if isinstance(self.caller, Person) and \
'admin' not in self.caller['roles']:
import fcntl
import time
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Faults import *
from PLC.Method import Method
from PLC.Parameter import Parameter, Mixed
#focus_ids=[621,1088]
#################### helpers
-def message (to_print=None,verbose_only=False):
+def message(to_print=None, verbose_only=False):
if verbose_only and not verbose:
return
- print >> log, time.strftime("%m-%d-%H-%M-%S:"),
- if to_print:
- print >>log, to_print
+ logger.info(to_print)
def message_verbose(to_print=None, header='VERBOSE'):
- message("%s> %r"%(header,to_print),verbose_only=True)
+ message("%s> %r"%(header, to_print), verbose_only=True)
#################### to avoid several instances running at the same time
return peer_object_id in focus_ids or \
(object and primary_key in object and object[primary_key] in focus_ids)
- def message_focus (message):
+ def message_focus(message):
if in_focus():
# always show remote
message_verbose("peer_obj : %d [[%r]]"%(peer_object_id,peer_object),
return True
else:
result=True
-# print >> log, 'COMPARING ',
for column in columns:
test= object[column] == peer_object[column]
-# print >> log, column,test,
if not test: result=False
-# print >> log, '=>',result
return result
# Add/update new/existing objects
for peer_object_id, peer_object in peer_objects.iteritems():
peer_object_name=""
if secondary_key: peer_object_name="(%s)"%peer_object[secondary_key]
- message_verbose ('%s peer_object_id=%d %s (%d/%d)'%(classname,peer_object_id,peer_object_name,count,total))
+ message_verbose('%s peer_object_id=%d %s (%d/%d)'
+ %(classname,peer_object_id,peer_object_name,count,total))
count += 1
if peer_object_id in synced:
message("Warning: %s Skipping already added %s: %r"%(
if not equal_fields(object,peer_object, columns):
# Only update intrinsic fields
object.update(object.db_fields(peer_object))
- message_focus ("DIFFERENCES : updated / syncing")
+ message_focus("DIFFERENCES : updated / syncing")
sync = True
action = "changed"
else:
- message_focus ("UNCHANGED - left intact / not syncing")
+ message_focus("UNCHANGED - left intact / not syncing")
sync = False
action = None
object = classobj(self.api, peer_object)
# Replace foreign identifier with new local identifier
del object[primary_key]
- message_focus ("NEW -- created with clean id - syncing")
+ message_focus("NEW -- created with clean id - syncing")
sync = True
action = "added"
if sync:
- message_verbose("syncing %s %d - commit_mode=%r"%(classname,peer_object_id,commit_mode))
+ message_verbose("syncing %s %d - commit_mode=%r"
+ %(classname,peer_object_id,commit_mode))
try:
object.sync(commit = commit_mode)
except PLCInvalidArgument, err:
# Skip if validation fails
# XXX Log an event instead of printing to logfile
- message("Warning: %s Skipping invalid %s %r : %r"%(\
- peer['peername'], classname, peer_object, err))
+ message("Warning: %s Skipping invalid %s %r : %r"%
+ (peer['peername'], classname, peer_object, err))
continue
synced[peer_object_id] = object
if action:
- message("%s: (%d/%d) %s %d %s %s"%(peer['peername'], count,total, classname,
- object[primary_key], peer_object_name, action))
+ message("%s: (%d/%d) %s %d %s %s"
+ %(peer['peername'], count,total, classname,
+ object[primary_key], peer_object_name, action))
message_verbose("Exiting sync on %s"%classname)
sites_at_peer = dict([(site['site_id'], site) for site in peer_tables['Sites']])
# Synchronize new set (still keyed on foreign site_id)
- peer_sites = sync(old_peer_sites, sites_at_peer, Site, ignore (columns, RefreshPeer.ignore_site_fields))
+ peer_sites = sync(old_peer_sites, sites_at_peer, Site,
+ ignore(columns, RefreshPeer.ignore_site_fields))
for peer_site_id, site in peer_sites.iteritems():
# Bind any newly cached sites to peer
continue
# Synchronize new set (still keyed on foreign key_id)
- peer_keys = sync(old_peer_keys, keys_at_peer, Key, ignore (columns, RefreshPeer.ignore_key_fields))
+ peer_keys = sync(old_peer_keys, keys_at_peer, Key,
+ ignore(columns, RefreshPeer.ignore_key_fields))
for peer_key_id, key in peer_keys.iteritems():
# Bind any newly cached keys to peer
if peer_key_id not in old_peer_keys:
# XXX Do we care about membership in foreign site(s)?
# Synchronize new set (still keyed on foreign person_id)
- peer_persons = sync(old_peer_persons, persons_at_peer, Person, ignore (columns, RefreshPeer.ignore_person_fields))
+ peer_persons = sync(old_peer_persons, persons_at_peer, Person,
+ ignore(columns, RefreshPeer.ignore_person_fields))
# transcoder : retrieve a local key_id from a peer_key_id
key_transcoder = dict ( [ (key['key_id'],peer_key_id) \
from types import StringTypes
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Faults import *
from PLC.Method import Method
from PLC.Parameter import Parameter, Mixed
Subject = message['subject'] % params,
Body = message['template'] % params)
else:
- print >> log, "Warning: No message template '%s'" % message_id
+ logger.warning("No message template '%s'" % message_id)
# Logging variables
self.event_objects = {'Person': [person['person_id']]}
import time
import urllib
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Faults import *
from PLC.Method import Method
from PLC.Parameter import Parameter, Mixed
Subject = message['subject'] % params,
Body = message['template'] % params)
else:
- print >> log, "Warning: No message template '%s'" % message_id
+ logger.warning("No message template '%s'" % message_id)
# Logging variables
self.event_objects = {'Person': [person['person_id']]}
from urlparse import urlparse
import PLC.Auth
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Faults import *
from PLC.Namespace import hostname_to_hrn
from PLC.Parameter import Parameter, Mixed
tags = {'hrn': hrn}
Node(self.api, node).update_tags(tags)
except:
- print >> log, "WARNING: (beg) could not find out hrn on hostname=%s"%node['hostname']
- traceback.print_exc(5,file=log)
- print >> log, "WARNING: (end) could not find out hrn on hostname=%s"%node['hostname']
+ logger.exception("Could not find out hrn on hostname=%s"%node['hostname'])
def remove_node(self, node, commit = True):
"""
import crypt
from PLC.Faults import *
-from PLC.Debug import log
from PLC.Parameter import Parameter, Mixed
from PLC.Filter import Filter
from PLC.Table import Row, Table
import re
from pprint import pformat
-from PLC.Debug import profile, log
+from PLC.Logger import logger
+from PLC.Debug import profile
from PLC.Faults import *
from datetime import datetime as DateTimeType
if not params:
if self.debug:
- print >> log,'execute0',query
+ logger.debug('execute0: {}'.format(query))
cursor.execute(query)
- elif isinstance(params,dict):
+ elif isinstance(params, dict):
if self.debug:
- print >> log,'execute-dict: params',params,'query',query%params
- cursor.execute(query,params)
+ logger.debug('execute-dict: params {} query {}'
+ .format(params, query%params))
+ cursor.execute(query, params)
elif isinstance(params,tuple) and len(params)==1:
if self.debug:
- print >> log,'execute-tuple',query%params[0]
+ logger.debug('execute-tuple {}'.format(query%params[0]))
cursor.execute(query,params[0])
else:
param_seq=(params,)
if self.debug:
for params in param_seq:
- print >> log,'executemany',query%params
+ logger.debug('executemany {}'.format(query%params))
cursor.executemany(query, param_seq)
(self.rowcount, self.description, self.lastrowid) = \
(cursor.rowcount, cursor.description, cursor.lastrowid)
except:
pass
uuid = commands.getoutput("uuidgen")
- print >> log, "Database error %s:" % uuid
- print >> log, e
- print >> log, "Query:"
- print >> log, query
- print >> log, "Params:"
- print >> log, pformat(params)
+ message = "Database error {}: - Query {} - Params {}".format(uuid, query, pformat(params))
+ logger.exception(message)
raise PLCDBError("Please contact " + \
self.api.config.PLC_NAME + " Support " + \
"<" + self.api.config.PLC_MAIL_SUPPORT_ADDRESS + ">" + \
from email.Header import Header
from smtplib import SMTP
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.Faults import *
def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
msg['X-Mailer'] = "Python/" + sys.version.split(" ")[0]
if not api.config.PLC_MAIL_ENABLED:
- print >> log, "From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg
+ logger.info("PLC_MAIL_ENABLED not set")
+ logger.info("From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg)
return
s = SMTP()
import xmlrpclib
from mod_python import apache
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.API import PLCAPI
api = PLCAPI()
return apache.OK
- except Exception, err:
- # Log error in /var/log/httpd/(ssl_)?error_log
- t = "[" + time.ctime() + "] [error]"
- print >> log, t, err, traceback.format_exc()
+ except Exception as err:
+ logger.exception("INTERNAL ERROR !!")
return apache.HTTP_INTERNAL_SERVER_ERROR
import xmlrpclib
from mod_python import apache
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.API import PLCAPI
api = PLCAPI()
return apache.OK
except Exception, err:
- # Log error in /var/log/httpd/(ssl_)?error_log
- print >> log, err, traceback.format_exc()
+ logger.exception("INTERNAL ERROR !!")
return apache.HTTP_INTERNAL_SERVER_ERROR
sys.path.append('/usr/share/plc_api')
sys.stdout = sys.stderr
import traceback
-from PLC.Debug import log
+from PLC.Logger import logger
from PLC.API import PLCAPI
def application(environ, start_response):
# Shut down database connection, otherwise up to MaxClients DB
# connections will remain open.
api.db.close()
- except Exception, err:
+ except Exception as err:
status = '500 Internal Server Error'
content_type = 'text/html'
output = 'Internal Server Error'
- print >> log, err, traceback.format_exc()
+ logger.exception("INTERNAL ERROR !!")
# Write response
response_headers = [('Content-type', '%s' % content_type),
slice_nodes = plc_api.GetNodes(auth,node_ids,['hostname'])
# store in a file
-f=open('mynodes.txt','w')
-for node in slice_nodes:
- print >>f,node['hostname']
+with ('mynodes.txt','a') as f:
+ for node in slice_nodes:
+ f.write(node['hostname'] + "\n")
f.close()
</programlisting>
</section>
def parse (self):
if self.output:
- outfile = open(self.output,"w")
+ outfile = open(self.output, "a")
else:
outfile = sys.stdout
contents = file(self.input).read()
out_line=match.group(1)
match=Schema.view.match(out_line)
if match:
- print >>outfile, out_line,';'
+ outfile.write("{};\n".format(out_line))
if outfile != sys.stdout:
outfile.close()