Add support to run as sudo user on BMC
Change-Id: I81ee5536fc1b59e70a8e7f5987babdfe412b445a
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/bmc_ssh_utils.py b/lib/bmc_ssh_utils.py
index 3aa90fa..7f7c23f 100755
--- a/lib/bmc_ssh_utils.py
+++ b/lib/bmc_ssh_utils.py
@@ -4,6 +4,7 @@
This module provides many valuable bmc ssh functions such as bmc_execute_command.
"""
+import os
import gen_valid as gv
import gen_robot_ssh as grs
from robot.libraries.BuiltIn import BuiltIn
@@ -62,6 +63,10 @@
'timeout': '25.0', 'prompt': '# ', 'port': ssh_port}
login_args = {'username': openbmc_username, 'password': openbmc_password}
+ openbmc_user_type = os.environ.get('USER_TYPE', "") or \
+ BuiltIn().get_variable_value("${USER_TYPE}", default="")
+ if openbmc_user_type == 'sudo':
+ cmd_buf = 'sudo ' + cmd_buf
return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
print_out, print_err, ignore_err, fork,
quiet, test_mode, time_out)
@@ -78,7 +83,6 @@
os_host="",
os_username="",
os_password=""):
-
r"""
Run the given command in an OS SSH session and return the stdout, stderr and the return code.
diff --git a/lib/resource.robot b/lib/resource.robot
index f5d0d81..e8b5ccf 100755
--- a/lib/resource.robot
+++ b/lib/resource.robot
@@ -33,6 +33,9 @@
${OPENBMC_ADMIN_PASSWORD} 0penBmc
${SERVICE_USER_PASSWORD} 0penBmc
+# For users priviledge admin or sudo.
+${USER_TYPE} ${EMPTY}
+
${CHASSIS_ID} chassis
${COMPONENT_NAME_OF_POWER_SUPPLY} powersupply
diff --git a/lib/state.py b/lib/state.py
index b688566..26c3f79 100755
--- a/lib/state.py
+++ b/lib/state.py
@@ -631,8 +631,8 @@
if 'uptime' in req_states:
# Sometimes reading uptime results in a blank value. Call with
# wait_until_keyword_succeeds to ensure a non-blank value is obtained.
- remote_cmd_buf = "read uptime filler 2>/dev/null < /proc/uptime" +\
- " && [ ! -z \"${uptime}\" ] && echo ${uptime}"
+ remote_cmd_buf = "bash -c 'read uptime filler 2>/dev/null < /proc/uptime" +\
+ " && [ ! -z \"${uptime}\" ] && echo ${uptime}'"
cmd_buf = ["BMC Execute Command",
re.sub('\\$', '\\$', remote_cmd_buf), 'quiet=1',
'test_mode=0', 'time_out=5']