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
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
from StringIO import StringIO
from utils.services import Dropbox, Google
class Actor():
@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
@param name name of the class to instantiate
@param args arguments to be passed in {configuration}
"""
o = None
r = []
if not isinstance(name,list):
name = [name]
for id in name :
try:
o = eval("".join([name,"()"]))
o.init(args)
o = eval("".join([id,"()"]))
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 o
return r[0] if len(r) == 1 else r
def __init__(self):
"""
Initializing the class with configuration. The configuration will be specific to each subclass
@ -51,8 +60,9 @@ class Actor():
# def getIdentifier(self):
# return self.__class__.__name__.lower()
def init(self,args):
def Initialize(self,args,logger=None):
self.config = args
self.logger = logger
def isValid(self,**item):
return False
@ -67,6 +77,10 @@ class Actor():
pass
def post(self,**args):
pass
def log(self,**args):
if self.logger :
args['subject'] = self.getName()
self.logger.log(args)
class Apps(Actor) :
"""
This class is designed to handle application, restart, if need be.
@ -100,13 +114,16 @@ class Apps(Actor) :
"""
self.action = action
self.params = params
self.log(action='init',object=action,value=params)
def startup(self,cmd) :
"""
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:
os.system(cmd +" &")
self.log(action='startup',value=cmd)
except Exception, e:
print e
@ -206,21 +223,38 @@ class Folders(Actor):
{threshold:value}
@params threshold in terms of size, or age. It will be applied to all folders
"""
def init(self,args):
def init(self,action,params):
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.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):
action = args['action']
params = args['params']
p = action in ['clean','archive','backup']
p = len(set(action) & set(['clean','archive','backup'])) > 0
q = False
r = False
if p :
q = params.keys()[0] in ['label','folder']
r = os.path.exists(params.values[0])
q = len(set(params.keys()) & set( ['label','folder'])) > 0
if q :
folder = params['label'] if 'label' in params else params['folder']
r = os.path.exists(folder)
return p and q and r
def archive(self,item):
"""
@ -231,10 +265,13 @@ class Folders(Actor):
folder = item['label']
name = folder.split(os.sep)
name = name[len(name)-1]
date = ''.join([str(i) for i in item['date'].values()])
signature='-'.join([name,date,str(item['stats']['file_count']),'files'])
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])
tarball=os.sep.join([folder,'..',signature])
shutil.make_archive(tarball,'tar',folder)
#self.clean(item)
#
# @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
#
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):
"""
This function consists in deleting files from a given folder
@ -283,7 +347,15 @@ class Folders(Actor):
p = os.path.exists(item['label']) and item['label'] in self.lfolders
q = item['stats']['size']['mean'] >= self.threshold and self.threshold > 0
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):
r = {'clean':self.clean,'archive':self.archive}
self.lfolders = [ folder['label'] for folder in logs]

Loading…
Cancel
Save