def __get_access_token(self): """ Request access_token from ChinaTelecom :rtype boolean :return True if get access_token successfully. False if fails to get. """ data = { "grant_type": "client_credentials", "app_id": self.app_id, "app_secret": self.app_secret } try: req = urllib2.Request(self.url_access_token) post_data = urllib.urlencode(data) req.add_data(post_data) response = urllib2.urlopen(req) response_json = json.loads(response.read()) if response_json[ "res_code"] == CHINATELECOM_ACCESS_TOKEN_STATUSCODE.SUCCESS: # access_token's expiration time is 30 days. self.access_token = response_json["access_token"] self.access_token_expiration_time = get_now() + timedelta( days=30) return True log.error("ChinaTelecom-SMS Error: request access_token fails") return False except Exception as e: log.error(e) return False
def create_or_update_register(self, **args): try: register = db_adapter.find_first_object( Register, Register.email == args['email'], Register.hackathon_id == g.hackathon_id) if register is None: #create a register log.debug("create a new register") db_adapter.add_object_kwargs( Register, register_name=args['name'], email=args['email'], create_time=datetime.utcnow(), description=args['description'], enabled=1, # 0: disabled 1:enabled jstrom_api='', jstrom_mgmt_portal='', hackathon_id=g.hackathon_id) else: #update a aready existe register log.debug("update a new register") db_adapter.update_object( register, register_name=args['name'], email=args['email'], create_time=datetime.utcnow(), description=args['description'], enabled=args['enabled'], # 0: disabled 1:enabled strom_api='', jstrom_mgmt_portal='', hackathon_id=g.hackathon_id) except Exception: log.error("create or update register faild") return {"error": "INTERNAL SERVER ERROR"}, 500
def db_logout(self, user): try: self.db.update_object(user, online=0) return "OK" except Exception as e: log.error(e) return "log out failed"
def __get_access_token(self): """ Request access_token from ChinaTelecom :rtype boolean :return True if get access_token successfully. False if fails to get. """ data = {"grant_type": "client_credentials", "app_id": self.app_id, "app_secret": self.app_secret} try: req = urllib2.Request(self.url_access_token) post_data = urllib.urlencode(data) req.add_data(post_data) response = urllib2.urlopen(req) response_json = json.loads(response.read()) if response_json["res_code"] == CHINATELECOM_ACCESS_TOKEN_STATUSCODE.SUCCESS: # access_token's expiration time is 30 days. self.access_token = response_json["access_token"] self.access_token_expiration_time = get_now() + timedelta(days=30) return True log.error("ChinaTelecom-SMS Error: request access_token fails") return False except Exception as e: log.error(e) return False
def handle_error(self, e): log.error(e) if isinstance(e, HTTPException): message = e.description if hasattr(e, "data") and "message" in e.data: message = e.data["message"] if e.code == 400: return self.make_response(bad_request(message), 200) if e.code == 401: return self.make_response(unauthorized(message), 200) if e.code == 403: return self.make_response(forbidden(message), 200) if e.code == 404: return self.make_response(not_found(message), 200) if e.code == 409: return self.make_response(conflict(message), 200) if e.code == 412: return self.make_response(precondition_failed(message), 200) if e.code == 415: return self.make_response(unsupported_mediatype(message), 200) if e.code == 500: return self.make_response(internal_server_error(message), 200) # if exception cannot be handled, return error 500 return self.make_response(internal_server_error(e.message), 200)
def create_or_update_register(self, **args): try: register = db_adapter.find_first_object(Register,Register.email==args['email'],Register.hackathon_id==g.hackathon_id) if register is None: #create a register log.debug("create a new register") db_adapter.add_object_kwargs(Register, register_name=args['name'], email=args['email'], create_time=datetime.utcnow(), description=args['description'], enabled=1, # 0: disabled 1:enabled jstrom_api='', jstrom_mgmt_portal='', hackathon_id=g.hackathon_id) else: #update a aready existe register log.debug("update a new register") db_adapter.update_object(register, register_name=args['name'], email=args['email'], create_time=datetime.utcnow(), description=args['description'], enabled=args['enabled'], # 0: disabled 1:enabled strom_api='', jstrom_mgmt_portal='', hackathon_id=g.hackathon_id) except Exception: log.error("create or update register faild") return {"error": "INTERNAL SERVER ERROR"}, 500
def delete(self, name, docker_host): try: containers_url = self.get_vm_url(docker_host) + "/containers/%s?force=1" % name req = requests.delete(containers_url) log.debug(req.content) except: log.error("container %s fail to stop" % name) raise
def start(self, docker_host, container_id, start_config={}): try: url = self.get_vm_url(docker_host) + "/containers/%s/start" % container_id req = requests.post(url, data=json.dumps(start_config), headers=default_http_headers) log.debug(req.content) except: log.error("container %s fail to start" % container_id) raise
def containers_info(self, docker_host): try: containers_url = self.get_vm_url(docker_host) + "/containers/json" req = requests.get(containers_url) log.debug(req.content) return convert(json.loads(req.content)) except: log.error("cannot get containers' info") raise
def ping(self, docker_host): try: ping_url = self.get_vm_url(docker_host) + '/_ping' req = requests.get(ping_url) log.debug(req.content) return req.status_code == 200 and req.content == 'OK' except Exception as e: log.error(e) return False
def stop(self, name, docker_host): if self.get_container(name, docker_host) is not None: try: containers_url = self.get_vm_url(docker_host) + "/containers/%s/stop" % name req = requests.post(containers_url) log.debug(req.content) except: log.error("container %s fail to stop" % name) raise
def validate_request(self): if HTTP_HEADER.TOKEN not in request.headers: log.error('invailed request from checking TOKEN') return False admin = self.__validate_token(request.headers['token']) if admin is None: return False g.admin = admin return True
def delete_register(self, **args): if args['id'] is None: return {"error": "Bad request"}, 400 try: register = db_adapter.find_first_object(Register, Register.id == args['id']) if register is None: return {"message": "already removed"}, 200 db_adapter.delete_object(register) except Exception: log.error("delete register faild") return {"error": "INTERNAL SERVER ERROR"}, 500
def login(self, args): access_token = args.get('access_token') uid = args.get('uid') # get user info # https://api.weibo.com/2/users/show.json?access_token=2.005RDjXC0rYD8d39ca83156aLZWgZE&uid=1404376560 user_info_resp = get_remote(get_config('login.weibo.user_info_url') + access_token + "&uid=" + uid) user_info = json.loads(user_info_resp) log.debug("get user base info from Weibo:" + user_info_resp) # {"id":2330622122,"idstr":"2330622122","class":1,"screen_name":"test name","name":"test name", # "province":"31","city":"10","location":"shanghai yangpu","description":"","url":"", # "profile_image_url":"http://tp3.sinaimg.cn/2330622122/50/5629035320/1", # "profile_url":"u/2330622122","domain":"","weihao":"","gender":"m","followers_count":34, # "friends_count":42,"pagefriends_count":0,"statuses_count":0,"favourites_count":1, # "created_at":"Mon Aug 22 17:58:15 +0800 2011","following":false,"allow_all_act_msg":false, # "geo_enabled":true,"verified":false,"verified_type":-1,"remark":"","ptype":0,"allow_all_comment":true, # "avatar_large":"http://tp3.sinaimg.cn/2330622122/180/5629035320/1","avatar_hd":"http://tp3.sinaimg.cn/2330622122/180/5629035320/1", # "verified_reason":"","verified_trade":"","verified_reason_url":"","verified_source":"","verified_source_url":"", # "follow_me":false,"online_status":0,"bi_followers_count":8,"lang":"zh-cn","star":0,"mbtype":0,"mbrank":0, # "block_word":0,"block_app":0,"credit_score":80,"urank":6} openid = user_info['id'] name = user_info['name'] nickname = user_info['screen_name'] avatar_url = user_info['avatar_hd'] # get user email email_info = [] try: email_info_resp = get_remote(get_config('login.weibo.email_info_url') + access_token) log.debug("get email from github:" + email_info_resp) email_info_resp_json = json.loads(email_info_resp) email = email_info_resp_json['email'] email_info = [ {'name': name, 'email': email, 'id': openid, 'verified': 1, 'primary': 1, 'nickname': nickname, 'avatar_url': avatar_url}] except Exception as e: log.debug("fail to get user email from weibo") log.error(e) user_with_token = user_manager.db_login(openid, name=name, nickname=nickname, access_token=access_token, email_info=email_info, avatar_url=avatar_url) user = user_with_token["user"] log.info("weibo user login successfully:" + repr(user)) hackathon_name = args.get('hackathon_name') detail = user_manager.get_user_detail_info(user, hackathon_name=hackathon_name) detail["token"] = user_with_token["token"].token log.debug("weibo user login successfully: %r" % detail) return detail
def create(self, docker_host, container_config, container_name): containers_url = self.get_vm_url(docker_host) + "/containers/create?name=%s" % container_name try: req = requests.post(containers_url, data=json.dumps(container_config), headers=default_http_headers) log.debug(req.content) container = json.loads(req.content) except Exception as err: log.error(err) raise if container is None: raise AssertionError("container is none") return container
def __get_available_host_port(self, port_bindings, port): host_port = port + 10000 while host_port in port_bindings: host_port += 1 if host_port >= 65535: log.error("port used up on this host server") raise Exception("no port available") log.debug("host_port is %d " % host_port) return host_port
def remove_job(self, job_id): """Remove job from APScheduler job store :type job_id: str | unicode :param job_id: the id of job """ if self.__apscheduler: try: self.__apscheduler.remove_job(job_id, self.jobstore) except JobLookupError: log.debug("remove job failed because job %s not found" % job_id) except Exception as e: log.error(e)
def load_template(url): """Load hackathon template from file into a dict :type url: str|unicode :param url: the absolute path of the template. :return dict indicates a hackathon template :rtype dict """ try: template = json.load(file(url)) except Exception as e: log.error(e) return None return template
def send_emails(self, sender, receivers, subject, content, cc=[], bcc=[], attachments=[]): """Send emails notes: No all email-service providers support. if using Gmail, enable "Access for less secure apps" for the sender's account, Examples: xxx.send_emails("James [email protected]", ['*****@*****.**', '*****@*****.**'], 'Subject: Hello', '<b>Hi! Here is the content of email</b>', ['*****@*****.**', '*****@*****.**'], ['*****@*****.**', '*****@*****.**'], ['C:/apache-maven-3.3.3-bin.zip']) :type sender: str|unicode :param sender: the nickname and email address of sender. Example:"James [email protected]" :type receivers: list :param receivers: receivers' emails address. Example:['*****@*****.**', '*****@*****.**'] :type subject: str|unicode :param subject: subject of email's header. Example:'Hello' :type content: str|unicode :param content: content of the email. Example:'<b>Hi!</b>' :type cc: list :param cc: CarbonCopy. Example:['*****@*****.**', '*****@*****.**'] :type bcc: list :param bcc: BlindCarbonCopy. Example:['*****@*****.**', '*****@*****.**'] :type attachments: list :param attachments: Example:['C:/Users/Administrator/Downloads/apache-maven-3.3.3-bin.zip'] :rtype boolean :return True if send emails successfully. False if fails to send. """ if not self.available: log.error(self.error_message) return False e = email(sender=sender, receivers=receivers, cc=cc, bcc=bcc, subject=subject, content=content, attachments=attachments) try: response = self.postman.send(e) if response.status_code == EMAIL_SMTP_STATUSCODE.SUCCESS: return True log.error("Send emails fail: " + response.message) return False except Exception as e: log.error(e) return False
def send_sms(self, receiver, template_id, content): """ Send SMS through ChinaTelecom Plateform :type receiver: str|unicode :param receiver: the telephone number. Example: "18217511111" :type template_id: constant integer :param template_id: the id of sms-template. Example: SMS_CHINATELECOM_TEMPLATE.DEFAULT :type content: dict :param content: the content of SMS to replace slots in sms-template. Example: {"param1":"1849"} :rtype boolean :return True if SMS sends successfully. False if fails to send. """ if not self.available: log.error(self.error_message) return False elif not self.access_token_expiration_time or self.access_token_expiration_time < get_now( ): if not self.__get_access_token(): return False try: # timestamp should be Beijing local time timestamp = (get_now() + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") data = { "acceptor_tel": receiver, "template_id": template_id, "template_param": str(content), "app_id": self.app_id, "access_token": self.access_token, "timestamp": timestamp } req = urllib2.Request(self.url) post_data = urllib.urlencode(data) req.add_data(post_data) response = urllib2.urlopen(req) response_json = json.loads(response.read()) if response_json[ "res_code"] == SMS_CHINATELECOM_STATUSCODE.SUCCESS: return True log.error("Send SMS(ChinaTelecom) fails: " + str(response_json)) return False except Exception as e: log.error(e) return False
def send_voice_verify(self, receiver, content): """ Send voice_verify through RongLian_YunTongXun service Example: XXX.send_voice_verify_by_RongLian("18217511111", "1849") :type receiver: str|unicode :param receiver: the telephone number. Example:"18217511111" :type content: str|unicode :param content: the content of voice-verify. It should contain 4 words or numbers. Example:"1849" :rtype boolean :return True if voice-verify sends successfully. False if fails to send. """ if not self.available: log.error(self.error_message) return False # generate voice_verify request req = self.__generate_voice_verify_request() try: # generate request-body body = { "to": receiver, "verifyCode": content, "playTimes": self.play_times, "displayNum": self.display_number, "respUrl": self.response_url, "lang": self.language, "appId": self.app_id } req.add_data(str(body)) res = urllib2.urlopen(req) data = res.read() res.close() response = json.loads(data) if response[ "statusCode"] == VOICEVERIFY_RONGLIAN_STATUSCODE.SUCCESS: return True log.error("Send VoiceVerify(RongLian) fails: " + str(response)) return False except Exception as e: log.error(e) return False
def send_sms(self, receiver, template_id, content): """ Send SMS through ChinaTelecom Plateform :type receiver: str|unicode :param receiver: the telephone number. Example: "18217511111" :type template_id: constant integer :param template_id: the id of sms-template. Example: SMS_CHINATELECOM_TEMPLATE.DEFAULT :type content: dict :param content: the content of SMS to replace slots in sms-template. Example: {"param1":"1849"} :rtype boolean :return True if SMS sends successfully. False if fails to send. """ if not self.available: log.error(self.error_message) return False elif not self.access_token_expiration_time or self.access_token_expiration_time < get_now(): if not self.__get_access_token(): return False try: # timestamp should be Beijing local time timestamp = (get_now() + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") data = {"acceptor_tel": receiver, "template_id": template_id, "template_param": str(content), "app_id": self.app_id, "access_token": self.access_token, "timestamp": timestamp} req = urllib2.Request(self.url) post_data = urllib.urlencode(data) req.add_data(post_data) response = urllib2.urlopen(req) response_json = json.loads(response.read()) if response_json["res_code"] == SMS_CHINATELECOM_STATUSCODE.SUCCESS: return True log.error("Send SMS(ChinaTelecom) fails: " + str(response_json)) return False except Exception as e: log.error(e) return False
def send_voice_verify(self, receiver, content): """ Send voice_verify through RongLian_YunTongXun service Example: XXX.send_voice_verify_by_RongLian("18217511111", "1849") :type receiver: str|unicode :param receiver: the telephone number. Example:"18217511111" :type content: str|unicode :param content: the content of voice-verify. It should contain 4 words or numbers. Example:"1849" :rtype boolean :return True if voice-verify sends successfully. False if fails to send. """ if not self.available: log.error(self.error_message) return False # generate voice_verify request req = self.__generate_voice_verify_request() try: # generate request-body body = {"to": receiver, "verifyCode": content, "playTimes": self.play_times, "displayNum": self.display_number, "respUrl": self.response_url, "lang": self.language, "appId": self.app_id} req.add_data(str(body)) res = urllib2.urlopen(req) data = res.read() res.close() response = json.loads(data) if response["statusCode"] == VOICEVERIFY_RONGLIAN_STATUSCODE.SUCCESS: return True log.error("Send VoiceVerify(RongLian) fails: " + str(response)) return False except Exception as e: log.error(e) return False
def exception_handler(error): log.error(error) return internal_server_error(error.message)
def precondition_failed_handler(error): log.error(error) return precondition_failed(error.message)
def bad_request_handler(error): log.error(error) return bad_request(error.message)
def run(self, args, docker_host): container_name = args["container_name"] exist = self.get_container(container_name, docker_host) result = { "container_name": container_name } if exist is not None: result["container_id"] = exist["Id"] else: image = args["image"] # ports foramt: [from, to ,from2, to2]. e.g.[9080,8080,3306,3306].Similar with 'mnts' ports = args["docker_ports"] if "docker_ports" in args else [] port_bingings = dict(zip(ports[1::2], ports[::2])) mnts = args["mnt"] if "mnt" in args else [] mnts_f = map( lambda s: s if "%s" not in s or "scm" not in args else s % args["scm"]["local_repo_path"], mnts[::2]) mnts_t = map(lambda s: {"bind": s, "ro": False}, mnts[1::2]) mnt_bindings = dict(zip(mnts_f, mnts_t)) command = args["command"] if "command" in args else None stdin_open = args["stdin_open"] if "stdin_open" in args else False tty = args["tty"] if "tty" in args else False dns = args["dns"] if "dns" in args else None entrypoint = args["entrypoint"] if "entrypoint" in args else None working_dir = args["working_dir"] if "working_dir" in args else None attach_std_in = args["AttachStdin"] if "AttachStdin" in args else False attach_std_out = args["AttachStdout"] if "AttachStdout" in args else False attach_std_err = args["AttachStderr"] if "AttachStderr" in args else False env_variable = args["Env"] if "Env" in args else None # headers = {'content-type': 'application/json'} container_config = {"Image": image, "ExposedPorts": {}} for key in port_bingings: container_config["ExposedPorts"][str(key) + "/tcp"] = {} if mnts_f: for v in mnts_f: container_config["Volumes"] = {} container_config["Volumes"][v] = {} container_config["Env"] = env_variable if command is not None: container_config["Cmd"] = command.split(" ") container_config["OpenStdin"] = stdin_open container_config["Tty"] = tty container_config["Dns"] = dns container_config["Entrypoint"] = entrypoint container_config["WorkingDir"] = working_dir container_config["AttachStdin"] = attach_std_in container_config["AttachStdout"] = attach_std_out container_config["AttachStderr"] = attach_std_err try: container = self.create(docker_host, container_config, container_name) except Exception as e: log.error(e) log.error("container %s fail to create" % container_name) return None # start container # start_config = { "PortBindings":{"22/tcp":["10022"]}}, "Binds":[]} start_config = {"PortBindings": {}} # "Binds" = ["/host/path:/container/path:ro/rw"] if mnt_bindings: start_config["Binds"] = [] for key in mnt_bindings: start_config["Binds"].append(key + ":" + mnt_bindings[key]["bind"] + ":rw") for key in port_bingings: temp = [] config = {"HostPort": str(port_bingings[key])} temp.append(config) start_config["PortBindings"][str(key) + "/tcp"] = temp result["container_id"] = container["Id"] # start container try: self.start(docker_host, container["Id"], start_config) except Exception as e: log.error(e) log.error("container %s fail to start" % container["Id"]) return None if self.get_container(container_name, docker_host) is None: log.error("container %s has started, but can not find it in containers' info, maybe it exited again." % container_name) return None return result