def start(self, name, *args, **kwargs): kwargs = kwargs or request.json LOG.info("Received rebuild job [%s] from: %s" % ( name, request.client_addr)) if not getattr(request, "user", None): key = kwargs.pop('key', None) if not key: return O.error(msg="Missing auth key") api_key = get_api_key(key) if not api_key: return abort(401) request.user = cached_user(api_key.user) depl = request.db.query(Deployment).filter( Deployment.name == name).first() if not depl: return O.error(msg="Deployment '%s' not found" % depl) if depl.status not in ['Pending', 'Stopped']: return O.error(msg="Deployment must be Pending or Stopped " "to be Started.") request.db.commit() task_ids = _execute(depl, env=kwargs) return O.success(msg="Started", task_ids=task_ids)
def start(self, name, *args, **kwargs): kwargs = kwargs or request.json LOG.info("Received rebuild job [%s] from: %s" % (name, request.client_addr)) if not getattr(request, "user", None): key = kwargs.pop('key', None) if not key: return O.error(msg="Missing auth key") api_key = get_api_key(key) if not api_key: return abort(401) request.user = cached_user(api_key.user) depl = request.db.query(Deployment).filter( Deployment.name == name).first() if not depl: return O.error(msg="Deployment '%s' not found" % depl) if depl.status not in ['Pending', 'Stopped']: return O.error(msg="Deployment must be Pending or Stopped " "to be Started.") request.db.commit() task_ids = _execute(depl, env=kwargs) return O.success(msg="Started", task_ids=task_ids)
def update_data(self, *args, **kwargs): kwargs = kwargs or request.json if "cc" in kwargs: cc_data = kwargs['cc'] CS = request.braintree.CustomerSearch customers = [ c for c in request.braintree.Customer.search( CS.company == request.user.org).items ] if not customers: return O.error("Customer not found") customer = customers[0] expire_m, expire_y = cc_data['expire_date'].split("/") token = "" if customer.credit_cards: # Update cc = customer.credit_cards[0] token = cc.token result = request.braintree.PaymentMethod.update( token, { "number": cc_data['number'], "cardholder_name": cc_data['cardholder_name'], "expiration_month": expire_m, "expiration_year": expire_y, "cvv": cc_data['cvv'], }) else: # Create result = request.braintree.CreditCard.create({ "customer_id": customer.id, "number": cc_data["number"], "expiration_month": expire_m, "expiration_year": expire_y, "cardholder_name": cc_data['cardholder_name'], "cvv": cc_data['cvv'] }) if not result.is_success: errors = filter(None, result.errors.deep_errors) if errors: return O.error(msg=errors[0].message) return O.success(msg="Account updated") return O.error(msg="No data provided for update")
def restart(self, name, *args, **kwargs): name = name or kwargs['name'] depl = Deployment.my(request).filter(Deployment.name == name).first() if not depl: return O.error(msg="Cannot find deployment '%s'" % name) content = json.loads(depl.content) _validate(content) request.db.commit() depl.status = "Starting" task_ids = _execute(depl, **kwargs) return O.success(status="ok", task_ids=task_ids)
def wrapper(*args, **kwargs): # Call function try: ret = f(*args, **kwargs) if ret and ret.get('error'): return ret _m = None _id = None if hasattr(request, '_model_id'): _id = request._model_id else: if method == 'create': for m in request.db.new: if isinstance(m, model): _m = m break elif method == 'delete': for m in request.db.deleted: if isinstance(m, model): _id = m.id break elif method in ('update', 'modify'): for m in request.db.dirty: if isinstance(m, model): _id = m.id break request.db.commit() if method == 'create' and _m: _id = _m.id if _id: ev_action = "%s:%s" % (model.__tablename__, method) response.fire_up_event = ev_action response.fire_up_id = _id if not ret: return O.success(status='ok') return ret except KeyError, kerr: request.db.rollback() if key_error and callable(key_error): return key_error(kerr) return O.error(msg="Field not present: %s" % kerr, field=str(kerr))
def start(self, name, *args, **kwargs): name = name or kwargs['name'] depl = Deployment.my(request).filter(Deployment.name == name).first() if not depl: return O.error(msg="Cannot find deployment '%s'" % name) if depl.status not in ['Pending', 'Stopped']: return O.error(msg="Deployment must be Pending or Stopped " "to be Started.") content = json.loads(depl.content) _validate(content) request.db.commit() depl.status = "Starting" task_ids = _execute(depl, **kwargs) return O.success(status="ok", task_ids=task_ids)
def update_data(self, *args, **kwargs): kwargs = kwargs or request.json if "cc" in kwargs: cc_data = kwargs['cc'] CS = request.braintree.CustomerSearch customers = [c for c in request.braintree.Customer.search( CS.company == request.user.org).items] if not customers: return O.error("Customer not found") customer = customers[0] expire_m, expire_y = cc_data['expire_date'].split("/") token = "" if customer.credit_cards: # Update cc = customer.credit_cards[0] token = cc.token result = request.braintree.PaymentMethod.update(token, { "number": cc_data['number'], "cardholder_name": cc_data['cardholder_name'], "expiration_month": expire_m, "expiration_year": expire_y, "cvv": cc_data['cvv'], }) else: # Create result = request.braintree.CreditCard.create({ "customer_id": customer.id, "number": cc_data["number"], "expiration_month": expire_m, "expiration_year": expire_y, "cardholder_name": cc_data['cardholder_name'], "cvv": cc_data['cvv'] }) if not result.is_success: errors = filter(None, result.errors.deep_errors) if errors: return O.error(msg=errors[0].message) return O.success(msg="Account updated") return O.error(msg="No data provided for update")
def script(self, *args, **kwargs): full_path = "/" + "/".join(args) LOG.info("Received execute script request [%s] from: %s" % ( full_path, request.client_addr)) key = kwargs.pop('key', None) if not getattr(request, "user", None): if not key: return O.error(msg="Missing auth key") api_key = get_api_key(key) if not api_key: return abort(401) user_id = api_key.user_id request.user = cached_user(api_key.user) else: user_id = request.user.id user = request.db.query(User).filter(User.id == user_id).one() targets = kwargs.pop("targets") if not targets: return O.error(msg="Targets is a mandatory field") targets = [t.strip() for t in SPLITTER.split(targets) if t.strip()] env = kwargs.pop('env', {}) env.update(flatten_params(request.params)) dep_data = dict() dep_data['steps'] = [] step = dict(target=targets, content=dict(path=full_path)) dep_data['steps'].append(step) depl = Deployment(name="Execute: %s" % full_path, content=json.dumps(dep_data), status='Pending', owner=user) task_ids = _execute(depl, env=env, dont_save=True, **kwargs) if task_ids: return O.success(status="ok", **task_ids) else: return O.error(msg="Cannot execute script")
def register(self, node=None, **kwargs): return O.error(msg="Not implemented") node = node or kwargs['node'] org = request.db.query(Org).filter(Org.name == request.user.org).one() n = Node(name=node, org=org) r = Reservation(node=n, username=kwargs['username'], password=kwargs['password'], ssh_pubkey=kwargs['ssh_pubkey'], disable_pass=kwargs.get('disable_pass') in ['1', 'true', 'True']) request.db.add(n) request.db.add(r) request.db.commit() t = threading.Thread( target=node_registration, args=n.serialize(rel=['reservations', 'reservations'])) t.start() return O.success(msg="Node registration started")
def script(self, *args, **kwargs): full_path = "/" + "/".join(args) LOG.info("Received execute script request [%s] from: %s" % (full_path, request.client_addr)) key = kwargs.pop('key', None) if not getattr(request, "user", None): if not key: return O.error(msg="Missing auth key") api_key = get_api_key(key) if not api_key: return abort(401) user_id = api_key.user_id request.user = cached_user(api_key.user) else: user_id = request.user.id user = request.db.query(User).filter(User.id == user_id).one() targets = kwargs.pop("targets") if not targets: return O.error(msg="Targets is a mandatory field") targets = [t.strip() for t in SPLITTER.split(targets) if t.strip()] env = kwargs.pop('env', {}) env.update(flatten_params(request.params)) dep_data = dict() dep_data['steps'] = [] step = dict(target=targets, content=dict(path=full_path)) dep_data['steps'].append(step) depl = Deployment(name="Execute: %s" % full_path, content=json.dumps(dep_data), status='Pending', owner=user) task_ids = _execute(depl, env=env, dont_save=True, **kwargs) if task_ids: return O.success(status="ok", **task_ids) else: return O.error(msg="Cannot execute script")
def rebuild(self, name, **kwargs): name = name or kwargs['name'] depl = Deployment.my(request).filter(Deployment.name == name).first() if not depl: return O.error(msg="Cannot find deployment '%s'" % name) content = kwargs.pop('content') if not isinstance(content, dict): content = json.loads(content) depl.status = 'Rebuilding' if "new_name" in kwargs: depl.name = kwargs["new_name"] request.db.commit() _validate(content) depl.content = json.dumps(content) task_ids = _execute(depl, **kwargs) return O.success(status="ok", task_ids=task_ids)
def reset(self, email=None, **kwargs): if request.method != "POST": return O.none() if not email: email = request.json.get("email") user = request.db.query(User).filter(User.email == email).first() if not user: return O.success(msg="Message sent to the specified email.") token = User.create_token(request, user.id, scope='RECOVER') ACTION_URL = "%s/index.html#page/recovery/%s" % ( conf.DASH_SERVER_URL.rstrip('/'), token.value) html = render('email/recover.html', dict(ACTION_URL=ACTION_URL)) requests.post("https://api.mailgun.net/v2/cloudrunner.io/messages", auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"), data={ "from": "CloudRunner.IO Team <*****@*****.**>", "to": [email], "subject": "[CloudRunner.IO] Recover lost password", "html": html })
def reset(self, email=None, **kwargs): if request.method != "POST": return O.none() if not email: email = request.json.get("email") user = request.db.query(User).filter( User.email == email).first() if not user: return O.success(msg="Message sent to the specified email.") token = User.create_token(request, user.id, scope='RECOVER') ACTION_URL = "%s/index.html#page/recovery/%s" % ( conf.DASH_SERVER_URL.rstrip('/'), token.value) html = render('email/recover.html', dict(ACTION_URL=ACTION_URL)) requests.post( "https://api.mailgun.net/v2/cloudrunner.io/messages", auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"), data={"from": "CloudRunner.IO Team <*****@*****.**>", "to": [email], "subject": "[CloudRunner.IO] Recover lost password", "html": html})
def logout(self): """ .. http:get:: /auth/logout/ Log out the user """ user = (request.headers.get('Cr-User') or request.headers.get('X-Cr-User')) token = (request.headers.get('Cr-Token') or request.headers.get('X-Cr-Token')) if user and token: try: tokens = request.db.query(Token).join(User).filter( User.username == user, Token.value == token).all() map(request.db.delete, tokens) request.db.commit() return O.success(status="ok") except Exception, ex: LOG.error(ex) return O.error(msg="Cannot logout") finally:
def logout(self): """ .. http:get:: /auth/logout/ Log out the user """ user = (request.headers.get('Cr-User') or request.headers.get('X-Cr-User')) token = (request.headers.get('Cr-Token') or request.headers.get('X-Cr-Token')) if user and token: try: tokens = request.db.query(Token).join(User).filter( User.username == user, Token.value == token).all() map(request.db.delete, tokens) request.db.commit() return O.success(status="ok") except Exception, ex: LOG.error(ex) return O.error(msg="Cannot logout") finally:
html = render('email/activate.html', dict(ACTION_URL=ACTION_URL, KEY=key.value)) requests.post("https://api.mailgun.net/v2/cloudrunner.io/messages", auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"), data={ "from": "CloudRunner.IO Team <*****@*****.**>", "to": [email], "bcc": [bcc], "subject": "[CloudRunner.IO] Complete your registration", "html": html }) return O.success(msg="Check your email how to activate your account") @expose('json') @wrap_command(User, method='activate', model_name='Account') def activate(self, **kwargs): if request.method != "POST": return O.none() if not kwargs: kwargs = request.json username = kwargs['user'] key = kwargs['code'] user = request.db.query(User).join(Org, ApiKey).filter( User.username == username, ApiKey.value == key, ApiKey.enabled == True).first() # noqa if not user: user = request.db.query(User).join(Org, ApiKey).filter(
bcc = [] if hasattr(conf, "registration_bcc") and conf.registration_bcc: bcc = [conf.registration_bcc] html = render('email/activate.html', dict(ACTION_URL=ACTION_URL, KEY=key.value)) requests.post( "https://api.mailgun.net/v2/cloudrunner.io/messages", auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"), data={"from": "CloudRunner.IO Team <*****@*****.**>", "to": [email], "bcc": [bcc], "subject": "[CloudRunner.IO] Complete your registration", "html": html}) return O.success(msg="Check your email how to activate your account") @expose('json') @wrap_command(User, method='activate', model_name='Account') def activate(self, **kwargs): if request.method != "POST": return O.none() if not kwargs: kwargs = request.json username = kwargs['user'] key = kwargs['code'] user = request.db.query(User).join(Org, ApiKey).filter( User.username == username, ApiKey.value == key, ApiKey.enabled == True).first() # noqa if not user: user = request.db.query(User).join(Org, ApiKey).filter(