Bug fix with factory and actor initialization

data-collector
Steve L. Nyemba 7 years ago
parent a9574f741c
commit 06180c42a8

@ -22,22 +22,31 @@ from utils.params import PARAMS
import smtplib import smtplib
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from datetime import datetime
from StringIO import StringIO
from utils.services import Dropbox, Google
class Actor(): class Actor():
@staticmethod @staticmethod
def instance(name,args): def instance(name,args,logger=None):
""" """
This function is a singleton that acts as a factory object for all the instances of this subclass This function is a singleton that acts as a factory object for all the instances of this subclass
@param name name of the class to instantiate @param name name of the class to instantiate
@param args arguments to be passed in {configuration} @param args arguments to be passed in {configuration}
""" """
o = None r = []
try: if not isinstance(name,list):
o = eval("".join([name,"()"])) name = [name]
o.init(args) for id in name :
except Exception,e: try:
print str(e) o = eval("".join([id,"()"]))
return o o.Initialize(args,logger)
r.append(o)
except Exception,e:
if logger is not None :
logger.log(subject='Actor',object='Factory',action='error',value=e.message)
print str(e)
return r[0] if len(r) == 1 else r
def __init__(self): def __init__(self):
""" """
Initializing the class with configuration. The configuration will be specific to each subclass Initializing the class with configuration. The configuration will be specific to each subclass
@ -51,8 +60,9 @@ class Actor():
# def getIdentifier(self): # def getIdentifier(self):
# return self.__class__.__name__.lower() # return self.__class__.__name__.lower()
def init(self,args): def Initialize(self,args,logger=None):
self.config = args self.config = args
self.logger = logger
def isValid(self,**item): def isValid(self,**item):
return False return False
@ -67,6 +77,10 @@ class Actor():
pass pass
def post(self,**args): def post(self,**args):
pass pass
def log(self,**args):
if self.logger :
args['subject'] = self.getName()
self.logger.log(args)
class Apps(Actor) : class Apps(Actor) :
""" """
This class is designed to handle application, restart, if need be. This class is designed to handle application, restart, if need be.
@ -99,14 +113,17 @@ class Apps(Actor) :
@param args {"apps_o":"","app_x":params} @param args {"apps_o":"","app_x":params}
""" """
self.action = action self.action = action
self.params = params self.params = params
self.log(action='init',object=action,value=params)
def startup(self,cmd) : def startup(self,cmd) :
""" """
This function is intended to start a program given the configuration This function is intended to start a program given the configuration
@TODO We need to find the command in case the app has crashed
""" """
try: try:
os.system(cmd +" &") os.system(cmd +" &")
self.log(action='startup',value=cmd)
except Exception, e: except Exception, e:
print e print e
@ -206,21 +223,38 @@ class Folders(Actor):
{threshold:value} {threshold:value}
@params threshold in terms of size, or age. It will be applied to all folders @params threshold in terms of size, or age. It will be applied to all folders
""" """
def init(self,action,params):
def init(self,args): self.action = action
# print args
# def init(self,args):
"""
This is initialized with parameters from the plan.
The parameters should be specific to the actor (folder)
folder_size
"""
#self.lfolders = args['folders'] #config['folders'] #self.lfolders = args['folders'] #config['folders']
#self.action = args['action'] #{clear,archive} config['actions']['folders'] #self.action = args['action'] #{clear,archive} config['actions']['folders']
self.threshold = self.get_size( args['threshold']) #self.config['threshold'])
plan = params['plan']
self.threshold = self.get_size( plan['folder_size']) #self.config['threshold'])
# self.action = action
self.params = params
def isValid(self,**args): def isValid(self,**args):
action = args['action'] action = args['action']
params = args['params'] params = args['params']
p = action in ['clean','archive','backup'] p = len(set(action) & set(['clean','archive','backup'])) > 0
q = False q = False
r = False r = False
if p : if p :
q = params.keys()[0] in ['label','folder'] q = len(set(params.keys()) & set( ['label','folder'])) > 0
r = os.path.exists(params.values[0]) if q :
folder = params['label'] if 'label' in params else params['folder']
r = os.path.exists(folder)
return p and q and r return p and q and r
def archive(self,item): def archive(self,item):
""" """
@ -231,10 +265,13 @@ class Folders(Actor):
folder = item['label'] folder = item['label']
name = folder.split(os.sep) name = folder.split(os.sep)
name = name[len(name)-1] name = name[len(name)-1]
date = ''.join([str(i) for i in item['date'].values()]) date = str(datetime.now()).replace(' ','@')#''.join([str(i) for i in item['date'].values()])
signature='-'.join([name,date,str(item['stats']['file_count']),'files'])
# signature='-'.join([name,date,str(item['stats']['file_count']),'files'])
signature='-'.join([name,date])
tarball=os.sep.join([folder,'..',signature]) tarball=os.sep.join([folder,'..',signature])
shutil.make_archive(tarball,'tar',folder) shutil.make_archive(tarball,'tar',folder)
#self.clean(item) #self.clean(item)
# #
# @TODO: The archive can be uploaded to the cloud or else where # @TODO: The archive can be uploaded to the cloud or else where
@ -242,7 +279,34 @@ class Folders(Actor):
# @param key authorization key for the given service # @param key authorization key for the given service
# #
pass pass
return tarball+".tar"
def backup(self,tarball):
"""
This function will initiate backup to the cloud given
"""
if os.path.exists(tarball) :
key = self.params['key']
sid = self.params['user']['sid']
if sid == 'dropbox' :
cloud = Dropbox()
elif sid == 'google-drive' :
cloud = Google()
cloud.init(key)
file = open(tarball)
out = cloud.upload('backup','application/octet-stream',file)
file.close()
print out
pass
else:
pass
print tarball
print self.params['user']['sid']
print self.params['key']
#
# let's upload to the cloud
pass
def clean(self,item): def clean(self,item):
""" """
This function consists in deleting files from a given folder This function consists in deleting files from a given folder
@ -257,7 +321,7 @@ class Folders(Actor):
os.remove(path) os.remove(path)
# #
# #
def get_size(self,value): def get_size(self,value):
""" """
converts size values into MB and returns the value without units converts size values into MB and returns the value without units
@ -283,7 +347,15 @@ class Folders(Actor):
p = os.path.exists(item['label']) and item['label'] in self.lfolders p = os.path.exists(item['label']) and item['label'] in self.lfolders
q = item['stats']['size']['mean'] >= self.threshold and self.threshold > 0 q = item['stats']['size']['mean'] >= self.threshold and self.threshold > 0
return p and q return p and q
def run(self):
tarball = None
if 'archive' in self.action :
tarball = self.archive(self.params)
if 'backup' in self.action and tarball:
self.backup(tarball)
if 'delete' in self.action and self.can_clean():
self.clean()
def analyze(self,logs): def analyze(self,logs):
r = {'clean':self.clean,'archive':self.archive} r = {'clean':self.clean,'archive':self.archive}
self.lfolders = [ folder['label'] for folder in logs] self.lfolders = [ folder['label'] for folder in logs]

Loading…
Cancel
Save