Beispiel #1
0
def get_from_tenant_by_name(name):
    keystone = get_keystone('from')
    tenants = get_from_tenant_list()
    auth = AuthStack()
    for tenant in tenants:
        if tenant.name == name:
            return tenant
Beispiel #2
0
def get_users_based_on_project(destination, name):
    keystone = get_keystone(destination)
    auth = AuthStack()
    version = '3'
    domain_id = 'default'
    if destination == 'from':
        domain_id = auth.from_domain_id
        if auth.from_keystone_version == '2':
            version = '2'
    if destination == 'to':
        domain_id = auth.to_domain_id
        if auth.to_keystone_version == '2':
            version = '2'
    if version == '2':
        projects = keystone.tenants.list()
    else:
        # projects = keystone.projects.list(domain=domain_id, name=name)
        projects = keystone.projects.list(name=name)
    # print projects
    users = []
    if version == '2':
        users = get_users(destination)
    else:
        for project in projects:
            # roles = keystone.role_assignments.list(project=project.id)
            roles = keystone.role_assignments.list(project=project.id)
            for role in roles:
                # print role.user['id']
                user = keystone.users.get(role.user['id'])
                if user not in users:
                    users.append(user)
    return users
Beispiel #3
0
def get_solid_fire():
    auth = AuthStack()
    sf = ElementFactory.create(auth.solid_fire_ip,
                               auth.solid_fire_user,
                               auth.solid_fire_password,
                               print_ascii_art=False)
    return sf
Beispiel #4
0
def print_user_names(destination):
    auth = AuthStack()
    us = get_users_based_on_project(destination, auth.from_tenant_name)
    users = sorted(us, key=lambda x: x.name)
    version = '3'
    if destination == "from":
        if auth.from_keystone_version == '2':
            version = '2'
    if destination == 'to':
        if auth.to_keystone_version == '2':
            version = '2'
    if version == '2':
        print "Name:                     Tenant ID:"
        for u in users:
            if hasattr(u, 'tenantId'):
                print '{:25}'.format(u.name), u.tenantId
            else:
                print u.name
    else:
        print "Name:                     Domain:                             Default Project ID:"
        for u in users:
            if hasattr(u, 'default_project_id'):
                print '{:25}'.format(u.name), '{:35}'.format(
                    u.domain_id), u.default_project_id
            else:
                print '{:25}'.format(u.name), u.domain_id
Beispiel #5
0
def get_to_project_list():
    auth = AuthStack()
    keystone = get_keystone('to')
    if auth.to_keystone_version == '2':
        projects = keystone.tenants.list()
    else:
        projects = keystone.projects.list(domain=auth.to_domain_id)
    #print tenants
    return projects
Beispiel #6
0
def create_project(destination, tenant):
    keystone = get_keystone(destination)
    auth = AuthStack()
    new_tenant = keystone.projects.create(tenant.name,
                                          auth.to_project_domain_id,
                                          description=tenant.description,
                                          enabled=tenant.enabled)
    print "Created project", new_tenant.name
    return new_tenant
Beispiel #7
0
def upload_images_by_url(uuid_file):
    ids = utils.read_ids_from_file(uuid_file)
    auth = AuthStack()
    from_path = auth.from_nfs_glance_location
    for uuid in ids:
        image_name = "migration_vm_image_" + uuid
        image = get_image_by_name('from', image_name)
        filename = from_path + image.id
        print "Uploading image name:", image_name
        start_image_create_process('to', image, filename)
Beispiel #8
0
def compare_and_create_users_by_project(password=None):
    auth = AuthStack()
    from_matrix = build_matrix()
    from_users = get_users_based_on_project('from', auth.from_tenant_name)
    to_users = get_users_based_on_project('to', auth.to_tenant_name)
    from_names = map(lambda from_users: from_users.name, from_users)
    to_names = map(lambda to_users: to_users.name, to_users)
    for name in from_names:
        if name not in to_names:
            from_user = filter(lambda from_users: from_users.name == name,
                               from_users)
            new_user = create_user('to', from_user[0], password, from_matrix)
Beispiel #9
0
def build_matrix(user_name_file=None):
    keystone = get_keystone("from")
    auth = AuthStack()
    matrix = list()
    projects = list()
    project_list = build_project_from_to_id_list()
    role_list = build_role_from_to_list()
    if auth.from_keystone_version == '2':
        if user_name_file:
            users = get_users_from_name_list('from', user_name_file)
        else:
            users = get_users('from')
        # print users
        for user in users:
            # print user
            get_user_roles('from', matrix, user, project_list, role_list)
        # matrix.append(mat)
    else:
        asl = keystone.role_assignments.list()
        project = ''
        print "Building user project role matrix between the two environments. This will take a bit."
        for a in asl:
            project = ''
            if hasattr(a, 'scope'):
                if a.scope.has_key('project'):
                    pro = a.scope['project']['id']
                    p = filter(lambda to_vms: to_vms['from_id'] == pro,
                               projects)
                    if p:
                        project = p[0]
                    if not project:
                        project = find_opposite_project_id(
                            a.scope['project']['id'])
                        if not project:
                            print "WARNING: Project not found" + a.scope[
                                'project']['id']
                        else:
                            projects.append(project)
                    if project:
                        role = find_opposite_role(a.role['id'])
                        mat = {
                            'from_role_id:': a.role['id'],
                            'role_name': role['name'],
                            'from_project_id': a.scope['project']['id'],
                            'from_user_id': a.user['id'],
                            'to_project_id': project['to_id'],
                            'to_role_id': role['to_id'],
                            'project_name': project['name']
                        }
                        print mat
                        matrix.append(mat)
    return matrix
Beispiel #10
0
def create_user(destination, user, password, from_matrix):
    auth = AuthStack()
    keystone = get_keystone(destination)
    u_id = None
    project = None

    if hasattr(user, 'default_project_id'):
        u_id = user.default_project_id
        project = find_opposite_project_id(u_id)
    else:
        #     #todo: not all v3 have defaults, so need to check for v2/v3 and then might not have project associated
        if auth.from_keystone_version == '2':
            if hasattr(user, 'tenantId'):
                u_id = user.tenantId
                project = find_opposite_project_id(u_id)
        #else:
        #v3 and no default

    try:
        if project:
            if hasattr(user, 'email'):
                new_user = keystone.users.create(
                    name=user.name,
                    password=password,
                    default_project=project['to_id'],
                    email=user.email,
                    enabled=user.enabled)
            else:
                new_user = keystone.users.create(
                    name=user.name,
                    password=password,
                    default_project=project['to_id'],
                    enabled=user.enabled)
        else:
            if hasattr(user, 'email'):
                new_user = keystone.users.create(name=user.name,
                                                 password=password,
                                                 email=user.email,
                                                 enabled=user.enabled)
            else:
                new_user = keystone.users.create(name=user.name,
                                                 password=password,
                                                 enabled=user.enabled)
        if password:
            print "Created new user:"******". This user has default password set. Change password manually."
        else:
            print "Created new user:"******". This user has no password. Set password manually."

        update_roles(user, new_user, from_matrix)
        return new_user
    except keystone_exceptions.http.Conflict:
        print "WARNING: Duplicate user creation attempted, skipping user:", user.name
Beispiel #11
0
def main(opts, args):
    auth = AuthStack()
    print "From:", auth.from_auth_ip, " Username:"******" Project:", auth.from_tenant_name
    print "To:  ", auth.to_auth_ip, " Username:"******" Project:", auth.to_tenant_name

    if opts.report:
        try:
            keystone_common.print_projects('from')
        except Exception, e:
            print "To print project info, switch to admin user"
        try:
            keystone_common.print_projects('to')
        except Exception, e:
            print "To print project info, switch to admin user"
Beispiel #12
0
def main(opts, args):
    auth = AuthStack()
    print "From:", auth.from_auth_ip, " Username:"******" Project:", auth.from_tenant_name
    print "To:  ", auth.to_auth_ip, " Username:"******" Project:", auth.to_tenant_name

    if opts.fromusers:
        print "\n--------------- From Users: ------------------------"
        keystone_common.print_user_names('from')
    if opts.tousers:
        print "\n--------------- To Users: ------------------------"
        keystone_common.print_user_names('to')
    if opts.fromprojects:
        keystone_common.print_projects('from')
    if opts.toprojects:
        keystone_common.print_projects('to')
Beispiel #13
0
def copy_nfs_volume(volume_id):
    auth = AuthStack()

    volume = "volume-" + volume_id
    location = utils.get_nfs_location(auth.nfs_dir, volume)
    shares = location.split("/")
    share = shares[len(shares) - 1]
    print "share: " + share
    nfs_host = auth.nfs_host + share
    print "host: " + nfs_host
    # netapp_id = auth.nfs_ip + ":/" + share + "/" + volume
    # netapp_path = auth.nfs_ip + ":/" + share + "/"
    cinder_path = auth.nfs_cinder_location + share + "/"
    print cinder_path
    new_name = "migration-" + volume
    utils.start_copy_process(cinder_path, volume, new_name)
Beispiel #14
0
def main(opts, args):
    auth = AuthStack()
    print "From:", auth.from_auth_ip, " Username:"******" Project:", auth.from_tenant_name
    print "To:  ", auth.to_auth_ip, " Username:"******" Project:", auth.to_tenant_name

    if opts.nets:
        print "\n--------------- From Networks (with subnets): ---------------------"
        neutron_common.print_network_list('from')
    if opts.Nets:
        print "\n--------------- To Networks (with subnets): ------------------------"
        neutron_common.print_network_list('to')
    if opts.routers:
        print "\n--------------- From Routers: ---------------------"
        neutron_common.print_routers('from')
    if opts.Routers:
        print "\n--------------- To Routers: ---------------------"
        neutron_common.print_routers('to')
Beispiel #15
0
def get_users_based_on_domain(destination):
    auth = AuthStack()
    keystone = get_keystone(destination)
    version = '3'
    if auth.from_keystone_version == '2':
        version = '2'
    domain = 'default'
    if destination == 'from':
        domain = auth.from_domain_id
    else:
        domain = auth.to_domain_id
    if version == '2':
        users = keystone.users.list()
    else:
        users = keystone.users.list(domain=domain)
    #print users
    return users
Beispiel #16
0
def list_roles(destination, user_id, tenant_id):
    keystone = get_keystone(destination)
    auth = AuthStack()
    version = '3'
    if destination == "from":
        if auth.from_keystone_version == '2':
            version = '2'
    if destination == 'to':
        if auth.to_keystone_version == '2':
            version = '2'
    if version == '2':
        user = keystone.users.get(user_id)
        tenant = keystone.tenants.get(tenant_id)
        roles = keystone.roles.roles_for_user(user, tenant=tenant)
    else:
        roles = keystone.roles.list(user_id, project=tenant_id)
    return roles
Beispiel #17
0
def print_projects(destination):
    auth = AuthStack()
    project = "Project"
    dest = "To"
    if destination == 'to':
        projects = get_to_project_list()
    else:
        projects = get_from_project_list()
        dest = "From"
        if auth.from_keystone_version == '2':
            project = "Tenant"
    projects.sort(key=lambda x: x.name)
    newlist = sorted(projects, key=lambda x: x.name)
    print "\n--------------- ", dest, project, " ------------------------"
    print project, "ID:                         Name:                  Description:"
    for project in newlist:
        print project.id, " ", '{:20}'.format(
            project.name), " ", project.description, " "
Beispiel #18
0
def easy_manage_netapp(volume_name, volume_type, volume_id):
    auth = AuthStack()

    volume = "volume-" + volume_id
    location = utils.get_nfs_location(auth.nfs_dir, volume)
    shares = location.split("/")
    share = shares[len(shares) - 1]
    print "share: " + share
    nfs_host = auth.nfs_host + share
    print "host: " + nfs_host
    netapp_id = auth.nfs_ip + ":/" + share + "/" + volume
    print netapp_id
    resource = {'source-name': netapp_id}
    print resource
    manage_volume('to',
                  host=nfs_host,
                  reference=resource,
                  name=volume_name,
                  volume_type=volume_type)
Beispiel #19
0
def compare_and_create_users_by_domain(password=None, user_name_file=None):
    auth = AuthStack()
    if user_name_file:
        from_users = get_users_from_name_list('from', user_name_file)
    else:
        from_users = get_users_based_on_domain('from')
    to_users = get_users_based_on_domain('to')
    from_names = map(lambda from_users: from_users.name, from_users)
    to_names = map(lambda to_users: to_users.name, to_users)
    from_matrix = build_matrix(user_name_file=user_name_file)
    for name in from_names:
        if name not in to_names:
            from_user = filter(lambda from_users: from_users.name == name,
                               from_users)
            new_user = create_user('to', from_user[0], password, from_matrix)
        elif name in to_names:
            print "user already exists, update roles"
            from_user = filter(lambda from_users: from_users.name == name,
                               from_users)
            to_user = filter(lambda to_users: to_users.name == name, to_users)
            update_roles(from_user[0], to_user[0], from_matrix)
Beispiel #20
0
def main(opts, args):
    auth = AuthStack()
    print "From:", auth.from_auth_ip, " Username:"******" Project:", auth.from_tenant_name
    print "To:  ", auth.to_auth_ip, " Username:"******" Project:", auth.to_tenant_name

    if opts.report:
        print "\n--------------- From Volumes: ------------------------"
        volumes = cinder_common.print_detail_volumes('from')
    if opts.Report:
        print "\n--------------- To Volumes: ------------------------"
        volumes = cinder_common.print_detail_volumes('to')
    if opts.types:
        print "\n--------------- From Volume Types: ------------------------"
        cinder_common.print_volume_types('from')
        print "\n--------------- To Volume Types: ------------------------"
        cinder_common.print_volume_types('to')
    if opts.backups:
        print "\n--------------- From Volume Backups: ------------------------"
        cinder_common.print_volume_backups('from')
        print "\n--------------- To Volume Backups: ------------------------"
        cinder_common.print_volume_backups('to')
    if opts.pools:
        print "\n--------------- From Volume Pools: ------------------------"
        cinder_common.print_cinder_pools('from')
        print "\n--------------- To Volume Pools: ------------------------"
        cinder_common.print_cinder_pools('to')
    if opts.Manage:
        if args:
            print "\n--------------- To Manageable Volumes: ------------------------"
            cinder_common.print_manageable_volumes('to', host=args[0])
        else:
            print "Please provide host name, similar to sample-aio-liberty-2@lvm#LVM_iSCSI"
    if opts.manage:
        if args:
            print "\n--------------- From Volume Pools: ------------------------"
            cinder_common.print_manageable_volumes('from', host=args[0])
        else:
            print "Please provide host name, similar to sample-aio-liberty-2@lvm#LVM_iSCSI"
Beispiel #21
0
def main(opts, args):
    auth = AuthStack()
    print "From:", auth.from_auth_ip, " Username:"******" Project:", auth.from_tenant_name
    print "To:  ", auth.to_auth_ip, " Username:"******" Project:", auth.to_tenant_name

    if opts.scf:
        print "\n--------------- From Security Groups: ------------------------"
        nova_common.print_security_groups('from')
    if opts.Scf:
        print "\n--------------- To Security Groups: ------------------------"
        nova_common.print_security_groups('to')
    if opts.ff:
        print "\n--------------- From Flavors: ------------------------"
        nova_common.print_flavor_list('from')
    if opts.Ff:
        print "\n--------------- To Flavors: ------------------------"
        nova_common.print_flavor_list('to')
    if opts.vf:
        print "\n--------------- From VMs: ------------------------"
        vms = nova_common.print_vm_list_ids('from')
    if opts.vF:
        print "\n--------------- To VMs: ------------------------"
        vms = nova_common.print_vm_list_ids('to')
Beispiel #22
0
def get_glance(destination):
    auth = AuthStack()
    client = auth.get_glance_client(destination)
    return client
Beispiel #23
0
def manage_copy_volume_from_id(destination, volume):

    auth = AuthStack()
    meta = volume.metadata
    print "meta:"
    print meta
    meta.update({'clone_volume_id': volume.id})
    original_cinder = get_cinder('from')
    version = float(original_cinder.version)
    if version >= 2.0:
        meta.update({'clone_volume_name': volume.name})
    else:
        meta.update({'clone_volume_name': volume.display_name})
    meta.update({'clone_boot_status': volume.bootable})
    print meta
    if volume.attachments:
        for att in volume.attachments:
            meta.update({'original_vm_id': att['server_id']})
            meta.update({'original_device': att['device']})
    volume_type = volume.volume_type
    bootable = False
    # if volume.bootable == 'true':
    #     bootable = True  # for some reason api returns a string and the next call expects a boolean.

    # there is an issue with cinder v1 where volume set bootable didn't work, so using metadata instead.
    if volume.bootable == 'true':
        bootable = True

    if hasattr(volume, 'display_name'):
        name = volume.display_name
    else:
        name = volume.name

    source = {}
    host = None
    print "Volume type is:", volume_type
    if volume_type == 'SolidFire':
        print "Volume is SolidFire."
        copy_id = volume.id + "-migration"
        sfid = solidfire_common.get_volume_by_volume_name(copy_id)
        ref = "%(id)s" % {"id": sfid}
        source = {'source-id': ref}
        host = auth.solid_fire_host
        volume_type = 'legacy_solidfire'
    else:
        ref = "migration-volume-" + volume.id
        location = utils.get_nfs_location(auth.nfs_dir, ref)
        shares = location.split("/")
        share = shares[len(shares) - 1]
        print "share: " + share
        host = auth.nfs_host + share
        print "host: " + host
        netapp_id = auth.nfs_ip + ":/" + share + "/" + ref
        print netapp_id
        source = {'source-name': netapp_id}
        print source

    print "reference: "
    print source
    manage_volume(destination,
                  source,
                  host,
                  name,
                  volume_type=volume_type,
                  bootable=bootable,
                  metadata=meta)
    print "Volume id: " + volume.id + " managed!"
Beispiel #24
0
def get_neutron(destination):
    auth = AuthStack()
    client = auth.get_neutron_client(destination)
    return client
Beispiel #25
0
def manage_volume_from_id(destination, ssd_host, hdd_host, volume):
    # cinder = get_cinder(destination)
    #todo: verify tenant/user info
    # try:
    #     from_tenant = volume.__dict__['os-vol-tenant-attr:tenant_id']
    #     tenant = keystone_common.find_opposite_tenant_id(from_tenant)
    # except Exception, e:
    #     print "No tenant ID found, setting tenant to None"
    #     tenant = None

    # manage_volume("to", )
    #reference, host, type, name, bootable=False, metadata=None
    auth = AuthStack()
    meta = volume.metadata
    print "meta:"
    print meta
    meta.update({'clone_volume_id': volume.id})
    original_cinder = get_cinder('from')
    version = float(original_cinder.version)
    if version >= 2.0:
        meta.update({'clone_volume_name': volume.name})
    else:
        meta.update({'clone_volume_name': volume.display_name})
    meta.update({'clone_boot_status': volume.bootable})
    print meta
    if volume.attachments:
        for att in volume.attachments:
            meta.update({'original_vm_id': att['server_id']})
            meta.update({'original_device': att['device']})
    volume_type = volume.volume_type
    bootable = False
    # if volume.bootable == 'true':
    #     bootable = True  # for some reason api returns a string and the next call expects a boolean.

    # there is an issue with cinder v1 where volume set bootable didn't work, so using metadata instead.
    if meta['bootable'] == 'true':
        bootable = True

    #name = volume.name
    if 'original_volume_name' in meta:
        name = meta['original_volume_name']
    elif 'image_name' in meta:
        name = meta['image_name']
    else:
        name = 'no_original_name_found'
    print "original name: " + name
    source = {}
    print "Volume type is:", volume_type
    if volume_type == 'SolidFire':
        sfid = solidfire_common.get_volume_by_volume_name(volume.id)
        ref = "%(id)s" % {"id": sfid}
        # source = {'source-id': ref}
        source = {'source-id': ref}
        host = ssd_host
    else:
        if hdd_host:
            ref = 'volume-' + volume.id
            source = {'source-name': ref}
            host = hdd_host
        else:
            ref = "volume-" + volume.id
            location = utils.get_nfs_location(auth.nfs_dir, ref)
            shares = location.split("/")
            share = shares[len(shares) - 1]
            print "share: " + share
            host = auth.nfs_host + share
            print "host: " + host
            netapp_id = auth.nfs_ip + ":/" + share + "/" + ref
            print netapp_id
            source = {'source-name': netapp_id}
            print source

    print "reference: "
    print source
    manage_volume(destination,
                  source,
                  host,
                  name,
                  volume_type=volume_type,
                  bootable=bootable,
                  metadata=meta)
    print "Volume id: " + volume.id + " managed!"
Beispiel #26
0
def get_cinder(destination):

    auth = AuthStack()
    client = auth.get_cinder_client(destination)
    return client
Beispiel #27
0
def get_nova(destination):
    auth = AuthStack()
    client = auth.get_nova_client(destination)
    return client
Beispiel #28
0
def get_keystone(destination):

    auth = AuthStack()
    client = auth.get_keystone_client(destination)
    return client