[node-patches] Change in ovirt-node[master]: Add ovirt-node generic registration script
rbarry at redhat.com
rbarry at redhat.com
Wed May 21 14:19:57 UTC 2014
Ryan Barry has uploaded a new change for review.
Change subject: Add ovirt-node generic registration script
......................................................................
Add ovirt-node generic registration script
To replace vdsm-reg, put in place a generic registration script
which fulfills basic requirements so the engine can use otopi/
host-deploy
Change-Id: I9457b05716c4dc5a47a280b8335a41e6fae73c0d
Signed-off-by: Ryan Barry <rbarry at redhat.com>
---
A registration/example.json
A registration/ovirt-node-registration.py
2 files changed, 180 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/76/27976/1
diff --git a/registration/example.json b/registration/example.json
new file mode 100644
index 0000000..9434f3f
--- /dev/null
+++ b/registration/example.json
@@ -0,0 +1,64 @@
+{
+ "register": {
+ "steps": [
+ {
+ "name": "get_ca",
+ "action": "get",
+ "parameters": {
+ "host": "engine.example.com",
+ "url": "http://{host}/url?cmd=get-ca-roots",
+ "filename": "/etc/pki/ovirt-engine.key"
+ }
+ },
+ {
+ "name": "approve",
+ "action": "ui",
+ "parameters": {
+ "key": "OVIRT_ENGINE_FINGERPRINT"
+ }
+ },
+ {
+ "name": "persist_ca",
+ "action": "persist",
+ "parameters": {
+ "file": "/etc/pki/ovirt-engine.key"
+ }
+ },
+ {
+ "name": "get_ssh",
+ "action": "get",
+ "parameters": {
+ "host": "engine.example.com",
+ "url": "http://{host}/url?=cmd=get-sshkey&type=manager",
+ "filename": "/tmp/ovirt-engine.key"
+ }
+ },
+ {
+ "name": "authorize",
+ "action": "exec",
+ "parameters": {
+ "cmd": "cat/tmp/ovirt-engine.key>>/home/admin/.ssh/.authorized_keys"
+ }
+ },
+ {
+ "name": "persist_ssh",
+ "action": "persist",
+ "parameters": {
+ "file": "/home/admin/.ssh/.authorized_keys"
+ }
+ },
+ {
+ "name": "finish",
+ "action": "get",
+ "parameters": {
+ "host": "engine.example.com",
+ "url": "http://{host}/url?cmd=register&name={name}&ip={ip}&sshkeyfingerprint={fingerprint}&user={user}",
+ "name": "node.example.com",
+ "ip": "10.0.0.1",
+ "fingerprint": "DE:AD:BE:EF",
+ "user": "admin"
+ }
+ }
+ ]
+ }
+}
diff --git a/registration/ovirt-node-registration.py b/registration/ovirt-node-registration.py
new file mode 100644
index 0000000..f10ef12
--- /dev/null
+++ b/registration/ovirt-node-registration.py
@@ -0,0 +1,116 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# ovirt-node-registration.py - Copyright (C) 2014 Red Hat, Inc.
+# Written by Ryan Barry <rbarry at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+import requests
+import json
+import logging
+from optparse import OptionParser
+from ovirt.node.utils import AugeasWrapper, fs, process
+
+
+class Client:
+ def __init__(self):
+ logging.basicConfig(
+ filename="/tmp/ovirt-node-registration.log",
+ filemode='a',
+ format='%(levelname)10s %(asctime)s ' \
+ '%(pathname)s:%(lineno)s:%(funcName)s %(message)s',
+ level=logging.DEBUG
+ )
+ self.logger = logging.getLogger('ovirt-node-registration')
+
+ self.maps = {"get": self.get,
+ "ui": self.ui,
+ "persist": self.persist,
+ "exec": self.run}
+
+ def get(self, *args, **kwargs):
+ if "url" not in kwargs:
+ raise RuntimeError("A url is required for get()!")
+
+ url = kwargs["url"].format(**kwargs)
+ self.logger.debug("Getting %s" % url)
+ if "filename" in kwargs:
+ if not kwargs["filename"].startswith("/tmp"):
+ self.persist(file=kwargs["filename"])
+ with open(kwargs["filename"], "w") as f:
+ response = requests.get(url, stream=True)
+ if response.ok:
+ for chunk in request.iter_content():
+ f.write(chunk)
+ else:
+ self.logger.info("Failed to get %s" % url)
+ else:
+ r = requests.get(url)
+ if not r.ok:
+ self.logger.info("Failed to get %s" % url)
+
+ def ui(self, *args, **kwargs):
+ if "key" not in kwargs or "value" not in kwargs:
+ raise RuntimeError("A key and value are required to prompt "
+ "for UI interaction")
+ aug = AugeasWrapper()
+ aug.set("/files/etc/default/ovirt/%s" % kwargs["key"],
+ kwargs["value"])
+
+ def persist(self, *args, **kwargs):
+ if "file" not in kwargs:
+ raise RuntimeError("A file is required for persist()!")
+
+ self.logger.debug("Persisting %s" % kwargs["file"])
+ fs.Config().persist(kwargs["file"])
+
+ def run(self, *args, **kwargs):
+ if "cmd" not in kwargs:
+ raise RuntimeError("A command is required for exec()!")
+
+ self.logger.debug("Running %s" % kwargs["cmd"])
+ output = process.check_output(kwargs["cmd"], shell=True)
+ self.logger.debug("Output of %s was: %s" % (kwargs["cmd"], output))
+
+
+def main():
+ parser = OptionParser()
+ parser.add_option("-f", "--file", dest="filename",
+ help="read from FILE", metavar="FILE")
+ parser.add_option("-a", "--action", dest="action",
+ help="action to execute")
+ parser.add_option("-p", "--params", dest="params",
+ help="list of comma-separated parameters in the "
+ "form of 'foo=bar,x=y'")
+ (options, args) = parser.parse_args()
+
+ params = {}
+ for x in options.params.split(","):
+ params[x.split('=')[0]] = x.split('=')[1]
+
+ json_data = open(options.filename).read()
+ data = json.loads(json_data)
+
+ c = Client()
+
+ for x in data[options.action]["steps"]:
+ args = x["parameters"] if x["parameters"] else {}
+ args.update(params)
+ del(args["url"])
+ c.maps[x["action"]](**args)
+
+if __name__ == "__main__":
+ main()
--
To view, visit http://gerrit.ovirt.org/27976
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9457b05716c4dc5a47a280b8335a41e6fae73c0d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Ryan Barry <rbarry at redhat.com>
More information about the node-patches
mailing list