Commit 7a1f12cb by Máhonfai Bálint

Update mass create to log created VMs and handle errors

parent 6f4187b1
Pipeline #1442 passed with stage
in 0 seconds
...@@ -14,8 +14,9 @@ ...@@ -14,8 +14,9 @@
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>. # with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from vm.models import Instance, InstanceTemplate from vm.models import Instance, InstanceTemplate
...@@ -32,11 +33,26 @@ class Command(BaseCommand): ...@@ -32,11 +33,26 @@ class Command(BaseCommand):
with open(options['users']) as f: with open(options['users']) as f:
users = f.read().splitlines() users = f.read().splitlines()
missing_users = Instance.mass_create_for_users( missing_users, error_users = [], []
template, users, options['admin'], options['operator'] for user_id in users:
try:
Instance.create_for_user(
template, user_id, options['admin'], options['operator']
) )
self.stdout.write('Created VM for user: %s' % user_id)
except User.DoesNotExist:
missing_users.append(user_id)
except:
error_users.append(user_id)
if len(missing_users) > 0: if len(missing_users) > 0:
self.stdout.write('These users do not exist:') self.stdout.write('\nThese users do not exist:')
for user in missing_users: for user in missing_users:
self.stdout.write(user) self.stdout.write(user)
if len(error_users) > 0:
self.stdout.write('\nFailed to create VM for these users:')
for user in error_users:
self.stdout.write(user)
if len(missing_users) + len(error_users) == 0:
self.stdout.write('\nCreated all VMs successfully.')
...@@ -443,20 +443,12 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -443,20 +443,12 @@ 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, admin=None, operator=None, **kwargs): def create_for_user(cls, template, user_id, admin=None, operator=None, **kwargs):
""" """
Create and deploy an instance of a template for each user Create and deploy an instance of a template for a given user.
in a list of users. Returns the user IDs of missing users.
""" """
user_instances = [] user = User.objects.get(profile__org_id=user_id)
missing_users = []
for user_id in users:
try:
user_instances.append(User.objects.get(profile__org_id=user_id))
except User.DoesNotExist:
missing_users.append(user_id)
for user in user_instances:
instance = cls.create_from_template(template, user, **kwargs) instance = cls.create_from_template(template, user, **kwargs)
if admin: if admin:
instance.set_level(User.objects.get(username=admin), 'owner') instance.set_level(User.objects.get(username=admin), 'owner')
...@@ -464,8 +456,6 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -464,8 +456,6 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
instance.set_level(User.objects.get(username=operator), 'operator') instance.set_level(User.objects.get(username=operator), 'operator')
instance.deploy(user=user) instance.deploy(user=user)
return missing_users
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
self.time_of_suspend, self.time_of_delete = self.get_renew_times() self.time_of_suspend, self.time_of_delete = self.get_renew_times()
super(Instance, self).clean(*args, **kwargs) super(Instance, self).clean(*args, **kwargs)
......
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