def action(self, query, args=None): with db_lock: if query == None: return sqlResult = None attempt = 0 while attempt < 5: try: if args == None: sqlResult = self.connection.execute(query) else: sqlResult = self.connection.execute(query, args) self.connection.commit() break except sqlite3.OperationalError, e: if "unable to open database file" in e.message or "database is locked" in e.message: logger.warn('Database Error: %s' % e) attempt += 1 time.sleep(1) else: logger.error('Database error: %s' % e) raise except sqlite3.DatabaseError, e: logger.error('Fatal error executing %s :: %s' % (query, e)) raise
def __init__(self): filename = cherrystrap.APP_NAME+".db" try: global sqlite3 import sqlite3 except Exception, e: logger.error("There was an error importing SQLite: %s" % e)
def __init__(self): try: global MySQLdb import MySQLdb except ImportError: logger.warn("The MySQLdb module is missing. Install this " \ "module to enable MySQL. Reverting to SQLite.") cherrystrap.DATABASE_TYPE = "sqlite" host = cherrystrap.MYSQL_HOST port = cherrystrap.MYSQL_PORT if port: try: port = int(cherrystrap.MYSQL_PORT) except: port = 3306 logger.error("The port number supplied is not an integer") else: port = 3306 if not host: host = 'localhost' user = cherrystrap.MYSQL_USER passwd = formatter.decode('obscure', cherrystrap.MYSQL_PASS) self.connection = MySQLdb.Connection(host=host, port=port, user=user, passwd=passwd, db=cherrystrap.APP_NAME, charset='utf8', use_unicode=True)
def action(self, query, args=None): with db_lock: if query == None: return sqlResult = None attempt = 0 while attempt < 5: try: if args == None: #logger.debug(self.filename+": "+query) sqlResult = self.connection.execute(query) else: #logger.debug(self.filename+": "+query+" with args "+str(args)) sqlResult = self.connection.execute(query, args) self.connection.commit() break except sqlite3.OperationalError, e: if "unable to open database file" in e.message or "database is locked" in e.message: logger.warn('Database Error: %s' % e) attempt += 1 time.sleep(1) else: logger.error('Database error: %s' % e) raise except sqlite3.DatabaseError, e: logger.error('Fatal error executing %s :: %s' % (query, e)) raise
def __init__(self): try: global MySQLdb import MySQLdb except ImportError: logger.warn("The MySQLdb module is missing. Install this " \ "module to enable MySQL. Reverting to SQLite.") cherrystrap.DATABASE_TYPE = "sqlite" host = cherrystrap.MYSQL_HOST port = cherrystrap.MYSQL_PORT if port: try: port = int(cherrystrap.MYSQL_PORT) except: port = 3306 logger.error("The port number supplied is not an integer") else: port = 3306 if not host: host = 'localhost' user = cherrystrap.MYSQL_USER passwd = formatter.decode('obscure', cherrystrap.MYSQL_PASS) self.connection = MySQLdb.Connection(host=host, port=port, user=user, passwd=passwd, charset='utf8', use_unicode=True)
def injectVarCheck(CFG): global RSA_PRIVATE_KEY, RSA_PUBLIC_KEY, CONSUMER_KEY, CONSUMER_SECRET, \ JIRA_BASE_URL, JIRA_OAUTH_TOKEN, JIRA_OAUTH_SECRET, \ JIRA_LOGIN_STATUS, JIRA_LOGIN_USER CheckSection(CFG, 'JIRA') RSA_PRIVATE_KEY = check_setting_str(CFG, 'JIRA', 'rsa_private_key', 'keys/RSA.pem') RSA_PUBLIC_KEY = check_setting_str(CFG, 'JIRA', 'rsa_public_key', 'keys/RSA.pub') CONSUMER_KEY = check_setting_str(CFG, 'JIRA', 'consumer_key', '') CONSUMER_SECRET = check_setting_str(CFG, 'JIRA', 'consumer_secret', '') JIRA_BASE_URL = check_setting_str(CFG, 'JIRA', 'jira_base_url', '') JIRA_OAUTH_TOKEN = check_setting_str(CFG, 'JIRA', 'jira_oauth_token', '') JIRA_OAUTH_SECRET = check_setting_str(CFG, 'JIRA', 'jira_oauth_secret', '') JIRA_LOGIN_STATUS = False JIRA_LOGIN_USER = None # Test OAuth for logged in user try: backend.check_oauth() except Exception, e: logger.error("Can't verify OAuth user: %s" % e)
def start(): global __INITIALIZED__, scheduler_started if __INITIALIZED__: try: # Crons and scheduled jobs go here # testInterval = IntervalTrigger(weeks=0, days=0, hours=0, minutes=2, seconds=0, start_date=None, end_date=None, timezone=None) # testCron = CronTrigger(year=None, month=None, day=None, week=None, day_of_week=None, hour=None, minute='*/2', second=None, start_date=None, end_date=None, timezone=None) # SCHED.add_job(formatter.schedulerTest, testCron) if GIT_ENABLED and GIT_INTERVAL != 0: gitInterval = IntervalTrigger(weeks=0, days=0, hours=GIT_INTERVAL, minutes=0, seconds=0, start_date=None, end_date=None, timezone=None) SCHED.add_job(versioncheck.checkGithub, gitInterval) SCHED.start() for job in SCHED.get_jobs(): logger.info("Job scheduled: %s" % job) scheduler_started = True except Exception, e: logger.error("Can't start scheduled job(s): %s" % e)
def _get_credentials(self, key): request_token = {} request_token['oauth_token'] = orielpy.TWITTER_TOKEN request_token['oauth_token_secret'] = orielpy.TWITTER_SECRET request_token['oauth_callback_confirmed'] = 'true' token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(key) logger.info('Generating and signing request for an access token using key '+key) signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() #@UnusedVariable oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret) logger.info('oauth_consumer: '+str(oauth_consumer)) oauth_client = oauth.Client(oauth_consumer, token) logger.info('oauth_client: '+str(oauth_client)) resp, content = oauth_client.request(self.ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % key) logger.info('resp, content: '+str(resp)+','+str(content)) access_token = dict(parse_qsl(content)) logger.info('access_token: '+str(access_token)) logger.info('resp[status] = '+str(resp['status'])) if resp['status'] != '200': logger.error('The request for a token with did not succeed: '+str(resp['status'])) return False else: logger.info('Your Twitter Access Token key: %s' % access_token['oauth_token']) logger.info('Access Token secret: %s' % access_token['oauth_token_secret']) orielpy.TWITTER_TOKEN = access_token['oauth_token'] orielpy.TWITTER_SECRET = access_token['oauth_token_secret'] return True
def create_https_certificates(ssl_cert, ssl_key): """ Create a pair of self-signed HTTPS certificares and store in them in 'ssl_cert' and 'ssl_key'. Method assumes pyOpenSSL is installed. """ import os from cherrystrap import logger from OpenSSL import crypto from lib.certgen import createKeyPair, createCertRequest, createCertificate, \ TYPE_RSA, serial # Create the CA Certificate cakey = createKeyPair(TYPE_RSA, 2048) careq = createCertRequest(cakey, CN="Certificate Authority") cacert = createCertificate(careq, (careq, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)) # ten years pkey = createKeyPair(TYPE_RSA, 2048) req = createCertRequest(pkey, CN="CherryStrap") cert = createCertificate(req, (cacert, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)) # ten years # Save the key and certificate to disk try: if not os.path.exists('keys'): os.makedirs('keys') with open('keys/server.key', "w+") as fp: fp.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)) with open('keys/server.crt', "w+") as fp: fp.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) except IOError: logger.error("Error creating SSL key and certificate") return False return True
def launch_browser(host, port, root): if host == '0.0.0.0': host = 'localhost' try: webbrowser.open('http://%s:%i%s' % (host, port, root)) except Exception, e: logger.error('Could not launch browser: %s' % e)
def shutdown(restart=False, update=False): config_write() cherrypy.engine.exit() try: SCHED.shutdown(wait=True) except Exception, e: logger.error("Can't shutdown scheduler: %s" % e)
def PUT(self, token=None, **kwargs): if token != cherrystrap.API_TOKEN: return "{\"status\": \"error\", \"message\": \"Invalid Token\"}" errorList = [] # Commented section below shows an example of how to receive # application/json formatted items. We'll keep it default # try: # data = json.loads(cherrypy.request.body.read()) # for kvPair in data: # dictName = kvPair['name'] # dictValue = kvPair['value'] # print dictName, dictValue # except: # pass if 'appName' in kwargs: cherrystrap.APP_NAME = kwargs.pop('appName', 'CherryStrap') if 'logDir' in kwargs: cherrystrap.LOGDIR = kwargs.pop('logDir', None) if 'httpHost' in kwargs: cherrystrap.HTTP_HOST = kwargs.pop('httpHost', '0.0.0.0') if 'httpPort' in kwargs: try: cherrystrap.HTTP_PORT = int(kwargs.pop('httpPort', 7889)) except: errorList.append("httpPort must be an integer") kwargs.pop('httpPort', 7889) if 'sslEnabled' in kwargs: cherrystrap.HTTPS_ENABLED = kwargs.pop('sslEnabled', False) == 'true' elif 'sslEnabledHidden' in kwargs: cherrystrap.HTTPS_ENABLED = kwargs.pop('sslEnabledHidden', False) == 'true' if 'sslKey' in kwargs: cherrystrap.HTTPS_KEY = kwargs.pop('sslKey', 'keys/server.key') if 'sslCert' in kwargs: cherrystrap.HTTPS_CERT = kwargs.pop('sslCert', 'keys/server.crt') if 'sslVerify' in kwargs: cherrystrap.VERIFY_SSL = kwargs.pop('sslVerify', True) == 'true' elif 'sslVerifyHidden' in kwargs: cherrystrap.VERIFY_SSL = kwargs.pop('sslVerifyHidden', True) == 'true' if 'launchBrowser' in kwargs: cherrystrap.LAUNCH_BROWSER = kwargs.pop('launchBrowser', False) == 'true' elif 'launchBrowserHidden' in kwargs: cherrystrap.LAUNCH_BROWSER = kwargs.pop('launchBrowserHidden', False) == 'true' if 'httpUser' in kwargs: cherrystrap.HTTP_USER = kwargs.pop('httpUser', None) if 'httpPass' in kwargs: httpPassProcess = kwargs.pop('httpPass', None) if httpPassProcess != cherrystrap.HTTP_PASS and httpPassProcess != "": try: cherrystrap.HTTP_PASS = sha256_crypt.encrypt(httpPassProcess) except Exception, e: logger.error('There was a problem generating password hash: %s' % e) elif httpPassProcess == "": cherrystrap.HTTP_PASS = ""
def downloadLog(self, logFile=None): message = {} if logFile: try: return serve_file(logFile, "application/x-download", "attachment") except Exception, e: message['status'] = 'danger' message['message'] = 'There was a problem downloading log file %s' % logFile logger.error('There was a problem downloading log file %s: %s' % (logFile, e)) return serve_template(templatename="logs.html", title="Logs", message=message)
def check_setting_str(config, cfg_name, item_name, def_val): try: my_val = str(config[cfg_name][item_name]) except: my_val = def_val try: config[cfg_name][item_name] = ast.literal_eval(my_val) logger.warn("Bad value for %s in config.ini. Reverting to default" % item_name) except: config[cfg_name] = {} config[cfg_name][item_name] = my_val logger.error("Bad default value for %s. Application may break" % item_name) logger.debug(item_name + " -> " + str(my_val)) return my_val
def launch_browser(host, port, root): if host == '0.0.0.0': host = 'localhost' if HTTPS_ENABLED: protocol = 'https' else: protocol = 'http' try: import webbrowser webbrowser.open('%s://%s:%i%s' % (protocol, host, port, root)) except Exception, e: logger.error('Could not launch browser: %s' % e)
def _send_tweet(self, message=None): username=self.consumer_key password=self.consumer_secret access_token_key=orielpy.TWITTER_TOKEN access_token_secret=orielpy.TWITTER_SECRET logger.info(u"Sending tweet: "+message) api = twitter.Api(username, password, access_token_key, access_token_secret) try: api.PostUpdate(message) except Exception, e: logger.error(u"Error Sending Tweet: %s" %e) return False
def getVersion(): # This first statement will always fail. if cherrystrap.GIT_UPSTREAM.startswith('win32build'): cherrystrap.INSTALL_TYPE = 'win' # Don't have a way to update exe yet, but don't want to set VERSION to None return 'Windows Install', 'master' elif os.path.isdir(os.path.join(cherrystrap.PROG_DIR, '.git')): cherrystrap.INSTALL_TYPE = 'git' output, err = runGit('rev-parse HEAD') if not output: logger.error('Couldn\'t find latest installed version.') cur_commit_hash = None cur_commit_hash = str(output) if not re.match('^[a-z0-9]+$', cur_commit_hash): logger.error('Output doesn\'t look like a hash, not using it') cur_commit_hash = None if cherrystrap.GIT_OVERRIDE and cherrystrap.GIT_BRANCH: branch_name = cherrystrap.GIT_BRANCH else: branch_name, err = runGit('rev-parse --abbrev-ref HEAD') branch_name = branch_name if not branch_name and cherrystrap.GIT_BRANCH: logger.error('Could not retrieve branch name from git. Falling back to %s' % cherrystrap.GIT_BRANCH) branch_name = cherrystrap.GIT_BRANCH if not branch_name: logger.error('Could not retrieve branch name from git. Defaulting to master') branch_name = 'master' return cur_commit_hash, branch_name else: cherrystrap.INSTALL_TYPE = 'source' version_file = os.path.join(cherrystrap.PROG_DIR, 'version.txt') if not os.path.isfile(version_file): return None, 'master' with open(version_file, 'r') as f: current_version = f.read().strip(' \n\r') if current_version: return current_version, cherrystrap.GIT_BRANCH else: return None, 'master'
def __init__(self): host = cherrystrap.XBMC_HOST port = cherrystrap.XBMC_PORT if port: try: port = int(cherrystrap.VANILLA_PORT) except: logger.error("The port number supplied is not an integer") else: port = 3306 if not host: host = 'localhost' user = cherrystrap.XBMC_USER passwd = cherrystrap.XBMC_PASSWORD self.connection = MySQLdb.Connection(host=host, port=port, user=user, passwd=passwd, charset='utf8', use_unicode=True)
def save_playlist(playlist_name): try: myDB = database.SQLite_DBConnection() pass except: logger.info("There was a SQLite connection error.") return serve_template(templatename="config.html") PLDIR = os.path.join(cherrystrap.DATADIR, 'playlists') if not os.path.exists(PLDIR): try: os.makedirs(PLDIR) except OSError: logger.error('Could not create playlist directory. Check permissions of: ' + cherrystrap.DATADIR) filename = datetime.datetime.now().strftime('%Y-%m-%d')+" - "+playlist_name xbmc_playlist = os.path.join(cherrystrap.DATADIR, 'playlists', filename+'_XBMC.m3u') win_playlist = os.path.join(cherrystrap.DATADIR, 'playlists', filename+'_Win.m3u') mac_playlist = os.path.join(cherrystrap.DATADIR, 'playlists', filename+'_Mac.m3u') xbmc = open(xbmc_playlist,'w') pl_db = myDB.select("SELECT * FROM generated_playlist") if pl_db: for item in pl_db: xbmc.write(item['filepath'].encode('utf-8')+'\n') xbmc.close() win = open(win_playlist,'w') pl_db = myDB.select("SELECT * FROM generated_playlist") if pl_db: for item in pl_db: songpath = item['filepath'].replace('smb:','').replace("/","\\") win.write(songpath.encode('utf-8')+'\n') win.close() mac = open(mac_playlist,'w') pl_db = myDB.select("SELECT * FROM generated_playlist") if pl_db: for item in pl_db: songpath = item['filepath'].replace('smb://MAXPOWER/','/Volumes/') mac.write(songpath.encode('utf-8')+'\n') mac.close()
def generateUsers(self): for root, dirs, files in os.walk(os.path.join(cherrystrap.DATADIR,'cache')): for f in files: os.unlink(os.path.join(root, f)) for d in dirs: shutil.rmtree(os.path.join(root, d)) try: myDB = database.DBConnection() pass except: logger.info("There was a MySQL connection error. Please check your Vanilla MySQL connection credentials") return serve_template(templatename="index.html") JSONDIR = os.path.join(cherrystrap.DATADIR,'cache','users') if not os.path.exists(JSONDIR): try: os.makedirs(JSONDIR) except OSError: logger.error('Could not create user director. Check permissions of: ' + cherrystrap.DATADIR) userInfo=[] userCount = 0 support_users = ([support_user.strip() for support_user in cherrystrap.VANILLA_SUPPORT.split(',')]) users = myDB.select("SELECT * FROM %s" % cherrystrap.VANILLA_DB+'.GDN_User') for user in users: userCount += 1 if user['Name'] in support_users: user_state = "support" else: user_state = "user" userInfo = { "name": user['Name'], "email": user['Email'], "title": "", "created_at": user['DateInserted'].strftime('%Y-%m-%dT%H:%M:%SZ'), "state": user_state, "password": "******" } OUTDIR = os.path.join(JSONDIR,str(userCount)+'.json') with open(OUTDIR, 'w') as outfile: json.dump(userInfo, outfile)
def generateCategories(self): try: myDB = database.DBConnection() pass except: logger.info("There was a MySQL connection error. Please check your Vanilla MySQL connection credentials") return serve_template(templatename="index.html") JSONDIR = os.path.join(cherrystrap.DATADIR,'cache','categories') if not os.path.exists(JSONDIR): try: os.makedirs(JSONDIR) except OSError: logger.error('Could not create user director. Check permissions of: ' + cherrystrap.DATADIR) categoryInfo=[] categoryCount = 0 categories = myDB.select("SELECT * FROM %s" % cherrystrap.VANILLA_DB+'.GDN_Category') for category in categories: if category['Name'] != 'Root': categoryCount += 1 categoryID = category['CategoryID'] CATDIR = os.path.join(cherrystrap.DATADIR,'cache','categories',str(categoryID)) if not os.path.exists(CATDIR): try: os.makedirs(CATDIR) except OSError: logger.error('Could not create user director. Check permissions of: ' + cherrystrap.DATADIR) categoryInfo = { "name": category['Name'], "summary": category['Description'] } OUTDIR = os.path.join(JSONDIR,str(categoryID)+'.json') with open(OUTDIR, 'w') as outfile: json.dump(categoryInfo, outfile)
def runGit(args): if cherrystrap.GIT_PATH: git_locations = ['"' + cherrystrap.GIT_PATH + '"'] else: git_locations = ['git'] if platform.system().lower() == 'darwin': git_locations.append('/usr/local/git/bin/git') output = err = None for cur_git in git_locations: cmd = cur_git + ' ' + args try: logger.debug('Trying to execute: "' + cmd + '" with shell in ' + cherrystrap.PROG_DIR) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=cherrystrap.PROG_DIR) output, err = p.communicate() output = output.strip() logger.debug('Git output: ' + output) except OSError: logger.debug('Command failed: %s', cmd) continue if 'not found' in output or "not recognized as an internal or external command" in output: logger.debug('Unable to find git with command ' + cmd) output = None elif 'fatal:' in output or err: logger.error('Git returned bad info. Are you sure this is a git installation?') output = None elif output: break return (output, err)
def PUT(self, token=None, **kwargs): if token != cherrystrap.API_TOKEN: return "{\"status\": \"error\", \"message\": \"Invalid Token\"}" errorList = [] # Commented section below shows an example of how to receive # application/json formatted items. We'll keep it default # try: # data = json.loads(cherrypy.request.body.read()) # for kvPair in data: # dictName = kvPair['name'] # dictValue = kvPair['value'] # print dictName, dictValue # except: # pass if 'appName' in kwargs: cherrystrap.APP_NAME = kwargs.pop('appName', 'CherryStrap') if 'logDir' in kwargs: cherrystrap.LOGDIR = kwargs.pop('logDir', None) if 'httpHost' in kwargs: cherrystrap.HTTP_HOST = kwargs.pop('httpHost', '0.0.0.0') if 'httpPort' in kwargs: try: cherrystrap.HTTP_PORT = int(kwargs.pop('httpPort', 7889)) except: errorList.append("httpPort must be an integer") kwargs.pop('httpPort', 7889) if 'sslEnabled' in kwargs: cherrystrap.HTTPS_ENABLED = kwargs.pop('sslEnabled', False) == 'true' elif 'sslEnabledHidden' in kwargs: cherrystrap.HTTPS_ENABLED = kwargs.pop('sslEnabledHidden', False) == 'true' if 'sslKey' in kwargs: cherrystrap.HTTPS_KEY = kwargs.pop('sslKey', 'keys/server.key') if 'sslCert' in kwargs: cherrystrap.HTTPS_CERT = kwargs.pop('sslCert', 'keys/server.crt') if 'sslVerify' in kwargs: cherrystrap.VERIFY_SSL = kwargs.pop('sslVerify', True) == 'true' elif 'sslVerifyHidden' in kwargs: cherrystrap.VERIFY_SSL = kwargs.pop('sslVerifyHidden', True) == 'true' if 'launchBrowser' in kwargs: cherrystrap.LAUNCH_BROWSER = kwargs.pop('launchBrowser', False) == 'true' elif 'launchBrowserHidden' in kwargs: cherrystrap.LAUNCH_BROWSER = kwargs.pop('launchBrowserHidden', False) == 'true' if 'httpUser' in kwargs: cherrystrap.HTTP_USER = kwargs.pop('httpUser', None) if 'httpPass' in kwargs: httpPassProcess = kwargs.pop('httpPass', None) if httpPassProcess != cherrystrap.HTTP_PASS and httpPassProcess != "": try: cherrystrap.HTTP_PASS = sha256_crypt.encrypt( httpPassProcess) except Exception, e: logger.error( 'There was a problem generating password hash: %s' % e) elif httpPassProcess == "": cherrystrap.HTTP_PASS = ""
cherrystrap.MYSQL_HOST = kwargs.pop('mysqlHost', 'localhost') if 'mysqlPort' in kwargs: try: cherrystrap.MYSQL_PORT = int(kwargs.pop('mysqlPort', 3306)) except: errorList.append("mysqlPort must be an integer") kwargs.pop('MySQLPort', 3306) if 'mysqlUser' in kwargs: cherrystrap.MYSQL_USER = kwargs.pop('mysqlUser', None) if 'mysqlPass' in kwargs: mysqlPassProcess = kwargs.pop('mysqlPass', None) if mysqlPassProcess != cherrystrap.MYSQL_PASS and mysqlPassProcess != "": try: cherrystrap.MYSQL_PASS = formatter.encode('obscure', mysqlPassProcess) except Exception, e: logger.error('There was a problem encoding MySQL password: %s' % e) elif mysqlPassProcess == "": cherrystrap.MYSQL_PASS = "" if 'gitEnabled' in kwargs: cherrystrap.GIT_ENABLED = kwargs.pop('gitEnabled', False) == 'true' elif 'gitEnabledHidden' in kwargs: cherrystrap.GIT_ENABLED = kwargs.pop('gitEnabledHidden', False) == 'true' if 'gitPath' in kwargs: cherrystrap.GIT_PATH = kwargs.pop('gitPath', None) if 'gitUser' in kwargs: cherrystrap.GIT_USER = kwargs.pop('gitUser', 'theguardian') if 'gitRepo' in kwargs: cherrystrap.GIT_REPO = kwargs.pop('gitRepo', 'CherryStrap') if 'gitBranch' in kwargs: cherrystrap.GIT_BRANCH = kwargs.pop('gitBranch', 'master')
def health(notify=True): health_list=[] error_msg=0 myDB = database.SQLite_DBConnection() subcall = subroutines() json_disk = subcall.diskio_subroutine() json_sysfiles = subcall.sysfiles_subroutine() json_cpu = subcall.cpuload_subroutine() json_mem = subcall.memload_subroutine() json_swap = subcall.swapload_subroutine() json_partition = subcall.partitions_subroutine() json_networking = subcall.networkload_subroutine() json_log = subcall.syslogs_subroutine() #process_json = subcall.sysprocesses_subroutine() rulelist = myDB.select('SELECT * from rules') for entries in rulelist: flag = entries['rule1'] program = entries['rule2'] condition = entries['rule3'] comparison_value = entries['rule4'] comparison_units = entries['rule5'] message_rule = entries['rule6'] custom_message = entries['rule7'] if flag == "Log File": try: for logline in json_log: if logline['program'] == program: prog_name = logline['program'] log_string = logline['logline'] if condition == "Contains String": if comparison_value in log_string: messaging(message_rule, custom_message, prog_name+" log file contains ["+comparison_value+"]", notify, health_list) error_msg+=1 elif condition == "Does Not Contain String": if comparison_value not in log_string: messaging(message_rule, custom_message, prog_name+" log file does not contain ["+comparison_value+"]", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of log file/s") elif flag == "CPU Utilization": try: cpu_index = 0 cpu_arr = [] while cpu_index < len(json_cpu): cpu_arr.append(json_cpu[cpu_index]['cpuPercent']) cpu_index+=1 cpu_value = max(cpu_arr) try: comparison_value = int(comparison_value) cpu_value = int(cpu_value) except: pass if condition == "Is Less Than": if comparison_value > cpu_value: messaging(message_rule, custom_message, "CPU utilization less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than": if comparison_value < cpu_value: messaging(message_rule, custom_message, "CPU utilization greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of CPU utilization") elif flag == "CPU Temperature": try: for key, value in json_sysfiles.iteritems(): if key == "cpuTemp": cpu_temp = value elif key == "cpuTempPercent": cpu_temp_percent = value try: comparison_value = int(comparison_value) cpu_temp = int(cpu_temp) cpu_temp_percent = int(cpu_temp_percent) except: pass if condition == "Is Less Than" and comparison_units == "Deg-C": if comparison_value > cpu_temp: messaging(message_rule, custom_message, "CPU temperature less than "+str(comparison_value)+" Deg-C", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Deg-C": if comparison_value < cpu_temp: messaging(message_rule, custom_message, "CPU temperature greater than "+str(comparison_value)+" Deg-C", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > cpu_temp_percent: messaging(message_rule, custom_message, "CPU temperature less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < cpu_temp_percent: messaging(message_rule, custom_message, "CPU temperature greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of CPU temperature") elif flag == "System Temperature": try: for key, value in json_sysfiles.iteritems(): if key == "sysTemp": sys_temp = value elif key == "sysTempPercent": sys_temp_percent = value try: comparison_value = int(comparison_value) sys_temp = int(sys_temp) sys_temp_percent = int(sys_temp_percent) except: pass if condition == "Is Less Than" and comparison_units == "Deg-C": if comparison_value > sys_temp: messaging(message_rule, custom_message, "System temperature less than "+str(comparison_value)+" Deg-C", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Deg-C": if comparison_value < sys_temp: messaging(message_rule, custom_message, "System temperature greater than "+str(comparison_value)+" Deg-C", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > sys_temp_percent: messaging(message_rule, custom_message, "System temperature less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < sys_temp_percent: messaging(message_rule, custom_message, "System temperature greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of system temperature") elif flag == "CPU Fan Speed": try: for key, value in json_sysfiles.iteritems(): if key == "cpuFan": cpu_fan = value elif key == "cpuFanPercent": cpu_fan_percent = value try: comparison_value = int(comparison_value) cpu_fan = int(cpu_fan) cpu_fan_percent = int(cpu_fan_percent) except: pass if condition == "Is Less Than" and comparison_units == "RPM": if comparison_value > cpu_fan: messaging(message_rule, custom_message, "CPU fan speed less than "+str(comparison_value)+" RPM", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "RPM": if comparison_value < cpu_fan: messaging(message_rule, custom_message, "CPU fan speed greater than "+str(comparison_value)+" RPM", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > cpu_fan_percent: messaging(message_rule, custom_message, "CPU fan speed less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < cpu_fan_percent: messaging(message_rule, custom_message, "CPU fan speed greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of CPU fan speed") elif flag == "System Fan Speed": try: for key, value in json_sysfiles.iteritems(): if key == "sysFan": sys_fan = value elif key == "sysFanPercent": sys_fan_percent = value try: comparison_value = int(comparison_value) sys_fan = int(sys_fan) sys_fan_percent = int(sys_fan_percent) except: pass if condition == "Is Less Than" and comparison_units == "RPM": if comparison_value > sys_fan: messaging(message_rule, custom_message, "System fan speed less than "+str(comparison_value)+" RPM", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "RPM": if comparison_value < sys_fan: messaging(message_rule, custom_message, "System fan speed greater than "+str(comparison_value)+" RPM", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > sys_fan_percent: messaging(message_rule, custom_message, "System fan speed less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < sys_fan_percent: messaging(message_rule, custom_message, "System fan speed greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of system fan speed") elif flag == "Network Rx Rate": try: for key, value in json_networking.iteritems(): if key == "downloadRate": if "kB/s" in value: rx_rate = 0 else: rx_rate = value.replace(" MB/s","") elif key == "downloadPercent": rx_percent = value try: comparison_value = float(comparison_value) rx_rate = float(rx_rate) rx_percent = float(rx_percent) except: pass if condition == "Is Less Than" and comparison_units == "MB/s": if comparison_value > rx_rate: messaging(message_rule, custom_message, "Network Rx rate less than "+str(comparison_value)+" MB/s", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "MB/s": if comparison_value < rx_rate: messaging(message_rule, custom_message, "Network Rx rate greater than "+str(comparison_value)+" MB/s", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > rx_percent: messaging(message_rule, custom_message, "Network Rx rate less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < rx_percent: messaging(message_rule, custom_message, "Network Rx rate greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of network Rx rate") elif flag == "Network Tx Rate": try: for key, value in json_networking.iteritems(): if key == "uploadRate": if "kB/s" in value: tx_rate = 0 else: tx_rate = value.replace(" MB/s","") elif key == "uploadPercent": tx_percent = value try: comparison_value = float(comparison_value) tx_rate = float(tx_rate) tx_percent = float(tx_percent) except: pass if condition == "Is Less Than" and comparison_units == "MB/s": if comparison_value > tx_rate: messaging(message_rule, custom_message, "Network Tx rate less than "+str(comparison_value)+" MB/s", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "MB/s": if comparison_value < tx_rate: messaging(message_rule, custom_message, "Network Tx rate greater than "+str(comparison_value)+" MB/s", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > tx_percent: messaging(message_rule, custom_message, "Network Tx rate less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < tx_percent: messaging(message_rule, custom_message, "Network Tx rate greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of network Tx rate") elif flag == "RAM Free Space": try: for key, value in json_mem.iteritems(): if key == "memFree": mem_free = value elif key == "memPercent": mem_percent = 100-int(value) try: comparison_value = int(comparison_value) mem_free = int(mem_free) mem_percent = int(mem_percent) except: pass if condition == "Is Less Than" and comparison_units == "MB": if comparison_value > mem_free: messaging(message_rule, custom_message, "Free RAM less than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "MB": if comparison_value < mem_free: messaging(message_rule, custom_message, "Free RAM greater than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > mem_percent: messaging(message_rule, custom_message, "Free RAM less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < mem_percent: messaging(message_rule, custom_message, "Free RAM greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of RAM free space") elif flag == "Swap Memory Free Space": try: for key, value in json_swap.iteritems(): if key == "swapFree": swap_free = value elif key == "swapPercent": swap_percent = 100-int(value) try: comparison_value = int(comparison_value) swap_free = int(swap_free) swap_percent = int(swap_percent) except: pass if condition == "Is Less Than" and comparison_units == "MB": if comparison_value > swap_free: messaging(message_rule, custom_message, "Free Swap Memory less than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "MB": if comparison_value < swap_free: messaging(message_rule, custom_message, "Free Swap Memory greater than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > swap_percent: messaging(message_rule, custom_message, "Free Swap Memory less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < swap_percent: messaging(message_rule, custom_message, "Free Swap Memory greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of swap memory free space") elif flag == "Volume Free Space": try: volume_index = 0 while volume_index < len(json_partition): volume = json_partition[volume_index]['mountpoint'] if volume == program: volume_free = json_partition[volume_index]['free'] if "MB" in volume_free: volume_free = volume_free.replace(" MB","") volume_units = "MB" elif "GB" in volume_free: volume_free = volume_free.replace(" GB","") volume_units = "GB" volume_percent = 100-int(json_partition[volume_index]['percent']) try: comparison_value = float(comparison_value) volume_free = float(volume_free) volume_percent = float(volume_percent) except: pass if condition == "Is Less Than" and comparison_units == "MB" and volume_units == "MB": if comparison_value > volume_free: messaging(message_rule, custom_message, volume+" free space less than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "MB" and volume_units == "MB": if comparison_value < volume_free: messaging(message_rule, custom_message, volume+" free space greater than "+str(comparison_value)+" MB", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "GB" and volume_units == "GB": if comparison_value > volume_free: messaging(message_rule, custom_message, volume+" free space less than "+str(comparison_value)+" GB", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "GB" and volume_units == "GB": if comparison_value < volume_free: messaging(message_rule, custom_message, volume+" free space greater than "+str(comparison_value)+" GB", notify, health_list) error_msg+=1 elif condition == "Is Less Than" and comparison_units == "Percent": if comparison_value > volume_percent: messaging(message_rule, custom_message, volume+" free space less than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 elif condition == "Is Greater Than" and comparison_units == "Percent": if comparison_value < volume_percent: messaging(message_rule, custom_message, volume+" free space greater than "+str(comparison_value)+"%", notify, health_list) error_msg+=1 volume_index+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of volume free space") elif flag == "Disk Status": try: disk_index = 0 while disk_index < len(json_disk): disk = json_disk[disk_index]['diskId'] if disk == program: disk_status = json_disk[disk_index]['status'] if condition == "Is": if comparison_value == disk_status: messaging(message_rule, custom_message, disk+" is "+str(comparison_value), notify, health_list) error_msg+=1 elif condition == "Is Not": if comparison_value != disk_status: messaging(message_rule, custom_message, disk+" is not "+str(comparison_value), notify, health_list) error_msg+=1 disk_index+=1 except: error_msg+=1 logger.error("%s" % "There is a problem finding health of disk status") if error_msg == 0 and orielpy.NOTIFY_NOMINAL == 1: notifiers.notify_health(formatter.now()+": Server Status Nominal") if error_msg == 0: health_array = collections.defaultdict() health_array['status'] = "success" health_list.append(health_array) else: health_array = collections.defaultdict() health_array['status'] = "warning" health_list.append(health_array) health_json = json.dumps(health_list) return health_json
def handle_error(): cherrypy.response.status = 500 logger.error("500 Error: %s" % _cperror.format_exc()) cherrypy.response.body = ["<html><body>Sorry, an error occured</body></html>"]
def initialize(): with INIT_LOCK: global __INITIALIZED__, FULL_PATH, PROG_DIR, LOGLEVEL, DAEMON, \ DATADIR, CONFIGFILE, CFG, LOGDIR, APP_NAME, HTTP_HOST, HTTP_PORT, \ HTTP_USER, HTTP_PASS, HTTP_ROOT, HTTP_LOOK, VERIFY_SSL, \ LAUNCH_BROWSER, HTTPS_ENABLED, HTTPS_KEY, HTTPS_CERT, API_TOKEN, \ DATABASE_TYPE, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASS, \ GIT_ENABLED, GIT_PATH, GIT_BRANCH, GIT_USER, GIT_STARTUP, GIT_INTERVAL, \ GIT_OVERRIDE, GIT_REPO, GIT_UPSTREAM, GIT_LOCAL, GIT_EXISTS if __INITIALIZED__: return False CheckSection(CFG, 'Server') CheckSection(CFG, 'Interface') CheckSection(CFG, 'Database') CheckSection(CFG, 'Git') LOGDIR = check_setting_str(CFG, 'Server', 'logDir', '') if not LOGDIR: LOGDIR = os.path.join(DATADIR, 'Logs') # Create logdir if not os.path.exists(LOGDIR): try: os.makedirs(LOGDIR) except OSError: if LOGLEVEL: print LOGDIR + ":" print ' Unable to create folder for logs. Only logging to console.' # Start the logger, silence console logging if we need to logger.cherrystrap_log.initLogger(loglevel=LOGLEVEL) # Put the cache dir in the data dir for now CACHEDIR = os.path.join(DATADIR, 'cache') if not os.path.exists(CACHEDIR): try: os.makedirs(CACHEDIR) except OSError: logger.error( 'Could not create cachedir. Check permissions of: ' + DATADIR) GIT_EXISTS = os.path.isdir(os.path.join(DATADIR, '.git')) # Attempt to find location of git in this environment if GIT_EXISTS: output = err = None cmd = 'which git' try: logger.debug('Trying to execute: "' + cmd + '" with shell in ' + os.getcwd()) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=os.getcwd()) output, err = p.communicate() output = output.strip() logger.debug('Git output: ' + output) except OSError: logger.debug('Command failed: %s', cmd) if not output or 'not found' in output or "not recognized as an internal or external command" in output: logger.debug('Unable to find git with command ' + cmd) git_enabled = False git_path = '' git_startup = False git_interval = 0 else: git_enabled = True git_path = output git_startup = True git_interval = 12 else: git_enabled = False git_path = '' git_startup = False git_interval = 0 try: HTTP_PORT = check_setting_int(CFG, 'Server', 'httpPort', 7889) except: HTTP_PORT = 7889 if HTTP_PORT < 21 or HTTP_PORT > 65535: HTTP_PORT = 7889 APP_NAME = check_setting_str(CFG, 'Server', 'appName', 'CherryStrap') HTTP_ROOT = check_setting_str(CFG, 'Server', 'httpRoot', '') HTTP_HOST = check_setting_str(CFG, 'Server', 'httpHost', '0.0.0.0') HTTPS_ENABLED = check_setting_bool(CFG, 'Server', 'sslEnabled', False) HTTPS_KEY = check_setting_str(CFG, 'Server', 'sslKey', 'keys/server.key') HTTPS_CERT = check_setting_str(CFG, 'Server', 'sslCert', 'keys/server.crt') VERIFY_SSL = check_setting_bool(CFG, 'Server', 'sslVerify', True) LAUNCH_BROWSER = check_setting_bool(CFG, 'Server', 'launchBrowser', False) HTTP_USER = check_setting_str(CFG, 'Interface', 'httpUser', '') HTTP_PASS = check_setting_str(CFG, 'Interface', 'httpPass', '') HTTP_LOOK = check_setting_str(CFG, 'Interface', 'httpLook', 'bootstrap') API_TOKEN = check_setting_str(CFG, 'Interface', 'apiToken', uuid.uuid4().hex) DATABASE_TYPE = check_setting_str(CFG, 'Database', 'dbType', '') MYSQL_HOST = check_setting_str(CFG, 'Database', 'mysqlHost', 'localhost') MYSQL_PORT = check_setting_int(CFG, 'Database', 'mysqlPort', 3306) MYSQL_USER = check_setting_str(CFG, 'Database', 'mysqlUser', '') MYSQL_PASS = check_setting_str(CFG, 'Database', 'mysqlPass', '') GIT_ENABLED = check_setting_bool(CFG, 'Git', 'gitEnabled', git_enabled) GIT_PATH = check_setting_str(CFG, 'Git', 'gitPath', git_path) GIT_USER = check_setting_str(CFG, 'Git', 'gitUser', 'theguardian') GIT_REPO = check_setting_str(CFG, 'Git', 'gitRepo', 'CherryStrap') GIT_BRANCH = check_setting_str(CFG, 'Git', 'gitBranch', 'master') GIT_UPSTREAM = check_setting_str(CFG, 'Git', 'gitUpstream', '') GIT_LOCAL = check_setting_str(CFG, 'Git', 'gitLocal', '') GIT_STARTUP = check_setting_bool(CFG, 'Git', 'gitStartup', git_startup) GIT_INTERVAL = check_setting_int(CFG, 'Git', 'gitInterval', git_interval) GIT_OVERRIDE = check_setting_bool(CFG, 'Git', 'gitOverride', False) #=============================================================== # Import a variable definer / checker from your app's __init__.py try: from jiraappy import injectVarCheck injectVarCheck(CFG) except Exception, e: logger.debug( "There was a problem importing application variable definitions: %s" % e) #================================================================ # Initialize the database try: createDb(DATABASE_TYPE, DATADIR, APP_NAME, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASS) except Exception, e: logger.error("Error initializing the database: %s" % e)
def handle_error(): cherrypy.response.status = 500 logger.error("500 Error: %s" % _cperror.format_exc()) cherrypy.response.body = [ "<html><body>Sorry, an error occured</body></html>" ]
# Get the currently installed version. Returns None, 'win32' or the git # hash. GIT_LOCAL, GIT_BRANCH = versioncheck.getVersion() # Write current version to a file, so we know which version did work. # This allows one to restore to that version. The idea is that if we # arrive here, most parts of the app seem to work. if GIT_LOCAL: version_lock_file = os.path.join(DATADIR, "version.lock") try: with open(version_lock_file, "w") as fp: fp.write(GIT_LOCAL) except IOError as e: logger.error("Unable to write current version to file '%s': %s", version_lock_file, e) # Check for new versions if GIT_ENABLED and GIT_STARTUP: try: GIT_UPSTREAM = versioncheck.checkGithub() except: logger.error("Unhandled exception in version check") GIT_UPSTREAM = GIT_LOCAL else: GIT_UPSTREAM = GIT_LOCAL # Store the original umask UMASK = os.umask(0) os.umask(UMASK)
if 'mysqlPort' in kwargs: try: cherrystrap.MYSQL_PORT = int(kwargs.pop('mysqlPort', 3306)) except: errorList.append("mysqlPort must be an integer") kwargs.pop('MySQLPort', 3306) if 'mysqlUser' in kwargs: cherrystrap.MYSQL_USER = kwargs.pop('mysqlUser', None) if 'mysqlPass' in kwargs: mysqlPassProcess = kwargs.pop('mysqlPass', None) if mysqlPassProcess != cherrystrap.MYSQL_PASS and mysqlPassProcess != "": try: cherrystrap.MYSQL_PASS = formatter.encode( 'obscure', mysqlPassProcess) except Exception, e: logger.error( 'There was a problem encoding MySQL password: %s' % e) elif mysqlPassProcess == "": cherrystrap.MYSQL_PASS = "" if 'gitEnabled' in kwargs: cherrystrap.GIT_ENABLED = kwargs.pop('gitEnabled', False) == 'true' elif 'gitEnabledHidden' in kwargs: cherrystrap.GIT_ENABLED = kwargs.pop('gitEnabledHidden', False) == 'true' if 'gitPath' in kwargs: cherrystrap.GIT_PATH = kwargs.pop('gitPath', None) if 'gitUser' in kwargs: cherrystrap.GIT_USER = kwargs.pop('gitUser', 'theguardian') if 'gitRepo' in kwargs: cherrystrap.GIT_REPO = kwargs.pop('gitRepo', 'CherryStrap') if 'gitBranch' in kwargs:
# Get the currently installed version. Returns None, 'win32' or the git # hash. GIT_LOCAL, GIT_BRANCH = versioncheck.getVersion() # Write current version to a file, so we know which version did work. # This allows one to restore to that version. The idea is that if we # arrive here, most parts of the app seem to work. if GIT_LOCAL: version_lock_file = os.path.join(DATADIR, "version.lock") try: with open(version_lock_file, "w") as fp: fp.write(GIT_LOCAL) except IOError as e: logger.error( "Unable to write current version to file '%s': %s", version_lock_file, e) # Check for new versions if GIT_ENABLED and GIT_STARTUP: try: GIT_UPSTREAM = versioncheck.checkGithub() except: logger.error("Unhandled exception in version check") GIT_UPSTREAM = GIT_LOCAL else: GIT_UPSTREAM = GIT_LOCAL # Store the original umask UMASK = os.umask(0) os.umask(UMASK)
def update(): if cherrystrap.INSTALL_TYPE == 'win': logger.info('Windows .exe updating not supported yet.') elif cherrystrap.INSTALL_TYPE == 'git': output, err = runGit('pull origin ' + cherrystrap.GIT_BRANCH) if not output: logger.error('Couldn\'t download latest version') for line in output.split('\n'): if 'Already up-to-date.' in line: logger.info('No update available, not updating') logger.info('Output: ' + str(output)) elif line.endswith('Aborting.'): logger.error('Unable to update from git: ' + line) logger.info('Output: ' + str(output)) else: tar_download_url = 'https://github.com/%s/%s/tarball/%s' % (cherrystrap.GIT_USER, cherrystrap.GIT_REPO, cherrystrap.GIT_BRANCH) update_dir = os.path.join(cherrystrap.PROG_DIR, 'update') version_path = os.path.join(cherrystrap.PROG_DIR, 'version.txt') logger.info('Downloading update from: ' + tar_download_url) data = urllib2.urlopen(tar_download_url) if not data: logger.error("Unable to retrieve new version from '%s', can't update", tar_download_url) return download_name = cherrystrap.GIT_BRANCH + '-github' tar_download_path = os.path.join(cherrystrap.PROG_DIR, download_name) # Save tar to disk with open(tar_download_path, 'wb') as f: f.write(data) # Extract the tar to update folder logger.info('Extracting file: ' + tar_download_path) tar = tarfile.open(tar_download_path) tar.extractall(update_dir) tar.close() # Delete the tar.gz logger.info('Deleting file: ' + tar_download_path) os.remove(tar_download_path) # Find update dir name update_dir_contents = [x for x in os.listdir(update_dir) if os.path.isdir(os.path.join(update_dir, x))] if len(update_dir_contents) != 1: logger.error("Invalid update data, update failed: " + str(update_dir_contents)) return content_dir = os.path.join(update_dir, update_dir_contents[0]) # walk temp folder and move files to main folder for dirname, dirnames, filenames in os.walk(content_dir): dirname = dirname[len(content_dir) + 1:] for curfile in filenames: old_path = os.path.join(content_dir, dirname, curfile) new_path = os.path.join(cherrystrap.PROG_DIR, dirname, curfile) if os.path.isfile(new_path): os.remove(new_path) os.renames(old_path, new_path) # Update version.txt try: with open(version_path, 'w') as f: f.write(str(cherrystrap.GIT_UPSTREAM)) except IOError as e: logger.error( "Unable to write current version to version.txt, update not complete: %s", e ) return
def initialize(): with INIT_LOCK: global __INITIALIZED__, FULL_PATH, PROG_DIR, LOGLEVEL, DAEMON, \ DATADIR, CONFIGFILE, CFG, LOGDIR, APP_NAME, HTTP_HOST, HTTP_PORT, \ HTTP_USER, HTTP_PASS, HTTP_ROOT, HTTP_LOOK, VERIFY_SSL, \ LAUNCH_BROWSER, HTTPS_ENABLED, HTTPS_KEY, HTTPS_CERT, API_TOKEN, \ DATABASE_TYPE, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASS, \ GIT_ENABLED, GIT_PATH, GIT_BRANCH, GIT_USER, GIT_STARTUP, GIT_INTERVAL, \ GIT_OVERRIDE, GIT_REPO, GIT_UPSTREAM, GIT_LOCAL, GIT_EXISTS if __INITIALIZED__: return False CheckSection(CFG, 'Server') CheckSection(CFG, 'Interface') CheckSection(CFG, 'Database') CheckSection(CFG, 'Git') LOGDIR = check_setting_str(CFG, 'Server', 'logDir', '') if not LOGDIR: LOGDIR = os.path.join(DATADIR, 'Logs') # Create logdir if not os.path.exists(LOGDIR): try: os.makedirs(LOGDIR) except OSError: if LOGLEVEL: print LOGDIR + ":" print ' Unable to create folder for logs. Only logging to console.' # Start the logger, silence console logging if we need to logger.cherrystrap_log.initLogger(loglevel=LOGLEVEL) # Put the cache dir in the data dir for now CACHEDIR = os.path.join(DATADIR, 'cache') if not os.path.exists(CACHEDIR): try: os.makedirs(CACHEDIR) except OSError: logger.error('Could not create cachedir. Check permissions of: ' + DATADIR) GIT_EXISTS = os.path.isdir(os.path.join(DATADIR, '.git')) # Attempt to find location of git in this environment if GIT_EXISTS: output = err = None cmd = 'which git' try: logger.debug('Trying to execute: "' + cmd + '" with shell in ' + os.getcwd()) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=os.getcwd()) output, err = p.communicate() output = output.strip() logger.debug('Git output: ' + output) except OSError: logger.debug('Command failed: %s', cmd) if not output or 'not found' in output or "not recognized as an internal or external command" in output: logger.debug('Unable to find git with command ' + cmd) git_enabled = False git_path = '' git_startup = False git_interval = 0 else: git_enabled = True git_path = output git_startup = True git_interval = 12 else: git_enabled = False git_path = '' git_startup = False git_interval = 0 try: HTTP_PORT = check_setting_int(CFG, 'Server', 'httpPort', 7889) except: HTTP_PORT = 7889 if HTTP_PORT < 21 or HTTP_PORT > 65535: HTTP_PORT = 7889 APP_NAME = check_setting_str(CFG, 'Server', 'appName', 'CherryStrap') HTTP_ROOT = check_setting_str(CFG, 'Server', 'httpRoot', '') HTTP_HOST = check_setting_str(CFG, 'Server', 'httpHost', '0.0.0.0') HTTPS_ENABLED = check_setting_bool(CFG, 'Server', 'sslEnabled', False) HTTPS_KEY = check_setting_str(CFG, 'Server', 'sslKey', 'keys/server.key') HTTPS_CERT = check_setting_str(CFG, 'Server', 'sslCert', 'keys/server.crt') VERIFY_SSL = check_setting_bool(CFG, 'Server', 'sslVerify', True) LAUNCH_BROWSER = check_setting_bool(CFG, 'Server', 'launchBrowser', False) HTTP_USER = check_setting_str(CFG, 'Interface', 'httpUser', '') HTTP_PASS = check_setting_str(CFG, 'Interface', 'httpPass', '') HTTP_LOOK = check_setting_str(CFG, 'Interface', 'httpLook', 'bootstrap') API_TOKEN = check_setting_str(CFG, 'Interface', 'apiToken', uuid.uuid4().hex) DATABASE_TYPE = check_setting_str(CFG, 'Database', 'dbType', '') MYSQL_HOST = check_setting_str(CFG, 'Database', 'mysqlHost', 'localhost') MYSQL_PORT = check_setting_int(CFG, 'Database', 'mysqlPort', 3306) MYSQL_USER = check_setting_str(CFG, 'Database', 'mysqlUser', '') MYSQL_PASS = check_setting_str(CFG, 'Database', 'mysqlPass', '') GIT_ENABLED = check_setting_bool(CFG, 'Git', 'gitEnabled', git_enabled) GIT_PATH = check_setting_str(CFG, 'Git', 'gitPath', git_path) GIT_USER = check_setting_str(CFG, 'Git', 'gitUser', 'theguardian') GIT_REPO = check_setting_str(CFG, 'Git', 'gitRepo', 'CherryStrap') GIT_BRANCH = check_setting_str(CFG, 'Git', 'gitBranch', 'master') GIT_UPSTREAM = check_setting_str(CFG, 'Git', 'gitUpstream', '') GIT_LOCAL = check_setting_str(CFG, 'Git', 'gitLocal', '') GIT_STARTUP = check_setting_bool(CFG, 'Git', 'gitStartup', git_startup) GIT_INTERVAL = check_setting_int(CFG, 'Git', 'gitInterval', git_interval) GIT_OVERRIDE = check_setting_bool(CFG, 'Git', 'gitOverride', False) #=============================================================== # Import a variable definer / checker from your app's __init__.py try: from jiraappy import injectVarCheck injectVarCheck(CFG) except Exception, e: logger.debug("There was a problem importing application variable definitions: %s" % e) #================================================================ # Initialize the database try: createDb(DATABASE_TYPE, DATADIR, APP_NAME, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASS) except Exception, e: logger.error("Error initializing the database: %s" % e)
def initialize(): with INIT_LOCK: global __INITIALIZED__, FULL_PATH, PROG_DIR, LOGLEVEL, DAEMON, DATADIR, CONFIGFILE, CFG, LOGDIR, SERVER_NAME, HTTP_HOST, HTTP_PORT, HTTP_USER, HTTP_PASS, HTTP_ROOT, HTTP_LOOK, LAUNCH_BROWSER, \ XBMC_VERSION, XBMC_HOST, XBMC_PORT, XBMC_USER, XBMC_PASSWORD, XBMC_THUMB_PATH if __INITIALIZED__: return False CheckSection('General') CheckSection('XBMC') try: HTTP_PORT = check_setting_int(CFG, 'General', 'http_port', 7949) except: HTTP_PORT = 7949 if HTTP_PORT < 21 or HTTP_PORT > 65535: HTTP_PORT = 7949 SERVER_NAME = check_setting_str(CFG, 'General', 'server_name', 'KodiDB') HTTP_HOST = check_setting_str(CFG, 'General', 'http_host', '0.0.0.0') HTTP_USER = check_setting_str(CFG, 'General', 'http_user', '') HTTP_PASS = check_setting_str(CFG, 'General', 'http_pass', '') HTTP_ROOT = check_setting_str(CFG, 'General', 'http_root', '') HTTP_LOOK = check_setting_str(CFG, 'General', 'http_look', 'default') LAUNCH_BROWSER = bool(check_setting_int(CFG, 'General', 'launch_browser', 0)) LOGDIR = check_setting_str(CFG, 'General', 'logdir', '') XBMC_VERSION = check_setting_str(CFG, 'XBMC', 'xbmc_version', 'Helix') XBMC_HOST = check_setting_str(CFG, 'XBMC', 'xbmc_host', '') XBMC_PORT = check_setting_str(CFG, 'XBMC', 'xbmc_port', '') XBMC_USER = check_setting_str(CFG, 'XBMC', 'xbmc_user', '') XBMC_PASSWORD = check_setting_str(CFG, 'XBMC', 'xbmc_password', '') XBMC_THUMB_PATH = check_setting_str(CFG, 'XBMC', 'xbmc_thumb_path', '') if not LOGDIR: LOGDIR = os.path.join(DATADIR, 'Logs') THUMBDIR = os.path.join(DATADIR, 'data/images/Thumbnails') if not os.path.exists(THUMBDIR) and os.path.exists(XBMC_THUMB_PATH): try: os.symlink(XBMC_THUMB_PATH, THUMBDIR) logger.info("Thumbnail directory %s symlinked in data/images/Thumbnails" % XBMC_THUMB_PATH) except: logger.error("There was a problem creating the XBMC Thumbnails symlink") # Put the cache dir in the data dir for now CACHEDIR = os.path.join(DATADIR, 'cache') if not os.path.exists(CACHEDIR): try: os.makedirs(CACHEDIR) except OSError: logger.error('Could not create cachedir. Check permissions of: ' + DATADIR) # Create logdir if not os.path.exists(LOGDIR): try: os.makedirs(LOGDIR) except OSError: if LOGLEVEL: print LOGDIR + ":" print ' Unable to create folder for logs. Only logging to console.' # Start the logger, silence console logging if we need to logger.cherrystrap_log.initLogger(loglevel=LOGLEVEL) # Initialize the database try: dbcheck() except Exception, e: logger.error("Can't connect to the database: %s" % e) __INITIALIZED__ = True return True
def generateDiscussions(self): try: myDB = database.DBConnection() pass except: logger.info("There was a MySQL connection error. Please check your Vanilla MySQL connection credentials") return serve_template(templatename="index.html") JSONDIR = os.path.join(cherrystrap.DATADIR,'cache','categories') if not os.path.exists(JSONDIR): try: os.makedirs(JSONDIR) except OSError: logger.error('Could not create user director. Check permissions of: ' + cherrystrap.DATADIR) discussionInfo=[] discussionCount = 0 discussions = myDB.select("SELECT * FROM %s" % cherrystrap.VANILLA_DB+'.GDN_Discussion') for discussion in discussions: discussionCount += 1 categoryID = discussion['CategoryID'] discussionID = discussion['DiscussionID'] posterID = discussion['InsertUserID'] findUser = myDB.select("SELECT * FROM %s WHERE UserID = %s" % (cherrystrap.VANILLA_DB+'.GDN_User', posterID)) for user in findUser: posterEmail = user['Email'] commentsInfo = [] #ORIGINAL POSTER commentsInfo.append({ "author_email": posterEmail, "created_at": discussion['DateInserted'].strftime('%Y-%m-%dT%H:%M:%SZ'), "body": discussion['Body'] }) comments = myDB.select("SELECT * FROM %s WHERE DiscussionID = %s" % (cherrystrap.VANILLA_DB+'.GDN_Comment', discussionID)) for comment in comments: commentUserID = comment['InsertUserID'] commentCreated = comment['DateInserted'].strftime('%Y-%m-%dT%H:%M:%SZ') commentBody = comment['Body'] findCommenter = myDB.select("SELECT * FROM %s WHERE UserID = %s" % (cherrystrap.VANILLA_DB+'.GDN_User', commentUserID)) for commenter in findCommenter: commentEmail = commenter['Email'] commentsInfo.append({ "author_email": commentEmail, "created_at": commentCreated, "body": commentBody }) discussionInfo = { "title": discussion['Name'], "author_email": posterEmail, "created_at": discussion['DateInserted'].strftime('%Y-%m-%dT%H:%M:%SZ'), "comments": commentsInfo } OUTDIR = os.path.join(JSONDIR,str(categoryID),str(discussionID)+'.json') with open(OUTDIR, 'w') as outfile: json.dump(discussionInfo, outfile)