Example #1
0
 def test_base64_sha1_string(self):
     str_to_sha1 = [
         # no special cases str (no altchars)
         ('77e7d60d-e137-4246-8a84-a25db33571cd',
          'iVZVQ5AKmk2Ae0uGLP0Ue4OseRM='),
         # backward compat amphorae with - in str[1:]
         ('9c6e5f27-a0da-4ceb-afe5-5a81230be42e',
          'NjrNgt3Egl-H5ScbYM5ChtUH3M8='),
         # sha1 would start with -, now replaced with x
         ('4db4a3cf-9fef-4057-b1fd-b2afbf7a8a0f',
          'xxqntK8jJ_gE3QEmh-D1-XgCW_E=')
     ]
     for str, sha1 in str_to_sha1:
         self.assertEqual(sha1, utils.base64_sha1_string(str))
Example #2
0
 def test_render_active_active_loadbalancer_obj(self):
     j_cfg = jinja_cfg.JinjaTemplater(
         base_amp_path='/var/lib/octavia',
         base_crt_dir='/var/lib/octavia/certs',
         connection_logging=False)
     lb = sample_configs.sample_listener_loadbalancer_tuple(
         topology='ACTIVE_ACTIVE')
     listener = sample_configs.sample_listener_tuple(load_balancer=lb)
     defaults = ("defaults\n"
                 "    no log\n"
                 "    retries 3\n"
                 "    option redispatch\n\n")
     peers = ("peers sample_listener_id_1_peers\n"
              "    peer {peer1} 10.10.10.6:1024\n\n\n").format(
                  peer1=utils.base64_sha1_string(
                      lb.amphorae[0].id).replace('=', ''))
     ref_conf = sample_configs.sample_base_expected_config(
         defaults=defaults, peers=peers)
     rendered_obj = j_cfg.render_loadbalancer_obj(lb.amphorae[0], listener)
     self.assertEqual(ref_conf, rendered_obj)
Example #3
0
    def upload_haproxy_config(self, amphora_id, listener_id):
        """Upload the haproxy config

        :param amphora_id: The id of the amphora to update
        :param listener_id: The id of the listener
        """
        stream = Wrapped(flask.request.stream)
        # We have to hash here because HAProxy has a string length limitation
        # in the configuration file "peer <peername>" lines
        peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
        if not os.path.exists(util.haproxy_dir(listener_id)):
            os.makedirs(util.haproxy_dir(listener_id))

        name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        # mode 00600
        mode = stat.S_IRUSR | stat.S_IWUSR
        b = stream.read(BUFFER)
        s_io = io.StringIO()
        while b:
            # Write haproxy configuration to StringIO
            s_io.write(b.decode('utf8'))
            b = stream.read(BUFFER)

        # Since haproxy user_group is now auto-detected by the amphora agent,
        # remove it from haproxy configuration in case it was provided
        # by an older Octavia controller. This is needed in order to prevent
        # a duplicate entry for 'group' in haproxy configuration, which will
        # result an error when haproxy starts.
        new_config = re.sub(r"\s+group\s.+", "", s_io.getvalue())

        # Handle any haproxy version compatibility issues
        new_config = haproxy_compatibility.process_cfg_for_version_compat(
            new_config)

        with os.fdopen(os.open(name, flags, mode), 'w') as file:
            file.write(new_config)

        # use haproxy to check the config
        cmd = "haproxy -c -L {peer} -f {config_file} -f {haproxy_ug}".format(
            config_file=name,
            peer=peer_name,
            haproxy_ug=consts.HAPROXY_USER_GROUP_CFG)

        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.error("Failed to verify haproxy file: %s %s", e, e.output)
            # Save the last config that failed validation for debugging
            os.rename(name, ''.join([name, '-failed']))
            return webob.Response(json=dict(message="Invalid request",
                                            details=e.output),
                                  status=400)

        # file ok - move it
        os.rename(name, util.config_path(listener_id))

        try:

            init_system = util.get_os_init_system()

            LOG.debug('Found init system: %s', init_system)

            init_path = util.init_path(listener_id, init_system)

            if init_system == consts.INIT_SYSTEMD:
                template = SYSTEMD_TEMPLATE
                # Render and install the network namespace systemd service
                util.install_netns_systemd_service()
                util.run_systemctl_command(
                    consts.ENABLE, consts.AMP_NETNS_SVC_PREFIX + '.service')
            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=init_path)
            else:
                raise util.UnknownInitError()

        except util.UnknownInitError:
            LOG.error("Unknown init system found.")
            return webob.Response(json=dict(
                message="Unknown init system in amphora",
                details="The amphora image is running an unknown init "
                "system.  We can't create the init configuration "
                "file for the load balancing process."),
                                  status=500)

        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)

        hap_major, hap_minor = haproxy_compatibility.get_haproxy_versions()
        if not os.path.exists(init_path):
            with os.fdopen(os.open(init_path, flags, mode), 'w') as text_file:

                text = template.render(
                    peer_name=peer_name,
                    haproxy_pid=util.pid_path(listener_id),
                    haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                    haproxy_cfg=util.config_path(listener_id),
                    haproxy_user_group_cfg=consts.HAPROXY_USER_GROUP_CFG,
                    respawn_count=util.CONF.haproxy_amphora.respawn_count,
                    respawn_interval=(
                        util.CONF.haproxy_amphora.respawn_interval),
                    amphora_netns=consts.AMP_NETNS_SVC_PREFIX,
                    amphora_nsname=consts.AMPHORA_NAMESPACE,
                    HasIFUPAll=self._osutils.has_ifup_all(),
                    haproxy_major_version=hap_major,
                    haproxy_minor_version=hap_minor)
                text_file.write(text)

        # Make sure the new service is enabled on boot
        if init_system == consts.INIT_SYSTEMD:
            util.run_systemctl_command(
                consts.ENABLE, "haproxy-{list}".format(list=listener_id))
        elif init_system == consts.INIT_SYSVINIT:
            try:
                subprocess.check_output(init_enable_cmd.split(),
                                        stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                LOG.error(
                    "Failed to enable haproxy-%(list)s service: "
                    "%(err)s %(out)s", {
                        'list': listener_id,
                        'err': e,
                        'out': e.output
                    })
                return webob.Response(json=dict(
                    message="Error enabling haproxy-{0} service".format(
                        listener_id),
                    details=e.output),
                                      status=500)

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

        return res
Example #4
0
    def test_haproxy(self, mock_remove, mock_subprocess, mock_rename,
                     mock_makedirs, mock_exists):

        mock_exists.return_value = True

        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        file_name = '/var/lib/octavia/123/haproxy.cfg.new'
        m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open

        # happy case init file exists
        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            mode = stat.S_IRUSR | stat.S_IWUSR
            mock_open.assert_called_with(file_name, flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            self.assertEqual(202, rv.status_code)

            handle = m()
            handle.write.assert_called_once_with(six.b('test'))
            calls = [
                mock.call("haproxy -c -L {peer} -f {config_file}".format(
                    config_file=file_name,
                    peer=(octavia_utils.base64_sha1_string('amp_123')
                          .rstrip('='))).split(), stderr=-2)
            ]
            mock_subprocess.assert_has_calls(calls)
            mock_rename.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new',
                '/var/lib/octavia/123/haproxy.cfg')

        # exception writing
        m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open
        m.side_effect = IOError()  # open crashes
        with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(500, rv.status_code)

        # check if files get created
        mock_exists.return_value = False
        init_path = '/etc/init/haproxy-123.conf'
        m = self.useFixture(test_utils.OpenFixture(init_path)).mock_open

        # happy case init file exists
        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')

            self.assertEqual(202, rv.status_code)
            mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
                    stat.S_IROTH | stat.S_IXOTH)
            mock_open.assert_called_with(util.SYSVINIT_DIR + '/haproxy-123',
                                         flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            handle = mock_fdopen()
            handle.write.assert_any_call(six.b('test'))
            # skip the template stuff
            mock_makedirs.assert_called_with('/var/lib/octavia/123')

        # unhappy case haproxy check fails
        mock_exists.return_value = True
        mock_subprocess.side_effect = [subprocess.CalledProcessError(
            7, 'test', RANDOM_ERROR)]

        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(400, rv.status_code)
            self.assertEqual(
                {'message': 'Invalid request', u'details': u'random error'},
                json.loads(rv.data.decode('utf-8')))
            mode = stat.S_IRUSR | stat.S_IWUSR
            mock_open.assert_called_with(file_name, flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            handle = mock_fdopen()
            handle.write.assert_called_with(six.b('test'))
            mock_subprocess.assert_called_with(
                "haproxy -c -L {peer} -f {config_file}".format(
                    config_file=file_name,
                    peer=(octavia_utils.
                          base64_sha1_string('amp_123').rstrip('='))).split(),
                stderr=-2)
            mock_remove.assert_called_once_with(file_name)
Example #5
0
def upload_haproxy_config(amphora_id, listener_id):
    stream = Wrapped(flask.request.stream)
    # We have to hash here because HAProxy has a string length limitation
    # in the configuration file "peer <peername>" lines
    peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
    if not os.path.exists(util.haproxy_dir(listener_id)):
        os.makedirs(util.haproxy_dir(listener_id))

    name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
    flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
    # mode 00600
    mode = stat.S_IRUSR | stat.S_IWUSR
    with os.fdopen(os.open(name, flags, mode), 'w') as file:
        b = stream.read(BUFFER)
        while (b):
            file.write(b)
            b = stream.read(BUFFER)

    # use haproxy to check the config
    cmd = "haproxy -c -L {peer} -f {config_file}".format(config_file=name,
                                                         peer=peer_name)

    try:
        subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        LOG.debug("Failed to verify haproxy file: %s", e)
        os.remove(name)  # delete file
        return flask.make_response(
            flask.jsonify(dict(message="Invalid request", details=e.output)),
            400)

    # file ok - move it
    os.rename(name, util.config_path(listener_id))

    use_upstart = util.CONF.haproxy_amphora.use_upstart
    file = util.init_path(listener_id)
    # mode 00755
    mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH
            | stat.S_IXOTH)
    if not os.path.exists(file):
        with os.fdopen(os.open(file, flags, mode), 'w') as text_file:
            template = UPSTART_TEMPLATE if use_upstart else SYSVINIT_TEMPLATE
            text = template.render(
                peer_name=peer_name,
                haproxy_pid=util.pid_path(listener_id),
                haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                haproxy_cfg=util.config_path(listener_id),
                respawn_count=util.CONF.haproxy_amphora.respawn_count,
                respawn_interval=util.CONF.haproxy_amphora.respawn_interval,
                amphora_nsname=consts.AMPHORA_NAMESPACE)
            text_file.write(text)

    if not use_upstart:
        insrvcmd = ("insserv {file}".format(file=file))

        try:
            subprocess.check_output(insrvcmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to make %(file)s executable: %(err)s", {
                'file': file,
                'err': e
            })
            return flask.make_response(
                flask.jsonify(
                    dict(message="Error making file {0} executable".format(
                        file),
                         details=e.output)), 500)

    res = flask.make_response(flask.jsonify({'message': 'OK'}), 202)
    res.headers['ETag'] = stream.get_md5()
    return res
Example #6
0
    def upload_haproxy_config(self, amphora_id, listener_id):
        """Upload the haproxy config

        :param amphora_id: The id of the amphora to update
        :param listener_id: The id of the listener
        """
        stream = Wrapped(flask.request.stream)
        # We have to hash here because HAProxy has a string length limitation
        # in the configuration file "peer <peername>" lines
        peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
        if not os.path.exists(util.haproxy_dir(listener_id)):
            os.makedirs(util.haproxy_dir(listener_id))

        name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        # mode 00600
        mode = stat.S_IRUSR | stat.S_IWUSR
        with os.fdopen(os.open(name, flags, mode), 'wb') as file:
            b = stream.read(BUFFER)
            while (b):
                file.write(b)
                b = stream.read(BUFFER)

        # use haproxy to check the config
        cmd = "haproxy -c -L {peer} -f {config_file}".format(config_file=name,
                                                             peer=peer_name)

        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.error(_LE("Failed to verify haproxy file: %s"), e)
            os.remove(name)  # delete file
            return flask.make_response(
                flask.jsonify(dict(message="Invalid request",
                                   details=e.output)), 400)

        # file ok - move it
        os.rename(name, util.config_path(listener_id))

        try:

            init_system = util.get_os_init_system()

            LOG.debug('Found init system: {0}'.format(init_system))

            init_path = util.init_path(listener_id, init_system)

            if init_system == consts.INIT_SYSTEMD:
                template = SYSTEMD_TEMPLATE
                init_enable_cmd = "systemctl enable haproxy-{list}".format(
                    list=listener_id)
            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=init_path)
            else:
                raise util.UnknownInitError()

        except util.UnknownInitError:
            LOG.error(_LE("Unknown init system found."))
            return flask.make_response(
                flask.jsonify(
                    dict(
                        message="Unknown init system in amphora",
                        details="The amphora image is running an unknown init "
                        "system.  We can't create the init configuration "
                        "file for the load balancing process.")), 500)

        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(init_path):
            with os.fdopen(os.open(init_path, flags, mode), 'w') as text_file:

                text = template.render(
                    peer_name=peer_name,
                    haproxy_pid=util.pid_path(listener_id),
                    haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                    haproxy_cfg=util.config_path(listener_id),
                    respawn_count=util.CONF.haproxy_amphora.respawn_count,
                    respawn_interval=(
                        util.CONF.haproxy_amphora.respawn_interval),
                    amphora_nsname=consts.AMPHORA_NAMESPACE,
                    HasIFUPAll=self._osutils.has_ifup_all())
                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.error(
                    _LE("Failed to enable haproxy-%(list)s "
                        "service: %(err)s"), {
                            'list': listener_id,
                            'err': e
                        })
                return flask.make_response(
                    flask.jsonify(
                        dict(message="Error enabling haproxy-{0} service".
                             format(listener_id),
                             details=e.output)), 500)

        res = flask.make_response(flask.jsonify({'message': 'OK'}), 202)
        res.headers['ETag'] = stream.get_md5()
        return res
Example #7
0
    def test_haproxy(self, mock_remove, mock_subprocess, mock_rename,
                     mock_makedirs, mock_exists):
        mock_exists.return_value = True
        m = mock.mock_open()

        # happy case upstart file exists
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(202, rv.status_code)
            m.assert_called_once_with('/var/lib/octavia/123/haproxy.cfg.new',
                                      'w')
            handle = m()
            handle.write.assert_called_once_with(six.b('test'))
            mock_subprocess.assert_called_once_with(
                "haproxy -c -L {peer} -f {config_file}".format(
                    config_file='/var/lib/octavia/123/haproxy.cfg.new',
                    peer=(octavia_utils.base64_sha1_string('amp_123').rstrip(
                        '='))).split(),
                stderr=-2)
            mock_rename.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new',
                '/var/lib/octavia/123/haproxy.cfg')

        # exception writing
        m = mock.mock_open()
        m.side_effect = IOError()  # open crashes
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(500, rv.status_code)

        # check if files get created
        mock_exists.return_value = False
        m = mock.mock_open()

        # happy case upstart file exists
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(202, rv.status_code)
            m.assert_any_call('/var/lib/octavia/123/haproxy.cfg.new', 'w')
            m.assert_any_call(util.UPSTART_DIR + '/haproxy-123.conf', 'w')
            handle = m()
            handle.write.assert_any_call(six.b('test'))
            # skip the template stuff
            mock_makedirs.assert_called_with('/var/lib/octavia/123')

        # unhappy case haproxy check fails
        mock_exists.return_value = True
        mock_subprocess.side_effect = [
            subprocess.CalledProcessError(7, 'test', RANDOM_ERROR)
        ]
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(400, rv.status_code)
            self.assertEqual(
                {
                    'message': 'Invalid request',
                    u'details': u'random error'
                }, json.loads(rv.data.decode('utf-8')))
            m.assert_called_with('/var/lib/octavia/123/haproxy.cfg.new', 'w')
            handle = m()
            handle.write.assert_called_with(six.b('test'))
            mock_subprocess.assert_called_with(
                "haproxy -c -L {peer} -f {config_file}".format(
                    config_file='/var/lib/octavia/123/haproxy.cfg.new',
                    peer=(octavia_utils.base64_sha1_string('amp_123').rstrip(
                        '='))).split(),
                stderr=-2)
            mock_remove.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new')
Example #8
0
    def upload_haproxy_config(self, amphora_id, listener_id):
        """Upload the haproxy config

        :param amphora_id: The id of the amphora to update
        :param listener_id: The id of the listener
        """
        stream = Wrapped(flask.request.stream)
        # We have to hash here because HAProxy has a string length limitation
        # in the configuration file "peer <peername>" lines
        peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
        if not os.path.exists(util.haproxy_dir(listener_id)):
            os.makedirs(util.haproxy_dir(listener_id))

        name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        # mode 00600
        mode = stat.S_IRUSR | stat.S_IWUSR
        b = stream.read(BUFFER)
        s_io = io.StringIO()
        while b:
            # Write haproxy configuration to StringIO
            s_io.write(b.decode('utf8'))
            b = stream.read(BUFFER)

        # Since haproxy user_group is now auto-detected by the amphora agent,
        # remove it from haproxy configuration in case it was provided
        # by an older Octavia controller. This is needed in order to prevent
        # a duplicate entry for 'group' in haproxy configuration, which will
        # result an error when haproxy starts.
        new_config = re.sub(r"\s+group\s.+", "", s_io.getvalue())

        # Handle any haproxy version compatibility issues
        new_config = haproxy_compatibility.process_cfg_for_version_compat(
            new_config)

        with os.fdopen(os.open(name, flags, mode), 'w') as file:
            file.write(new_config)

        # use haproxy to check the config
        cmd = "haproxy -c -L {peer} -f {config_file} -f {haproxy_ug}".format(
            config_file=name, peer=peer_name,
            haproxy_ug=consts.HAPROXY_USER_GROUP_CFG)

        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.error("Failed to verify haproxy file: %s %s", e, e.output)
            # Save the last config that failed validation for debugging
            os.rename(name, ''.join([name, '-failed']))
            return webob.Response(
                json=dict(message="Invalid request", details=e.output),
                status=400)

        # file ok - move it
        os.rename(name, util.config_path(listener_id))

        try:

            init_system = util.get_os_init_system()

            LOG.debug('Found init system: %s', init_system)

            init_path = util.init_path(listener_id, init_system)

            if init_system == consts.INIT_SYSTEMD:
                template = SYSTEMD_TEMPLATE
                # Render and install the network namespace systemd service
                util.install_netns_systemd_service()
                util.run_systemctl_command(
                    consts.ENABLE, consts.AMP_NETNS_SVC_PREFIX + '.service')
            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=init_path)
            else:
                raise util.UnknownInitError()

        except util.UnknownInitError:
            LOG.error("Unknown init system found.")
            return webob.Response(json=dict(
                message="Unknown init system in amphora",
                details="The amphora image is running an unknown init "
                        "system.  We can't create the init configuration "
                        "file for the load balancing process."), status=500)

        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)

        hap_major, hap_minor = haproxy_compatibility.get_haproxy_versions()
        if not os.path.exists(init_path):
            with os.fdopen(os.open(init_path, flags, mode), 'w') as text_file:

                text = template.render(
                    peer_name=peer_name,
                    haproxy_pid=util.pid_path(listener_id),
                    haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                    haproxy_cfg=util.config_path(listener_id),
                    haproxy_user_group_cfg=consts.HAPROXY_USER_GROUP_CFG,
                    respawn_count=util.CONF.haproxy_amphora.respawn_count,
                    respawn_interval=(util.CONF.haproxy_amphora.
                                      respawn_interval),
                    amphora_netns=consts.AMP_NETNS_SVC_PREFIX,
                    amphora_nsname=consts.AMPHORA_NAMESPACE,
                    HasIFUPAll=self._osutils.has_ifup_all(),
                    haproxy_major_version=hap_major,
                    haproxy_minor_version=hap_minor
                )
                text_file.write(text)

        # Make sure the new service is enabled on boot
        if init_system == consts.INIT_SYSTEMD:
            util.run_systemctl_command(
                consts.ENABLE, "haproxy-{list}".format(list=listener_id))
        elif init_system == consts.INIT_SYSVINIT:
            try:
                subprocess.check_output(init_enable_cmd.split(),
                                        stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                LOG.error("Failed to enable haproxy-%(list)s service: "
                          "%(err)s %(out)s", {'list': listener_id, 'err': e,
                                              'out': e.output})
                return webob.Response(json=dict(
                    message="Error enabling haproxy-{0} service".format(
                            listener_id), details=e.output), status=500)

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

        return res
    def test_haproxy(self, mock_remove, mock_subprocess, mock_rename,
                     mock_makedirs, mock_exists):
        mock_exists.return_value = True
        m = mock.mock_open()

        # happy case init file exists
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(202, rv.status_code)
            m.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new', 'w')
            handle = m()
            handle.write.assert_called_once_with(six.b('test'))
            calls = [
                mock.call("haproxy -c -L {peer} -f {config_file}".format(
                    config_file='/var/lib/octavia/123/haproxy.cfg.new',
                    peer=(octavia_utils.base64_sha1_string('amp_123')
                          .rstrip('='))).split(), stderr=-2),
                mock.call(['chmod', '755', '/etc/init.d/haproxy-123'],
                          stderr=-2),
                mock.call(['insserv', '/etc/init.d/haproxy-123'], stderr=-2)
            ]
            mock_subprocess.assert_has_calls(calls)
            mock_rename.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new',
                '/var/lib/octavia/123/haproxy.cfg')

        # exception writing
        m = mock.mock_open()
        m.side_effect = IOError()  # open crashes
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(500, rv.status_code)

        # check if files get created
        mock_exists.return_value = False
        m = mock.mock_open()

        # happy case init file exists
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(202, rv.status_code)
            m.assert_any_call('/var/lib/octavia/123/haproxy.cfg.new', 'w')
            m.assert_any_call(util.SYSVINIT_DIR + '/haproxy-123', 'w')
            handle = m()
            handle.write.assert_any_call(six.b('test'))
            # skip the template stuff
            mock_makedirs.assert_called_with('/var/lib/octavia/123')

        # unhappy case haproxy check fails
        mock_exists.return_value = True
        mock_subprocess.side_effect = [subprocess.CalledProcessError(
            7, 'test', RANDOM_ERROR)]
        with mock.patch.object(builtins, 'open', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(400, rv.status_code)
            self.assertEqual(
                {'message': 'Invalid request', u'details': u'random error'},
                json.loads(rv.data.decode('utf-8')))
            m.assert_called_with('/var/lib/octavia/123/haproxy.cfg.new', 'w')
            handle = m()
            handle.write.assert_called_with(six.b('test'))
            mock_subprocess.assert_called_with(
                "haproxy -c -L {peer} -f {config_file}".format(
                    config_file='/var/lib/octavia/123/haproxy.cfg.new',
                    peer=(octavia_utils.
                          base64_sha1_string('amp_123').rstrip('='))).split(),
                stderr=-2)
            mock_remove.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new')
Example #10
0
def upload_haproxy_config(amphora_id, listener_id):
    stream = Wrapped(flask.request.stream)
    # We have to hash here because HAProxy has a string length limitation
    # in the configuration file "peer <peername>" lines
    peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
    if not os.path.exists(util.haproxy_dir(listener_id)):
        os.makedirs(util.haproxy_dir(listener_id))

    name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
    with open(name, 'w') as file:
        b = stream.read(BUFFER)
        while (b):
            file.write(b)
            b = stream.read(BUFFER)

    # use haproxy to check the config
    cmd = "haproxy -c -L {peer} -f {config_file}".format(config_file=name,
                                                         peer=peer_name)

    try:
        subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        LOG.debug("Failed to verify haproxy file: %s", e)
        os.remove(name)  # delete file
        return flask.make_response(flask.jsonify(dict(
            message="Invalid request",
            details=e.output)), 400)

    # file ok - move it
    os.rename(name, util.config_path(listener_id))

    use_upstart = util.CONF.haproxy_amphora.use_upstart
    if not os.path.exists(util.init_path(listener_id)):
        with open(util.init_path(listener_id), 'w') as text_file:
            template = UPSTART_TEMPLATE if use_upstart else SYSVINIT_TEMPLATE
            text = template.render(
                peer_name=peer_name,
                haproxy_pid=util.pid_path(listener_id),
                haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                haproxy_cfg=util.config_path(listener_id),
                respawn_count=util.CONF.haproxy_amphora.respawn_count,
                respawn_interval=util.CONF.haproxy_amphora.respawn_interval
            )
            text_file.write(text)

    if not use_upstart:
        # make init.d script executable
        file = util.init_path(listener_id)
        permcmd = ("chmod 755 {file}".format(file=file))
        insrvcmd = ("insserv {file}".format(file=file))

        try:
            subprocess.check_output(permcmd.split(), stderr=subprocess.STDOUT)
            subprocess.check_output(insrvcmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to make %(file)s executable: %(err)s",
                      {'file': file, 'err': e})
            return flask.make_response(flask.jsonify(dict(
                message="Error making file {0} executable".format(file),
                details=e.output)), 500)

    res = flask.make_response(flask.jsonify({
        'message': 'OK'}), 202)
    res.headers['ETag'] = stream.get_md5()
    return res
Example #11
0
    def upload_haproxy_config(self, amphora_id, listener_id):
        """Upload the haproxy config

        :param amphora_id: The id of the amphora to update
        :param listener_id: The id of the listener
        """
        stream = Wrapped(flask.request.stream)
        # We have to hash here because HAProxy has a string length limitation
        # in the configuration file "peer <peername>" lines
        peer_name = octavia_utils.base64_sha1_string(amphora_id).rstrip('=')
        if not os.path.exists(util.haproxy_dir(listener_id)):
            os.makedirs(util.haproxy_dir(listener_id))

        name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        # mode 00600
        mode = stat.S_IRUSR | stat.S_IWUSR
        with os.fdopen(os.open(name, flags, mode), 'w') as file:
            b = stream.read(BUFFER)
            while (b):
                file.write(b)
                b = stream.read(BUFFER)

        # use haproxy to check the config
        cmd = "haproxy -c -L {peer} -f {config_file}".format(config_file=name,
                                                             peer=peer_name)

        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to verify haproxy file: %s", e)
            os.remove(name)  # delete file
            return flask.make_response(flask.jsonify(dict(
                message="Invalid request",
                details=e.output)), 400)

        # file ok - move it
        os.rename(name, util.config_path(listener_id))

        use_upstart = util.CONF.haproxy_amphora.use_upstart
        file = util.init_path(listener_id)
        # mode 00755
        mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
                stat.S_IROTH | stat.S_IXOTH)
        if not os.path.exists(file):
            with os.fdopen(os.open(file, flags, mode), 'w') as text_file:
                template = (UPSTART_TEMPLATE if use_upstart
                            else SYSVINIT_TEMPLATE)
                text = template.render(
                    peer_name=peer_name,
                    haproxy_pid=util.pid_path(listener_id),
                    haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                    haproxy_cfg=util.config_path(listener_id),
                    respawn_count=util.CONF.haproxy_amphora.respawn_count,
                    respawn_interval=(util.CONF.haproxy_amphora.
                                      respawn_interval),
                    amphora_nsname=consts.AMPHORA_NAMESPACE
                )
                text_file.write(text)

        if not use_upstart:
            insrvcmd = ("insserv {file}".format(file=file))

            try:
                subprocess.check_output(insrvcmd.split(),
                                        stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                LOG.debug("Failed to make %(file)s executable: %(err)s",
                          {'file': file, 'err': e})
                return flask.make_response(flask.jsonify(dict(
                    message="Error making file {0} executable".format(file),
                    details=e.output)), 500)

        res = flask.make_response(flask.jsonify({
            'message': 'OK'}), 202)
        res.headers['ETag'] = stream.get_md5()
        return res
Example #12
0
    def test_haproxy(self, mock_remove, mock_subprocess, mock_rename,
                     mock_makedirs, mock_exists):

        mock_exists.return_value = True

        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
        file_name = '/var/lib/octavia/123/haproxy.cfg.new'
        m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open

        # happy case init file exists
        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            mode = stat.S_IRUSR | stat.S_IWUSR
            mock_open.assert_called_with(file_name, flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            self.assertEqual(202, rv.status_code)

            handle = m()
            handle.write.assert_called_once_with(six.b('test'))
            calls = [
                mock.call("haproxy -c -L {peer} -f {config_file}".format(
                    config_file=file_name,
                    peer=(octavia_utils.base64_sha1_string('amp_123').rstrip(
                        '='))).split(),
                          stderr=-2)
            ]
            mock_subprocess.assert_has_calls(calls)
            mock_rename.assert_called_once_with(
                '/var/lib/octavia/123/haproxy.cfg.new',
                '/var/lib/octavia/123/haproxy.cfg')

        # exception writing
        m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open
        m.side_effect = IOError()  # open crashes
        with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m):
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(500, rv.status_code)

        # check if files get created
        mock_exists.return_value = False
        init_path = '/etc/init/haproxy-123.conf'
        m = self.useFixture(test_utils.OpenFixture(init_path)).mock_open

        # happy case init file exists
        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')

            self.assertEqual(202, rv.status_code)
            mode = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH
                    | stat.S_IXOTH)
            mock_open.assert_called_with(util.SYSVINIT_DIR + '/haproxy-123',
                                         flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            handle = mock_fdopen()
            handle.write.assert_any_call(six.b('test'))
            # skip the template stuff
            mock_makedirs.assert_called_with('/var/lib/octavia/123')

        # unhappy case haproxy check fails
        mock_exists.return_value = True
        mock_subprocess.side_effect = [
            subprocess.CalledProcessError(7, 'test', RANDOM_ERROR)
        ]

        with mock.patch('os.open') as mock_open, mock.patch.object(
                os, 'fdopen', m) as mock_fdopen:
            mock_open.return_value = 123
            rv = self.app.put('/' + api_server.VERSION +
                              '/listeners/amp_123/123/haproxy',
                              data='test')
            self.assertEqual(400, rv.status_code)
            self.assertEqual(
                {
                    'message': 'Invalid request',
                    u'details': u'random error'
                }, json.loads(rv.data.decode('utf-8')))
            mode = stat.S_IRUSR | stat.S_IWUSR
            mock_open.assert_called_with(file_name, flags, mode)
            mock_fdopen.assert_called_with(123, 'w')
            handle = mock_fdopen()
            handle.write.assert_called_with(six.b('test'))
            mock_subprocess.assert_called_with(
                "haproxy -c -L {peer} -f {config_file}".format(
                    config_file=file_name,
                    peer=(octavia_utils.base64_sha1_string('amp_123').rstrip(
                        '='))).split(),
                stderr=-2)
            mock_remove.assert_called_once_with(file_name)