Esempio n. 1
0
    def migrate_back(self):
        migrations_command = (
            'python manage.py showmigrations --plan '  # execute plan
            '| egrep \'^\\[X\\]\' '  # show only applied migrations
            '| awk \'{print $2}\' '  # take migration name
            '&& test ${PIPESTATUS[0]} -eq 0'  # fail if couldn't execute plan
        )
        image = self.image
        options = self.safe_options

        with utils.patch(self, 'info', self.info, force_delete=True):
            current_migrations = image.run(
                migrations_command,
                options=options,
            )
            backup = self.get_backup_version()
            with utils.patch(backup, 'info', backup.info, force_delete=True):
                backup_migrations = backup.image.run(
                    migrations_command,
                    options=options,
                )

                for migration in self.get_revert_migrations(
                        current_migrations,
                        backup_migrations,
                ):
                    command = (
                        'python manage.py migrate --no-input {app} {migration}'
                    ).format(
                        app=migration.app,
                        migration=migration.name,
                    )
                    image.run(command, quiet=False, options=options)

                self._migrate(backup.image, options)
Esempio n. 2
0
    def migrate_back(self):
        migrations_command = (
            'python manage.py showmigrations --plan '  # execute plan
            '| egrep \'^\\[X\\]\' '  # show only applied migrations
            '| awk \'{print $2}\' '  # take migration name
            '&& test ${PIPESTATUS[0]} -eq 0'  # fail if couldn't execute plan
        )
        image = self.image
        options = self.safe_options

        with utils.patch(self, 'info', self.info, force_delete=True):
            current_migrations = image.run(
                migrations_command,
                options=options,
            )
            backup = self.get_backup_version()
            with utils.patch(backup, 'info', backup.info, force_delete=True):
                backup_migrations = backup.image.run(
                    migrations_command,
                    options=options,
                )

                for migration in self.get_revert_migrations(
                    current_migrations,
                    backup_migrations,
                ):
                    command = (
                        'python manage.py migrate --no-input {app} {migration}'
                    ).format(
                        app=migration.app,
                        migration=migration.name,
                    )
                    image.run(command, quiet=False, options=options)

                self._migrate(backup.image, options)
Esempio n. 3
0
    def pull(self, tag=None):
        """
        pull Docker image from registry
        """
        if self.ssh_tunnel_port:
            if self.registry:
                local_port = self.registry.port
                local_host = self.registry.host
            elif self.image.registry:
                local_port = self.image.registry.port
                local_host = self.image.registry.host
            else:
                raise ValueError(
                    'Either local host or local port for SSH tunnel '
                    'can not be obtained'
                )
            with contextlib.closing(open(os.devnull, 'w')) as output:
                with patch(sys, 'stdout', output):
                    # forward sys.stdout to os.devnull to prevent
                    # printing debug messages by fab.remote_tunnel

                    with fab.remote_tunnel(
                        remote_port=self.ssh_tunnel_port,
                        local_port=local_port,
                        local_host=local_host,
                    ):
                        registry = 'localhost:{0}'.format(self.ssh_tunnel_port)
                        self.pull_image(tag=tag, registry=registry)
        else:
            self.pull_image(tag=tag, registry=self.registry)
Esempio n. 4
0
 def __details__(self):
     with utils.patch(
         six.get_method_function(self.tasks.service.destroy),
         '__doc__',
         self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
     ):
         return get_task_details(self.tasks.service.destroy)
Esempio n. 5
0
    def _update(self, image, force=False):
        image = image.digest
        try:
            service_info = self.info
        except ServiceNotFoundError:
            service_info = {}

        with utils.patch(self, 'info', service_info, force_delete=True):
            labels = service_info.get('Spec', {}).get('Labels', {})
            current_options = labels.pop(self.options_label_name, None)
            new_options = self._encode_options(dict(
                self.options,
                image=image,
                args=self.cmd,
            ))

            if force or current_options != new_options:
                label_with_new_options = {
                    self.options_label_name: new_options,
                }
                self._update_labels(label_with_new_options)

                if service_info:
                    options = utils.Options(self.update_options, image=image)
                    self._update_service(options)
                else:
                    self._create_service(image)

                return True
        return False
Esempio n. 6
0
    def remote_tunnel(self):
        if self.ssh_tunnel_port:
            if self.registry:
                local_port = self.registry.port
                local_host = self.registry.host
            elif self.image.registry:
                local_port = self.image.registry.port
                local_host = self.image.registry.host
            else:
                raise ValueError(
                    'Either local host or local port for SSH tunnel '
                    'can not be obtained')
            with contextlib.closing(open(os.devnull, 'w')) as output:
                with utils.patch(sys, 'stdout', output):
                    # forward sys.stdout to os.devnull to prevent
                    # printing debug messages by fab.remote_tunnel

                    with fab.remote_tunnel(
                            remote_port=self.host_registry.port,
                            local_port=local_port,
                            local_host=local_host,
                    ):
                        yield
        else:
            yield
Esempio n. 7
0
    def _update(self, image, force=False):
        image = image.digest
        try:
            service_info = self.info
        except ServiceNotFoundError:
            service_info = {}

        with utils.patch(self, 'info', service_info, force_delete=True):
            labels = service_info.get('Spec', {}).get('Labels', {})
            current_options = labels.pop(self.options_label_name, None)
            new_options = self._encode_options(
                dict(
                    self.options,
                    image=image,
                    args=self.cmd,
                ))

            if force or current_options != new_options:
                label_with_new_options = {
                    self.options_label_name: new_options,
                }
                self._update_labels(label_with_new_options)

                if service_info:
                    options = utils.Options(self.update_options, image=image)
                    self._update_service(options)
                else:
                    self._create_service(image)

                return True
        return False
Esempio n. 8
0
 def __details__(self):
     with utils.patch(
             six.get_method_function(self.tasks.service.destroy),
             '__doc__',
             self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
     ):
         return get_task_details(self.tasks.service.destroy)
Esempio n. 9
0
def execute(*args, **kwargs):
    try:
        task, args = args[0], args[1:]
    except IndexError:
        raise TypeError('must provide task to execute')
    default_name = '{command}.{task_name}({id})'.format(
        command=fab.env.command,
        task_name=getattr(task, 'name', task.__name__),
        id=id(task),
    )
    with utils.patch(task, 'name', _uncrawl(task) or default_name):
        return fab.execute(task, *args, **kwargs)
Esempio n. 10
0
 def default(self, *args, **kwargs):
     """
     delete service
     """
     with utils.patch(fab.env, 'parallel', False):
         message = 'Are you sure you want to destroy {name} service?'
         if not console.confirm(
             message.format(name=colors.red(self.tasks.service.name)),
             default=False,
         ):
             fab.abort('Aborted')
     return self.confirm(*args, **kwargs)
Esempio n. 11
0
 def default(self, *args, **kwargs):
     """
     select {title} infrastructure
     """
     with utils.patch(fab.env, 'parallel', False):
         if not console.confirm(
             'Are you sure you want to select {title} '
             'infrastructure to run task(s) on?'.format(title=self.title),
             default=False,
         ):
             fab.abort('Aborted')
     return self.confirm(*args, **kwargs)
Esempio n. 12
0
 def default(self, *args, **kwargs):
     """
     delete service
     """
     with utils.patch(fab.env, 'parallel', False):
         message = 'Are you sure you want to destroy {name} service?'
         if not console.confirm(
                 message.format(
                     name=colors.red(self.tasks.service.name)),
                 default=False,
         ):
             fab.abort('Aborted')
     return self.confirm(*args, **kwargs)
Esempio n. 13
0
 def default(self, *args, **kwargs):
     """
     select {title} infrastructure
     """
     with utils.patch(fab.env, 'parallel', False):
         if not console.confirm(
                 'Are you sure you want to select {title} '
                 'infrastructure to run task(s) on?'.format(
                     title=self.title),
                 default=False,
         ):
             fab.abort('Aborted')
     return self.confirm(*args, **kwargs)
Esempio n. 14
0
 def remote_tunnel(self):
     with contextlib.ExitStack() as stack:
         if self.ssh_tunnel:
             output = stack.enter_context(
                 contextlib.closing(open(os.devnull, 'w')))  # noqa
             # forward sys.stdout to os.devnull to prevent
             # printing debug messages by fab.remote_tunnel
             stack.enter_context(utils.patch(sys, 'stdout', output))
             stack.enter_context(
                 fab.remote_tunnel(
                     remote_bind_address=self.ssh_tunnel.bind_address,
                     remote_port=self.ssh_tunnel.port,
                     local_host=self.ssh_tunnel.host,
                     local_port=self.ssh_tunnel.host_port,
                 ))
         yield
Esempio n. 15
0
 def remote_tunnel(self):
     devnull = open(os.devnull, 'w')
     stack = self.ssh_tunnel and [
         contextlib.closing(devnull),
         # forward sys.stdout to os.devnull to prevent
         # printing debug messages by fab.remote_tunnel
         utils.patch(sys, 'stdout', devnull),
         fab.remote_tunnel(
             remote_bind_address=self.ssh_tunnel.bind_address,
             remote_port=self.ssh_tunnel.port,
             local_host=self.ssh_tunnel.host,
             local_port=self.ssh_tunnel.host_port,
         ),
     ] or []
     with nested(*stack):
         yield
Esempio n. 16
0
 def remote_tunnel(self):
     devnull = open(os.devnull, 'w')
     stack = self.ssh_tunnel and [
         contextlib.closing(devnull),
         # forward sys.stdout to os.devnull to prevent
         # printing debug messages by fab.remote_tunnel
         utils.patch(sys, 'stdout', devnull),
         fab.remote_tunnel(
             remote_bind_address=self.ssh_tunnel.bind_address,
             remote_port=self.ssh_tunnel.port,
             local_host=self.ssh_tunnel.host,
             local_port=self.ssh_tunnel.host_port,
         ),
     ] or []
     with nested(*stack):
         yield
Esempio n. 17
0
    def pull(self, tag=None):
        """
        pull Docker image from registry
        """
        if self.tunnel_required:
            with contextlib.closing(open(os.devnull, 'w')) as output:
                with utils.patch(sys, 'stdout', output):
                    # forward sys.stdout to os.devnull to prevent
                    # printing debug messages by fab.remote_tunnel

                    with fab.remote_tunnel(
                            remote_port=self.registry.port,
                            local_port=self.local_registry.port,
                            local_host=self.local_registry.host,
                    ):
                        _DockerTasks.pull(self, tag=tag)
        else:
            _DockerTasks.pull(self, tag=tag)
Esempio n. 18
0
def execute(*args, **kwargs):  # pragma: no cover
    warnings.warn(
        'fabricio.execute() is deprecated in favour of fabric.api.execute()',
        DeprecationWarning,
    )
    warnings.warn(
        'fabricio.execute() is deprecated and will be removed in v0.6, '
        'use fabric.api.execute() instead',
        RuntimeWarning, stacklevel=2,
    )
    try:
        task, args = args[0], args[1:]
    except IndexError:
        raise TypeError('must provide task to execute')
    default_name = '{command}.{task_name}({id})'.format(
        command=fab.env.command,
        task_name=getattr(task, 'name', task.__name__),
        id=id(task),
    )
    with utils.patch(task, 'name', get_task_name(task) or default_name):
        return fab.execute(task, *args, **kwargs)
Esempio n. 19
0
def execute(*args, **kwargs):  # pragma: no cover
    warnings.warn(
        'fabricio.execute() is deprecated in favour of fabric.api.execute()',
        DeprecationWarning,
    )
    warnings.warn(
        'fabricio.execute() is deprecated and will be removed in v0.6, '
        'use fabric.api.execute() instead',
        RuntimeWarning,
        stacklevel=2,
    )
    try:
        task, args = args[0], args[1:]
    except IndexError:
        raise TypeError('must provide task to execute')
    default_name = '{command}.{task_name}({id})'.format(
        command=fab.env.command,
        task_name=getattr(task, 'name', task.__name__),
        id=id(task),
    )
    with utils.patch(task, 'name', get_task_name(task) or default_name):
        return fab.execute(task, *args, **kwargs)
Esempio n. 20
0
def log(message, color=colors.yellow, output=sys.stdout):
    with utils.patch(sys, 'stdout', output):
        fab.puts(color(message))
Esempio n. 21
0
def log(message, color=colors.yellow, output=sys.stdout):
    with utils.patch(sys, 'stdout', output):
        fab.puts(color(message))
Esempio n. 22
0
 def options(self):
     with utils.patch(self, 'config', os.path.basename(self.config)):
         return super(Stack, self).options
Esempio n. 23
0
 def __details__(self):
     doc = self.__doc__ + (self.callback.__doc__ or '')
     with utils.patch(self.callback, '__doc__', doc):
         return get_task_details(self.callback)
Esempio n. 24
0
 def __details__(self):
     doc = self.__doc__ + (self.callback.__doc__ or '')
     with utils.patch(self.callback, '__doc__', doc):
         return get_task_details(self.callback)