[Kimchi-devel] [PATCH 4/5] Discover Kimchi peers using openSLP

Aline Manera alinefm at linux.vnet.ibm.com
Tue Aug 26 13:31:46 UTC 2014


On 08/26/2014 04:00 AM, Royce Lv wrote:
> On 2014年08月21日 05:15, Aline Manera wrote:
>> When federation feature is enabled, Kimchi server will be registered
>> on openSLP service on server starting up. It will expose the Kimchi URL
>> to others Kimchi peers.
>>
>> To discover Kimchi peers in the same network, the following API is 
>> provided:
>>
>> GET /peers
>> [
>>      https://ubuntu-vm:8001,
>>      https://rhel-vm:8001
>> ]
>>
>> Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
>> ---
>>   src/kimchi/control/peers.py | 29 ++++++++++++++++++++++
>>   src/kimchi/mockmodel.py     |  6 +++++
>>   src/kimchi/model/peers.py   | 59 
>> +++++++++++++++++++++++++++++++++++++++++++++
>>   tests/test_rest.py          |  4 +++
>>   4 files changed, 98 insertions(+)
>>   create mode 100644 src/kimchi/control/peers.py
>>   create mode 100644 src/kimchi/model/peers.py
>>
>> diff --git a/src/kimchi/control/peers.py b/src/kimchi/control/peers.py
>> new file mode 100644
>> index 0000000..f72a38c
>> --- /dev/null
>> +++ b/src/kimchi/control/peers.py
>> @@ -0,0 +1,29 @@
>> +#
>> +# Project Kimchi
>> +#
>> +# Copyright IBM, Corp. 2014
>> +#
>> +# This library is free software; you can redistribute it and/or
>> +# modify it under the terms of the GNU Lesser General Public
>> +# License as published by the Free Software Foundation; either
>> +# version 2.1 of the License, or (at your option) any later version.
>> +#
>> +# This library is distributed in the hope that it will be useful,
>> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +# Lesser General Public License for more details.
>> +#
>> +# You should have received a copy of the GNU Lesser General Public
>> +# License along with this library; if not, write to the Free Software
>> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
>> 02110-1301 USA
>> +
>> +from kimchi.control.base import SimpleCollection
>> +from kimchi.control.utils import UrlSubNode
>> +
>> +
>> + at UrlSubNode("peers", True)
>> +class Peers(SimpleCollection):
>> +    def __init__(self, model):
>> +        super(Peers, self).__init__(model)
>> +        self.role_key = 'peers'
>> +        self.admin_methods = ['GET']
>> diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
>> index 57a58d7..107aef4 100644
>> --- a/src/kimchi/mockmodel.py
>> +++ b/src/kimchi/mockmodel.py
>> @@ -884,6 +884,12 @@ class MockModel(object):
>>       def groups_get_list(self):
>>           return ["groupA", "groupB", "groupC", "groupD"]
>>
>> +    def peers_get_list(self):
>> +        if kconfig.get("server", "federation") == "off":
>> +            return []
>> +
>> +        return ["https://serverA:8001", "https://serverB:8001"]
>> +
>>       def vms_get_list_by_state(self, state):
>>           ret_list = []
>>           for name in self.vms_get_list():
>> diff --git a/src/kimchi/model/peers.py b/src/kimchi/model/peers.py
>> new file mode 100644
>> index 0000000..01e9210
>> --- /dev/null
>> +++ b/src/kimchi/model/peers.py
>> @@ -0,0 +1,59 @@
>> +#
>> +# Project Kimchi
>> +#
>> +# Copyright IBM, Corp. 2014
>> +#
>> +# This library is free software; you can redistribute it and/or
>> +# modify it under the terms of the GNU Lesser General Public
>> +# License as published by the Free Software Foundation; either
>> +# version 2.1 of the License, or (at your option) any later version.
>> +#
>> +# This library is distributed in the hope that it will be useful,
>> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +# Lesser General Public License for more details.
>> +#
>> +# You should have received a copy of the GNU Lesser General Public
>> +# License along with this library; if not, write to the Free Software
>> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
>> 02110-1301 USA
>> +
>> +import re
>> +import socket
>> +
>> +from kimchi.config import config
>> +from kimchi.utils import kimchi_log, run_command
>> +
>> +
>> +class PeersModel(object):
>> +    def __init__(self, **kargs):
>> +        # check federation feature is enabled on Kimchi server
>> +        if config.get("server", "federation") == "off":
>> +            return
>> +
>> +        # register server on openslp
>> +        hostname = socket.getfqdn(config.get("server", "host"))
>> +        port = config.get("server", "ssl_port")
>> +        self.url = hostname + ":" + port
>> +
>> +        cmd = ["slptool", "register",
>> +               "service:kimchid://%s:%s" % (hostname, port)]
> Dependencies?

Good point. As I said before I don't think federation must be a 
mandatory feature as it is not related to virtualization.
Because that I added a new README-federation file to guide the user on 
how setup the Kimchi server to enable federation.

I know the deb package has a "Suggested" packages section, we can add 
openslp there
But rpm does not have a similar approach in this case.

>> +        out, error, ret = run_command(cmd)
>> +        if len(out) != 0:
>> +            kimchi_log.error("Unable to register server on openSLP."
>> +                             " Details: %s" % out)
>> +
>> +    def get_list(self):
>> +        # check federation feature is enabled on Kimchi server
>> +        if config.get("server", "federation") == "off":
>> +            return []
>> +
>> +        peers = []
>> +        cmd = ["slptool", "findsrvs", "service:kimchid"]
>> +        out, error, ret = run_command(cmd)
>> +        for server in out.strip().split("\n"):
>> +            match = re.match("service:kimchid://(.*?),.*", server)
>> +            peer = match.group(1)
>> +            if peer != self.url:
>> +                peers.append("https://" + peer)
>> +
>> +        return peers
>> diff --git a/tests/test_rest.py b/tests/test_rest.py
>> index 9066857..a070f14 100644
>> --- a/tests/test_rest.py
>> +++ b/tests/test_rest.py
>> @@ -1539,6 +1539,10 @@ class RestTests(unittest.TestCase):
>>           self.assertIn('update_tool', conf)
>>           self.assertIn('repo_mngt_tool', conf)
>>
>> +    def test_peers(self):
>> +        resp = self.request('/peers').read()
>> +        self.assertEquals([], json.loads(resp))
>> +
>>       def test_auth_unprotected(self):
>>           hdrs = {'AUTHORIZATION': ''}
>>           uris = ['/js/kimchi.min.js',
>




More information about the Kimchi-devel mailing list