You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2021/03/31 15:07:41 UTC

[trafficcontrol] branch master updated: Add `goose` to the `PATH` and log to `stdout` (#5693)

This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new c928be0  Add `goose` to the `PATH` and log to `stdout` (#5693)
c928be0 is described below

commit c928be04412f9054c5426774a0961c1cbd4a8d7a
Author: Zach Hoffman <zr...@apache.org>
AuthorDate: Wed Mar 31 09:07:18 2021 -0600

    Add `goose` to the `PATH` and log to `stdout` (#5693)
    
    * Use Popen.communicate() to get process output
    
    * Cast dict_values to list
    
    * Add goose to the path
    
    * Log to stdout, not stderr
    
    * Python binary options: Second 3.8 should be 3.6
    
    * More platform-python versions
---
 traffic_ops/install/bin/_postinstall        | 21 +++++++++++++--------
 traffic_ops/install/bin/postinstall         |  3 +++
 traffic_ops/install/bin/postinstall.test.sh | 13 +++++++------
 3 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/traffic_ops/install/bin/_postinstall b/traffic_ops/install/bin/_postinstall
index ca49478..f4a22b0 100755
--- a/traffic_ops/install/bin/_postinstall
+++ b/traffic_ops/install/bin/_postinstall
@@ -1,5 +1,5 @@
 #!/usr/bin/env bash
-"exec" "bash" "-c" "PATH+=:/usr/libexec/; exec \$(type -p python38 python3.8 python36 python3.8 python3 python python27 python2.7 python2 platform-python | head -n1) \"$0\" $*"
+"exec" "bash" "-c" "PATH+=:/usr/libexec/; exec \$(type -p python38 python3.8 python36 python3.6 python3 python27 python2.7 python2 python platform-python38 platform-python3.8 platform-python36 platform-python3.6 platform-python3 platform-python27 platform-python2.7 platform-python2 platform-python | head -n1) \"$0\" $*"
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -777,7 +777,8 @@ def exec_openssl(description, *cmd_args): # type: (str, ...) -> bool
 		if proc.returncode == 0:
 			return True
 
-		logging.debug("openssl exec failed with code %s; stderr: %s", proc.returncode, proc.stderr)
+		output = proc.communicate()
+		logging.debug("openssl exec failed with code %s; stderr: %s", proc.returncode, output[1])
 		while True:
 			ans = input("{description} failed. Try again (y/n) [y]: ".format(description=description))
 			if not ans or ans.lower().startswith('n'):
@@ -1060,7 +1061,7 @@ def db_connection_string(dbconf): # type: (dict) -> str
 	port = dbconf["port"]
 	return "postgresql://{user}:{password}@{hostname}:{port}/{db_name}".format(user=user, password=password, hostname=hostname, port=port, db_name=db_name)
 
-def exec_psql(conn_str, query): # type: (str, str) -> str
+def exec_psql(conn_str, query, **args): # type: (str, str, dict) -> str
 	"""
 	Executes SQL queries by forking and exec-ing '/usr/bin/psql'.
 
@@ -1069,7 +1070,7 @@ def exec_psql(conn_str, query): # type: (str, str) -> str
 	:param query: The query to be run. It can actually be a script containing multiple queries.
 	:returns: The comma-separated columns of each line-delimited row of the results of the query.
 	"""
-	cmd = ["/usr/bin/psql", "--tuples-only", "-d", conn_str, "-c", query]
+	cmd = ["/usr/bin/psql", "--tuples-only", "-d", conn_str, "-c", query] + list(args.values())
 	proc = subprocess.Popen(
 		cmd,
 		stderr=subprocess.PIPE,
@@ -1077,13 +1078,14 @@ def exec_psql(conn_str, query): # type: (str, str) -> str
 		universal_newlines=True,
 	)
 	proc.wait()
+	output = proc.communicate()
 	if proc.returncode != 0:
-		logging.debug("psql exec failed; stderr: %s\n\tstdout: %s", proc.stderr, proc.stdout)
+		logging.debug("psql exec failed; stderr: %s\n\tstdout: %s", output[1], output[0])
 		raise OSError("failed to execute database query")
 	if sys.version_info.major >= 3:
-		return proc.stdout.read().strip()
+		return output[0].strip()
 	else:
-		return string.strip(proc.stdout.read())
+		return string.strip(output[0])
 
 def invoke_db_admin_pl(action, root): # type: (str, str) -> None
 	"""
@@ -1376,8 +1378,9 @@ no_database, # type: bool
 			if proc.wait():
 				raise subprocess.CalledProcessError(proc.returncode, cmd)
 		except (subprocess.CalledProcessError, OSError) as e:
+			output = proc.communicate()
 			logging.critical("Failed to restart Traffic Ops, return code %s: %s", e.returncode, e)
-			logging.debug("stderr: %s\n\tstdout: %s", proc.stderr, proc.stdout)
+			logging.debug("stderr: %s\n\tstdout: %s", output[1], output[0])
 			return 1
 		except OSError as e:
 			logging.critical("Failed to restart Traffic Ops: unknown error occurred: %s", e)
@@ -1392,6 +1395,8 @@ no_database, # type: bool
 	return 0
 
 if __name__ == '__main__':
+	logging.basicConfig(stream=sys.stdout)
+
 	PARSER = argparse.ArgumentParser()
 	PARSER.add_argument(
 		"-a",
diff --git a/traffic_ops/install/bin/postinstall b/traffic_ops/install/bin/postinstall
index a121852..f46d4a4 100755
--- a/traffic_ops/install/bin/postinstall
+++ b/traffic_ops/install/bin/postinstall
@@ -43,6 +43,9 @@ fi
 # Install go and goose
 /opt/traffic_ops/install/bin/install_goose.sh
 
+# Add goose to the PATH
+source /etc/profile.d/traffic_ops.sh
+
 /opt/traffic_ops/install/bin/_postinstall "$@"
 
 # should all be owned by trafops user
diff --git a/traffic_ops/install/bin/postinstall.test.sh b/traffic_ops/install/bin/postinstall.test.sh
index be5a62b..d4f7cb5 100755
--- a/traffic_ops/install/bin/postinstall.test.sh
+++ b/traffic_ops/install/bin/postinstall.test.sh
@@ -115,7 +115,7 @@ cat > "$ROOT_DIR/opt/traffic_ops/app/conf/cdn.conf" <<EOF
 }
 EOF
 
-"$python_bin" <<SETUP_MAXMIND_TEST 2>/dev/null | tee -a "${ROOT_DIR}/stdout";
+"$python_bin" <<TESTS 2>/dev/null | tee -a "${ROOT_DIR}/stdout";
 from __future__ import print_function
 import subprocess
 import sys
@@ -151,7 +151,9 @@ else:
 	except Exception as e:
 		print(unexpected_exception_message.format(exception_type=type(e).__name__), file=sys.stderr)
 		exit(1)
-SETUP_MAXMIND_TEST
+
+_postinstall.exec_psql('N/A', 'N/A', '--version')
+TESTS
 
 mkdir -p "$ROOT_DIR/opt/traffic_ops/install/data/json";
 mkdir "$ROOT_DIR/opt/traffic_ops/install/bin";
@@ -364,12 +366,11 @@ cat <<- EOF > "$ROOT_DIR/defaults.json"
 }
 EOF
 
-"$python_bin" "$MY_DIR/_postinstall" --no-root --root-directory="$ROOT_DIR" --no-restart-to --no-database --ops-user="$(whoami)" --ops-group="$(id -gn)" --automatic --cfile="$ROOT_DIR/defaults.json" --debug 2>>"$ROOT_DIR/stderr" | tee -a "$ROOT_DIR/stdout"
+"$python_bin" "$MY_DIR/_postinstall" --no-root --root-directory="$ROOT_DIR" --no-restart-to --no-database --ops-user="$(whoami)" --ops-group="$(id -gn)" --automatic --cfile="$ROOT_DIR/defaults.json" --debug > >(tee -a "$ROOT_DIR/stdout") 2> >(tee -a "$ROOT_DIR/stderr" >&2);
 
-if grep -q 'ERROR' $ROOT_DIR/stderr; then
+if grep -q 'ERROR' $ROOT_DIR/stdout; then
 	echo "Errors found in script logs" >&2;
-	cat "$ROOT_DIR/stderr";
-	cat "$ROOT_DIR/stdout";
+	cat "$ROOT_DIR/stdout" "$ROOT_DIR/stderr";
 	exit 1;
 fi