From 29f6fa75cfa0dd6e2d98a07891fc4d7f295f7e3b Mon Sep 17 00:00:00 2001 From: XuWei Date: Mon, 26 Feb 2018 03:37:08 -0500 Subject: [PATCH] rspconfig gard&admin_passwd in python --- .../lib/python/agent/hwctl/bmcconfig.py | 3 ++ .../agent/hwctl/executor/openbmc_bmcconfig.py | 37 ++++++++++++++++++- .../lib/python/agent/hwctl/openbmc_client.py | 15 ++++++++ .../lib/python/agent/xcatagent/openbmc.py | 16 +++++--- xCAT-server/lib/xcat/plugins/openbmc2.pm | 7 +++- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/bmcconfig.py b/xCAT-openbmc-py/lib/python/agent/hwctl/bmcconfig.py index ec19b596c..76355be85 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/bmcconfig.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/bmcconfig.py @@ -25,6 +25,9 @@ class BmcConfigInterface(object): def dump_process(self, task): return task.run("dump_process") + def gard_clear(self, task): + return task.run("gard_clear") + def set_sshcfg(self, task): return task.run("set_sshcfg") diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_bmcconfig.py b/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_bmcconfig.py index a3240bc48..5b29ced08 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_bmcconfig.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_bmcconfig.py @@ -179,6 +179,20 @@ class OpenBMCBmcConfigTask(ParallelNodesCommand): except SelfServerException as e: self.callback.info('%s: %s' % (node, e.message)) + def gard_clear(self, **kw): + + node = kw['node'] + obmc = openbmc.OpenBMCRest(name=node, nodeinfo=kw['nodeinfo'], messager=self.callback, + debugmode=self.debugmode, verbose=self.verbose) + + try: + obmc.login() + obmc.clear_gard() + self.callback.info('%s: GARD cleared' % node) + + except SelfServerException as e: + self.callback.info('%s: %s' % (node, e.message)) + def pre_set_sshcfg(self, *arg, **kw): local_home_dir=os.path.expanduser('~') self.local_ssh_dir = local_home_dir + "/.ssh/" @@ -271,6 +285,8 @@ rmdir \"/tmp/$userid\" \n") netinfo_dict[k] = v elif k == 'hostname': self._set_hostname(v, **kw) + elif k == 'admin_passwd': + self._set_admin_password(v, **kw) elif k in openbmc.RSPCONFIG_APIS: self._set_apis_values(k, v, **kw) else: @@ -292,7 +308,26 @@ rmdir \"/tmp/$userid\" \n") self._set_apis_values("hostname", hostname, **kw) self._get_netinfo(hostname=True, ntpserver=False, **kw) return - + + def _set_admin_password(self, admin_passwd, **kw): + node = kw['node'] + node_info = kw['nodeinfo'] + + origin_passwd, new_passwd = admin_passwd.split(',') + + if origin_passwd != node_info['password']: + self.callback.info('%s: Current BMC password is incorrect, cannot set the new password.' % node) + return + + obmc = openbmc.OpenBMCRest(name=node, nodeinfo=node_info, messager=self.callback, + debugmode=self.debugmode, verbose=self.verbose) + try: + obmc.login() + obmc.set_admin_passwd(new_passwd) + self.callback.info("%s: BMC Setting Password..." % node) + except (SelfServerException, SelfClientException) as e: + self.callback.info("%s: %s" % (node, e.message)) + def _set_apis_values(self, key, value, **kw): node = kw['node'] obmc = openbmc.OpenBMCRest(name=node, nodeinfo=kw['nodeinfo'], messager=self.callback, diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py index 29169ff8a..55ced2396 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py @@ -46,6 +46,8 @@ DUMP_URLS = { "list" : "/dump/enumerate", } +GARD_CLEAR_URL = "/org/open_power/control/gard/action/Reset" + INVENTORY_URL = "/inventory/enumerate" LEDS_URL = "/led/physical/enumerate" @@ -166,6 +168,8 @@ RSPCONFIG_NETINFO_URL = { 'ntpserver': "/network/#NIC#/attr/NTPServers", } +PASSWD_URL = '/user/root/action/SetPassword' + RSPCONFIG_APIS = { 'hostname': { 'baseurl': "/network/config/", @@ -571,6 +575,11 @@ class OpenBMCRest(object): data={"data": attr_info['get_data']} return self.request(method, get_url, payload=data, cmd="get_%s" % key) + def set_admin_passwd(self, passwd): + + payload = { "data": [passwd] } + self.request('POST', PASSWD_URL, payload=payload, cmd='set_admin_password') + def clear_dump(self, clear_arg): if clear_arg == 'all': @@ -612,6 +621,12 @@ class OpenBMCRest(object): path = DUMP_URLS['download'].replace('#ID#', download_id) self.download('GET', path, file_path, headers=headers, cmd='download_dump') + def clear_gard(self): + + payload = { "data": [] } + url = HTTP_PROTOCOL + self.bmcip + GARD_CLEAR_URL + return self.request('POST', url, payload=payload, cmd='clear_gard') + def get_netinfo(self): data = self.request('GET', RSPCONFIG_NETINFO_URL['get_netinfo'], cmd="get_netinfo") try: diff --git a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py index 4d089d1c8..29ca44559 100644 --- a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py +++ b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py @@ -91,13 +91,15 @@ RSPCONFIG_SET_OPTIONS = { 'powersupplyredundancy':"^enabled$|^disabled$", 'powerrestorepolicy':"^always_on$|^always_off$|^restore$", 'bootmode':"^regular$|^safe$|^setup$", + 'admin_passwd':'.*,.*', } RSPCONFIG_USAGE = """ Handle rspconfig operations. Usage: rspconfig -h|--help - rspconfig dump [[-l|--list] | [-g|--generate] | [-c|--clear ] | [-d|--download ]] [-V|--verbose] + rspconfig dump [[-l|--list] | [-g|--generate] | [-c|--clear --id ] | [-d|--download --id ]] [-V|--verbose] + rspconfig gard -c|--clear [-V|--verbose] rspconfig sshcfg [-V|--verbose] rspconfig ip=dhcp [-V|--verbose] rspconfig get [...] [-V|--verbose] @@ -107,8 +109,9 @@ Options: -V,--verbose Show verbose message -l,--list List are dump files -g,--generate Trigger a new dump file - -c,--clear The id of file to clear or all if specify 'all' - -d,--download The id of file to download or all if specify 'all' + -c,--clear To clear the specified dump file + -d,--download To download specified dump file + --id The dump file id or 'all' The supported attributes to get are: %s @@ -724,11 +727,14 @@ class OpenBMCManager(base.BaseManager): elif opts['--generate']: DefaultBmcConfigManager().dump_generate(runner) elif opts['--clear']: - DefaultBmcConfigManager().dump_clear(runner, opts['--clear'][0]) + DefaultBmcConfigManager().dump_clear(runner, opts['--id']) elif opts['--download']: - DefaultBmcConfigManager().dump_download(runner, opts['--download'][0]) + DefaultBmcConfigManager().dump_download(runner, opts['--id']) else: DefaultBmcConfigManager().dump_process(runner) + elif opts['gard']: + if opts['--clear']: + DefaultBmcConfigManager().gard_clear(runner) elif opts['sshcfg']: DefaultBmcConfigManager().set_sshcfg(runner) elif opts['ip=dhcp']: diff --git a/xCAT-server/lib/xcat/plugins/openbmc2.pm b/xCAT-server/lib/xcat/plugins/openbmc2.pm index 439c99dc6..8a29ae223 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc2.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc2.pm @@ -344,13 +344,18 @@ sub refactor_args { my $extrargs = $request->{arg}; if ($command eq "rspconfig") { my $subcommand = $extrargs->[0]; - if ($subcommand !~ /^dump$|^sshcfg$|^ip=dhcp$/) { + if ($subcommand !~ /^dump$|^sshcfg$|^ip=dhcp$|^gard$/) { if (grep /=/, @$extrargs) { unshift @$extrargs, "set"; } else { unshift @$extrargs, "get"; } } + if ($subcommand eq "dump") { + if (defined($extrargs->[1]) and $extrargs->[1] =~ /-c|--clear|-d|--download/){ + splice(@$extrargs, 2, 0, "--id"); + } + } } return 0; }