From 29f6fa75cfa0dd6e2d98a07891fc4d7f295f7e3b Mon Sep 17 00:00:00 2001 From: XuWei Date: Mon, 26 Feb 2018 03:37:08 -0500 Subject: [PATCH 1/3] 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; } From d914a4a09cdb3b0e1a034382462db00afdac8245 Mon Sep 17 00:00:00 2001 From: XuWei Date: Tue, 27 Feb 2018 01:42:35 -0500 Subject: [PATCH 2/3] rspconfig ntpservers --- .../agent/hwctl/executor/openbmc_bmcconfig.py | 51 ++++++++++++++++--- .../lib/python/agent/hwctl/openbmc_client.py | 14 ++++- .../lib/python/agent/xcatagent/openbmc.py | 3 +- xCAT-server/lib/xcat/plugins/openbmc2.pm | 3 +- 4 files changed, 60 insertions(+), 11 deletions(-) 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 5b29ced08..9524b0d1b 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 @@ -23,7 +23,7 @@ from scp import SCPClient import logging logger = logging.getLogger('xcatagent') -RSPCONFIG_GET_NETINFO=['ip', 'netmask', 'gateway', 'vlan', 'ipsrc', 'hostname'] +RSPCONFIG_GET_NETINFO=['ip', 'netmask', 'gateway', 'vlan', 'ipsrc', 'hostname', 'ntpservers'] RSPCONFIG_SET_NETINFO=['ip', 'netmask', 'gateway', 'vlan'] XCAT_LOG_DUMP_DIR = "/var/log/xcat/dump/" @@ -275,7 +275,8 @@ rmdir \"/tmp/$userid\" \n") if len(netinfo_dict): self._get_netinfo(ip=netinfo_dict.get('ip', False), ipsrc=netinfo_dict.get('ipsrc', False), netmask=netinfo_dict.get('netmask', False), gateway=netinfo_dict.get('gateway', False),vlan= netinfo_dict.get('vlan', False), - hostname=netinfo_dict.get('hostname', False), **kw) + hostname=netinfo_dict.get('hostname', False), + ntpservers=netinfo_dict.get('ntpservers', False), **kw) def set_attributes(self, attributes, **kw): netinfo_dict={'vlan':False} @@ -287,6 +288,8 @@ rmdir \"/tmp/$userid\" \n") self._set_hostname(v, **kw) elif k == 'admin_passwd': self._set_admin_password(v, **kw) + elif k == 'ntpservers': + self._set_ntp_servers(v, **kw) elif k in openbmc.RSPCONFIG_APIS: self._set_apis_values(k, v, **kw) else: @@ -309,6 +312,40 @@ rmdir \"/tmp/$userid\" \n") self._get_netinfo(hostname=True, ntpserver=False, **kw) return + def _set_ntp_servers(self, servers, **kw): + node = kw['node'] + node_info = kw['nodeinfo'] + obmc = openbmc.OpenBMCRest(name=node, nodeinfo=node_info, messager=self.callback, + debugmode=self.debugmode, verbose=self.verbose) + + try: + obmc.login() + netinfo = obmc.get_netinfo() + except (SelfServerException, SelfClientException) as e: + self.callback.info('%s: %s' % (node, e.message)) + return + + if not netinfo: + return self.callback.error("%s: No network information get" % node) + + nic = None + for k,v in netinfo.items(): + if 'ip' in v and v['ip'] == node_info['bmcip']: + nic = k + + try: + obmc.set_ntp_servers(nic, servers) + self.callback.info('%s: BMC Setting NTPServers...' % node) + netinfo = obmc.get_netinfo() + except (SelfServerException, SelfClientException) as e: + self.callback.info('%s: %s' % (node, e.message)) + return + + ntpservers = None + if nic in netinfo: + ntpservers = netinfo[nic]['ntpservers'] + self.callback.info('%s: BMC NTP Servers: %s' % (node, ntpservers)) + def _set_admin_password(self, admin_passwd, **kw): node = kw['node'] node_info = kw['nodeinfo'] @@ -362,7 +399,7 @@ rmdir \"/tmp/$userid\" \n") result = "set net(%s, %s, %s) for eth0" % (ip, netmask, gateway) return self.callback.info("set_netinfo %s" % result) - def _get_netinfo(self, ip=False, ipsrc=False, netmask=False, gateway=False, vlan=False, hostname=False, ntpserver=True, **kw): + def _get_netinfo(self, ip=False, ipsrc=False, netmask=False, gateway=False, vlan=False, hostname=False, ntpservers=False, **kw): node = kw['node'] obmc = openbmc.OpenBMCRest(name=node, nodeinfo=kw['nodeinfo'], messager=self.callback, debugmode=self.debugmode, verbose=self.verbose) @@ -387,7 +424,7 @@ rmdir \"/tmp/$userid\" \n") self.callback.info("%s: BMC Hostname: %s" %(node, bmchostname)) dic_length = len(netinfo) netinfodict = {'ip':[], 'netmask':[], 'gateway':[], - 'vlan':[], 'ipsrc':[], 'ntpserver':[]} + 'vlan':[], 'ipsrc':[], 'ntpservers':[]} for nic,attrs in netinfo.items(): addon_string = '' if dic_length > 1: @@ -397,7 +434,7 @@ rmdir \"/tmp/$userid\" \n") netinfodict['gateway'].append("BMC Gateway"+addon_string+": %s (default: %s)" % (attrs["gateway"], defaultgateway)) netinfodict['vlan'].append("BMC VLAN ID"+addon_string+": %s" % attrs["vlanid"]) netinfodict['ipsrc'].append("BMC IP Source"+addon_string+": %s" % attrs["ipsrc"]) - netinfodict['ntpserver'].append("BMC NTP Servers"+addon_string+": %s" % attrs["ntpservers"]) + netinfodict['ntpservers'].append("BMC NTP Servers"+addon_string+": %s" % attrs["ntpservers"]) if ip: for i in netinfodict['ip']: self.callback.info("%s: %s" % (node, i)) @@ -413,7 +450,7 @@ rmdir \"/tmp/$userid\" \n") if vlan: for i in netinfodict['vlan']: self.callback.info("%s: %s" % (node, i)) - if ntpserver: - for i in netinfodict['netserver']: + if ntpservers: + for i in netinfodict['ntpservers']: self.callback.info("%s: %s" % (node, i)) return netinfo 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 55ced2396..7c0e6e974 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py @@ -165,7 +165,7 @@ RSPCONFIG_NETINFO_URL = { 'nic_ip': "/network/#NIC#/action/IP", 'vlan': "/network/action/VLAN", 'ipdhcp': "/network/action/Reset", - 'ntpserver': "/network/#NIC#/attr/NTPServers", + 'ntpservers': "/network/#NIC#/attr/NTPServers", } PASSWD_URL = '/user/root/action/SetPassword' @@ -580,6 +580,12 @@ class OpenBMCRest(object): payload = { "data": [passwd] } self.request('POST', PASSWD_URL, payload=payload, cmd='set_admin_password') + def set_ntp_servers(self, nic, servers): + + payload = { "data": [servers] } + url = RSPCONFIG_NETINFO_URL['ntpservers'].replace('#NIC#', nic) + self.request('PUT', url, payload=payload, cmd='set_ntp_servers') + def clear_dump(self, clear_arg): if clear_arg == 'all': @@ -659,7 +665,11 @@ class OpenBMCRest(object): info = data[dev] utils.update2Ddict(netinfo, nicid, "vlanid", info.get("Id", "Disable")) utils.update2Ddict(netinfo, nicid, "mac", info["MACAddress"]) - utils.update2Ddict(netinfo, nicid, "ntpservers", info["NTPServers"]) + ntpservers = None + tmp_ntpservers = ''.join(info["NTPServers"]) + if tmp_ntpservers: + ntpservers = tmp_ntpservers + utils.update2Ddict(netinfo, nicid, "ntpservers", ntpservers) return netinfo except KeyError: error = 'Error: Received wrong format response: %s' % data diff --git a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py index 29ca44559..19e8e9726 100644 --- a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py +++ b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py @@ -80,13 +80,14 @@ RFLASH_URLS = { } } -RSPCONFIG_GET_OPTIONS = ['ip','ipsrc','netmask','gateway','vlan','hostname','bootmode','autoreboot','powersupplyredundancy','powerrestorepolicy'] +RSPCONFIG_GET_OPTIONS = ['ip','ipsrc','netmask','gateway','vlan','ntpservers','hostname','bootmode','autoreboot','powersupplyredundancy','powerrestorepolicy'] RSPCONFIG_SET_OPTIONS = { 'ip':'.*', 'netmask':'.*', 'gateway':'.*', 'vlan':'\d+', 'hostname':"\*|.*", + 'ntpservers':'.*', 'autoreboot':"^0|1$", 'powersupplyredundancy':"^enabled$|^disabled$", 'powerrestorepolicy':"^always_on$|^always_off$|^restore$", diff --git a/xCAT-server/lib/xcat/plugins/openbmc2.pm b/xCAT-server/lib/xcat/plugins/openbmc2.pm index 8a29ae223..975cafaaf 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc2.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc2.pm @@ -342,8 +342,9 @@ sub refactor_args { my $request = shift; my $command = $request->{command}->[0]; my $extrargs = $request->{arg}; + my $subcommand; if ($command eq "rspconfig") { - my $subcommand = $extrargs->[0]; + $subcommand = $extrargs->[0]; if ($subcommand !~ /^dump$|^sshcfg$|^ip=dhcp$|^gard$/) { if (grep /=/, @$extrargs) { unshift @$extrargs, "set"; From 32c8e3a21a63525943b40b962c162ca8bf99bc2d Mon Sep 17 00:00:00 2001 From: XuWei Date: Thu, 1 Mar 2018 02:44:29 -0500 Subject: [PATCH 3/3] modified depending on comments --- .../agent/hwctl/executor/openbmc_bmcconfig.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 9524b0d1b..aa91a56be 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 @@ -328,10 +328,10 @@ rmdir \"/tmp/$userid\" \n") if not netinfo: return self.callback.error("%s: No network information get" % node) - nic = None - for k,v in netinfo.items(): - if 'ip' in v and v['ip'] == node_info['bmcip']: - nic = k + bmcip = node_info['bmcip'] + nic = self._get_facing_nic(bmcip, netinfo) + if not nic: + return self.callback.error('%s: Can not get facing NIC for %s' % (node, bmcip)) try: obmc.set_ntp_servers(nic, servers) @@ -346,6 +346,12 @@ rmdir \"/tmp/$userid\" \n") ntpservers = netinfo[nic]['ntpservers'] self.callback.info('%s: BMC NTP Servers: %s' % (node, ntpservers)) + def _get_facing_nic(self, bmcip, netinfo): + for k,v in netinfo.items(): + if 'ip' in v and v['ip'] == bmcip: + return k + return None + def _set_admin_password(self, admin_passwd, **kw): node = kw['node'] node_info = kw['nodeinfo']