def _run_command(self, cmd, warn_only=False, use_sudo=True):
     self.f.write('{}\n'.format(cmd))
     with settings(warn_only=warn_only):
         if use_sudo:
             output = sudo(cmd, stdout=self.f)
         else:
             output = run(cmd, stdout=self.f)
         if output.failed:
             self.succeeded = False
             if warn_only is False:
                 self.host_deploy = HostDeploy.update(self.host_deploy.uuid, succeeded=self.succeeded)
                 raise Exception("Deploying failed on command {}".format(cmd))
     return output.stdout
    def deploy_instance(self, deploying):
        if self.service.is_database:
            return self.deploy_database(deploying)
        else:
            self.deploying = deploying

            self.host_deploy = HostDeploy.get_or_create(deploy_uuid=self.deploying.uuid, host_uuid=self.host.uuid)
            self.f.write('Deploying to {}@{} with key {}.\n'.format(env.user, env.host_string, env.key_filename[0]))

            self._update_instance_files()
            self._pull_new_container()
            self._kill_running_containers()
            self._start_container()

            self.host_deploy = HostDeploy.update(self.host_deploy.uuid, succeeded=self.succeeded)
            return self.succeeded
    def deploy_database(self, deploying):
        self.deploying = deploying

        self.host_deploy = HostDeploy.get_or_create(deploy_uuid=self.deploying.uuid, host_uuid=self.host.uuid)
        self.f.write('Deploying database to {}@{} with key {}.\n'.format(env.user, env.host_string, env.key_filename[0]))

        self._pull_new_container()
        self._kill_container(self.deploying.docker_tag)
        self.delete_container(self.deploying.docker_tag)
        # check for data volumes
        if not self._check_for_data_volume():
            self._create_data_volume()
        self._start_container()
        # restart container?
        self._kill_container(self.deploying.docker_tag)
        self._restart_container()

        self.host_deploy = HostDeploy.update(self.host_deploy.uuid, succeeded=self.succeeded)
        return self.succeeded
    def restart_deploy(self, deploying, hard_restart=False):
        self.deploying = deploying
        self.host_deploy = HostDeploy.get_or_create(deploy_uuid=self.deploying.uuid, host_uuid=self.host.uuid)
        self.f.write('Restarting container for service {} (hard_restart = {}) on'
                     '{}@{} with key {}.\n'.format(self.service.name,
                                                   hard_restart,
                                                   env.user,
                                                   env.host_string,
                                                   env.key_filename[0]))

        self._kill_running_containers()
        if hard_restart:
            self.f.write('Updating deploy files for service {}.\n'.format(self.service.name))
            self._update_instance_files()
            self.delete_container(self.deploying.docker_tag)
            self._start_container()
        else:
            self._restart_container()

        self.host_deploy = HostDeploy.update(self.host_deploy.uuid, succeeded=self.succeeded)
        return self.succeeded