public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Bjorge, Erik C" <erik.c.bjorge@intel.com>
To: "Desimone, Nathaniel L" <nathaniel.l.desimone@intel.com>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>
Cc: "Desimone, Ashley E" <ashley.e.desimone@intel.com>,
	"Pandya, Puja" <puja.pandya@intel.com>,
	Bret Barkelew <Bret.Barkelew@microsoft.com>,
	"Agyeman, Prince" <prince.agyeman@intel.com>
Subject: Re: [PATCH] EdkRepo: Fix Linux install failures
Date: Tue, 21 Apr 2020 16:00:40 +0000	[thread overview]
Message-ID: <MW3PR11MB455488679D5597795AA713D7AED50@MW3PR11MB4554.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20200421034406.6541-1-nathaniel.l.desimone@intel.com>

Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com>

-----Original Message-----
From: Desimone, Nathaniel L <nathaniel.l.desimone@intel.com> 
Sent: Monday, April 20, 2020 8:44 PM
To: devel@edk2.groups.io
Cc: Desimone, Ashley E <ashley.e.desimone@intel.com>; Pandya, Puja <puja.pandya@intel.com>; Bjorge, Erik C <erik.c.bjorge@intel.com>; Bret Barkelew <Bret.Barkelew@microsoft.com>; Agyeman, Prince <prince.agyeman@intel.com>
Subject: [PATCH] EdkRepo: Fix Linux install failures

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 <nathaniel.l.desimone@intel.com>
Cc: Ashley E Desimone <ashley.e.desimone@intel.com>
Cc: Puja Pandya <puja.pandya@intel.com>
Cc: Erik Bjorge <erik.c.bjorge@intel.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Prince Agyeman <prince.agyeman@intel.com>
---
 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


  reply	other threads:[~2020-04-21 16:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21  3:44 [PATCH] EdkRepo: Fix Linux install failures Nate DeSimone
2020-04-21 16:00 ` Bjorge, Erik C [this message]
2020-04-21 17:42 ` Ashley E Desimone

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MW3PR11MB455488679D5597795AA713D7AED50@MW3PR11MB4554.namprd11.prod.outlook.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox