From 41698b2bad938e618f60659ff4bc62b21940ad70 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 19 Mar 2015 15:06:31 -0400 Subject: [PATCH] Include omitted changes from last commit I did not include all intended changes in last commit. --- confluent_client/bin/confetty | 3 +- confluent_client/bin/nodepower | 20 +++++++---- confluent_client/bin/nodesetboot | 36 +++++++++++++++++++ confluent_client/confluent/client.py | 52 ++++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 9 deletions(-) diff --git a/confluent_client/bin/confetty b/confluent_client/bin/confetty index e76f7ee5..76068cdb 100755 --- a/confluent_client/bin/confetty +++ b/confluent_client/bin/confetty @@ -236,7 +236,8 @@ currchildren = None def print_result(res): - if 'errorcode' in res: + if 'errorcode' in res or 'error' in res: + print res['error'] return for key in res.iterkeys(): notes = [] diff --git a/confluent_client/bin/nodepower b/confluent_client/bin/nodepower index 5c38fc4f..a9c3cceb 100644 --- a/confluent_client/bin/nodepower +++ b/confluent_client/bin/nodepower @@ -1,7 +1,7 @@ #!/usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,14 +19,22 @@ import os import sys path = os.path.dirname(os.path.realpath(__file__)) -path = os.path.realpath(os.path.join(path, '..')) -sys.path.append(path) +path = os.path.realpath(os.path.join(path, '..', 'lib', 'python')) +if path.startswith('/opt'): + sys.path.append(path) import confluent.client as client -node = sys.argv[1] +noderange = sys.argv[1] +setstate = None +if len(sys.argv) > 2: + if setstate == 'softoff': + setstate = 'shutdown' + elif not sys.argv[2] in ('stat', 'state', 'status'): + setstate = sys.argv[2] session = client.Command() -for res in session.read("/node/{0}/power/state".format(node)): - print("%s: %s" % (node, res['state']['value'])) +exitcode = 0 +sys.exit( + session.simple_noderange_command(noderange, '/power/state', setstate)) \ No newline at end of file diff --git a/confluent_client/bin/nodesetboot b/confluent_client/bin/nodesetboot index e69de29b..5a770e60 100644 --- a/confluent_client/bin/nodesetboot +++ b/confluent_client/bin/nodesetboot @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2015 Lenovo +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import sys + +path = os.path.dirname(os.path.realpath(__file__)) +path = os.path.realpath(os.path.join(path, '..', 'lib', 'python')) +if path.startswith('/opt'): + sys.path.append(path) + +import confluent.client as client + + +noderange = sys.argv[1] +bootdev = None +if len(sys.argv) > 2: + bootdev = sys.argv[2] +session = client.Command() +exitcode = 0 +sys.exit( + session.simple_noderange_command(noderange, '/boot/nextdevice', bootdev)) \ No newline at end of file diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index 77c4f302..c16f108c 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,6 +21,7 @@ import hashlib import os import socket import ssl +import sys import confluent.tlvdata as tlvdata SO_PASSCRED = 16 @@ -43,8 +45,14 @@ class Command(object): def __init__(self, server="/var/run/confluent/api.sock"): self.connection = None - self.serverloc = server - if os.path.isabs(server) and os.path.exists(server): + if server is None: + if 'CONFLUENT_HOST' in os.environ: + self.serverloc = os.environ['CONFLUENT_HOST'] + else: + self.serverloc = '/var/run/confluent/api.sock' + else: + self.serverloc = server + if os.path.isabs(self.serverloc) and os.path.exists(self.serverloc): self._connect_unix() else: self._connect_tls() @@ -62,6 +70,43 @@ class Command(object): if authdata['authpassed'] == 1: self.authenticated = True + def handle_results(self, ikey, rc, res): + if 'error' in res and type(res['error']) in (str, unicode): + sys.stderr.write('Error: {0}\n'.format(res['error'])) + if 'errorcode' in res: + return res['errorcode'] + else: + return 1 + for node in res: + if 'error' in res[node]: + sys.stderr.write('{0}: Error: {1}\n'.format( + node, res[node]['error'])) + if 'errorcode' in res[node]: + rc |= res[node]['errorcode'] + else: + rc |= 1 + else: + print('{0}: {1}'.format(node, res[node][ikey]['value'])) + return rc + + def simple_noderange_command(self, noderange, resource, input=None): + rc = 0 + if resource[0] == '/': + resource = resource[1:] + # The implicit key is the resource basename + ikey = resource.rpartition('/')[-1] + if input is None: + for res in self.read('/noderange/{0}/{1}'.format( + noderange, resource)): + rc = self.handle_results(ikey, rc, res) + else: + for res in self.update('/noderange/{0}/{1}'.format( + noderange, resource), {ikey: input}): + rc = self.handle_results(ikey, rc, res) + return rc + + + def read(self, path, parameters=None): return send_request('retrieve', path, self.connection, parameters) @@ -155,3 +200,6 @@ def send_request(operation, path, server, parameters=None): while '_requestdone' not in result: yield result result = tlvdata.recv(server) + + +