def index(self, zone=None, **params): """main page rendering """ user = '******' user = self.auth.check_auth() parse_query_string(cherrypy.request.query_string) # zone is defined by the query string parameter # if query string is empty, use the default zone if zone is None: zone = self.zone_default try: records = self._refresh_zone(zone) # exception handling if impossible to get the zone # (dns unavailable, misconfiguration, etc) except Exception as e: return self._error_handler(e, zone) return self.temp_index.render( logout_button=self.auth.logout_button, records=records, zone_list=self.zone_list, default_ttl=self.default_ttl, type_written=self.type_written, current_zone=zone, notifications=self._empty_notification(), )
def index(self, **params): # Get the ID for this user from the query string query_string = parse_query_string(cherrypy.request.query_string) user_id = query_string["user_id"] # Create a session ID for this interaction session_id = str(uuid.uuid4()) sessions[session_id] = user_id print("UserID: " + user_id + " Session: " + sessions[session_id]) scope = "alexa_all" sd = json.dumps({ "alexa:all": { "productID": DEVICE_TYPE_ID, "productInstanceAttributes": { "deviceSerialNumber": user_id } } }) url = "https://www.amazon.com/ap/oa" callback = cherrypy.url() + "authresponse" payload = { "client_id": CLIENT_ID, "scope": "alexa:all", "scope_data": sd, "response_type": "code", "redirect_uri": callback } req = requests.Request('GET', url, params=payload) p = req.prepare() cherrypy.response.cookie['session_id'] = session_id cherrypy.response.cookie['session_id']['path'] = '/' raise cherrypy.HTTPRedirect(p.url)
def put(self, **kwargs): input_json = cherrypy.request.json msg = input_json msgOut = "" query = parse_query_string(cherrypy.request.query_string) queue = str(query["queue"]) if queue in available_queues: temporalElement = msg queues[queue].put(msg) queues["temporal"].put(msg) msgOut = { "status": "OK", "method": "put", "queue": queue, "size": queues[queue].qsize() } else: msgOut = { "status": "ERROR", "method": "put", "msg": "Cola no existente." } #stomp = Client("http://activemq-mitmactivemq.rhcloud.com", 61613) #stomp.connect("producer", "pass") #stomp.put(json.dumps(Dict_Message), destination="/queue/test",conf={'Test':'Test123'}) #stomp.disconnect() return msgOut
def get(self, **kwargs): query = parse_query_string(cherrypy.request.query_string) msgOut = "" #queue = kwargs["queue"] queue = str(query["queue"]) if queue in available_queues: if queues[queue].qsize() > 0: msgOut = { "status": "OK", "method": "get", "queue": queue, "size": queues[queue].qsize(), "element": queues[queue].get() } else: msgOut = { "status": "OK", "method": "get", "queue": queue, "size": 0 } else: msgOut = { "status": "ERROR", "method": "get", "msg": "Cola no existente." } return msgOut
def POST(self, profile, **params): # required payload required = ['title', 'provider', 'platform'] js_in = cherrypy.request.json qs_in = parse_query_string(cherrypy.request.query_string) cherrypy.log("DEBUG: job request: {}".format(json.dumps(js_in))) parms = {**js_in, **qs_in} if not all([k in parms.keys() for k in required]): raise cherrypy.HTTPError(400, "missing fields in request") available_profiles = [p['name'] for p in db.fetch_all('profiles', list(['name']))] if profile not in available_profiles: # raise APIError(status=404, message="FIO workload profile '{}' not found in ./data/fio/jobs".format(profile)) raise cherrypy.HTTPError(404, "profile not found") # if we have a jobspec passed to us, use that, otherwise read the spec from the profile in the db profile_spec = parms.get('spec', None) if not profile_spec: profile_spec = db.fetch_row('profiles', 'name', profile)['spec'] job = AsyncJob() job.type = 'startfio' job.stale = False job.status = 'queued' job.uuid = str(uuid.uuid4()) job.profile = profile job.spec = profile_spec job.outfile = '{}.{}'.format(job.uuid, profile) job.workers = parms.get('workers', 9999) # FIX ME job.provider = parms.get('provider') job.platform = parms.get('platform') job.title = parms.get('title', '{} on '.format(profile, datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))) with sqlite3.connect(self.dbpath) as c: csr = c.cursor() csr.execute(""" INSERT into jobs (id, title, profile, workers, status, type, provider, platform) VALUES(?,?,?,?,?,?,?,?);""", (job.uuid, job.title, profile, job.workers, job.status, 'fio', job.provider, job.platform, )) # post request using a valid profile, place on the queue and in the tracker dict work_queue.put(job) add_tracker(job) cherrypy.log("job {} queued, for profile {}".format(job.uuid, profile)) self.service_state.tasks_queued = work_queue.qsize() cherrypy.response.status = 202 # 202 = Accepted return {'data': {"message": "run requested, current work queue size {}".format(work_queue.qsize()), "uuid": job.uuid}}
def getDLS(self, uniq_id=None, **params): query = parse_query_string(cherrypy.request.query_string) self.conf = Config(self.config_file) if uniq_id: query['uniq_id'] = uniq_id output = [] for odr in self.conf.config['odr']: if (('uniq_id' in query) and (query['uniq_id'] == odr['uniq_id']) or ('uniq_id' not in query)): if 'padenc' in odr: if odr['padenc']['enable'] == 'true': dls=None dlplus=None dlplus_data=[] try: with open(odr['padenc']['dls_file'], 'r') as f: for line in f: if line.startswith('#'): continue if line.startswith('DL_PLUS='): dlplus=True #continue if line.startswith('DL_PLUS_TAG='): v = line.split("=", 1) d = v[1].split(" ") dlplusCode = { 1: 'title', 2: 'album', 3: 'tracknumber', 4: 'artist', 5: 'composition', 6: 'movement', 7: 'conductor', 8: 'composer', 9: 'band', 10: 'comment', 11: 'genre' } dlplus_data.append( {'name': dlplusCode[int(d[0])], 'code': int(d[0]), 'start': int(d[1]), 'len': int(d[2])} ) dls = line.rstrip() except Exception as e: output.append({'coder_uniq_id': odr['uniq_id'], 'coder_name': odr['name'], 'coder_description': odr['description'], 'dls': 'Fail to read DLS data' }) else: if dlplus: dlplus= {} for d in dlplus_data: dlplus[d['name']] = dls[d['start']:d['start']+d['len']+1] output.append({'status': '0', 'statusText': 'Ok', 'coder_uniq_id': odr['uniq_id'], 'coder_name': odr['name'], 'coder_description': odr['description'], 'dls': str(dls), 'dlplus': dlplus}) else: output.append({'status': '0', 'statusText': 'Ok', 'coder_uniq_id': odr['uniq_id'], 'coder_name': odr['name'], 'coder_description': odr['description'], 'dls': str(dls)}) else: output.append({'status': '-10', 'statusText': 'DLS is disabled', 'coder_uniq_id': odr['uniq_id'], 'coder_name': odr['name'], 'coder_description': odr['description'], 'dls': ''}) else: output.append({'status': '-20', 'statusText': 'Encoder is not configured', 'coder_uniq_id': odr['uniq_id'], 'coder_name': odr['name'], 'coder_description': odr['description'], 'dls': ''}) if ('uniq_id' in query) and (len(output) == 0): return {'status': '0', 'statusText': 'uniq_id not found', 'data': output} return {'status': '0', 'statusText': 'Ok', 'data': output}
def content(): parse_query_string(cherrypy.request.query_string) if repo == "redhat6": red_hat_6_rpm_list_path = "/local/pulp/published/yum/master/yum_distributor/ihg-redhat-6Server-x86_64-" + release.get_current_release( ) + "/*" red_hat_6_rpm_list = repo_list.get_repo_list( "ls " + red_hat_6_rpm_list_path) elif repo == "eus": red_hat_6_rpm_list_path = "/local/pulp/published/yum/master/yum_distributor/ihg-redhat-6Server-eus-x86_64-" + release.get_current_release( ) + "/*" red_hat_6_rpm_list = repo_list.get_repo_list( "ls " + red_hat_6_rpm_list_path) elif repo == "optional": red_hat_6_rpm_list_path = "/local/pulp/published/yum/master/yum_distributor/ihg-redhat-6Server-optional-x86_64-" + release.get_current_release( ) + "/*" red_hat_6_rpm_list = repo_list.get_repo_list( "ls " + red_hat_6_rpm_list_path) elif repo == "dts": red_hat_6_rpm_list_path = "/local/pulp/published/yum/master/yum_distributor/ihg-redhat-6Server-dts-x86_64-" + release.get_current_release( ) + "/*" red_hat_6_rpm_list = repo_list.get_repo_list( "ls " + red_hat_6_rpm_list_path) elif repo == "dts2": red_hat_6_rpm_list_path = "/local/pulp/published/yum/master/yum_distributor/ihg-redhat-6Server-dts2-x86_64-" + release.get_current_release( ) + "/*" red_hat_6_rpm_list = repo_list.get_repo_list( "ls " + red_hat_6_rpm_list_path) yield " " * 1024 yield "\n" for i in red_hat_6_rpm_list: cmd = "rpm -qp --changelog " + red_hat_6_rpm_list_path + "/" + i print(cmd) output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) output = output.stdout.read() if re.search(month + ' \d\d ' + year, output): output = re.split('\n\s*\n', output) yield "-------------------------------------------------------------------------------\n" yield i + "\n" yield "-------------------------------------------------------------------------------\n" for line in output: if re.search(month + ' \d\d ' + year, line): yield line + "\n" else: pass
def getCoder(self): query = parse_query_string(cherrypy.request.query_string) self.conf = Config(self.config_file) output = [] for data in self.conf.config['odr']: output.append( {'name': data['name'], 'description': data['description'], 'uniq_id': data['uniq_id']} ) return {'status': '0', 'statusText': 'Ok', 'data': output}
def getDLS(self, **params): query = parse_query_string(cherrypy.request.query_string) self.conf = Config(self.config_file) #cherrypy.response.headers["Content-Type"] = "application/json" output = [] if self.conf.config['odr']['padenc']['enable'] == 'true': dls = None dlplus = None dlplus_data = [] try: with open(self.conf.config['odr']['padenc']['dls_fifo_file'], 'r') as f: for line in f: if line.startswith('#'): continue if line.startswith('DL_PLUS='): dlplus = True #continue if line.startswith('DL_PLUS_TAG='): v = line.split("=", 1) d = v[1].split(" ") dlplusCode = { 1: 'title', 2: 'album', 3: 'tracknumber', 4: 'artist', 5: 'composition', 6: 'movement', 7: 'conductor', 8: 'composer', 9: 'band', 10: 'comment', 11: 'genre' } dlplus_data.append({ 'name': dlplusCode[int(d[0])], 'code': int(d[0]), 'start': int(d[1]), 'len': int(d[2]) }) dls = line.rstrip() except Exception as e: return {'dls': 'Fail to read DLS data'} else: if dlplus: dlplus = {} for d in dlplus_data: dlplus[d['name']] = dls[d['start']:d['start'] + d['len'] + 1] return {'dls': str(dls), 'dlplus': dlplus} else: return {'dls': str(dls)} else: return {'dls': 'DLS is disabled'}
def qSize(self, **kwargs): msgOut= "" query = parse_query_string(cherrypy.request.query_string) queue = str(query["queue"]) if queue in available_queues: msgOut = {"status" : "OK", "method" : "qSize", "queue" : queue, "size" : queues[queue].qsize()} else: msgOut = {"status" : "ERROR", "method" : "qSize", "msg" : "Cola no existente."} return msgOut
def process_query_string(self): """Parse the query string into Python structures. (Core)""" try: p = httputil.parse_query_string( self.query_string, encoding=self.query_string_encoding) except UnicodeDecodeError: raise cherrypy.HTTPError( 404, "The given query string could not be processed. Query " "strings for this resource must be encoded with %r." % self.query_string_encoding) self.params.update(p)
def getConfig(self, **params): query = parse_query_string(cherrypy.request.query_string) self.conf = Config(self.config_file) if 'uniq_id' in query: for data in self.conf.config['odr']: if data['uniq_id'] == query['uniq_id']: return {'status': '0', 'statusText': 'Ok', 'data': data} return {'status': '-299', 'statusText': 'coder not found', 'data': {}} else: return {'status': '0', 'statusText': 'Ok', 'data': self.conf.config['odr'][0]}
def process_query_string(self): try: p = httputil.parse_query_string(self.query_string, encoding=self.query_string_encoding) except UnicodeDecodeError: raise cherrypy.HTTPError(404, 'The given query string could not be processed. Query strings for this resource must be encoded with %r.' % self.query_string_encoding) for key, value in p.items(): if isinstance(key, unicode): del p[key] p[key.encode(self.query_string_encoding)] = value self.params.update(p)
def process_query_string(self): """Parse the query string into Python structures. (Core)""" try: p = httputil.parse_query_string(self.query_string, encoding=self.query_string_encoding) except UnicodeDecodeError: raise cherrypy.HTTPError(404, 'The given query string could not be processed. Query strings for this resource must be encoded with %r.' % self.query_string_encoding) for key, value in p.items(): if isinstance(key, unicode): del p[key] p[key.encode(self.query_string_encoding)] = value self.params.update(p)
def getNetworkCards(self, **params): self.conf = Config(self.config_file) query = parse_query_string(cherrypy.request.query_string) if 'card' in query: data={} for card in self.conf.config['global']['network']['cards']: if card['card'] == query['card']: data=card else: data = self.conf.config['global']['network']['cards'] return {'status': '0', 'statusText': 'Ok', 'data': data}
def index(self, **args): params = parse_query_string(cherrypy.request.query_string) print params print args recipients = "[" for rec in params['recipients'].split(' '): recipients += '"' + rec + '",' recipients = recipients[:-1] recipients += ']' motionEnable = 'true' if ('motion_detection' in params.keys()) else 'false' with open('settings.json', 'w') as datafile: datafile.write('{"motion_detection": %s,"temperature_threshold": %.1f,"humidity_threshold": %.1f,"recipients": %s}' % (motionEnable, float(params['temperature_threshold']), float(params['humidity_threshold']), recipients)) raise cherrypy.HTTPRedirect("/")
def getNetworkCards(self, **params): self.conf = Config(self.config_file) query = parse_query_string(cherrypy.request.query_string) if 'card' in query: data = {} for card in self.conf.config['global']['network']['cards']: if card['card'] == query['card']: data = card else: data = self.conf.config['global']['network']['cards'] return {'status': '0', 'statusText': 'Ok', 'data': data}
def GET(self, table='jobs', **params): # look at querystring to get the key/value qs = parse_query_string(cherrypy.request.query_string) # cherrypy.log(json.dumps(qs)) tf = tempfile.NamedTemporaryFile(delete=False) cherrypy.log("export file created - {}".format(tf.name)) with open(tf.name, 'w') as f: for line in db.dump_table(table_name=table, query=qs): f.write('{}\n'.format(line)) return static.serve_file(tf.name, 'text/plain', 'attachment', os.path.basename(tf.name))
def get(self, **kwargs): query = parse_query_string(cherrypy.request.query_string) msgOut = "" #queue = kwargs["queue"] queue = str(query["queue"]) if queue in available_queues: if queues[queue].qsize() > 0: msgOut = {"status" : "OK", "method" : "get", "queue" : queue, "size" : queues[queue].qsize(), "element" : queues[queue].get()} else: msgOut = {"status" : "OK", "method" : "get", "queue" : queue, "size" : 0} else: msgOut = {"status" : "ERROR", "method" : "get", "msg" : "Cola no existente."} return msgOut
def GET(self, uuid=None, **params): qs = parse_query_string(cherrypy.request.query_string) # cherrypy.log(json.dumps(qs)) if not qs or not qs.get('fields', None): fields = list(['id', 'status', 'title']) else: fields = qs['fields'].split(',') if uuid is None: return {"data": db.fetch_all('jobs', fields)} else: data = db.fetch_row('jobs', 'id', uuid) if data: return {"data": json.dumps(data)} else: raise cherrypy.HTTPError(404, "Invalid job id")
def put(self, **kwargs): input_json = cherrypy.request.json msg = input_json msgOut = "" query = parse_query_string(cherrypy.request.query_string) queue = str(query["queue"]) if queue in available_queues: queues[queue].put(msg) msgOut = {"status" : "OK", "method" : "put", "queue" : queue, "size" : queues[queue].qsize()} else: msgOut = {"status" : "ERROR", "method" : "put", "msg" : "Cola no existente."} #stomp = Client("http://activemq-mitmactivemq.rhcloud.com", 61613) #stomp.connect("producer", "pass") #stomp.put(json.dumps(Dict_Message), destination="/queue/test",conf={'Test':'Test123'}) #stomp.disconnect() return msgOut
def process_query_string(self): """Parse the query string into Python structures. (Core)""" try: p = httputil.parse_query_string( self.query_string, encoding=self.query_string_encoding) except UnicodeDecodeError: raise cherrypy.HTTPError( 404, "The given query string could not be processed. Query " "strings for this resource must be encoded with %r." % self.query_string_encoding) # Python 2 only: keyword arguments must be byte strings (type 'str'). for key, value in p.items(): if isinstance(key, unicode): del p[key] p[key.encode(self.query_string_encoding)] = value self.params.update(p)
def process_query_string(self): """Parse the query string into Python structures. (Core)""" try: p = httputil.parse_query_string( self.query_string, encoding=self.query_string_encoding) except UnicodeDecodeError: raise cherrypy.HTTPError( 404, 'The given query string could not be processed. Query ' 'strings for this resource must be encoded with %r.' % self.query_string_encoding) # Python 2 only: keyword arguments must be byte strings (type 'str'). if six.PY2: for key, value in p.items(): if isinstance(key, six.text_type): del p[key] p[key.encode(self.query_string_encoding)] = value self.params.update(p)
def qSize(self, **kwargs): msgOut = "" query = parse_query_string(cherrypy.request.query_string) queue = str(query["queue"]) if queue in available_queues: msgOut = { "status": "OK", "method": "qSize", "queue": queue, "size": queues[queue].qsize() } else: msgOut = { "status": "ERROR", "method": "qSize", "msg": "Cola no existente." } return msgOut
def GET(self, profile=None, **params): qs = parse_query_string(cherrypy.request.query_string) # cherrypy.log(json.dumps(qs)) if profile is None: response_data = {"data": db.fetch_all('profiles', list(['name']))} if qs.keys(): refresh = qs.get('refresh', None) if refresh == 'true': # need to refresh the profiles from the filesystem, before returning the profiles summary = db.load_db_profiles(out='cherrypy') # summary = db.load_db_profiles(JOB_DIR, dbpath=self.dbpath, out='cherrypy') response_data['summary'] = summary else: raise cherrypy.HTTPError(400, "profile endpoint only supports a refresh=true query string") return response_data else: return {"data": db.fetch_row('profiles', 'name', profile)['spec']}
def getAVTStatus(self, **params): def is_valid_ip(ip): m = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip) return bool(m) and all(map(lambda n: 0 <= int(n) <= 255, m.groups())) query = parse_query_string(cherrypy.request.query_string) if 'ip' in query: if is_valid_ip(query['ip']): avt = AVT(snmp_host=query['ip']) avt_get = avt.getAll() if avt_get['status'] == 0: return {'status': '0', 'statusText': 'Ok', 'data': avt_get['data']} else: return {'status': avt_get['status'], 'statusText': avt_get['statusText'], 'data': avt_get['data']} else: return {'status': '-241', 'statusText': 'Invalid AVT IP parameter', 'data': ''} else: return {'status': '-242', 'statusText': 'Missing AVT IP parameter', 'data': ''}
def DELETE(self, table, **params): """ allow the caller to delete a database row """ qstring = cherrypy.request.query_string qs = parse_query_string(qstring) if len(qs) != 1: raise cherrypy.HTTPError(400, "Invalid or missing query") if table == 'profiles' and not qs.get('name', None): raise cherrypy.HTTPError(400, "invalid key used for profiles table, must be name=") elif table == 'jobs' and not qs.get('id', None): raise cherrypy.HTTPError(400, "invalid key used for jobs table, must be id=") cherrypy.log("delete request for {}/{}".format(table, qstring)) err = db.delete_row(table, qs) if err: cherrypy.log("delete request failed: {}".format(err)) raise cherrypy.HTTPError(400, err) else: cherrypy.log("delete successful") return {"data": {"msg": "row successufully removed"}}
def index(self): params = parse_query_string(cherrypy.request.query_string) cherrypy.response.headers['Content-Type'] = 'application/json' with open('connection.json', 'r') as datafile: data = json.load(datafile) return json.dumps(data)
def index(self): params = parse_query_string(cherrypy.request.query_string)
def getNetworkNTP(self, **params): self.conf = Config(self.config_file) query = parse_query_string(cherrypy.request.query_string) data = self.conf.config['global']['network']['ntp'] return {'status': '0', 'statusText': 'Ok', 'data': data}
def POST(self, profile, **params): # required payload required = ['title', 'provider', 'storageclass', 'platform'] js_in = cherrypy.request.json qs_in = parse_query_string(cherrypy.request.query_string) cherrypy.log("DEBUG: job request: {}".format(json.dumps(js_in))) parms = {**js_in, **qs_in} for k in parms.keys(): try: required.remove(k) except ValueError: # just means we have an optional parameter continue if required: raise cherrypy.HTTPError(400, f'Missing fields: {",".join(required)}') workers = self.service_state._handler.workers if not workers: raise cherrypy.HTTPError(503, 'No worker pods are active') if parms.get('storageclass') not in workers: raise cherrypy.HTTPError( 400, f'Storageclass "{parms.get("storageclass")}" has no fioloadgen worker pods, or does not exist' ) available_profiles = [ p['name'] for p in db.fetch_all('profiles', list(['name'])) ] available_profiles.append('custom') if profile not in available_profiles: raise cherrypy.HTTPError(404, "profile not found") # if we have a jobspec passed to us, use that, otherwise read the spec from the profile in the db profile_spec = parms.get('spec', None) if profile_spec: if profile == 'custom': assert isinstance(profile_spec, dict) cherrypy.log( f"DEBUG: custom profile requested: {json.dumps(profile_spec)}" ) profile_spec = generate_fio_profile(profile_spec) else: cherrypy.log("DEBUG: using spec from the database") profile_spec = db.fetch_row('profiles', 'name', profile)['spec'] job = AsyncJob() job.type = 'startfio' job.stale = False job.status = 'queued' job.uuid = str(uuid.uuid4()) job.profile = profile job.spec = profile_spec job.outfile = '{}.{}'.format(job.uuid, profile) job.storageclass = parms.get('storageclass') job.workers = parms.get('workers', 9999) # FIXME job.provider = parms.get('provider') job.platform = parms.get('platform') job.title = parms.get( 'title', '{} on {}'.format( profile, datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))) with sqlite3.connect(self.dbpath) as c: csr = c.cursor() csr.execute( """ INSERT into jobs (id, title, profile, profile_spec, storageclass, workers, status, type, provider, platform) VALUES(?,?,?,?,?,?,?,?,?,?);""", ( job.uuid, job.title, profile, job.spec, job.storageclass, job.workers, job.status, 'fio', job.provider, job.platform, )) # post request using a valid profile, place on the queue and in the tracker dict work_queue.put(job) cherrypy.log(f"work queue is {work_queue.qsize()}") add_tracker(job) cherrypy.log("job {} queued, for profile {}".format(job.uuid, profile)) cherrypy.response.status = 202 # 202 = Accepted return { 'data': { "message": "run requested, current work queue size {}".format( work_queue.qsize()), "uuid": job.uuid } }
def POST(self, profile, **params): # required payload required = ['title', 'provider', 'platform'] js_in = cherrypy.request.json qs_in = parse_query_string(cherrypy.request.query_string) cherrypy.log("DEBUG: qs = {}".format(json.dumps(qs_in))) cherrypy.log("DEBUG: payload = {}".format(json.dumps(js_in))) parms = {**js_in, **qs_in} if not all([k in parms.keys() for k in required]): raise cherrypy.HTTPError(400, "missing fields in request") available_profiles = [ p['name'] for p in fetch_all('profiles', list(['name'])) ] if profile not in available_profiles: # raise APIError(status=404, message="FIO workload profile '{}' not found in ./data/fio/jobs".format(profile)) raise cherrypy.HTTPError(404, "profile not found") cherrypy.log("profile is {}".format(profile)) job = AsyncJob() job.type = 'startfio' job.uuid = str(uuid.uuid4()) job.profile = profile job.outfile = '{}.{}'.format(job.uuid, profile) job.workers = parms.get('workers', 9999) job.provider = parms.get('provider') job.platform = parms.get('platform') job.title = parms.get( 'title', '{} on '.format( profile, datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))) with sqlite3.connect(DBNAME) as c: csr = c.cursor() csr.execute( """ INSERT into jobs (id, title, profile, workers, status, type, provider, platform) VALUES(?,?,?,?,?,?,?,?);""", ( job.uuid, job.title, profile, job.workers, 'queued', 'fio', job.provider, job.platform, )) # post request using a valid profile, place on the queue to run work_queue.put(job) self.service_state.tasks_queued = work_queue.qsize() return { 'data': { "message": "run requested, current work queue size {}".format( work_queue.qsize()), "uuid": job.uuid } }
def setDLS(self, dls=None, artist=None, title=None, output=None, **params): self.conf = Config(self.config_file) if cherrypy.request.method == 'POST': query = {} if dls: query['dls'] = dls elif artist and title: query['artist'] = artist query['title'] = title if output: query['output'] = output elif cherrypy.request.method == 'GET': query = parse_query_string(cherrypy.request.query_string) else: cherrypy.response.status = 400 # it's impossible to use build_response here, because # with an invalid request, the `output` parameter is # also considered invalid. cherrypy.response.headers['content-type'] = "text/plain" return "Only HTTP POST or GET are available" def build_response(r): """Helper function to choose between plain text and JSON response. `r` shall be a dictionary. """ if 'output' in query and query['output'] == 'json': cherrypy.response.headers['content-type'] = "application/json" return json.dumps(r).encode() else: cherrypy.response.headers['content-type'] = "text/plain" return r['statusText'] # DLS (odr-padenc process) is enable if self.conf.config['odr']['padenc']['enable'] == 'true': # dls parameters is present and override all other if 'dls' in query: if self.getDLS( )['dls'] == query['dls'] and 'dlplus' not in self.getDLS(): r = { 'status': '0', 'statusText': 'Ok-oldegal', 'dls': query['dls'] } return build_response(r) try: with codecs.open( self.conf.config['odr']['padenc']['dls_fifo_file'], 'w', 'utf-8') as outfile: outfile.write(query['dls']) except Exception as e: r = { 'status': '-210', 'statusText': 'Fail to write dls data' } cherrypy.response.status = 500 return build_response(r) else: r = { 'status': '0', 'statusText': 'Ok', 'dls': query['dls'] } return build_response(r) # dls is not present and artist and title are available elif ('artist' in query) and ('title' in query): if 'dlplus' in self.getDLS() and self.getDLS( )['dlplus']['artist'] == query['artist'] and self.getDLS( )['dlplus']['title'] == query['title']: r = { 'status': '0', 'statusText': 'Ok-oldegal', 'dls': { 'artist': query['artist'], 'title': query['title'] } } return build_response(r) if (query['artist'] != '') and (query['title'] != ''): data = '##### parameters { #####\n' data += 'DL_PLUS=1\n' data += '# this tags \"%s\" as ITEM.ARTIST\n' % ( query['artist']) data += 'DL_PLUS_TAG=4 0 %s\n' % (len(query['artist']) - 1) data += '# this tags \"%s\" as ITEM.TITLE\n' % ( query['title']) data += 'DL_PLUS_TAG=1 %s %s\n' % ( len(query['artist']) + 3, len(query['title']) - 1) data += '##### parameters } #####\n' data += '%s - %s\n' % (query['artist'], query['title']) try: with codecs.open( self.conf.config['odr']['padenc'] ['dls_fifo_file'], 'w', 'utf-8') as outfile: outfile.write(data) except Exception as e: r = { 'status': '-210', 'statusText': 'Fail to write dls data' } cherrypy.response.status = 500 return build_response(r) else: r = { 'status': '0', 'statusText': 'Ok', 'dls': { 'artist': query['artist'], 'title': query['title'] } } return build_response(r) else: r = { 'status': '-215', 'statusText': 'Error, artist or title are blank' } cherrypy.response.status = 422 return build_response(r) # no needed parameters available else: r = { 'status': '-209', 'statusText': 'Error, you need to use dls or artist + title parameters' } cherrypy.response.status = 400 return build_response(r) # DLS (odr-padenc process) is disable else: r = {'status': '-208', 'statusTest': 'DLS is disable'} cherrypy.response.status = 422 return build_response(r)
def setDLS(self, dls=None, artist=None, title=None, output=None, uniq_id=None, **params): self.conf = Config(self.config_file) if cherrypy.request.method == 'POST': query = {} if dls: query['dls'] = dls elif artist and title: query['artist'] = artist query['title'] = title if output: query['output'] = output if uniq_id: query['uniq_id'] = uniq_id elif cherrypy.request.method == 'GET': query = parse_query_string(cherrypy.request.query_string) else: cherrypy.response.status = 400 # it's impossible to use build_response here, because # with an invalid request, the `output` parameter is # also considered invalid. cherrypy.response.headers['content-type'] = "text/plain" return "Only HTTP POST or GET are available" def build_response(r): """Helper function to choose between plain text and JSON response. `r` shall be a dictionary. """ if 'output' in query and query['output'] == 'json': cherrypy.response.headers['content-type'] = "application/json" return json.dumps(r).encode() else: cherrypy.response.headers['content-type'] = "text/plain" if len(r) == 0: return 'no data updated' else: output = '' for o in r: output += '%s: %s\n' % (o['coder_name'], o['statusText'] ) return output def process_query(odr, query): output = {} output['coder_name'] = odr['name'] output['coder_uniq_id'] = odr['uniq_id'] output['coder_description'] = odr['description'] if 'padenc' in odr: # DLS (odr-padenc process) is enable if odr['padenc']['enable'] == 'true': # dls parameters is present and override all other if 'dls' in query: if self.getDLS(output['coder_uniq_id'])['data'][0]['dls'] == query['dls'] and 'dlplus' not in self.getDLS(output['coder_uniq_id'])['data'][0]: output['status'] = 0 output['statusText'] = 'Ok-oldegal' output['dls'] = query['dls'] return output try: with codecs.open(odr['padenc']['dls_file'], 'w', 'utf-8') as outfile: outfile.write(query['dls']) except Exception as e: output['status'] = -210 output['statusText'] = 'Fail to write dls data' return output else: output['status'] = 0 output['statusText'] = 'Ok' output['dls'] = query['dls'] return output # dls is not present and artist and title are available elif ('artist' in query) and ('title' in query): if 'dlplus' in self.getDLS(output['coder_uniq_id'])['data'][0] and self.getDLS(output['coder_uniq_id'])['data'][0]['dlplus']['artist'] == query['artist'] and self.getDLS(output['coder_uniq_id'])['data'][0]['dlplus']['title'] == query['title']: output['status'] = 0 output['statusText'] = 'Ok-oldegal' output['dlplus'] = {'artist': query['artist'], 'title': query['title']} return output if (query['artist'] != '') and (query['title'] != ''): data = '##### parameters { #####\n' data += 'DL_PLUS=1\n' data += '# this tags \"%s\" as ITEM.ARTIST\n' % (query['artist']) data += 'DL_PLUS_TAG=4 0 %s\n' % ( len(query['artist']) - 1 ) data += '# this tags \"%s\" as ITEM.TITLE\n' % (query['title']) data += 'DL_PLUS_TAG=1 %s %s\n' % ( len(query['artist']) + 3 , len(query['title']) - 1 ) data += '##### parameters } #####\n' data += '%s - %s\n' % (query['artist'], query['title']) try: with codecs.open(odr['padenc']['dls_file'], 'w', 'utf-8') as outfile: outfile.write(data) except Exception as e: output['status'] = -210 output['statusText'] = 'Fail to write dls data' return output else: output['status'] = 0 output['statusText'] = 'Ok' output['dlplus'] = {'artist': query['artist'], 'title': query['title']} return output else: output['status'] = -210 output['statusText'] = 'artist or title are blank' return output # no needed parameters availablefor odr in self.conf.config['odr']: else: output['status'] = -209 output['statusText'] = 'Error, you need to use dls or artist + title parameters' return output # DLS (odr-padenc process) is disable else: output['status'] = -208 output['statusText'] = 'PAD Encoder is disable' return output # padenc is not present in configuration / encoder is not configured. else: output['status'] = -211 output['statusText'] = 'Encoder is not configured' return output output = [] if 'uniq_id' in query: for odr in self.conf.config['odr']: if odr['uniq_id'] == query['uniq_id']: output.append( process_query(odr, query) ) else: for odr in self.conf.config['odr']: output.append( process_query(odr, query) ) return build_response(output)
def dispatch(self, path_info): request = cherrypy.request request.config = {} request.error_page["default"] = Pkg5Dispatch.default_error_page toks = path_info.lstrip("/").split("/") params = request.params if not params: try: # Starting in CherryPy 3.2, it seems that # query_string doesn't pass into request.params, # so try harder here. from cherrypy.lib.httputil import parse_query_string params = parse_query_string(request.query_string) request.params.update(params) except ImportError: pass file_type = toks[-1].split(".")[-1] try: if "/search/1/" in path_info: cherrypy.response.stream = True cherrypy.response.body = self.app.search_1(*toks, **params) elif "/search/0/" in path_info: cherrypy.response.stream = True cherrypy.response.body = self.app.search_0(*toks) elif "/manifest/0/" in path_info: cherrypy.response.body = self.app.manifest(*toks) elif "/info/0/" in path_info: cherrypy.response.body = self.app.info(*toks, **params) elif "/p5i/0/" in path_info: cherrypy.response.body = self.app.p5i(*toks, **params) elif "/admin/0" in path_info: cherrypy.response.body = self.app.admin(*toks, **params) elif "/depot-keepalive" in path_info: return b"" elif "/depot-wait-refresh" in path_info: self.app.wait_refresh(*toks, **params) return b"" elif path_info == "/" or path_info == "/repos.shtml": cherrypy.response.body = self.app.repo_index(*toks, **params) elif file_type in ["css", "shtml", "png"]: cherrypy.response.body = self.app.default(*toks, **params) else: cherrypy.response.body = self.app.default(*toks, **params) except Exception as e: if isinstance(e, cherrypy.HTTPRedirect): raise elif isinstance(e, cherrypy.HTTPError): raise elif isinstance(e, AdminOpsDisabledException): raise cherrypy.HTTPError( e.http_status, "This operation has been disabled by the " "server administrator.") elif isinstance(e, AdminOpNotSupportedException): raise cherrypy.HTTPError(e.http_status, "This operation is not supported.") elif isinstance(e, IndexOpDisabledException): raise cherrypy.HTTPError( e.http_status, "This operation has been disabled by the " "server administrator.") else: # we leave this as a 500 for now. It will be # converted and logged by our error handler # before the client sees it. raise cherrypy.HTTPError( status=http_client.INTERNAL_SERVER_ERROR, message="".join(traceback.format_exc(e)))