Exemplo n.º 1
0
 def pg_restore(self, params):
     CLI.info(f'Restoring database from file {params}')
     CLI.underline(
         "Don't forget to drop database at first to prevent constraints collisions!"
     )
     env = self.load_environment()
     os.system(
         f'docker {self.docker_ssh} exec -it {self.CONTAINER_DB} bash -c \'pg_restore -h {env["POSTGRES_HOST"]} -U {env["POSTGRES_USER"]} -d {env["POSTGRES_DBNAME"]} -W < /backups/{params}\''
     )
Exemplo n.º 2
0
    def reload(self):  # todo deploy swarm
        CLI.info('Reloading containers...')
        zero_downtime_services = self.config['containers']['deploy'][
            'zero_downtime']
        restart_services = self.config['containers']['deploy']['restart']

        steps = 5

        step = 1
        CLI.step(step, steps,
                 f'Zero downtime services: {zero_downtime_services}')

        for service in zero_downtime_services:
            container = self.get_container_name(service)
            os.system(
                f'docker-compose {self.docker_ssh} -f {self.configs_path}/docker/{self.COMPOSE_PREFIX}.yml -f {self.configs_path}/docker/{self.COMPOSE_PREFIX}.{self.environment_id}.yml --project-name={self.PROJECT_NAME} run -d --service-ports --name={container}_new {service}'
            )
            CLI.info(f'Renaming old container [{container}_old]...')

            if container in self.get_containers():
                os.system(
                    f'docker {self.docker_ssh} container rename {container} {container}_old'
                )
            else:
                CLI.info(f'{container}_old was not running')

            CLI.info(f'Renaming new container [{container}]...')
            os.system(
                f'docker {self.docker_ssh} container rename {container}_new {container}'
            )

        step += 1
        CLI.step(step, steps, 'Collecting static files')
        os.system(
            f'docker {self.docker_ssh} exec -i {self.CONTAINER_APP} python manage.py collectstatic --noinput --verbosity 0'
        )

        step += 1
        CLI.step(step, steps, 'Reloading webserver...')
        os.system(
            f'docker {self.docker_ssh} exec -it {self.CONTAINER_WEBSERVER} {self.WEBSERVER} -s reload'
        )

        step += 1
        CLI.step(
            step, steps,
            f'Stopping old zero downtime services: {zero_downtime_services}')

        for service in zero_downtime_services:
            container = self.get_container_name(service)

            if container in self.get_containers():
                CLI.info(f'Stopping old container [{container}_old]...')
                os.system(
                    f'docker {self.docker_ssh} container stop {container}_old')

                CLI.info(f'Removing old container [{container}_old]...')
                os.system(
                    f'docker {self.docker_ssh} container rm {container}_old')
            else:
                CLI.info(f'{container}_old was not running')

        step += 1
        CLI.step(step, steps, f'Restart services: {restart_services}')

        for service in restart_services:
            container = self.get_container_name(service)

            CLI.underline(f'Recreating {service} container ({container})...')

            if container in self.get_containers():
                CLI.info(f'Stopping container [{container}]...')
                os.system(
                    f'docker {self.docker_ssh} container stop {container}')

                CLI.info(f'Removing container [{container}]...')
                os.system(f'docker {self.docker_ssh} container rm {container}')

                CLI.info(f'Creating new container [{container}]...')
                os.system(
                    f'docker-compose {self.docker_ssh} -f {self.configs_path}/docker/{self.COMPOSE_PREFIX}.yml -f {self.configs_path}/docker/{self.COMPOSE_PREFIX}.{self.environment_id}.yml --project-name={self.PROJECT_NAME} run -d --service-ports --name={container} {service}'
                )
            else:
                CLI.info(f'{container} was not running')