-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlxc_autoscale.py
88 lines (71 loc) · 2.95 KB
/
lxc_autoscale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Main module for LXC autoscaling daemon."""
import argparse
import logging
from typing import Optional
from config import DEFAULTS, LOG_FILE
from logging_setup import setup_logging
from lock_manager import acquire_lock
from lxc_utils import get_containers, rollback_container_settings
from resource_manager import main_loop
def parse_arguments() -> argparse.Namespace:
"""
Parses command-line arguments to configure the daemon's behavior.
Returns:
argparse.Namespace: The parsed arguments with options for poll interval, energy mode, and rollback.
"""
parser = argparse.ArgumentParser(description="LXC Resource Management Daemon")
# Polling interval argument
parser.add_argument(
"--poll_interval",
type=int,
default=DEFAULTS.get('poll_interval', 300),
help="Polling interval in seconds" # How often the main loop should run
)
# Energy mode argument for off-peak hours
parser.add_argument(
"--energy_mode",
action="store_true",
default=DEFAULTS.get('energy_mode', False),
help="Enable energy efficiency mode during off-peak hours" # Reduces resource allocation during low-usage periods
)
# Rollback argument to revert container configurations
parser.add_argument(
"--rollback",
action="store_true",
help="Rollback to previous container configurations" # Option to revert containers to their backed-up settings
)
# Add debug mode argument
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug logging"
)
args = parser.parse_args()
logging.debug(f"Parsed arguments: {args}")
return args
# Entry point of the script
if __name__ == "__main__":
# Parse arguments first to get debug flag
args: argparse.Namespace = parse_arguments()
# Setup logging with the configured log file and debug mode
setup_logging(LOG_FILE, args.debug)
logging.info("Starting LXC autoscaling daemon")
logging.debug("Arguments: %s", args)
# Acquire a lock to ensure that only one instance of the script runs at a time
with acquire_lock() as lock_file:
try:
if args.rollback:
# If the rollback argument is provided, start the rollback process
logging.info("Starting rollback process...")
for ctid in get_containers():
# Rollback settings for each container
rollback_container_settings(ctid)
logging.info("Rollback process completed.")
else:
# If not rolling back, enter the main loop to manage resources
main_loop(args.poll_interval, args.energy_mode)
except Exception as e:
logging.exception(f"An error occurred during main execution: {e}")
finally:
# Ensure that the lock is released and the script exits cleanly
logging.info("Releasing lock and exiting.")