def update(contxt, appnode_id, appnode):
    """update app node ssh status, log info or deleted"""
    if contxt is None:
        contxt = context.get_admin_context()

    id = utils.int_from_str(appnode_id)
    LOG.debug('app node id: %s ' % id)

    os_tenant_name = appnode['os_tenant_name']
    os_username = appnode['os_username']
    os_password = appnode['os_password']
    os_auth_url = appnode['os_auth_url']
    os_region_name = appnode['os_region_name']
    """validate openstack access info"""
    try:
        token_url_id = _get_token(os_tenant_name, os_username, os_password,
                                  os_auth_url, os_region_name)
        if token_url_id != None:
            appnode['ssh_status'] = "reachable"
        else:
            appnode['ssh_status'] = "no cinder-volume"
    except:
        LOG.exception(_("Error to access to openstack"))
        appnode['ssh_status'] = "unreachable"

    try:
        return db.appnodes_update(contxt, id, appnode)
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on updating Appnodes %s" % e))
        raise exception.AppNodeFailure()
def create(contxt, auth_openstack=None, allow_duplicate=False):
    """create app node from a dict"""
    if contxt is None:
        contxt = context.get_admin_context()

    if not auth_openstack:
        raise exception.AppNodeInvalidInfo()

    ref = []
    """validate openstack access info"""
    try:
        token_url_id = _get_token(auth_openstack['os_tenant_name'],
                                  auth_openstack['os_username'],
                                  auth_openstack['os_password'],
                                  auth_openstack['os_auth_url'],
                                  auth_openstack['os_region_name'])
        if token_url_id != None:
            auth_openstack['ssh_status'] = "reachable"
        else:
            auth_openstack['ssh_status'] = "no cinder-volume"
    except:
        LOG.exception(_("Error to access to openstack"))
        auth_openstack['ssh_status'] = "unreachable"

    try:
        ref.append(db.appnodes_create(contxt, auth_openstack, allow_duplicate))
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on creating Appnodes %s" % e))
        raise exception.AppNodeFailure()
    return ref
def update(contxt, appnode_id, ssh_status=None, log_info=None, ip=None):
    """update app node ssh status, log info or deleted"""
    if contxt is None:
        contxt = context.get_admin_context()

    id = utils.int_from_str(appnode_id)
    LOG.debug('app node id: %s ' % id)
    kargs = {}

    if ip:
        kargs['ip'] = ip

    if ssh_status:
        utils.check_string_length(ssh_status, 'ssh_status', 1, 50)
        kargs['ssh_status'] = ssh_status

    if log_info:
        utils.check_string_length(log_info, 'log_info', 1, 65535)
        kargs['log_info'] = log_info

    if kargs:
        try:
            return db.appnodes_update(contxt, id, kargs)
        except db_exc.DBError as e:
            LOG.exception(_("DB Error on updating Appnodes %s" % e))
            raise exception.AppNodeFailure()
def create(contxt, auth_openstack=None, allow_duplicate=False):
    """create app node from a dict"""
    if contxt is None:
        contxt = context.get_admin_context()

    appnode = auth_openstack
    if not appnode:
        raise exception.AppNodeInvalidInfo()

    auth_url = appnode['os_auth_url'].strip("/")
    ssh_user = appnode['ssh_user']
    tenant_name = appnode['os_tenant_name']
    username = appnode['os_username']
    password = appnode['os_password']
    region_name = appnode['os_region_name']

    os_controller_host = auth_url.split(":")[1][2:]
    result, err = utils.execute('check_xtrust_crudini',
                                ssh_user,
                                os_controller_host,
                                run_as_root=True)
    LOG.info("==============result: %s" % result)
    LOG.info("==============err: %s" % err)
    if "command not found" in err:
        raise Exception("Command not found on %s" % os_controller_host)
    if "Permission denied" in err:
        raise Exception(
            "Please check the mutual trust between vsm controller node "
            "and openstack controller node")
    if "No passwd entry" in err:
        raise Exception("Please check the trust user")

    # support keystone v3 ad v2.0
    keystone_version = auth_url.split("/")[-1]
    try:
        if keystone_version == "v3":
            result = _check_v3(tenant_name, username, password, auth_url,
                               region_name)
        elif keystone_version == "v2.0":
            result = _check_v2(tenant_name, username, password, auth_url,
                               region_name)
        else:
            raise Exception("Only support keystone v3 and v2.0 now.")
        if result:
            appnode['ssh_status'] = "reachable"
        else:
            appnode['ssh_status'] = "no cinder-volume"
    except:
        LOG.exception(_("Error to access to openstack"))
        appnode['ssh_status'] = "unreachable"

    ref = []
    try:
        ref.append(db.appnodes_create(contxt, appnode, allow_duplicate))
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on creating Appnodes %s" % e))
        raise exception.AppNodeFailure()
    return ref
def get_all_nodes(contxt):
    """get all non-deletd app nodes as a dict"""
    if contxt is None:
        contxt = context.get_admin_context()
    try:
        nodes = db.appnodes_get_all(contxt)
        return nodes
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on getting Appnodes %s" % e))
        raise exception.AppNodeFailure()
def destroy(contxt, appnode_id):
    if contxt is None:
        contxt = context.get_admin_context()

    appnode_id = utils.int_from_str(appnode_id)
    try:
        db.appnodes_destroy(contxt, appnode_id)
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on deleting Appnodes %s" % e))
        raise exception.AppNodeFailure()
def create(contxt, auth_openstack=None, allow_duplicate=False):
    """create app node from a dict"""
    if contxt is None:
        contxt = context.get_admin_context()

    if not auth_openstack:
        raise exception.AppNodeInvalidInfo()

    os_controller_host = auth_openstack['os_auth_url'].split(":")[1][2:]
    result, err = utils.execute('check_xtrust_crudini',
                                auth_openstack['ssh_user'],
                                os_controller_host,
                                run_as_root=True)
    LOG.info("==============result: %s" % result)
    LOG.info("==============err: %s" % err)
    if "command not found" in err:
        raise Exception("Command not found on %s" % os_controller_host)
    if "Permission denied" in err:
        raise Exception(
            "Please check the mutual trust between vsm controller node "
            "and openstack controller node")
    if "No passwd entry" in err:
        raise Exception("Please check the trust user")

    ref = []
    """validate openstack access info"""
    try:
        token_url_id = _get_token(auth_openstack['os_tenant_name'],
                                  auth_openstack['os_username'],
                                  auth_openstack['os_password'],
                                  auth_openstack['os_auth_url'],
                                  auth_openstack['os_region_name'])
        if token_url_id != None:
            auth_openstack['ssh_status'] = "reachable"
        else:
            auth_openstack['ssh_status'] = "no cinder-volume"
    except:
        LOG.exception(_("Error to access to openstack"))
        auth_openstack['ssh_status'] = "unreachable"

    try:
        ref.append(db.appnodes_create(contxt, auth_openstack, allow_duplicate))
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on creating Appnodes %s" % e))
        raise exception.AppNodeFailure()
    return ref
def update(contxt, appnode_id, appnode):
    """update app node ssh status, log info or deleted"""
    if contxt is None:
        contxt = context.get_admin_context()

    id = utils.int_from_str(appnode_id)
    LOG.debug('app node id: %s ' % id)

    os_controller_host = appnode['os_auth_url'].split(":")[1][2:]
    result, err = utils.execute('check_xtrust_crudini',
                                appnode['ssh_user'],
                                os_controller_host,
                                run_as_root=True)
    LOG.info("==============result: %s" % result)
    LOG.info("==============err: %s" % err)
    if "command not found" in err:
        raise Exception("Command not found on %s" % os_controller_host)
    if "Permission denied" in err:
        raise Exception(
            "Please check the mutual trust between vsm controller node "
            "and openstack controller node")
    if "No passwd entry" in err:
        raise Exception("Please check the trust user")
    """validate openstack access info"""
    try:
        token_url_id = _get_token(appnode['os_tenant_name'],
                                  appnode['os_username'],
                                  appnode['os_password'],
                                  appnode['os_auth_url'],
                                  appnode['os_region_name'])
        if token_url_id != None:
            appnode['ssh_status'] = "reachable"
        else:
            appnode['ssh_status'] = "no cinder-volume"
    except:
        LOG.exception(_("Error to access to openstack"))
        appnode['ssh_status'] = "unreachable"

    try:
        return db.appnodes_update(contxt, id, appnode)
    except db_exc.DBError as e:
        LOG.exception(_("DB Error on updating Appnodes %s" % e))
        raise exception.AppNodeFailure()
def create(contxt, ips=None, allow_duplicate=False):
    """create app node from a dict"""
    if contxt is None:
        contxt = context.get_admin_context()

    if not ips:
        raise exception.AppNodeInvalidInfo()
    """validate Ipv4 address"""
    ref = []
    for ip in ips:
        if not utils.is_valid_ipv4(ip):
            msg = _("Invalid Ipv4 address %s for app node." % ip)
            raise exception.InvalidInput(reason=msg)
        else:
            attr = {'ip': ip}
            try:
                ref.append(db.appnodes_create(contxt, attr, allow_duplicate))
            except db_exc.DBError as e:
                LOG.exception(_("DB Error on creating Appnodes %s" % e))
                raise exception.AppNodeFailure()
    return ref