Exemple #1
0
 def import_from_file(self, location):
     users = set()
     with open(location) as f:
         for line in f.readlines():
             try:
                 t = line.rstrip('\n').split(' ')
                 user = t[0]
                 resource = t[1]
                 capacity = t[2]
                 try:
                     capacity = units.parse(capacity)
                 except units.ParseError:
                     m = ("Capacity should be an integer, optionally "
                          "followed by a unit.")
                     raise CommandError(m)
             except (IndexError, TypeError):
                 self.stdout.write('Invalid line format: %s:\n' % t)
                 continue
             else:
                 try:
                     user = self.get_user(user)
                     users.add(user.id)
                 except CommandError:
                     self.stdout.write('Not found user: %s\n' % user)
                     continue
                 else:
                     try:
                         add_base_quota(user, resource, capacity)
                     except Exception, e:
                         self.stdout.write('Failed to add quota: %s\n' % e)
                         continue
Exemple #2
0
 def import_from_file(self, location):
     users = set()
     with open(location) as f:
         for line in f.readlines():
             try:
                 t = line.rstrip("\n").split(" ")
                 user = t[0]
                 resource = t[1]
                 capacity = t[2]
                 try:
                     capacity = units.parse(capacity)
                 except units.ParseError:
                     m = "Capacity should be an integer, optionally " "followed by a unit."
                     raise CommandError(m)
             except (IndexError, TypeError):
                 self.stdout.write("Invalid line format: %s:\n" % t)
                 continue
             else:
                 try:
                     user = self.get_user(user)
                     users.add(user.id)
                 except CommandError:
                     self.stdout.write("Not found user: %s\n" % user)
                     continue
                 else:
                     try:
                         add_base_quota(user, resource, capacity)
                     except Exception, e:
                         self.stdout.write("Failed to add quota: %s\n" % e)
                         continue
Exemple #3
0
    def set_limit(self, user, resource, capacity, force):
        style = None
        if capacity != 'default':
            try:
                capacity, style = units.parse_with_style(capacity)
            except:
                m = "Please specify capacity as a decimal integer or 'default'"
                raise CommandError(m)

        try:
            quota, default_capacity = user.get_resource_policy(resource)
        except Resource.DoesNotExist:
            raise CommandError("No such resource: %s" % resource)

        if not force:
            s_default = show_resource_value(default_capacity, resource, style)
            s_current = (show_resource_value(quota.capacity, resource, style)
                         if quota is not None else 'default')
            s_capacity = (show_resource_value(capacity, resource, style)
                          if capacity != 'default' else capacity)
            self.stdout.write("user: %s (%s)\n" % (user.uuid, user.username))
            self.stdout.write("default capacity: %s\n" % s_default)
            self.stdout.write("current capacity: %s\n" % s_current)
            self.stdout.write("new capacity: %s\n" % s_capacity)
            self.stdout.write("Confirm? (y/n) ")
            response = raw_input()
            if string.lower(response) not in ['y', 'yes']:
                self.stdout.write("Aborted.\n")
                return

        if capacity == 'default':
            try:
                quotas.remove_base_quota(user, resource)
            except Exception as e:
                import traceback
                traceback.print_exc()
                raise CommandError("Failed to remove policy: %s" % e)
        else:
            try:
                quotas.add_base_quota(user, resource, capacity)
            except Exception as e:
                raise CommandError("Failed to add policy: %s" % e)