Skip to content

Instantly share code, notes, and snippets.

@erijpkema
Created February 4, 2022 12:18
Show Gist options
  • Save erijpkema/69421e723c566708df0b94c42572d822 to your computer and use it in GitHub Desktop.
Save erijpkema/69421e723c566708df0b94c42572d822 to your computer and use it in GitHub Desktop.
#!/bin/env/python
"""
This script generates openstack cli commands to create user accounts for the
WCC_201 course.
"""
import string
import secrets
import csv
from pprint import pprint
from collections import defaultdict
alphabet = string.ascii_letters + string.digits
user_create_cmd = 'openstack user create --project-domain WCC_2021 --password {password} --project {project} {name}'
domain_user_create_cmd = 'openstack user create --domain WCC_2021 --password {password} {name}'
project_create_cmd = 'openstack project create --domain WCC_2021 {}'
def create_password():
"""
Returns: string
"""
return ''.join(secrets.choice(alphabet) for i in range(20))
def create_datastructure():
"""
Fils the projects dict.
returns: Dict
"""
projects = defaultdict(list)
with open('WCC_Studenten.csv', newline='') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csv_reader:
# prefix bare id with WCC_2021_
if row[0].isdigit():
project = 'WCC_2021_{:0>2d}'.format(int(row[0]))
else:
project = 'WCC_2021_staff'
projects[project] += [(person.replace(' ', '_'), create_password())
for person in row[1].split('\n')]
return projects
if __name__ == '__main__':
projects = create_datastructure()
print('I am going to create the following datastructure\n')
pprint(projects)
print('openstack domain create WCC_2021')
for project in projects.keys():
if project == 'WCC_2021_staff':
for member in projects[project]:
# We make these domain admins.
print(
domain_user_create_cmd.format(
domain='WCC_2021',
name=member[0],
password=member[1],
role='admin'))
print(
'openstack role add --user {user} --domain WCC_2021 admin'.
format(user=member[0]))
else:
print(project_create_cmd.format(project))
for member in projects[project]:
# The students are project admins,
password = ''.join(secrets.choice(alphabet) for i in range(20))
print(
user_create_cmd.format(
project=project,
name=member[0],
password=member[1],
role='admin'))
print(
'openstack role add --user {user} --project {project} admin'.
format(user=member[0], project=project))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment