11#!/usr/bin/env python3
22
3- # Copyright 2022 The MathWorks, Inc.
3+ # Copyright 2022-2026 The MathWorks, Inc.
44
55from mwplatforminterfaces import CloudInterface
66from mwplatforminterfaces import OSInterface
99import health_check
1010import scale_in_protection
1111
12+ import argparse
1213from datetime import datetime
1314import logging
1415from logging .handlers import RotatingFileHandler
1516import sys
1617
1718
19+ logger = logging .getLogger ('mw.autoscaling' )
20+
1821STATUS_SUCCESS = 0
1922STATUS_CLOUD_ISSUE = 1
2023STATUS_CLUSTER_ISSUE = 2
2124STATUS_CLOUD_AND_CLUSTER_ISSUE = 3
2225
2326
24- def main () -> int :
27+ def parse_args (args = None ):
28+ """Parse command-line arguments."""
29+ parser = argparse .ArgumentParser (description = 'Execute autoscaling routine.' )
30+ parser .add_argument (
31+ '--use-private-ip-mapping' ,
32+ type = lambda x : x .lower () == 'true' ,
33+ default = False ,
34+ help = 'Use private IP addresses for hostname mapping (True/False).'
35+ )
36+ parser .add_argument (
37+ '--dns-search-suffix' ,
38+ type = str ,
39+ default = '' ,
40+ help = 'DNS search suffix to append to worker hostnames.'
41+ )
42+ return parser .parse_args (args )
43+
44+
45+ def main (use_private_ip_mapping : bool = False , dns_search_suffix : str = '' ) -> int :
2546 """Execute autoscaling routine.
2647
2748 The routine has three stages:
@@ -32,47 +53,63 @@ def main() -> int:
3253 3. Scale-in protection: Ensures that we do not terminate nodes with
3354 ongoing work.
3455
56+ Args:
57+ use_private_ip_mapping (bool): Use private IP addresses for node mapping.
58+ dns_search_suffix (str): DNS search suffix to append to hostnames.
59+
3560 Returns:
3661 status (int): Status code of program.
3762 0: Successful
3863 1: Faced an issue with cloud provider
3964 2: Faced an issue with cluster
4065 3: Faced an issue with both
4166 """
42- # Retrieving capacity information
43- print ('Connecting to the cloud computing platform' )
44- cloud_interface = CloudInterface ()
67+ logger .info ('Connecting to the cloud computing platform' )
68+ cloud_interface = CloudInterface (
69+ dns_search_suffix = dns_search_suffix ,
70+ use_private_ip_mapping = use_private_ip_mapping ,
71+ )
4572
46- print ('Connecting to cluster' )
73+ logger . info ('Connecting to cluster' )
4774 os_interface = OSInterface ()
4875
49- print ( '# Starting capacity control' )
76+ logger . info ( ' Starting capacity control' )
5077 status_cc = capacity_control .main (cloud_interface , os_interface )
51- print ( f'# Finished capacity control: { status_cc } ' )
78+ logger . info ( ' Finished capacity control: %s' , status_cc )
5279
53- print ( '# Starting health check' )
80+ logger . info ( ' Starting health check' )
5481 status_hc = health_check .main (cloud_interface , os_interface )
55- print ( f'# Finished health check: { status_hc } ' )
82+ logger . info ( ' Finished health check: %s' , status_hc )
5683
57- print ( '# Starting scale-in protection' )
84+ logger . info ( ' Starting scale-in protection' )
5885 status_sp = scale_in_protection .main (cloud_interface , os_interface )
59- print ( f'# Finished scale-in protection: { status_sp } ' )
86+ logger . info ( ' Finished scale-in protection: %s' , status_sp )
6087
6188 return max (status_cc , status_hc , status_sp )
6289
6390
6491if __name__ == '__main__' :
65- # Create logger
66- logger = logging . getLogger ( 'mw.autoscaling' )
92+ args = parse_args ()
93+
6794 log_file = 'C:\\ ProgramData\\ MathWorks\\ autoscaling.log'
68- log_handler = RotatingFileHandler (log_file , maxBytes = 1e6 , backupCount = 5 )
69- log_handler .terminator = ''
70- logger .addHandler (log_handler )
71- logger .setLevel (logging .INFO )
72- sys .stdout .write , sys .stderr .write = logger .info , logger .warning
73-
74- print (f'## Starting autoscaling: { datetime .now ():%Y-%m-%d %H:%M:%S} ' )
75- status = main ()
76- print (f'## Finished autoscaling: { datetime .now ():%Y-%m-%d %H:%M:%S} \n ' )
95+ log_handler = RotatingFileHandler (
96+ log_file , maxBytes = 1_000_000 , backupCount = 5
97+ )
98+ log_formatter = logging .Formatter (
99+ '%(asctime)s [%(levelname)s] %(name)s: %(message)s' ,
100+ datefmt = '%Y-%m-%d %H:%M:%S'
101+ )
102+ log_handler .setFormatter (log_formatter )
103+
104+ root_logger = logging .getLogger ('mw' )
105+ root_logger .addHandler (log_handler )
106+ root_logger .setLevel (logging .DEBUG )
107+
108+ logger .info ('Starting autoscaling: %s' , datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
109+ status = main (
110+ use_private_ip_mapping = args .use_private_ip_mapping ,
111+ dns_search_suffix = args .dns_search_suffix
112+ )
113+ logger .info ('Finished autoscaling: %s' , datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
77114
78115 sys .exit (status )
0 commit comments