Ejemplo n.º 1
0
 def collect_operations(self, **options):
     model = options.get('model')
     backends = options.get('backends') or set()
     if backends:
         backends = set(backends.split(','))
     servers = options.get('servers') or set()
     if servers:
         servers = set([Server.objects.get(Q(address=server)|Q(name=server)) for server in servers.split(',')])
     action = options.get('action')
     if not model:
         models = set()
         if servers:
             for server in servers:
                 if backends:
                     routes = server.routes.filter(backend__in=backends)
                 else:
                     routes = server.routes.all()
         elif backends:
             routes = Route.objects.filter(backend__in=backends)
         else:
             raise CommandError("Model or --servers or --backends?")
         for route in routes.filter(is_active=True):
             model = route.backend_class.model_class()
             models.add(model)
         querysets = [model.objects.order_by('id') for model in models]
     else:
         kwargs = {}
         for comp in options.get('query', []):
             comps = iter(comp.split('='))
             for arg in comps:
                 kwargs[arg] = next(comps).strip().rstrip(',')
         model = apps.get_model(*model.split('.'))
         queryset = model.objects.filter(**kwargs).order_by('id')
         querysets = [queryset]
     
     operations = OrderedSet()
     route_cache = {}
     for queryset in querysets:
         for instance in queryset:
             manager.collect(instance, action, operations=operations, route_cache=route_cache)
     if backends:
         result = []
         for operation in operations:
             if operation.backend in backends:
                 result.append(operation)
         operations = result
     if servers:
         routes = []
         result = []
         for operation in operations:
             routes = [route for route in operation.routes if route.host in servers]
             operation.routes = routes
             if routes:
                 result.append(operation)
         operations = result
     return operations
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     list_backends = options.get('list_backends')
     if list_backends:
         for backend in ServiceBackend.get_backends():
             self.stdout.write(str(backend).split("'")[1])
         return
     model = apps.get_model(*options['model'].split('.'))
     action = options.get('action')
     interactive = options.get('interactive')
     servers = options.get('servers')
     backends = options.get('backends')
     if (servers and not backends) or (not servers and backends):
         raise CommandError("--backends and --servers go in tandem.")
     dry = options.get('dry')
     kwargs = {}
     for comp in options.get('query', []):
         comps = iter(comp.split('='))
         for arg in comps:
             kwargs[arg] = next(comps).strip().rstrip(',')
     operations = OrderedSet()
     route_cache = {}
     queryset = model.objects.filter(**kwargs).order_by('id')
     if servers:
         servers = servers.split(',')
         backends = backends.split(',')
         server_objects = []
         # Get and create missing Servers
         for server in servers:
             try:
                 server = Server.objects.get(address=server)
             except Server.DoesNotExist:
                 server = Server(name=server, address=server)
                 server.full_clean()
                 server.save()
             server_objects.append(server)
         # Generate operations for the given backend
         for instance in queryset:
             for backend in backends:
                 backend = import_class(backend)
                 operations.add(Operation(backend, instance, action, servers=server_objects))
     else:
         for instance in queryset:
             manager.collect(instance, action, operations=operations, route_cache=route_cache)
     scripts, block = manager.generate(operations)
     servers = []
     # Print scripts
     for key, value in scripts.items():
         server, __ = key
         backend, operations = value
         servers.append(server.name)
         self.stdout.write('# Execute on %s' % server.name)
         for method, commands in backend.scripts:
             script = '\n'.join(commands)
             self.stdout.write(script)
     if interactive:
         context = {
             'servers': ', '.join(servers),
         }
         msg = ("\n\nAre your sure to execute the previous scripts on %(servers)s (yes/no)? " % context)
         confirm = input(msg)
         while 1:
             if confirm not in ('yes', 'no'):
                 confirm = input('Please enter either "yes" or "no": ')
                 continue
             if confirm == 'no':
                 return
             break
     if not dry:
         logs = manager.execute(scripts, block=block)
         for log in logs:
             self.stdout.write(log.stdout)
             self.stderr.write(log.stderr)
         for log in logs:
             self.stdout.write(' '.join((log.backend, log.state)))
Ejemplo n.º 3
0
 def handle(self, *args, **options):
     list_backends = options.get('list_backends')
     if list_backends:
         for backend in ServiceBackend.get_backends():
             self.stdout.write(str(backend).split("'")[1])
         return
     model = apps.get_model(*options['model'].split('.'))
     action = options.get('action')
     interactive = options.get('interactive')
     servers = options.get('servers')
     backends = options.get('backends')
     if (servers and not backends) or (not servers and backends):
         raise CommandError("--backends and --servers go in tandem.")
     dry = options.get('dry')
     kwargs = {}
     for comp in options.get('query', []):
         comps = iter(comp.split('='))
         for arg in comps:
             kwargs[arg] = next(comps).strip().rstrip(',')
     operations = OrderedSet()
     route_cache = {}
     queryset = model.objects.filter(**kwargs).order_by('id')
     if servers:
         servers = servers.split(',')
         backends = backends.split(',')
         routes = []
         # Get and create missing Servers
         for server in servers:
             try:
                 server = Server.objects.get(address=server)
             except Server.DoesNotExist:
                 server = Server(name=server, address=server)
                 server.full_clean()
                 server.save()
             routes.append(AttrDict(
                 host=server,
                 async=False,
                 action_is_async=lambda self: False,
             ))
         # Generate operations for the given backend
         for instance in queryset:
             for backend in backends:
                 backend = import_class(backend)
                 operations.add(Operation(backend, instance, action, routes=routes))
     else:
         for instance in queryset:
             manager.collect(instance, action, operations=operations, route_cache=route_cache)
     scripts, serialize = manager.generate(operations)
     servers = []
     # Print scripts
     for key, value in scripts.items():
         route, __, __ = key
         backend, operations = value
         servers.append(str(route.host))
         self.stdout.write('# Execute %s on %s' % (backend.get_name(), route.host))
         for method, commands in backend.scripts:
             script = '\n'.join(commands)
             self.stdout.write(script)
     if interactive:
         context = {
             'servers': ', '.join(servers),
         }
         if not confirm("\n\nAre your sure to execute the previous scripts on %(servers)s (yes/no)? " % context):
             return
     if not dry:
         logs = manager.execute(scripts, serialize=serialize, async=True)
         running = list(logs)
         stdout = 0
         stderr = 0
         while running:
             for log in running:
                 cstdout = len(log.stdout)
                 cstderr = len(log.stderr)
                 if cstdout > stdout:
                     self.stdout.write(log.stdout[stdout:])
                     stdout = cstdout
                 if cstderr > stderr:
                     self.stderr.write(log.stderr[stderr:])
                     stderr = cstderr
                 if log.has_finished:
                     running.remove(log)
                 time.sleep(0.05)
         for log in logs:
             self.stdout.write(' '.join((log.backend, log.state)))
Ejemplo n.º 4
0
    def collect_operations(self, **options):
        model = options.get('model')
        backends = options.get('backends') or set()
        if backends:
            backends = set(backends.split(','))
        servers = options.get('servers') or set()
        if servers:
            servers = set([
                Server.objects.get(Q(address=server) | Q(name=server))
                for server in servers.split(',')
            ])
        action = options.get('action')
        if not model:
            models = set()
            if servers:
                for server in servers:
                    if backends:
                        routes = server.routes.filter(backend__in=backends)
                    else:
                        routes = server.routes.all()
            elif backends:
                routes = Route.objects.filter(backend__in=backends)
            else:
                raise CommandError("Model or --servers or --backends?")
            for route in routes.filter(is_active=True):
                model = route.backend_class.model_class()
                models.add(model)
            querysets = [model.objects.order_by('id') for model in models]
        else:
            kwargs = {}
            for comp in options.get('query', []):
                comps = iter(comp.split('='))
                for arg in comps:
                    kwargs[arg] = next(comps).strip().rstrip(',')
            model = apps.get_model(*model.split('.'))
            queryset = model.objects.filter(**kwargs).order_by('id')
            querysets = [queryset]

        operations = OrderedSet()
        route_cache = {}
        for queryset in querysets:
            for instance in queryset:
                manager.collect(instance,
                                action,
                                operations=operations,
                                route_cache=route_cache)
        if backends:
            result = []
            for operation in operations:
                if operation.backend in backends:
                    result.append(operation)
            operations = result
        if servers:
            routes = []
            result = []
            for operation in operations:
                routes = [
                    route for route in operation.routes
                    if route.host in servers
                ]
                operation.routes = routes
                if routes:
                    result.append(operation)
            operations = result
        return operations