Esempio n. 1
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
     interactive = options.get('interactive')
     dry = options.get('dry')
     operations = self.collect_operations(**options)
     scripts, serialize = manager.generate(operations)
     servers = set()
     # Print scripts
     for key, value in scripts.items():
         route, __, __ = key
         backend, operations = value
         servers.add(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.encode('ascii', errors='replace').decode())
     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,
                                run_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)))
Esempio 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
     interactive = options.get('interactive')
     dry = options.get('dry')
     operations = self.collect_operations(**options)
     scripts, serialize = manager.generate(operations)
     servers = set()
     # Print scripts
     for key, value in scripts.items():
         route, __, __ = key
         backend, operations = value
         servers.add(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.encode('ascii', errors='replace').decode())
     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)))
Esempio 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(',')
         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)))
Esempio n. 4
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)))