Commit f03239bb by Máhonfai Bálint

Add an optional argument for an additional admin user

parent 81054e66
Pipeline #1152 passed with stage
in 0 seconds
...@@ -21,14 +21,20 @@ from vm.models import Instance, InstanceTemplate ...@@ -21,14 +21,20 @@ from vm.models import Instance, InstanceTemplate
class Command(BaseCommand): class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('template_id', type=int) parser.add_argument('-t', '--template', type=int, required=True)
parser.add_argument('users_path') parser.add_argument('-u', '--users', required=True)
parser.add_argument('-a', '--admin')
def handle(self, *args, **options): def handle(self, *args, **options):
template = InstanceTemplate.objects.get(id=options['template_id']) template = InstanceTemplate.objects.get(id=options['template'])
with open(options['users_path']) as f:
with open(options['users']) as f:
users = f.read().splitlines() 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: if len(missing_users) > 0:
self.stdout.write('These users do not exist:') self.stdout.write('These users do not exist:')
for user in missing_users: for user in missing_users:
......
...@@ -440,7 +440,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -440,7 +440,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
for cps in customized_params] for cps in customized_params]
@classmethod @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 Create and deploy an instance of a template for each user
in a list of users. Returns the user IDs of missing users. in a list of users. Returns the user IDs of missing users.
...@@ -453,11 +453,11 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -453,11 +453,11 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
except User.DoesNotExist: except User.DoesNotExist:
missing_users.append(user_id) missing_users.append(user_id)
instances = []
for user in user_instances: for user in user_instances:
instance = cls.create_from_template(template, user, **kwargs) instance = cls.create_from_template(template, user, **kwargs)
if admin:
instance.set_level(User.objects.get(username=admin), 'owner')
instance.deploy(user=user) instance.deploy(user=user)
instances.append(instance)
return missing_users 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