Skip to content

Commit

Permalink
Improve getting user name for Yugabyte DB builds
Browse files Browse the repository at this point in the history
Summary:
Added logic to get user name from whoami if there is no USER environment (try for docker).
Same was made for get_version_info.py by copy-pasting get_username from run_tests_on_spark.py as there is no simple way to re-use.

Test Plan:
- unset USER
- build Yugabyte locally with yb_build.sh - it passes with this patch and fails without it

Reviewers: mikhail, bogdan, jharveysmith, steve.varnau

Reviewed By: steve.varnau

Subscribers: jharveysmith, steve.varnau, #devops

Differential Revision: https://phabricator.dev.yugabyte.com/D9788
  • Loading branch information
skorobogatydmitry committed Nov 24, 2020
1 parent 3b23991 commit c1174bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .arclint
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"type": "pep8",
"include": "(\\.py$)",
"exclude": "(^thirdparty/|^[.]ycm_extra_conf[.]py$)",
"bin": "pycodestyle"
"bin": "pycodestyle",
"flags": ["--max-line-length=100"]
},
"googlecpplint": {
"type": "googlecpplint",
Expand Down
8 changes: 8 additions & 0 deletions build-support/compiler-wrappers/compiler-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ if command -v ccache >/dev/null && ! "$compiling_pch" && [[ -z ${YB_NO_CCACHE:-}
export CCACHE_SLOPPINESS="file_macro,pch_defines,time_macros"
export CCACHE_BASEDIR=$YB_SRC_ROOT

if [ -z "${USER:-}" ]; then
if whoami &> /dev/null; then
USER="$(whoami)"
else
fatal_error "No USER variable in env and no whoami to detect it"
fi
fi

# Ensure CCACHE puts temporary files on the local disk.
export CCACHE_TEMPDIR=${CCACHE_TEMPDIR:-/tmp/ccache_tmp_$USER}
if [[ -n ${YB_CCACHE_DIR:-} ]]; then
Expand Down
25 changes: 24 additions & 1 deletion build-support/gen_version_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import argparse
import os
import re
import pwd
import subprocess
import sys
import time
Expand Down Expand Up @@ -92,7 +93,29 @@ def main():

hostname = socket.gethostname()
build_time = "%s %s" % (strftime("%d %b %Y %H:%M:%S", localtime()), time.tzname[0])
username = os.getenv("USER")

logging.info('Get user name')
try:
username = os.getlogin()
except OSError as ex:
logging.warning(("Got an OSError trying to get the current user name, " +
"trying a workaround: {}").format(ex))
# https://github.com/gitpython-developers/gitpython/issues/39
try:
username = pwd.getpwuid(os.getuid()).pw_name
except KeyError:
username = os.getenv('USER')
if not username:
id_output = subprocess.check_output('id').strip()
ID_OUTPUT_RE = re.compile(r'^uid=\d+[(]([^)]+)[)]\s.*')
match = ID_OUTPUT_RE.match(id_output)
if match:
username = match.group(1)
else:
logging.warning(
"Couldn't get user name from the environment, either parse 'id' output: %s",
id_output)
raise

git_repo_dir = get_yb_src_root_from_build_root(os.getcwd(), must_succeed=False, verbose=True)
clean_repo = bool(git_repo_dir) and is_git_repo_clean(git_repo_dir)
Expand Down

0 comments on commit c1174bb

Please sign in to comment.