On Tue, 2014-08-26 at 22:16 -0300, 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(a)linux.vnet.ibm.com>
> ---
> docs/API.md | 9 +++++++
> src/kimchi/control/peers.py | 29 ++++++++++++++++++++++
> src/kimchi/mockmodel.py | 6 +++++
> src/kimchi/model/peers.py | 59 +++++++++++++++++++++++++++++++++++++++++++++
> tests/test_rest.py | 4 +++
> 5 files changed, 107 insertions(+)
> create mode 100644 src/kimchi/control/peers.py
> create mode 100644 src/kimchi/model/peers.py
>
> diff --git a/docs/API.md b/docs/API.md
> index be2fd84..cb761e5 100644
> --- a/docs/API.md
> +++ b/docs/API.md
> @@ -1003,3 +1003,12 @@ http://, ftp:// or file:// URL.
>
> * enable: Enable the Repository as package source
> * disable: Disable the Repository as package source
> +
> +### Collection: Peers
> +
> +**URI:** /peers
> +
> +**Methods:**
> +
> +* **GET**: Return the list of Kimchi peers in the same network
> + (It uses openSLP for discovering)
> 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
> +
> +
> +@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)]
Why not use the self.url? "service:kimchid://%s" % self.url
ACK. Check v3.
> + 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',