Skip to content

Commit ee7a050

Browse files
Merge pull request #271 from ISISComputingGroup/logging
logging
2 parents 58b8ddc + 5a6e148 commit ee7a050

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

installation_and_upgrade/ibex_install_utils/admin_runner.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from time import sleep
88
from typing import Any, Generator
99

10+
from ibex_install_utils.logger import temporarily_disable_logging
11+
1012
# Plink is an SSH binary bundled with putty.
1113
# Use Plink as it allows passwords on the command-line, as opposed to
1214
# windows-bundled SSH which does not.
@@ -30,8 +32,9 @@ class AdminRunner:
3032
def _auth_ssh(cls) -> None:
3133
if not cls._ssh_authenticated:
3234
while True:
33-
cls._ssh_user = input("Enter admin username (without domain): ")
34-
cls._ssh_password = getpass.getpass("Enter admin password: ")
35+
with temporarily_disable_logging():
36+
cls._ssh_user = input("Enter admin username (without domain): ")
37+
cls._ssh_password = getpass.getpass("Enter admin password: ")
3538

3639
assert cls._ssh_user is not None
3740
assert cls._ssh_password is not None

installation_and_upgrade/ibex_install_utils/logger.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
11
import os
22
import sys
33
import time
4+
from contextlib import contextmanager
5+
from typing import Generator
6+
7+
ENABLE_LOGGING = True
8+
9+
10+
@contextmanager
11+
def temporarily_disable_logging() -> Generator[None, None, None]:
12+
global ENABLE_LOGGING
13+
previous_logging_enabled = ENABLE_LOGGING
14+
ENABLE_LOGGING = False
15+
try:
16+
yield
17+
finally:
18+
ENABLE_LOGGING = previous_logging_enabled
419

520

621
class Logger:
722
"""
823
Logger class used to capture output and input to a log file.
924
"""
1025

11-
def __init__(self):
12-
CURRENT_DATE = time.strftime("%Y%m%d")
13-
LOG_FILE = f"DEPLOY-{CURRENT_DATE}.log"
14-
LOG_DIRECTORY = os.path.join("C:\\", "Instrument", "var", "logs", "deploy")
15-
os.makedirs(LOG_DIRECTORY, exist_ok=True)
16-
LOG_PATH = os.path.join(LOG_DIRECTORY, LOG_FILE)
26+
def __init__(self) -> None:
27+
current_date = time.strftime("%Y%m%d")
28+
log_file = f"DEPLOY-{current_date}.log"
29+
log_directory = os.path.join("C:\\", "Instrument", "var", "logs", "deploy")
30+
os.makedirs(log_directory, exist_ok=True)
31+
log_path = os.path.join(log_directory, log_file)
1732

1833
self.console = sys.stdout
1934
self.input = sys.stdin
20-
self.log = open(LOG_PATH, "a")
21-
print(f"Log file is {LOG_PATH}")
22-
23-
def write(self, message):
24-
self.log.write(message)
35+
self.log = open(log_path, "a")
36+
print(f"Log file is {log_path}")
37+
38+
def write(self, message: str) -> int:
39+
if ENABLE_LOGGING:
40+
self.log.write(message)
41+
else:
42+
self.log.write("[concealed]\n")
2543
return self.console.write(message)
2644

27-
def flush(self):
45+
def flush(self) -> None:
2846
self.console.flush()
2947
self.log.flush()
3048

31-
def readline(self):
49+
def readline(self) -> str:
3250
text = self.input.readline()
33-
self.log.write(text)
51+
if ENABLE_LOGGING:
52+
self.log.write(text)
53+
else:
54+
self.log.write("[concealed]\n")
3455
return text
3556

3657
@staticmethod
37-
def set_up():
58+
def set_up() -> None:
3859
logger = Logger()
3960
sys.stdout = logger
4061
sys.stderr = logger

installation_and_upgrade/ibex_install_utils/tasks/mysql_tasks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ibex_install_utils.admin_runner import AdminCommandBuilder
1010
from ibex_install_utils.exceptions import ErrorInRun
11+
from ibex_install_utils.logger import temporarily_disable_logging
1112
from ibex_install_utils.run_process import RunProcess
1213
from ibex_install_utils.software_dependency.mysql import MySQL
1314
from ibex_install_utils.task import task
@@ -217,12 +218,13 @@ def _setup_database_users_and_tables(self, vhd_install: bool = True) -> None:
217218
sql_password = ""
218219
retry_count = 5
219220
while --retry_count > 0:
220-
sql_password = self.prompt.prompt(
221-
"Enter the MySQL root password:",
222-
UserPrompt.ANY,
223-
os.getenv("MYSQL_PASSWORD", "environment variable not set"),
224-
show_automatic_answer=False,
225-
).strip()
221+
with temporarily_disable_logging():
222+
sql_password = self.prompt.prompt(
223+
"Enter the MySQL root password:",
224+
UserPrompt.ANY,
225+
os.getenv("MYSQL_PASSWORD", "environment variable not set"),
226+
show_automatic_answer=False,
227+
).strip()
226228
if len(sql_password) > 0:
227229
break
228230
print("Please enter a non blank password")

0 commit comments

Comments
 (0)