Commit 2c87b1df by Szeberényi Imre

Merge branch 'mass_create_for_users' into 'master'

Add an optional argument for an additional admin user

See merge request !416
parents 54a4ad92 f03239bb
Pipeline #1153 passed with stage
in 0 seconds
......@@ -21,14 +21,20 @@ from vm.models import Instance, InstanceTemplate
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('template_id', type=int)
parser.add_argument('users_path')
parser.add_argument('-t', '--template', type=int, required=True)
parser.add_argument('-u', '--users', required=True)
parser.add_argument('-a', '--admin')
def handle(self, *args, **options):
template = InstanceTemplate.objects.get(id=options['template_id'])
with open(options['users_path']) as f:
template = InstanceTemplate.objects.get(id=options['template'])
with open(options['users']) as f:
users = f.read().splitlines()
missing_users = Instance.mass_create_for_users(template, users)
missing_users = Instance.mass_create_for_users(
template, users, options['admin']
)
if len(missing_users) > 0:
self.stdout.write('These users do not exist:')
for user in missing_users:
......
......@@ -440,7 +440,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
for cps in customized_params]
@classmethod
def mass_create_for_users(cls, template, users, **kwargs):
def mass_create_for_users(cls, template, users, admin=None, **kwargs):
"""
Create and deploy an instance of a template for each user
in a list of users. Returns the user IDs of missing users.
......@@ -453,11 +453,11 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
except User.DoesNotExist:
missing_users.append(user_id)
instances = []
for user in user_instances:
instance = cls.create_from_template(template, user, **kwargs)
if admin:
instance.set_level(User.objects.get(username=admin), 'owner')
instance.deploy(user=user)
instances.append(instance)
return missing_users
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment