예제 #1
0
def upload_keepalived_config():
    stream = listener.Wrapped(flask.request.stream)

    if not os.path.exists(util.keepalived_dir()):
        os.makedirs(util.keepalived_dir())
        os.makedirs(util.keepalived_check_scripts_dir())

    conf_file = util.keepalived_cfg_path()
    with open(conf_file, 'w') as f:
        b = stream.read(BUFFER)
        while b:
            f.write(b)
            b = stream.read(BUFFER)

    if not os.path.exists(util.keepalived_init_path()):
        with open(util.keepalived_init_path(), 'w') as text_file:
            text = template.render(
                keepalived_pid=util.keepalived_pid_path(),
                keepalived_cmd=consts.KEEPALIVED_CMD,
                keepalived_cfg=util.keepalived_cfg_path(),
                keepalived_log=util.keepalived_log_path()
            )
            text_file.write(text)
        cmd = "chmod +x {file}".format(file=util.keepalived_init_path())
        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to upload keepalived configuration. "
                      "Unable to chmod init script.")
            return flask.make_response(flask.jsonify(dict(
                message="Failed to upload keepalived configuration.  "
                        "Unable to chmod init script.",
                details=e.output)), 500)
        # Renders the Keepalived check script
        with open(util.keepalived_check_script_path(), 'w') as text_file:
            text = check_script_template.render(
                check_scripts_dir=util.keepalived_check_scripts_dir()
            )
            text_file.write(text)
        cmd = ("chmod +x {file}".format(
            file=util.keepalived_check_script_path()))
        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to upload keepalived configuration. "
                      "Unable to chmod check script.")
            return flask.make_response(flask.jsonify(dict(
                message="Failed to upload keepalived configuration. "
                        "Unable to chmod check script.",
                details=e.output)), 500)

    res = flask.make_response(flask.jsonify({
        'message': 'OK'}), 200)
    res.headers['ETag'] = stream.get_md5()

    return res
예제 #2
0
def upload_keepalived_config():
    stream = listener.Wrapped(flask.request.stream)

    if not os.path.exists(util.keepalived_dir()):
        os.makedirs(util.keepalived_dir())
        os.makedirs(util.keepalived_check_scripts_dir())

    conf_file = util.keepalived_cfg_path()
    with open(conf_file, 'w') as f:
        b = stream.read(BUFFER)
        while b:
            f.write(b)
            b = stream.read(BUFFER)

    if not os.path.exists(util.keepalived_init_path()):
        with open(util.keepalived_init_path(), 'w') as text_file:
            text = template.render(keepalived_pid=util.keepalived_pid_path(),
                                   keepalived_cmd=consts.KEEPALIVED_CMD,
                                   keepalived_cfg=util.keepalived_cfg_path(),
                                   keepalived_log=util.keepalived_log_path())
            text_file.write(text)
        cmd = "chmod +x {file}".format(file=util.keepalived_init_path())
        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to upload keepalived configuration. "
                      "Unable to chmod init script.")
            return flask.make_response(
                flask.jsonify(
                    dict(message="Failed to upload keepalived configuration.  "
                         "Unable to chmod init script.",
                         details=e.output)), 500)
        # Renders the Keepalived check script
        with open(util.keepalived_check_script_path(), 'w') as text_file:
            text = check_script_template.render(
                check_scripts_dir=util.keepalived_check_scripts_dir())
            text_file.write(text)
        cmd = ("chmod +x {file}".format(
            file=util.keepalived_check_script_path()))
        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to upload keepalived configuration. "
                      "Unable to chmod check script.")
            return flask.make_response(
                flask.jsonify(
                    dict(message="Failed to upload keepalived configuration. "
                         "Unable to chmod check script.",
                         details=e.output)), 500)

    res = flask.make_response(flask.jsonify({'message': 'OK'}), 200)
    res.headers['ETag'] = stream.get_md5()

    return res
예제 #3
0
    def vrrp_check_script_update(self, listener_id, action):
        listener_ids = util.get_listeners()
        if action == consts.AMP_ACTION_STOP:
            listener_ids.remove(listener_id)
        args = []
        for lid in listener_ids:
            args.append(util.haproxy_sock_path(lid))

        if not os.path.exists(util.keepalived_dir()):
            os.makedirs(util.keepalived_dir())
            os.makedirs(util.keepalived_check_scripts_dir())

        cmd = 'haproxy-vrrp-check {args}; exit $?'.format(args=' '.join(args))
        with open(util.haproxy_check_script_path(), 'w') as text_file:
            text_file.write(cmd)
예제 #4
0
    def vrrp_check_script_update(self, listener_id, action):
        listener_ids = util.get_listeners()
        if action == consts.AMP_ACTION_STOP:
            listener_ids.remove(listener_id)
        args = []
        for lid in listener_ids:
            args.append(util.haproxy_sock_path(lid))

        if not os.path.exists(util.keepalived_dir()):
            os.makedirs(util.keepalived_dir())
            os.makedirs(util.keepalived_check_scripts_dir())

        cmd = 'haproxy-vrrp-check {args}; exit $?'.format(args=' '.join(args))
        with open(util.haproxy_check_script_path(), 'w') as text_file:
            text_file.write(cmd)
예제 #5
0
def upload_keepalived_config():
    stream = listener.Wrapped(flask.request.stream)

    if not os.path.exists(util.keepalived_dir()):
        os.makedirs(util.keepalived_dir())
        os.makedirs(util.keepalived_check_scripts_dir())

    conf_file = util.keepalived_cfg_path()
    flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
    # mode 00644
    mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
    with os.fdopen(os.open(conf_file, flags, mode), 'w') as f:
        b = stream.read(BUFFER)
        while b:
            f.write(b)
            b = stream.read(BUFFER)

    file_path = util.keepalived_init_path()
    # mode 00755
    mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
            stat.S_IROTH | stat.S_IXOTH)
    if not os.path.exists(file_path):
        with os.fdopen(os.open(file_path, flags, mode), 'w') as text_file:
            text = template.render(
                keepalived_pid=util.keepalived_pid_path(),
                keepalived_cmd=consts.KEEPALIVED_CMD,
                keepalived_cfg=util.keepalived_cfg_path(),
                keepalived_log=util.keepalived_log_path(),
                amphora_nsname=consts.AMPHORA_NAMESPACE
            )
            text_file.write(text)

        # Renders the Keepalived check script
        keepalived_path = util.keepalived_check_script_path()
        open_obj = os.open(keepalived_path, flags, mode)
        with os.fdopen(open_obj, 'w') as text_file:
            text = check_script_template.render(
                check_scripts_dir=util.keepalived_check_scripts_dir()
            )
            text_file.write(text)

    res = flask.make_response(flask.jsonify({
        'message': 'OK'}), 200)
    res.headers['ETag'] = stream.get_md5()

    return res
예제 #6
0
def upload_keepalived_config():
    stream = listener.Wrapped(flask.request.stream)

    if not os.path.exists(util.keepalived_dir()):
        os.makedirs(util.keepalived_dir())
        os.makedirs(util.keepalived_check_scripts_dir())

    conf_file = util.keepalived_cfg_path()
    flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
    # mode 00644
    mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
    with os.fdopen(os.open(conf_file, flags, mode), 'w') as f:
        b = stream.read(BUFFER)
        while b:
            f.write(b)
            b = stream.read(BUFFER)

    file_path = util.keepalived_init_path()
    # mode 00755
    mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH
            | stat.S_IXOTH)
    if not os.path.exists(file_path):
        with os.fdopen(os.open(file_path, flags, mode), 'w') as text_file:
            text = template.render(keepalived_pid=util.keepalived_pid_path(),
                                   keepalived_cmd=consts.KEEPALIVED_CMD,
                                   keepalived_cfg=util.keepalived_cfg_path(),
                                   keepalived_log=util.keepalived_log_path(),
                                   amphora_nsname=consts.AMPHORA_NAMESPACE)
            text_file.write(text)

        # Renders the Keepalived check script
        keepalived_path = util.keepalived_check_script_path()
        open_obj = os.open(keepalived_path, flags, mode)
        with os.fdopen(open_obj, 'w') as text_file:
            text = check_script_template.render(
                check_scripts_dir=util.keepalived_check_scripts_dir())
            text_file.write(text)

    res = flask.make_response(flask.jsonify({'message': 'OK'}), 200)
    res.headers['ETag'] = stream.get_md5()

    return res
예제 #7
0
    def test_vrrp_check_script_update(self, mock_sock_path, mock_get_lbs,
                                      mock_join, mock_listdir, mock_exists,
                                      mock_makedirs, mock_get_listeners):
        mock_get_lbs.return_value = ['abc', LB_ID1]
        mock_sock_path.return_value = 'listener.sock'
        mock_exists.side_effect = [False, False, True]
        mock_get_lbs.side_effect = [['abc', LB_ID1], ['abc', LB_ID1], []]
        mock_get_listeners.return_value = []

        # Test the stop action path
        cmd = 'haproxy-vrrp-check ' + ' '.join(['listener.sock']) + '; exit $?'
        path = util.keepalived_dir()
        m = self.useFixture(test_utils.OpenFixture(path)).mock_open

        util.vrrp_check_script_update(LB_ID1, 'stop')

        handle = m()
        handle.write.assert_called_once_with(cmd)

        # Test the start action path
        cmd = ('haproxy-vrrp-check ' +
               ' '.join(['listener.sock', 'listener.sock']) + '; exit '
               '$?')
        m = self.useFixture(test_utils.OpenFixture(path)).mock_open
        util.vrrp_check_script_update(LB_ID1, 'start')
        handle = m()
        handle.write.assert_called_once_with(cmd)

        # Test the path with existing keepalived directory and no LBs
        mock_makedirs.reset_mock()
        cmd = 'exit 1'
        m = self.useFixture(test_utils.OpenFixture(path)).mock_open

        util.vrrp_check_script_update(LB_ID1, 'start')

        handle = m()
        handle.write.assert_called_once_with(cmd)
        mock_makedirs.assert_has_calls([
            mock.call(util.keepalived_dir(), exist_ok=True),
            mock.call(util.keepalived_check_scripts_dir(), exist_ok=True)
        ])
예제 #8
0
    def test_vrrp_check_script_update(self, mock_sock_path, mock_get_listeners,
                                      mock_join, mock_listdir, mock_exists,
                                      mock_makedirs):
        mock_get_listeners.return_value = ['abc', LISTENER_ID1]
        mock_sock_path.return_value = 'listener.sock'
        mock_exists.return_value = False
        cmd = 'haproxy-vrrp-check ' + ' '.join(['listener.sock']) + '; exit $?'

        path = agent_util.keepalived_dir()
        m = self.useFixture(test_utils.OpenFixture(path)).mock_open

        self.test_listener.vrrp_check_script_update(LISTENER_ID1, 'stop')
        handle = m()
        handle.write.assert_called_once_with(cmd)

        mock_get_listeners.return_value = ['abc', LISTENER_ID1]
        cmd = ('haproxy-vrrp-check ' +
               ' '.join(['listener.sock', 'listener.sock']) + '; exit '
               '$?')

        m = self.useFixture(test_utils.OpenFixture(path)).mock_open
        self.test_listener.vrrp_check_script_update(LISTENER_ID1, 'start')
        handle = m()
        handle.write.assert_called_once_with(cmd)
예제 #9
0
    def test_vrrp_check_script_update(self, mock_sock_path, mock_get_listeners,
                                      mock_join, mock_listdir, mock_exists,
                                      mock_makedirs):
        mock_get_listeners.return_value = ['abc', LISTENER_ID1]
        mock_sock_path.return_value = 'listener.sock'
        mock_exists.return_value = False
        cmd = 'haproxy-vrrp-check ' + ' '.join(['listener.sock']) + '; exit $?'

        path = agent_util.keepalived_dir()
        m = self.useFixture(test_utils.OpenFixture(path)).mock_open

        listener.vrrp_check_script_update(LISTENER_ID1, 'stop')
        handle = m()
        handle.write.assert_called_once_with(cmd)

        mock_get_listeners.return_value = ['abc', LISTENER_ID1]
        cmd = ('haproxy-vrrp-check ' + ' '.join(['listener.sock',
                                                 'listener.sock']) + '; exit '
                                                                     '$?')

        m = self.useFixture(test_utils.OpenFixture(path)).mock_open
        listener.vrrp_check_script_update(LISTENER_ID1, 'start')
        handle = m()
        handle.write.assert_called_once_with(cmd)
예제 #10
0
    def upload_keepalived_config(self):
        stream = listener.Wrapped(flask.request.stream)

        if not os.path.exists(util.keepalived_dir()):
            os.makedirs(util.keepalived_dir())
            os.makedirs(util.keepalived_check_scripts_dir())

        conf_file = util.keepalived_cfg_path()
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        # mode 00644
        mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
        with os.fdopen(os.open(conf_file, flags, mode), 'wb') as f:
            b = stream.read(BUFFER)
            while b:
                f.write(b)
                b = stream.read(BUFFER)

        init_system = util.get_os_init_system()

        file_path = util.keepalived_init_path(init_system)

        if init_system == consts.INIT_SYSTEMD:
            template = SYSTEMD_TEMPLATE
            init_enable_cmd = "systemctl enable octavia-keepalived"

            # Render and install the network namespace systemd service
            util.install_netns_systemd_service()
            util.run_systemctl_command(consts.ENABLE,
                                       consts.AMP_NETNS_SVC_PREFIX)
        elif init_system == consts.INIT_UPSTART:
            template = UPSTART_TEMPLATE
        elif init_system == consts.INIT_SYSVINIT:
            template = SYSVINIT_TEMPLATE
            init_enable_cmd = "insserv {file}".format(file=file_path)
        else:
            raise util.UnknownInitError()

        if init_system == consts.INIT_SYSTEMD:
            # mode 00644
            mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
        else:
            # mode 00755
            mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH
                    | stat.S_IXOTH)
        if not os.path.exists(file_path):
            with os.fdopen(os.open(file_path, flags, mode), 'w') as text_file:
                text = template.render(
                    keepalived_pid=util.keepalived_pid_path(),
                    keepalived_cmd=consts.KEEPALIVED_CMD,
                    keepalived_cfg=util.keepalived_cfg_path(),
                    keepalived_log=util.keepalived_log_path(),
                    amphora_nsname=consts.AMPHORA_NAMESPACE,
                    amphora_netns=consts.AMP_NETNS_SVC_PREFIX,
                    administrative_log_facility=(
                        CONF.amphora_agent.administrative_log_facility),
                )
                text_file.write(text)

            # Renders the Keepalived check script
            keepalived_path = util.keepalived_check_script_path()
            # mode 00755
            mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH
                    | stat.S_IXOTH)
            open_obj = os.open(keepalived_path, flags, mode)
            with os.fdopen(open_obj, 'w') as text_file:
                text = check_script_template.render(
                    check_scripts_dir=util.keepalived_check_scripts_dir())
                text_file.write(text)

        # Make sure the new service is enabled on boot
        if init_system != consts.INIT_UPSTART:
            try:
                subprocess.check_output(init_enable_cmd.split(),
                                        stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                LOG.debug(
                    'Failed to enable octavia-keepalived service: '
                    '%(err)s %(output)s', {
                        'err': e,
                        'output': e.output
                    })
                return webob.Response(json=dict(
                    message="Error enabling octavia-keepalived service",
                    details=e.output),
                                      status=500)

        res = webob.Response(json={'message': 'OK'}, status=200)
        res.headers['ETag'] = stream.get_md5()

        return res