In our previous article, user statuses were as follows:
First of all, let's create our connection code as a Python script. For example, name it cloud_connection.py. Then, let's write the following code snippet into this connection file.
import pprint
import openstack
# Initialize PP and turn on OpenStack debug logging
pp = pprint.PrettyPrinter(indent=4)
openstack.enable_logging(debug=False)
# Initialize connection
connold = openstack.connect(cloud='oldstack')
connnew = openstack.connect(cloud='newstack')
Afterwards, let's create a new Python script in the same folder as cloud_connection.py for the user migration process. Since this is our first migration step, let's name it 1_user_migration.py. In this step, our task will be to read user information from the old stack, and using the information obtained we will recreate the same user. Let's begin by importing the necessary parts from the existing cloud_connection.py script.
from cloud_connection import connnew, connold , pp
During this process, we might want to migrate multiple users at once or exclude certain users from migration. Additionally, we'll need to set passwords for the users we create on the newstack. Therefore, let's structure the beginning of our code as follows:
# --------------------------------------------------------------------------------- #
# USERS TO / NOT TO MIGRATE
# If users_to_migrate is empty, script migrates every user that is not in
# users_to_filter list to destination
users_to_migrate = [
]
users_to_filter = [
'cinder',
'glance',
'heat_domain_admin',
'heat',
'neutron',
'nova',
'admin',
'placement',
'ceilometer'
]
# USERS TO / NOT TO MIGRATE
# --------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------- #
# NEW USER PASSWORD
new_password = "123456"
# NEW USER PASSWORD
# --------------------------------------------------------------------------------- #
By writing the usernames of the users we want to migrate into our users_to_migrate list, we can migrate only those specific users. However, our goal is to migrate all users except the filtered ones. In our current situation, there is only one user that hasn't been filtered out, which is deneme_user. We will iterate through all oldstack users, applying these filters, and migrate the ones that match. We will achieve this with the following code snippet.
for user in connold.identity.users():
user = user.to_dict()
should_filter = False
# Check If User Should NOT Migrate
if user['name'] in users_to_filter:
should_filter = True
if users_to_migrate:
if user['name'] not in users_to_migrate:
should_filter = True
# Continue Loop
if should_filter:
continue
# Print Migrating User Details
print("----------------------------------------------------------")
pp.pprint(user)
print("----------------------------------------------------------")
print("USER: ", user['name'], " ID: ", user['id'], " will be migrated..." )
print("----------------------------------------------------------")
# Create New User Details
c_user_attrs = {
"name": user["name"],
"description": user["description"],
"email": user["email"],
"is_enabled": user["is_enabled"],
"password": new_password
}
# Create New User on Destination OpenStack
new_user = connnew.identity.create_user(**c_user_attrs)
new_user = new_user.to_dict()
# Print Migrating User Details
print("-----------------NEW USER DETAILS----------------------")
pp.pprint(user)
print("----------------------------------------------------------")
When we run the code, it will produce this output:
----------------------------------------------------------
{ 'default_project_id': '5b656fc728104f97a0d6b8c1e0ac8ed2',
'description': 'Deneme User',
'domain_id': 'default',
'email': '[email protected]',
'id': '65135f083b974ab38bbb032828eb039b',
'is_enabled': True,
'links': { 'self': 'http://172.16.0.10:5000/v3/users/65135f083b974ab38bbb032828eb039b'},
'location': Munch({'cloud': 'oldstack', 'region_name': 'RegionOne', 'zone': None, 'project': Munch({'id': '7525ce2c81a24e87a202dae3c99f3b56', 'name': 'admin', 'domain_id': None, 'domain_name': 'Default'})}),
'name': 'deneme_user',
'password': None,
'password_expires_at': None}
----------------------------------------------------------
USER: deneme_user ID: 65135f083b974ab38bbb032828eb039b will be migrated...
----------------------------------------------------------
-----------------NEW USER DETAILS----------------------
{ 'default_project_id': '5b656fc728104f97a0d6b8c1e0ac8ed2',
'description': 'Deneme User',
'domain_id': 'default',
'email': '[email protected]',
'id': '65135f083b974ab38bbb032828eb039b',
'is_enabled': True,
'links': { 'self': 'http://172.16.0.10:5000/v3/users/65135f083b974ab38bbb032828eb039b'},
'location': Munch({'cloud': 'oldstack', 'region_name': 'RegionOne', 'zone': None, 'project': Munch({'id': '7525ce2c81a24e87a202dae3c99f3b56', 'name': 'admin', 'domain_id': None, 'domain_name': 'Default'})}),
'name': 'deneme_user',
'password': None,
'password_expires_at': None}
----------------------------------------------------------
A user with a different ID but the same details is created. To make sure, run the CLI command to check if the user named "deneme_user" exists on the newstack side.
erdem@EWUL:~$ openstack --os-cloud newstack user list
+----------------------------------+-------------------+
| ID | Name |
+----------------------------------+-------------------+
| ce911f6f97bc4edead28f10d2ed394ee | admin |
| 1e20632f0e7c4f70b22e1b9412e6bca3 | glance |
| 6e7743f268b14ebca52f9859ca79bd94 | cinder |
| e19c7f206f6b40bca5c75d1c98eee828 | placement |
| af4c516502334ad8a9a7070bf3a6592e | nova |
| 9584b3c922ce4330b066ffb48eedcd34 | neutron |
| 321f8908e3754f93b2d3e6bdbf758f91 | heat |
| 4fe818ccd76f4ab4812d789cc7f4358a | heat_domain_admin |
| 415cbda201ff4f0db64d0ddbfb798b2f | deneme_user |
+----------------------------------+-------------------+
Yes, we have successfully transferred a user from the oldstack to the newstack. User migration tasks are completed up to this point, now we can proceed to Project Migration stage.
OpenStack to OpenStack Migration:
- Article Series
- Deployment Info
- Preparation
- User Migration
- Project Migration
- Flavor Migration
- Security Migration
- Network Migration
- TBC
References:
Thanks:
Main Photo: Jordi Fernandez on Unsplash