Example #1
0
    def do_POST_restart(self):
        status_code = 500
        data = 'restart failed'
        request = self._read_json_content(body_is_optional=True)
        cluster = self.server.patroni.dcs.get_cluster()
        if request is None:
            # failed to parse the json
            return
        if request:
            logger.debug("received restart request: {0}".format(request))

        if cluster.is_paused() and 'schedule' in request:
            self._write_response(status_code,
                                 "Can't schedule restart in the paused state")
            return

        for k in request:
            if k == 'schedule':
                (_, data,
                 request[k]) = self.parse_schedule(request[k], "restart")
                if _:
                    status_code = _
                    break
            elif k == 'role':
                if request[k] not in ('master', 'replica'):
                    status_code = 400
                    data = "PostgreSQL role should be either master or replica"
                    break
            elif k == 'postgres_version':
                if not is_valid_pg_version(request[k]):
                    status_code = 400
                    data = "PostgreSQL version should be in the first.major.minor format"
                    break
            elif k == 'timeout':
                request[k] = parse_int(request[k], 's')
                if request[k] is None or request[k] <= 0:
                    status_code = 400
                    data = "Timeout should be a positive number of seconds"
                    break
            elif k != 'restart_pending':
                status_code = 400
                data = "Unknown filter for the scheduled restart: {0}".format(
                    k)
                break
        else:
            if 'schedule' not in request:
                try:
                    status, data = self.server.patroni.ha.restart(request)
                    status_code = 200 if status else 503
                except Exception:
                    logger.exception('Exception during restart')
                    status_code = 400
            else:
                if self.server.patroni.ha.schedule_future_restart(request):
                    data = "Restart scheduled"
                    status_code = 202
                else:
                    data = "Another restart is already scheduled"
                    status_code = 409
        self._write_response(status_code, data)
Example #2
0
    def do_POST_restart(self):
        status_code = 500
        data = 'restart failed'
        request = self._read_json_content(body_is_optional=True)
        cluster = self.server.patroni.dcs.get_cluster()
        if request is None:
            # failed to parse the json
            return
        if request:
            logger.debug("received restart request: {0}".format(request))

        if cluster.is_paused() and 'schedule' in request:
            self._write_response(status_code, "Can't schedule restart in the paused state")
            return

        for k in request:
            if k == 'schedule':
                (_, data, request[k]) = self.parse_schedule(request[k], "restart")
                if _:
                    status_code = _
                    break
            elif k == 'role':
                if request[k] not in ('master', 'replica'):
                    status_code = 400
                    data = "PostgreSQL role should be either master or replica"
                    break
            elif k == 'postgres_version':
                if not is_valid_pg_version(request[k]):
                    status_code = 400
                    data = "PostgreSQL version should be in the first.major.minor format"
                    break
            elif k == 'timeout':
                request[k] = parse_int(request[k], 's')
                if request[k] is None or request[k] <= 0:
                    status_code = 400
                    data = "Timeout should be a positive number of seconds"
                    break
            elif k != 'restart_pending':
                status_code = 400
                data = "Unknown filter for the scheduled restart: {0}".format(k)
                break
        else:
            if 'schedule' not in request:
                try:
                    status, data = self.server.patroni.ha.restart(request)
                    status_code = 200 if status else 503
                except Exception:
                    logger.exception('Exception during restart')
                    status_code = 400
            else:
                if self.server.patroni.ha.schedule_future_restart(request):
                    data = "Restart scheduled"
                    status_code = 202
                else:
                    data = "Another restart is already scheduled"
                    status_code = 409
        self._write_response(status_code, data)
Example #3
0
def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, version, pending, timeout):
    cluster = get_dcs(obj, cluster_name).get_cluster()

    members = get_members(cluster, cluster_name, member_names, role, force, 'restart')
    if p_any:
        random.shuffle(members)
        members = members[:1]

    if version is None and not force:
        version = click.prompt('Restart if the PostgreSQL version is less than provided (e.g. 9.5.2) ',
                               type=str, default='')

    content = {}
    if pending:
        content['restart_pending'] = True

    if version:
        if not is_valid_pg_version(version):
            message = 'PostgreSQL version should be in the first.major.minor format'
            raise PatroniCtlException(message)
        else:
            content['postgres_version'] = version

    if scheduled is None and not force:
        scheduled = click.prompt('When should the restart take place (e.g. 2015-10-01T14:30) ', type=str, default='now')

    scheduled_at = parse_scheduled(scheduled)
    if scheduled_at:
        if cluster.is_paused():
            raise PatroniCtlException("Can't schedule restart in the paused state")
        content['schedule'] = scheduled_at.isoformat()

    if timeout is not None:
        content['timeout'] = timeout

    for member in members:
        if 'schedule' in content:
            if force and member.data.get('scheduled_restart'):
                r = request_patroni(member, 'delete', 'restart', headers=auth_header(obj))
                check_response(r, member.name, 'flush scheduled restart', True)

        r = request_patroni(member, 'post', 'restart', content, auth_header(obj))
        if r.status_code == 200:
            click.echo('Success: restart on member {0}'.format(member.name))
        elif r.status_code == 202:
            click.echo('Success: restart scheduled on member {0}'.format(member.name))
        elif r.status_code == 409:
            click.echo('Failed: another restart is already scheduled on member {0}'.format(member.name))
        else:
            click.echo('Failed: restart for member {0}, status code={1}, ({2})'.format(
                member.name, r.status_code, r.text)
            )
Example #4
0
 def do_POST_restart(self):
     status_code = 500
     data = 'restart failed'
     request = self._read_json_content(body_is_optional=True)
     if request is None:
         # failed to parse the json
         return
     if request:
         logger.debug("received restart request: {0}".format(request))
     for k in request:
         if k == 'schedule':
             (_, data,
              request[k]) = self.parse_schedule(request[k], "restart")
             if _:
                 status_code = _
                 break
         elif k == 'role':
             if request[k] not in ('master', 'replica'):
                 status_code = 400
                 data = "PostgreSQL role should be either master or replica"
                 break
         elif k == 'postgres_version':
             if not is_valid_pg_version(request[k]):
                 status_code = 400
                 data = "PostgreSQL version should be in the first.major.minor format"
                 break
         elif k != 'restart_pending':
             status_code = 400
             data = "Unknown filter for the scheduled restart: {0}".format(
                 k)
             break
     else:
         if 'schedule' not in request:
             try:
                 status, data = self.server.patroni.ha.restart(request)
                 status_code = 200 if status else 503
             except Exception:
                 logger.exception('Exception during restart')
                 status_code = 400
         else:
             request[
                 'postmaster_start_time'] = self.server.patroni.ha.state_handler.postmaster_start_time(
                 )
             if self.server.patroni.ha.schedule_future_restart(request):
                 data = "Restart scheduled"
                 status_code = 202
             else:
                 data = "Another restart is already scheduled"
                 status_code = 409
     self._write_response(status_code, data)
Example #5
0
def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, version, pending):
    cluster = get_dcs(obj, cluster_name).get_cluster()

    members = get_members(cluster, cluster_name, member_names, role, force, 'restart')
    if p_any:
        random.shuffle(members)
        members = members[:1]

    if version is None and not force:
        version = click.prompt('Restart if the PostgreSQL version is less than provided (e.g. 9.5.2) ',
                               type=str, default='')

    content = {}
    if pending:
        content['restart_pending'] = True

    if version:
        if not is_valid_pg_version(version):
            message = 'PostgreSQL version should be in the first.major.minor format'
            raise PatroniCtlException(message)
        else:
            content['postgres_version'] = version

    if scheduled is None and not force:
        scheduled = click.prompt('When should the restart take place (e.g. 2015-10-01T14:30) ', type=str, default='now')

    scheduled_at = parse_scheduled(scheduled)
    if scheduled_at:
        if cluster.is_paused():
            raise PatroniCtlException("Can't schedule restart in the paused state")
        content['schedule'] = scheduled_at.isoformat()

    for member in members:
        if 'schedule' in content:
            if force and member.data.get('scheduled_restart'):
                r = request_patroni(member, 'delete', 'restart', headers=auth_header(obj))
                check_response(r, member.name, 'flush scheduled restart', True)

        r = request_patroni(member, 'post', 'restart', content, auth_header(obj))
        if r.status_code == 200:
            click.echo('Success: restart on member {0}'.format(member.name))
        elif r.status_code == 202:
            click.echo('Success: restart scheduled on member {0}'.format(member.name))
        elif r.status_code == 409:
            click.echo('Failed: another restart is already scheduled on member {0}'.format(member.name))
        else:
            click.echo('Failed: restart for member {0}, status code={1}, ({2})'.format(
                member.name, r.status_code, r.text)
            )
Example #6
0
 def do_POST_restart(self):
     status_code = 500
     data = 'restart failed'
     request = self._read_json_content(body_is_optional=True)
     if request is None:
         # failed to parse the json
         return
     if request:
         logger.debug("received restart request: {0}".format(request))
     for k in request:
         if k == 'schedule':
             (_, data, request[k]) = self.parse_schedule(request[k], "restart")
             if _:
                 status_code = _
                 break
         elif k == 'role':
             if request[k] not in ('master', 'replica'):
                 status_code = 400
                 data = "PostgreSQL role should be either master or replica"
                 break
         elif k == 'postgres_version':
             if not is_valid_pg_version(request[k]):
                 status_code = 400
                 data = "PostgreSQL version should be in the first.major.minor format"
                 break
         elif k != 'restart_pending':
             status_code = 400
             data = "Unknown filter for the scheduled restart: {0}".format(k)
             break
     else:
         if 'schedule' not in request:
             try:
                 status, data = self.server.patroni.ha.restart(request)
                 status_code = 200 if status else 503
             except Exception:
                 logger.exception('Exception during restart')
                 status_code = 400
         else:
             request['postmaster_start_time'] = self.server.patroni.ha.state_handler.postmaster_start_time()
             if self.server.patroni.ha.schedule_future_restart(request):
                 data = "Restart scheduled"
                 status_code = 202
             else:
                 data = "Another restart is already scheduled"
                 status_code = 409
     self._write_response(status_code, data)