def schematic_get(sid=None):
    """Return a of Schematic.

    Method is accessible with GET /v1/schematics/<sid>

    :param sid: ``str`` # schematic ID
    :return json, status: ``tuple``
    """
    if not sid:
        return utils.return_msg(msg='missing information', status=400)

    user_id = utils.auth_mech(rdata=flask.request.headers)
    if not user_id:
        return utils.return_msg(msg='missing information', status=400)

    schematic = db_proc.get_schematic_id(sid=sid, uid=user_id)
    if not schematic:
        return utils.return_msg(msg='no schematic found', status=404)
    else:
        _schematic = utils.pop_ts(temp=schematic.__dict__)
        config = db_proc.get_configmanager(skm=schematic)
        if config:
            _config = utils.pop_ts(temp=config.__dict__)
            _schematic['config_manager'] = _config

        return utils.return_msg(msg=_schematic, status=200)
def schematic_put(sid=None):
    """Update a schematic.

    if a zone is in the put data add the zone to the schematic.
    The addition of a zone on a put will not build the zone automatically.

    Method is accessible with PUT /v1/schematics/<sid>

    :param sid: ``str`` # schematic ID
    :return json, status: ``tuple``
    """

    if not sid:
        return utils.return_msg(msg='missing information', status=400)

    auth = utils.auth_mech(hdata=flask.request.data,
                           rdata=flask.request.headers)
    if not auth:
        return utils.return_msg(msg='Authentication or Data Type Failure',
                                status=401)
    else:
        user_id, payload = auth

    schematic = db_proc.get_schematic_id(sid=sid, uid=user_id)
    if not schematic:
        return utils.return_msg(msg='no schematic found', status=404)

    if not all([user_id, payload]):
        build_response = 'missing information %s %s' % (user_id, payload)
        return utils.return_msg(msg=build_response, status=400)

    config_type = payload.get('config_type')
    try:
        sess = DB.session
        if config_type is not None:
            if _config_check(config_type).validate_plugin() is False:
                raise tribble.DeadOnArival('Plugin "%s" was not found.' %
                                           config_type)

        con = db_proc.get_configmanager(skm=schematic)
        db_proc.put_schematic_id(session=sess, skm=schematic, put=payload)
        db_proc.put_configmanager(session=sess, con=con, put=payload)
        if 'zones' in payload:
            _zone_builder(session=sess,
                          schematic=schematic,
                          con=con,
                          payload=payload)
    except tribble.DeadOnArival as exp:
        return utils.return_msg(msg='%s' % exp, status=405)
    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='Unexpected Error', status=500)
    else:
        LOG.debug(payload)
        db_proc.commit_session(session=sess)
        return utils.return_msg(msg='Updates Recieved', status=201)
def instance_delete(sid=None, zid=None, iid=None):
    """Delete an Instance from a Zone.

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :param iid: ``str`` # Instance ID
    :return json, status: ``tuple``
    """

    if not all([sid, zid, iid]):
        check_all = [check for check in sid, zid, iid if not check]
        return utils.return_msg(
            msg='missing Information %s' % check_all, status=400
        )

    user_id = utils.auth_mech(rdata=flask.request.headers)
    if not user_id:
        return utils.return_msg(msg='missing information', status=400)

    schematic = db_proc.get_schematic_id(sid=sid, uid=user_id)
    if not schematic:
        return utils.return_msg(msg='no schematic found', status=404)

    zone = db_proc.get_zones_by_id(skm=schematic, zid=zid)
    if not zone:
        return utils.return_msg(msg='No Zone Found', status=404)
    elif zone.zone_state == 'BUILDING':
        build_response = ("Instance Delete can not be performed because Zone"
                          " %s has a Pending Status" % zone.id)
        return utils.return_msg(msg=build_response, status=200)

    instances = db_proc.get_instances(zon=zone)
    instance_id = [
        instance.instance_id for instance in instances
        if instance.instance_id == iid
    ]
    if not instance_id:
        return utils.return_msg(msg='No Instance Found', status=404)

    try:
        config = db_proc.get_configmanager(skm=schematic)
        cell = utils.build_cell(
            job='instance_delete',
            schematic=schematic,
            zone=zone,
            config=config
        )
        cell['uuids'] = instance_id

    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='Unexpected Error', status=500)
    else:
        rpc.default_publisher(message=cell)
        return utils.return_msg(msg='Deletes Recieved', status=203)
Beispiel #4
0
def zone_post(sid=None):
    """Post a Zone.

    Method is accessible with POST /v1/schematics/<sid>/zones

    :param sid: ``str`` # schematic ID
    :return json, status: ``tuple``
    """
    parsed_data = utils.zone_data_handler(sid=sid, check_for_zone=True)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, payload, user_id = parsed_data
        LOG.debug('%s %s %s %s', _success, schematic, payload, user_id)

    config = db_proc.get_configmanager(skm=schematic)

    try:
        sess = DB.session
        for _zn in payload['zones']:
            ssh_user = _zn.get('ssh_user')
            pub = _zn.get('ssh_key_pub')
            pri = _zn.get('ssh_key_pri')
            key_name = _zn.get('key_name')
            ssh_key = db_proc.post_instanceskeys(
                pub=pub,
                pri=pri,
                sshu=ssh_user,
                key_name=key_name
            )
            db_proc.add_item(session=sess, item=ssh_key)

            zone = db_proc.post_zones(
                skm=schematic,
                zon=_zn,
                ssh=ssh_key
            )
            db_proc.add_item(session=sess, item=zone)

            packet = utils.build_cell(
                job='build',
                schematic=schematic,
                zone=zone,
                sshkey=ssh_key,
                config=config
            )
            rpc.default_publisher(message=packet)

    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='Unexpected Error', status=500)
    else:
        db_proc.commit_session(session=sess)
        msg = 'Application requests have been recieved for Schematic %s' % sid
        return utils.return_msg(msg=msg, status=200)
def instance_delete(sid=None, zid=None, iid=None):
    """Delete an Instance from a Zone.

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :param iid: ``str`` # Instance ID
    :return json, status: ``tuple``
    """

    if not all([sid, zid, iid]):
        check_all = [check for check in sid, zid, iid if not check]
        return utils.return_msg(msg='missing Information %s' % check_all,
                                status=400)

    user_id = utils.auth_mech(rdata=flask.request.headers)
    if not user_id:
        return utils.return_msg(msg='missing information', status=400)

    schematic = db_proc.get_schematic_id(sid=sid, uid=user_id)
    if not schematic:
        return utils.return_msg(msg='no schematic found', status=404)

    zone = db_proc.get_zones_by_id(skm=schematic, zid=zid)
    if not zone:
        return utils.return_msg(msg='No Zone Found', status=404)
    elif zone.zone_state == 'BUILDING':
        build_response = ("Instance Delete can not be performed because Zone"
                          " %s has a Pending Status" % zone.id)
        return utils.return_msg(msg=build_response, status=200)

    instances = db_proc.get_instances(zon=zone)
    instance_id = [
        instance.instance_id for instance in instances
        if instance.instance_id == iid
    ]
    if not instance_id:
        return utils.return_msg(msg='No Instance Found', status=404)

    try:
        config = db_proc.get_configmanager(skm=schematic)
        cell = utils.build_cell(job='instance_delete',
                                schematic=schematic,
                                zone=zone,
                                config=config)
        cell['uuids'] = instance_id

    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='Unexpected Error', status=500)
    else:
        rpc.default_publisher(message=cell)
        return utils.return_msg(msg='Deletes Recieved', status=203)
Beispiel #6
0
def build_schematic_list(schematic):
    """Return dict from a built schematic.

    The built schematic will container Zone and Config Manager information.

    :param schematic: ``dict``
    :return dict_schematic: ``dict``
    """

    dict_schematic = pop_ts(temp=schematic.__dict__)
    zones = db_proc.get_zones(skm=schematic)
    dict_schematic['num_zones'] = len(zones)
    config_manager = db_proc.get_configmanager(skm=schematic)
    dict_schematic['config_manager'] = pop_ts(temp=config_manager.__dict__)
    return dict_schematic
Beispiel #7
0
def build_schematic_list(schematic):
    """Return dict from a built schematic.

    The built schematic will container Zone and Config Manager information.

    :param schematic: ``dict``
    :return dict_schematic: ``dict``
    """

    dict_schematic = pop_ts(temp=schematic.__dict__)
    zones = db_proc.get_zones(skm=schematic)
    dict_schematic['num_zones'] = len(zones)
    config_manager = db_proc.get_configmanager(skm=schematic)
    dict_schematic['config_manager'] = pop_ts(temp=config_manager.__dict__)
    return dict_schematic
Beispiel #8
0
def zone_post(sid=None):
    """Post a Zone.

    Method is accessible with POST /v1/schematics/<sid>/zones

    :param sid: ``str`` # schematic ID
    :return json, status: ``tuple``
    """
    parsed_data = utils.zone_data_handler(sid=sid, check_for_zone=True)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, payload, user_id = parsed_data
        LOG.debug('%s %s %s %s', _success, schematic, payload, user_id)

    config = db_proc.get_configmanager(skm=schematic)

    try:
        sess = DB.session
        for _zn in payload['zones']:
            ssh_user = _zn.get('ssh_user')
            pub = _zn.get('ssh_key_pub')
            pri = _zn.get('ssh_key_pri')
            key_name = _zn.get('key_name')
            ssh_key = db_proc.post_instanceskeys(pub=pub,
                                                 pri=pri,
                                                 sshu=ssh_user,
                                                 key_name=key_name)
            db_proc.add_item(session=sess, item=ssh_key)

            zone = db_proc.post_zones(skm=schematic, zon=_zn, ssh=ssh_key)
            db_proc.add_item(session=sess, item=zone)

            packet = utils.build_cell(job='build',
                                      schematic=schematic,
                                      zone=zone,
                                      sshkey=ssh_key,
                                      config=config)
            rpc.default_publisher(message=packet)

    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='Unexpected Error', status=500)
    else:
        db_proc.commit_session(session=sess)
        msg = 'Application requests have been recieved for Schematic %s' % sid
        return utils.return_msg(msg=msg, status=200)
Beispiel #9
0
    def delete_schematic_resource(self):
        """Perform a Schematic delete.

        This will delete a provided Schematic as well as it's configuration
        management row.
        """
        try:
            sess = DB.session
            config = db_proc.get_configmanager(skm=self.schematic)
            db_proc.delete_item(session=sess, item=self.schematic)
            db_proc.delete_item(session=sess, item=config)
        except AttributeError as exp:
            msg = 'Issues while removing Schematic => %s', exp
            LOG.info(msg)
            self.cell['zone_state'] = 'DELETE FAILED'
            self.cell['zone_msg'] = msg
            self.state_update()
        else:
            db_proc.commit_session(session=sess)
Beispiel #10
0
    def delete_schematic_resource(self):
        """Perform a Schematic delete.

        This will delete a provided Schematic as well as it's configuration
        management row.
        """
        try:
            sess = DB.session
            config = db_proc.get_configmanager(skm=self.schematic)
            db_proc.delete_item(session=sess, item=self.schematic)
            db_proc.delete_item(session=sess, item=config)
        except AttributeError as exp:
            msg = 'Issues while removing Schematic => %s', exp
            LOG.info(msg)
            self.cell['zone_state'] = 'DELETE FAILED'
            self.cell['zone_msg'] = msg
            self.state_update()
        else:
            db_proc.commit_session(session=sess)
Beispiel #11
0
def zone_delete(sid=None, zid=None):
    """Delete a Zone.

    Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid>

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :return json, status: ``tuple``
    """

    parsed_data = utils.zone_basic_handler(sid=sid, zid=zid)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, zone, user_id = parsed_data
        if zone.zone_state == 'BUILDING':
            build_response = (
                'Zone Delete can not be performed because Zone "%s" has a'
                ' Pending Status' % zone.id
            )
            return utils.return_msg(msg=build_response, status=200)

        LOG.debug('%s %s %s %s', _success, schematic, zone, user_id)
        try:
            config = db_proc.get_configmanager(skm=schematic)
            instances = db_proc.get_instances(zon=zone)
            packet = utils.build_cell(
                job='zone_delete',
                schematic=schematic,
                zone=zone,
                config=config
            )
            packet['uuids'] = [instance.instance_id for instance in instances]
            rpc.default_publisher(message=packet)

            sess = DB.session
            zone_status.ZoneState(cell=packet).delete()
        except Exception:
            LOG.error(traceback.format_exc())
            return utils.return_msg(msg='unexpected error', status=500)
        else:
            db_proc.commit_session(session=sess)
            return utils.return_msg(msg='deletes received', status=203)
Beispiel #12
0
def zone_delete(sid=None, zid=None):
    """Delete a Zone.

    Method is accessible with DELETE /v1/schematics/<sid>/zones/<zid>

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :return json, status: ``tuple``
    """

    parsed_data = utils.zone_basic_handler(sid=sid, zid=zid)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, zone, user_id = parsed_data
        if zone.zone_state == 'BUILDING':
            build_response = (
                'Zone Delete can not be performed because Zone "%s" has a'
                ' Pending Status' % zone.id)
            return utils.return_msg(msg=build_response, status=200)

        LOG.debug('%s %s %s %s', _success, schematic, zone, user_id)
        try:
            config = db_proc.get_configmanager(skm=schematic)
            instances = db_proc.get_instances(zon=zone)
            packet = utils.build_cell(job='zone_delete',
                                      schematic=schematic,
                                      zone=zone,
                                      config=config)
            packet['uuids'] = [instance.instance_id for instance in instances]
            rpc.default_publisher(message=packet)

            sess = DB.session
            zone_status.ZoneState(cell=packet).delete()
        except Exception:
            LOG.error(traceback.format_exc())
            return utils.return_msg(msg='unexpected error', status=500)
        else:
            db_proc.commit_session(session=sess)
            return utils.return_msg(msg='deletes received', status=203)
def schematic_delete(sid=None):
    """Delete a Schematic.

    Method is accessible with GET /v1/schematics/<sid>

    :param sid: ``str`` # schematic ID
    :return json, status: ``tuple``
    """

    if not sid:
        return utils.return_msg(msg='missing information', status=400)

    user_id = utils.auth_mech(rdata=flask.request.headers)
    if not user_id:
        return utils.return_msg(msg='missing information', status=400)

    schematic = db_proc.get_schematic_id(sid=sid, uid=user_id)
    if not schematic:
        return utils.return_msg(msg='no schematic found', status=404)

    try:
        config = db_proc.get_configmanager(skm=schematic)
        zones = db_proc.get_zones(skm=schematic)
        if zones:
            zone_ids = [zone.id for zone in zones]
            build_response = ('can not delete the schematic, you have an'
                              ' active zone(s): %s' % zone_ids)
            return utils.return_msg(msg=build_response, status=405)

        sess = DB.session
        db_proc.delete_item(session=sess, item=schematic)
        db_proc.delete_item(session=sess, item=config)
    except Exception:
        LOG.error(traceback.format_exc())
        return utils.return_msg(msg='unexpected error', status=500)
    else:
        db_proc.commit_session(session=sess)
        return utils.return_msg(msg='deletes received', status=203)
Beispiel #14
0
def redeploy_zone(sid=None, zid=None):
    """Redploy a zone.

    This method will interate over an existing zone and ensure that all things
    known in the zone are built and in an active state.

    Method is accessible with POST /v1/schematics/<sid>/zones

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :return json, status: ``tuple``
    """
    parsed_data = utils.zone_basic_handler(sid=sid, zid=zid)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, zone, user_id = parsed_data
        LOG.debug('%s %s %s %s', _success, schematic, zone, user_id)

    config = db_proc.get_configmanager(skm=schematic)
    key = db_proc.get_instanceskeys(zon=zone)
    ints = db_proc.get_instances(zon=zone)
    base_qty = int(zone.quantity)
    numr_qty = len(ints)

    if base_qty > numr_qty:
        difference = base_qty - numr_qty
        packet = utils.build_cell(job='redeploy_build',
                                  schematic=schematic,
                                  zone=zone,
                                  sshkey=key,
                                  config=config)
        packet['quantity'] = difference
        LOG.debug(packet)
        rpc.default_publisher(message=packet)
        msg = 'Building %s Instances for Zone %s' % (difference, zone.id)
        return utils.return_msg(msg=msg, status=200)
    elif base_qty < numr_qty:
        difference = numr_qty - base_qty
        packet = utils.build_cell(job='redeploy_delete',
                                  schematic=schematic,
                                  zone=zone,
                                  sshkey=key,
                                  config=config)
        instances = [ins.instance_id for ins in ints]
        remove_instances = instances[:difference]
        packet['uuids'] = remove_instances
        LOG.debug(packet)
        remove_ids = [
            ins for ins in ints if ins.instance_id in remove_instances
        ]

        try:
            sess = DB.session
            for instance_id in remove_ids:
                db_proc.delete_item(session=sess, item=instance_id)
        except Exception:
            LOG.error(traceback.format_exc())
            return utils.return_msg(msg='Unexpected Error', status=500)
        else:
            rpc.default_publisher(message=packet)
            db_proc.commit_session(session=sess)
            msg = 'Removing %s Instances for Zone %s' % (difference, zone.id)
            return utils.return_msg(msg=msg, status=200)
    else:
        return utils.return_msg(msg='nothing to do', status=200)
Beispiel #15
0
def redeploy_zone(sid=None, zid=None):
    """Redploy a zone.

    This method will interate over an existing zone and ensure that all things
    known in the zone are built and in an active state.

    Method is accessible with POST /v1/schematics/<sid>/zones

    :param sid: ``str`` # schematic ID
    :param zid: ``str`` # Zone ID
    :return json, status: ``tuple``
    """
    parsed_data = utils.zone_basic_handler(sid=sid, zid=zid)
    if parsed_data[0] is False:
        return utils.return_msg(msg=parsed_data[1], status=parsed_data[2])
    else:
        _success, schematic, zone, user_id = parsed_data
        LOG.debug('%s %s %s %s', _success, schematic, zone, user_id)

    config = db_proc.get_configmanager(skm=schematic)
    key = db_proc.get_instanceskeys(zon=zone)
    ints = db_proc.get_instances(zon=zone)
    base_qty = int(zone.quantity)
    numr_qty = len(ints)

    if base_qty > numr_qty:
        difference = base_qty - numr_qty
        packet = utils.build_cell(
            job='redeploy_build',
            schematic=schematic,
            zone=zone,
            sshkey=key,
            config=config
        )
        packet['quantity'] = difference
        LOG.debug(packet)
        rpc.default_publisher(message=packet)
        msg = 'Building %s Instances for Zone %s' % (difference, zone.id)
        return utils.return_msg(msg=msg, status=200)
    elif base_qty < numr_qty:
        difference = numr_qty - base_qty
        packet = utils.build_cell(
            job='redeploy_delete',
            schematic=schematic,
            zone=zone,
            sshkey=key,
            config=config
        )
        instances = [ins.instance_id for ins in ints]
        remove_instances = instances[:difference]
        packet['uuids'] = remove_instances
        LOG.debug(packet)
        remove_ids = [
            ins for ins in ints
            if ins.instance_id in remove_instances
        ]

        try:
            sess = DB.session
            for instance_id in remove_ids:
                db_proc.delete_item(session=sess, item=instance_id)
        except Exception:
            LOG.error(traceback.format_exc())
            return utils.return_msg(msg='Unexpected Error', status=500)
        else:
            rpc.default_publisher(message=packet)
            db_proc.commit_session(session=sess)
            msg = 'Removing %s Instances for Zone %s' % (difference, zone.id)
            return utils.return_msg(msg=msg, status=200)
    else:
        return utils.return_msg(msg='nothing to do', status=200)