def opendns_my_ip(): from dns import resolver from socket import gethostbyname try: opendns = resolver.Resolver() opendns.nameservers = [gethostbyname('resolver1.opendns.com')] myiplookup = opendns.query("myip.opendns.com", 'A').response.answer[0] return str(myiplookup).split()[4] except Exception as exresolve: log("OpenDNS Error - Resolve: " + str(exresolve)) return False
def authenticate(aDict): """Function docstring for authenticate TBD Args: - node (required) - username (required) - project_id (required) - password (required) - project_name (required) Output: """ from sdcp.core.logger import log ret = {} controller = Device(SC['node'][aDict['node']], None) res = controller.auth({ 'project': aDict['project_name'], 'username': aDict['username'], 'password': aDict['password'] }) ret = {'authenticated': res['auth']} if res['auth'] == 'OK': with DB() as db: ret.update({ 'project_name': aDict['project_name'], 'project_id': aDict['project_id'], 'username': aDict['username'], 'token': controller.get_token(), 'expires': controller.get_cookie_expire() }) db.do( "INSERT INTO openstack_tokens(token,expires,project_id,username,node) VALUES('%s','%s','%s','%s','%s')" % (controller.get_token(), controller.get_token_expire(), aDict['project_id'], aDict['username'], SC['node'][aDict['node']])) token_id = db.get_last_id() for service in ['heat', 'nova', 'neutron', 'glance']: svc = controller.get_service(service, 'public') if len(svc['path']) > 0: svc['path'] = svc['path'] + '/' db.do( "INSERT INTO openstack_services(id,service,service_port,service_url,service_id) VALUES('%s','%s','%s','%s','%s')" % (token_id, service, svc['port'], svc['path'], svc['id'])) db.do( "INSERT INTO openstack_services(id,service,service_port,service_url,service_id) VALUES('%s','%s','%s','%s','%s')" % (token_id, "contrail", 8082, '', '')) log("openstack_authenticate - successful login and catalog init for %s@%s" % (aDict['username'], aDict['node'])) else: log("openstack_authenticate - error logging in for %s@%s" % (aDict['username'], ctrl)) return ret
def get_loopia_ip(subdomain): from sdcp.SettingsContainer import SC import xmlrpclib try: client = xmlrpclib.ServerProxy(uri=SC['loopia']['rpc_server'], encoding='utf-8') data = client.getZoneRecords(SC['loopia']['username'], SC['loopia']['password'], SC['loopia']['domain'], subdomain)[0] return data['rdata'] except Exception as exmlrpc: log("System Error - Loopia get: " + str(exmlrpc)) return False
def alarm(aDict): """Function docstring for alarm TBD Args: Output: """ from sdcp.core.logger import log log("appformix_alarm({})".format(str(aDict))) return { 'result': 'OK', 'info': 'got alarm', 'data': 'waiting to find out what to do with it :-)' }
def logs_clear(aDict): """Function docstring for logs_clear TBD Args: Output: """ from sdcp.SettingsContainer import SC from sdcp.core.logger import log ret = {'node': SC['system']['id'], 'file': {}} for name, file in SC['logs'].iteritems(): try: open(file, 'w').close() ret['file'][name] = 'CLEARED' log("Emptied log [{}]".format(name)) except Exception as err: ret['file'][name] = 'ERROR: %s' % (str(err)) return ret
def set_loopia_ip(subdomain, newip): from sdcp.SettingsContainer import SC import xmlrpclib try: client = xmlrpclib.ServerProxy(uri=SC['loopia']['rpc_server'], encoding='utf-8') data = client.getZoneRecords(SC['loopia']['username'], SC['loopia']['password'], SC['loopia']['domain'], subdomain)[0] oldip = data['rdata'] data['rdata'] = newip status = client.updateZoneRecord(SC['loopia']['username'], SC['loopia']['password'], SC['loopia']['domain'], subdomain, data)[0] except Exception as exmlrpc: log("System Error - Loopia set: " + str(exmlrpc)) return False return True
def process(aDict): """Function docstring for process. Process a media file Args: - filepath (cond optional) - path (cond optional) - file (cond optional) Output: """ from time import time from subprocess import check_call, call from os import devnull, chmod, rename, remove from sdcp.core.logger import log filename = aDict.get('filepath') if aDict.get('filepath') else ospath.join( aDict.get('path'), aDict.get('file')) ret = { 'prefix': filename[:-4], 'suffix': filename[-3:], 'timestamp': int(time()), 'rename': False, 'res': 'NOT_OK', 'error': None } srt = check_srt({'filepath': filename}) info = aDict if aDict.get('name') and aDict.get('info') else check_title( {'filepath': filename}) dest = ospath.abspath(ospath.join(info['path'], info['name'])) ret.update({ 'info': info, 'srt': srt, 'changes': { 'subtitle': "", 'audio': "", 'srt': "" }, 'dest': dest }) try: if filename != dest: try: rename(filename, dest) except Exception as e: ret['error'] = str(e) else: ret['rename'] = True if ret['suffix'] == 'mkv' and not ret['error']: if srt['code']: log("INFO - %s - SRT found:%s" % (filename, srt['code'])) srtfile = "%s.process" % srt['file'] ret['changes'][ 'srt'] = "--language 0:{0} --track-name 0:{0} -s 0 -D -A {1}".format( srt['code'], repr(ospath.abspath(srtfile))) rename(srt['file'], srtfile) probe = check_content({'filepath': dest, 'srt': srt.get('code')}) ret['probe'] = probe # if forced download or if there are subs to remove but no subs to add left if len(probe['subtitle']['remove']) > 0: ret['changes']['subtitle'] = "--no-subtitles" if len( probe['subtitle'] ['add']) == 0 else "--stracks " + ",".join( map(str, probe['subtitle']['add'])) if len(probe['audio']['remove']) and len( probe['audio']['add']) > 0: ret['changes']['audio'] = "--atracks " + ",".join( map(str, probe['audio']['add'])) if (ret['rename'] or probe['video']['set_default'] or probe['audio']['add_aac'] or len(ret['changes']['subtitle']) or len(ret['changes']['audio']) or srt['code']): log("INFO - %s - Modifying file" % filename) FNULL = open(devnull, 'w') if ret['rename']: ret['update_title'] = info['info'] call([ 'mkvpropedit', '--set', "title=" + info['info'], dest ], stdout=FNULL, stderr=FNULL) if probe['video']['set_default']: call([ 'mkvpropedit', '--edit', 'track:v1', '--set', 'language=eng', dest ], stdout=FNULL, stderr=FNULL) if probe['audio']['add_aac'] or len( ret['changes']['audio']) or len( ret['changes']['subtitle']) or srt['code']: from sdcp.SettingsContainer import SC from tempfile import mkdtemp log("INFO - %s - Modifying addaac:%s Modify: %s %s" % (probe['audio']['add_aac'], ret['changes']['subtitle'], ret['changes']['audio'], ret['changes']['srt'])) tmpfile = filename + ".process" rename(dest, tmpfile) tempd = SC['multimedia']['temp_directory'] tempdir = mkdtemp(suffix="", prefix='aac.', dir=tempd) if probe['audio']['add_aac']: check_call([ 'avconv', '-i', tmpfile, '-vn', '-acodec', 'pcm_s16le', '-ac', '2', tempdir + '/audiofile.wav' ], stdout=FNULL, stderr=FNULL) check_call( ['normalize-audio', tempdir + '/audiofile.wav'], stdout=FNULL, stderr=FNULL) check_call([ 'faac', '-c', '48000', '-b', '160', '-q', '100', '-s', tempdir + '/audiofile.wav', '-o', tempdir + '/audiofile.aac' ], stdout=FNULL, stderr=FNULL) ret['changes'][ 'srt'] = "--language 0:{} --default-track 0 {}/audiofile.aac ".format( probe['video']['language'], tempdir) + ret['changes']['srt'] call([ "mkvmerge -o '{}' {} {} '{}' {}".format( dest, ret['changes']['subtitle'], ret['changes']['audio'], tmpfile, ret['changes']['srt']) ], stdout=FNULL, stderr=FNULL, shell=True) call(['rm', '-fR', tempdir]) remove(tmpfile) if srt['code']: rename(srtfile, srtfile + "ed") elif ret['suffix'] == 'mp4' and ret['rename'] and (srt['code'] or aDict['modify']): FNULL = open(devnull, 'w') if srt['code']: tmpfile = filename + ".process" rename(dest, tmpfile) chmod(srt['file'], 0666) chmod(dest, 0666) call([ 'MP4Box -add {0}:hdlr=sbtl:lang={1}:name={2}:group=2:layer=-1:disable {3} -out {4}' .format(repr(srt['file']), srt['code'], srt['name'], repr(tmpfile), repr(dest)) ], shell=True, stdout=FNULL) rename(srt['file'], "%s.processed" % srt['file']) remove(tmpfile) if aDict['modify']: if info['episode']: call([ 'mp4tags', '-o', info['episode'], '-n', info['episode'][1:3], '-M', info['episode'][4:6], '-S', info['title'], '-m', info['info'], dest ], stdout=FNULL, stderr=FNULL) else: log("WARN - %s - Movie modifications not implemented" % filename) except Exception as err: ret['error'] = str(err) log("XCPT - %s - Error: %s" % (filename, str(err))) else: ret['seconds'] = (int(time()) - ret['timestamp']) if ospath.exists(dest): log("INFO - %s - Success - processed in % seconds" % (filename, ret['seconds'])) chmod(dest, 0666) ret['res'] = 'OK' else: log("ERRR - %s - FAILURE PROCESSING!" % filename) ret['error'] = 'COMPLETE_NO_FILE' return ret
def log_msg(self, aMsg): from sdcp.core.logger import log log(aMsg)