def execute_cmd(cmd, user=None):
LOG.debug("Executing cmd:\n%s", cmd_str(cmd))
kwargs = {
"shell": True,
"stdin": sys.stdin,
"stdout": sys.stdout,
"stderr": sys.stderr}
# If openstackclient command is being executed, appropriate environment
# variables will be set
for prefix in ["openstack ", "neutron ", "murano "]:
if cmd.startswith(prefix):
kwargs['preexec_fn'] = openstackclient_preexec_fn()
break
# Execute as user if `user` param is provided, execute as current user
# otherwise
else:
if user:
LOG.debug('Executing as user %s', user)
pw_record = pwd.getpwnam(user)
user_uid = pw_record.pw_uid
user_gid = pw_record.pw_gid
user_home = pw_record.pw_dir
kwargs['preexec_fn'] = preexec_fn(user_uid, user_gid, user_home)
return subprocess.Popen(cmd_str(cmd), **kwargs)
评论列表
文章目录