Skip to content

Commit de7a4a2

Browse files
feat(mcs): add -v option to enable logging to console
refactor(ci): switch S3 uploading to AWS CLI for logging S3 link docs: update README with to reflect changes in Drone pipeline feat(env): add activate script for portable Python fix(logging): log RequestException response body
1 parent a329f4d commit de7a4a2

File tree

13 files changed

+133
-23
lines changed

13 files changed

+133
-23
lines changed

.drone.jsonnet

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,22 @@ local Pipeline(branch, platform, event, arch='amd64', server='10.6-enterprise')
170170
publish(step_prefix='pkg', eventp=event + '/${DRONE_BUILD_NUMBER}'):: {
171171
name: 'publish ' + step_prefix,
172172
depends_on: [std.strReplace(step_prefix, ' latest', '')],
173-
image: 'plugins/s3-sync',
173+
image: 'amazon/aws-cli',
174174
when: {
175175
status: ['success', 'failure'],
176176
},
177-
settings: {
178-
bucket: 'cspkg',
179-
access_key: {
177+
environment: {
178+
AWS_ACCESS_KEY_ID: {
180179
from_secret: 'aws_access_key_id',
181180
},
182-
secret_key: {
181+
AWS_SECRET_ACCESS_KEY: {
183182
from_secret: 'aws_secret_access_key',
184183
},
185-
source: result,
186-
// branchp has slash if not empty
187-
target: branchp + eventp + '/' + server + '/' + arch + '/' + result,
188-
delete: 'true',
189184
},
185+
commands: [
186+
'aws s3 sync ' + result + ' s3://cspkg/' + branchp + eventp + '/' + server + '/' + arch + '/' + result + ' --delete',
187+
'echo "Data uploaded to: ' + publish_pkg_url + '"'
188+
],
190189
},
191190

192191
local regression_tests = if (event == 'cron') then [

cmapi/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ Packages have bundled python interpreter and python dependencies.
3737
## Get dependencies
3838

3939
# get portable python
40-
wget -qO- https://cspkg.s3.amazonaws.com/python-dist-no-nis.tar.gz | tar xzf - -C ./
40+
wget -qO- https://github.com/indygreg/python-build-standalone/releases/download/20220802/cpython-3.9.13+20220802-x86_64_v2-unknown-linux-gnu-pgo+lto-full.tar.zst | tar --use-compress-program=unzstd -xf - -C ./ && \
41+
mv python pp && mv pp/install python && rm -rf pp
42+
43+
There is a script dev_tools/activate that works like virtualenv activate (you can use it to work with portable Python like with virtualenv).
4144

4245
# install python dependencies
4346
python/bin/pip3 install -t deps --only-binary :all -r requirements.txt

cmapi/cmapi_server/controllers/dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262

263263

264264
def jsonify_error(status, message, traceback, version): \
265-
# pylint: disable=unused-argument
265+
# pylint: disable=unused-argument
266266
"""JSONify all CherryPy error responses (created by raising the
267267
cherrypy.HTTPError exception)
268268
"""

cmapi/cmapi_server/controllers/endpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def put_config(self):
417417
)
418418
if in_maintenance_state():
419419
module_logger.info(
420-
'Maintaninance state is active in new config. '
420+
'Maintenance state is active in new config. '
421421
'MCS processes should not be started.'
422422
)
423423
cherrypy.engine.publish('failover', False)

cmapi/cmapi_server/helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ async def update_config(node, success_nodes, failed_nodes, headers, body):
372372
logging.warning(
373373
f'Timeout while pushing new config to "{node}"'
374374
)
375+
except requests.exceptions.RequestException as e:
376+
logging.warning(
377+
'Error while pushing new config to "%s": %s"', node, str(e)
378+
)
379+
logging.debug('Response: %s', r.text)
375380
except Exception as e:
376381
logging.warning(
377382
f'Got an unexpected error pushing new config to "{node}"',
@@ -461,7 +466,7 @@ def get_config_parser(
461466
except PermissionError as e:
462467
# TODO: looks like it's useless here, because of creating config
463468
# from default on cmapi server startup
464-
# Anyway looks like it have to raise error and then
469+
# Anyway looks like it has to raise error and then
465470
# return 500 error
466471
logging.error(
467472
'CMAPI cannot create configuration file. '
@@ -826,7 +831,7 @@ def cmapi_config_check(cmapi_conf_path: str = CMAPI_CONF_PATH):
826831
"""
827832
if not os.path.exists(cmapi_conf_path):
828833
logging.info(
829-
f'There are no config file at "{cmapi_conf_path}". '
834+
f'There is no config file at "{cmapi_conf_path}". '
830835
f'So copy default config from {CMAPI_DEFAULT_CONF_PATH} there.'
831836
)
832837
copyfile(CMAPI_DEFAULT_CONF_PATH, cmapi_conf_path)

cmapi/cmapi_server/logging_management.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def add_logging_level(level_name, level_num, method_name=None):
9595
setattr(logging, method_name, partial(logging.log, level_num))
9696

9797

98+
def enable_console_logging(logger: logging.Logger) -> None:
99+
"""Enable logging to console for passed logger by adding a StreamHandler to it"""
100+
console_handler = logging.StreamHandler()
101+
logger.addHandler(console_handler)
102+
103+
98104
def config_cmapi_server_logging():
99105
# add custom level TRACE only for develop purposes
100106
# could be activated using API endpoints or cli tool without relaunching

cmapi/cmapi_server/node_manipulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def switch_node_maintenance(
5555
maintenance_element = etree.SubElement(config_root, 'Maintenance')
5656
maintenance_element.text = str(maintenance_state).lower()
5757
node_config.write_config(config_root, filename=output_config_filename)
58-
# TODO: probably move publishing to cherrypy.emgine failover channel here?
58+
# TODO: probably move publishing to cherrypy.engine failover channel here?
5959

6060

6161
def add_node(

cmapi/cmapi_server/process_dispatchers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def exec_command(
7979
del proc
8080
result = (True, output)
8181
else:
82-
logging.debug('Waiting command to finish.')
82+
logging.debug('Waiting for command to finish.')
8383
stdout_str, _ = proc.communicate()
8484
returncode = proc.wait()
8585
if stdout_str is not None:

cmapi/cmapi_server/process_dispatchers/systemd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def is_service_running(cls, service: str, use_sudo: bool = True) -> bool:
6464
..Note:
6565
Not working with multiple services at a time.
6666
"""
67-
logging.debug(f'Checking "{service}" is running.')
67+
logging.debug(f'Checking if "{service}" is running.')
6868
# TODO: remove conditions below when we'll drop CentOS 7 support
6969
cmd = 'show -p ActiveState --value'
7070
if cls.systemctl_version < 230: # not supported --value in old version

cmapi/dev_tools/activate

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Modified venv activation script used with portable Python
2+
# Adds cmapi/ and cmapi/deps/ dirs to PYTHONPATH
3+
# Expects to be located in cmapi/dev_tools and deps to be located in cmapi/deps (as written in README.md)
4+
5+
6+
# This file must be used with "source bin/activate" *from bash*
7+
# You cannot run it directly
8+
9+
cmapi_dir=$(realpath $(dirname "${BASH_SOURCE[0]}")/../)
10+
venv_dir=$cmapi_dir/python
11+
deps_dir=$cmapi_dir/deps
12+
13+
14+
deactivate () {
15+
# reset old environment variables
16+
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
17+
PATH="${_OLD_VIRTUAL_PATH:-}"
18+
export PATH
19+
unset _OLD_VIRTUAL_PATH
20+
fi
21+
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
22+
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
23+
export PYTHONHOME
24+
unset _OLD_VIRTUAL_PYTHONHOME
25+
fi
26+
if [ -n "${_OLD_VIRTUAL_PYTHONPATH:-}" ] ; then
27+
PYTHONPATH="${_OLD_VIRTUAL_PYTHONPATH:-}"
28+
export PYTHONPATH
29+
unset _OLD_VIRTUAL_PYTHONPATH
30+
fi
31+
32+
# Call hash to forget past commands. Without forgetting
33+
# past commands the $PATH changes we made may not be respected
34+
hash -r 2> /dev/null
35+
36+
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
37+
PS1="${_OLD_VIRTUAL_PS1:-}"
38+
export PS1
39+
unset _OLD_VIRTUAL_PS1
40+
fi
41+
42+
unset VIRTUAL_ENV
43+
unset VIRTUAL_ENV_PROMPT
44+
if [ ! "${1:-}" = "nondestructive" ] ; then
45+
# Self destruct!
46+
unset -f deactivate
47+
fi
48+
}
49+
50+
# unset irrelevant variables
51+
deactivate nondestructive
52+
53+
export VIRTUAL_ENV=$venv_dir
54+
55+
_OLD_VIRTUAL_PATH="$PATH"
56+
echo "Adding $VIRTUAL_ENV/bin to PATH"
57+
PATH="$VIRTUAL_ENV/bin:$PATH"
58+
export PATH
59+
60+
# unset PYTHONHOME if set
61+
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
62+
# could use `if (set -u; : $PYTHONHOME) ;` in bash
63+
if [ -n "${PYTHONHOME:-}" ] ; then
64+
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
65+
unset PYTHONHOME
66+
fi
67+
68+
# Save the old PYTHONPATH if it exists
69+
if [ -n "${PYTHONPATH:-}" ]; then
70+
_OLD_VIRTUAL_PYTHONPATH="${PYTHONPATH:-}"
71+
fi
72+
73+
# Add cmapi/deps directory to PYTHONPATH
74+
echo "Adding $cmapi_dir and $deps_dir to PYTHONPATH"
75+
export PYTHONPATH="${cmapi_dir}:${deps_dir}:${PYTHONPATH:-}"
76+
77+
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
78+
_OLD_VIRTUAL_PS1="${PS1:-}"
79+
PS1="(portpy) ${PS1:-}"
80+
export PS1
81+
VIRTUAL_ENV_PROMPT="(portpy) "
82+
export VIRTUAL_ENV_PROMPT
83+
fi
84+
85+
# Call hash to forget past commands. Without forgetting
86+
# past commands the $PATH changes we made may not be respected
87+
hash -r 2> /dev/null

cmapi/mcs_cluster_tool/__main__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import typer
66

7-
from cmapi_server.logging_management import dict_config, add_logging_level
7+
from cmapi_server.logging_management import dict_config, add_logging_level, enable_console_logging
88
from mcs_cluster_tool import (
99
cluster_app, cmapi_app, backup_commands, restore_commands
1010
)
@@ -38,10 +38,20 @@ def help_all():
3838
# Open the man page in interactive mode
3939
subprocess.run(['man', 'mcs'])
4040

41+
@app.callback()
42+
def main(verbose: bool = typer.Option(False, '--verbose', '-v', help='Enable verbose logging to console')):
43+
'''Add a -v option and setup logging in every subcommand'''
44+
setup_logging(verbose)
4145

42-
if __name__ == '__main__':
43-
add_logging_level('TRACE', 5) #TODO: remove when stadalone mode added.
46+
47+
def setup_logging(verbose: bool = False) -> None:
48+
add_logging_level('TRACE', 5)
4449
dict_config(MCS_CLI_LOG_CONF_PATH)
50+
if verbose:
51+
enable_console_logging(logging.getLogger())
52+
53+
54+
if __name__ == '__main__':
4555
logger = logging.getLogger('mcs_cli')
4656
# add separator between cli commands logging
4757
logger.debug(f'{"-":-^80}')

cmapi/mcs_cluster_tool/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def wrapper(*args, **kwargs):
2121
return_code = 0
2222
except CMAPIBasicError as err:
2323
typer.echo(err.message, err=True)
24-
logger.error('Error while command execution', exc_info=True)
24+
logger.error('Error during command execution', exc_info=True)
2525
except typer.BadParameter as err:
2626
logger.error('Bad command line parameter.')
2727
raise err
2828
except Exception:
2929
logger.error(
30-
'Undefined error while command execution',
30+
'Undefined error during command execution',
3131
exc_info=True
3232
)
3333
typer.echo('Unknown error, check the log file.', err=True)

cmapi/mcs_node_control/models/node_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class NodeConfig:
3737
def get_current_config_root(
3838
self, config_filename: str = DEFAULT_MCS_CONF_PATH, upgrade=True
3939
):
40-
"""Retrievs current configuration.
40+
"""Retrieves current configuration.
4141
4242
Read the config and returns Element.
4343
TODO: pretty the same function in misc.py - review

0 commit comments

Comments
 (0)