From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.2552.1587440663039021762 for ; Mon, 20 Apr 2020 20:44:23 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.88, mailfrom: nathaniel.l.desimone@intel.com) IronPort-SDR: OqF20OZ3Jr/hxjxHyhylCIpEL9+Sfc9vqPv52IxVlLMf0+/rp21a9O4x7UmQiKf953liepMCJI WICkQJc6CNSw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2020 20:44:22 -0700 IronPort-SDR: RNItqRMNQJqzKV2y8oY2I0xh51slXOvrYkvbqAm0cSmZJVBLDUGfTFpxSmPTmgB2fePZAm6HdN 1IQQpfH5uNtg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,408,1580803200"; d="scan'208";a="255152656" Received: from nldesimo-desk1.amr.corp.intel.com ([10.7.159.63]) by orsmga003.jf.intel.com with ESMTP; 20 Apr 2020 20:44:22 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Ashley E Desimone , Puja Pandya , Erik Bjorge , Bret Barkelew , Prince Agyeman Subject: [PATCH] EdkRepo: Fix Linux install failures Date: Mon, 20 Apr 2020 20:44:06 -0700 Message-Id: <20200421034406.6541-1-nathaniel.l.desimone@intel.com> X-Mailer: git-send-email 2.26.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Install script currently attempts to run EdkRepo as root, which fails. The fix ensures that EdkRepo is run under a user account. Signed-off-by: Nate DeSimone Cc: Ashley E Desimone Cc: Puja Pandya Cc: Erik Bjorge Cc: Bret Barkelew Cc: Prince Agyeman --- edkrepo_installer/linux-scripts/install.py | 52 ++++++++++++++++------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/edkrepo_installer/linux-scripts/install.py b/edkrepo_installer/linux-scripts/install.py index feecf49..f3a9248 100755 --- a/edkrepo_installer/linux-scripts/install.py +++ b/edkrepo_installer/linux-scripts/install.py @@ -102,7 +102,7 @@ def is_prompt_customization_installed(user_home_dir): return customization_installed __add_prompt_customization = None -def get_add_prompt_customization(args, user_home_dir): +def get_add_prompt_customization(args, username, user_home_dir): global __add_prompt_customization if __add_prompt_customization is not None: return __add_prompt_customization @@ -118,10 +118,16 @@ def get_add_prompt_customization(args, user_home_dir): return False #If the prompt has not been installed and EdkRepo >= 2.0.0 is installed, then don't install the prompt customization if shutil.which('edkrepo') is not None: - res = default_run(['edkrepo', '--version']) - if _check_version(res.stdout.replace('edkrepo ', '').strip(), '2.0.0') >= 0: - __add_prompt_customization = False - return False + try: + if is_current_user_root(): + res = subprocess.run("echo 'edkrepo --version; exit' | su - {}".format(username), shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True) + else: + res = default_run(['edkrepo', '--version']) + if _check_version(res.stdout.replace('edkrepo ', '').strip(), '2.0.0') >= 0: + __add_prompt_customization = False + return False + except: + pass #Show the user an advertisement to see if they want the prompt customization from select import select import termios @@ -251,6 +257,12 @@ def get_site_packages_directory(): res = default_run([def_python, '-c', 'import site; print(site.getsitepackages()[0])']) return res.stdout.strip() +def is_current_user_root(): + res = default_run(['id', '-u']) + if res.stdout.strip() == '0': + return True + return False + def set_execute_permissions(): site_packages = get_site_packages_directory() config = configparser.ConfigParser(allow_no_value=True, delimiters='=') @@ -457,7 +469,7 @@ def add_command_completions_to_shell(command_completion_script, args, username, comment = '\n# Add EdkRepo command completions' bash_rc_file = os.path.join(user_home_dir, '.bashrc') add_command_comment_to_startup_script(bash_rc_file, regex, new_source_line, comment, username) - if get_add_prompt_customization(args, user_home_dir): + if get_add_prompt_customization(args, username, user_home_dir): add_command_to_startup_script(bash_rc_file, prompt_regex, bash_prompt_customization, username) zsh_rc_file = os.path.join(user_home_dir, '.zshrc') add_command_to_startup_script(zsh_rc_file, zsh_autoload_compinit_regex, zsh_autoload_compinit, username) @@ -467,7 +479,7 @@ def add_command_completions_to_shell(command_completion_script, args, username, add_command_to_startup_script(zsh_rc_file, zsh_compinit_regex, zsh_compinit, username) add_command_to_startup_script(zsh_rc_file, zsh_bashcompinit_regex, zsh_bashcompinit, username) add_command_comment_to_startup_script(zsh_rc_file, regex, new_source_line, comment, username) - if get_add_prompt_customization(args, user_home_dir): + if get_add_prompt_customization(args, username, user_home_dir): add_command_to_startup_script(zsh_rc_file, prompt_regex, zsh_prompt_customization, username) def do_install(): @@ -515,11 +527,11 @@ def do_install(): log.info('\nCollecting system information:') if not install_to_local and ostype != MAC: try: - res = default_run(['id', '-u']) + user_is_root = is_current_user_root() except: log.info('- Failed to determine user ID') return 1 - if res.stdout.strip() != '0': + if not user_is_root: log.info('- Installer must be run as root') return 1 if username is None: @@ -536,17 +548,17 @@ def do_install(): return 1 if ostype == MAC: try: - res = default_run(['id', '-u']) + user_is_root = is_current_user_root() except: log.info('- Failed to determine user ID') return 1 - if res.stdout.strip() == '0': + if user_is_root: log.info('- Installer must NOT be run as root') return 1 if os.path.commonprefix([user_home_dir, sys.executable]) != user_home_dir: install_to_local = True default_cfg_dir = os.path.join(user_home_dir, cfg_dir) - get_add_prompt_customization(args, user_home_dir) + get_add_prompt_customization(args, username, user_home_dir) log.info('+ System information collected') # Display current system information. @@ -728,7 +740,21 @@ def do_install(): else: command_completion_script = os.path.join('/', 'etc', 'profile.d', 'edkrepo_completions.sh') try: - res = default_run(['edkrepo', 'generate-command-completion-script', command_completion_script]) + user_is_root = False + try: + user_is_root = is_current_user_root() + except: + pass + current_home = None + if 'HOME' in os.environ: + current_home = os.environ['HOME'] + try: + if user_is_root and current_home is not None and current_home != user_home_dir: + os.environ['HOME'] = user_home_dir + res = default_run(['edkrepo', 'generate-command-completion-script', command_completion_script]) + finally: + if current_home is not None and os.environ['HOME'] != current_home: + os.environ['HOME'] = current_home if install_to_local or ostype == MAC: shutil.chown(command_completion_script, user=username) os.chmod(command_completion_script, 0o644) -- 2.17.1