Skip to content

Commit

Permalink
working output again
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Mar 18, 2025
1 parent bea9255 commit de73677
Show file tree
Hide file tree
Showing 26 changed files with 5,375 additions and 535 deletions.
103 changes: 64 additions & 39 deletions local-app/python-tools/gfl-resource-actions/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# Makefile for development, testing, and execution

.PHONY: help setup install-dev code-check run-dry-run run-stop run-start \
run-stop-ec2 run-start-ec2 clean all
run-stop-ec2 run-start-ec2 clean clean-logs all install dist test lint format

# Variables
SCRIPT_DIR = $(shell pwd)
MODULE_PATH = $(SCRIPT_DIR)/aws_resource_management.py
PACKAGE_NAME = aws_resource_management
LOG_FILE = aws_resource_management.log
PYTHON = python
PIP = pip

# Default target
help:
Expand All @@ -17,76 +19,99 @@ help:
@echo " help - Show this help message"
@echo " setup - Set up the development environment"
@echo " install-dev - Install development dependencies"
@echo " install - Install package locally in development mode"
@echo " code-check - Basic code checks (uses 'python -m py_compile')"
@echo " lint - Run code linters (flake8, mypy)"
@echo " format - Format code (black, isort)"
@echo " test - Run tests"
@echo " dist - Build distribution package"
@echo " run-dry-run - Run the tool in dry-run mode (no changes)"
@echo " run-stop - Run the tool to stop all resources"
@echo " run-start - Run the tool to start all resources"
@echo " run-stop-ec2 - Run the tool to stop EC2 instances only"
@echo " run-start-ec2 - Run the tool to start EC2 instances only"
@echo " clean - Remove temporary and generated files"
@echo " all - Run code-check"
@echo " clean-logs - Remove log files only"
@echo " all - Setup dependencies, check code and run in dry-run mode"

# Setup the development environment
setup: install-dev
setup: install-dev install

# Install development dependencies
install-dev:
pip install boto3 pytest pytest-mock
$(PIP) install boto3 pytest pytest-mock flake8 mypy black isort pytest-cov

# Install package locally in development mode
install:
$(PIP) install -e .

# Basic code check - no external tools needed
code-check:
@echo "Checking Python files for syntax errors..."
@if [ -f $(MODULE_PATH) ]; then \
python -m py_compile $(MODULE_PATH) && echo "No syntax errors found in main script."; \
else \
echo "Warning: Main script $(MODULE_PATH) not found."; \
fi
@if [ -d aws_resource_management ]; then \
find aws_resource_management -name "*.py" -type f -exec python -m py_compile {} \; && \
@if [ -d $(PACKAGE_NAME) ]; then \
find $(PACKAGE_NAME) -name "*.py" -type f -exec $(PYTHON) -m py_compile {} \; && \
echo "No syntax errors found in module files."; \
else \
echo "Note: aws_resource_management directory not found. Skipping module checks."; \
echo "Warning: Package directory $(PACKAGE_NAME) not found."; \
fi

# Run basic test verification
# Run linters
lint:
flake8 $(PACKAGE_NAME)
mypy $(PACKAGE_NAME)

# Format code
format:
black $(PACKAGE_NAME)
isort $(PACKAGE_NAME)

# Run tests
test:
@echo "Basic verification:"
@if [ -f $(MODULE_PATH) ]; then \
echo "Main script exists."; \
else \
echo "Main script $(MODULE_PATH) not found."; \
fi
@echo "For full testing, install pytest: pip install pytest pytest-mock"
pytest -xvs tests/

# Build distribution package
dist:
$(PYTHON) setup.py sdist bdist_wheel

# Run the tool in dry-run mode
# Run in dry-run mode
run-dry-run:
python $(MODULE_PATH) --dry-run
$(PYTHON) -m $(PACKAGE_NAME).cli --stop --dry-run --resource-type all

# Run the tool to stop all resources
# Run to stop resources
run-stop:
python $(MODULE_PATH) --action stop
$(PYTHON) -m $(PACKAGE_NAME).cli --stop --resource-type all

# Run the tool to start all resources
# Run to start resources
run-start:
python $(MODULE_PATH) --action start
$(PYTHON) -m $(PACKAGE_NAME).cli --start --resource-type all

# Run the tool to stop EC2 instances only
# Run to stop EC2 instances only
run-stop-ec2:
python $(MODULE_PATH) --action stop --exclude-resources rds eks emr
$(PYTHON) -m $(PACKAGE_NAME).cli --stop --resource-type ec2

# Run the tool to start EC2 instances only
# Run to start EC2 instances only
run-start-ec2:
python $(MODULE_PATH) --action start --exclude-resources rds eks emr
$(PYTHON) -m $(PACKAGE_NAME).cli --start --resource-type ec2

# Clean up temporary files
# Clean temporary files
clean:
rm -rf __pycache__
rm -rf *.egg-info
rm -rf build/
rm -rf dist/
rm -rf .pytest_cache
rm -rf .coverage
rm -rf .mypy_cache
rm -f $(LOG_FILE)
find . -name "__pycache__" -type d -exec rm -rf {} +
find . -name "*.pyc" -delete
find . -name "*.pyo" -delete
find . -name "*.pyd" -delete
find . -name ".pytest_cache" -type d -exec rm -rf {} +
@echo "Cleaned up temporary files."
find . -name "__pycache__" -delete
find . -name "*.log" -delete

# Clean log files only
clean-logs:
rm -f $(LOG_FILE)
find . -name "*.log" -delete
find ./logs -type f -name "*.csv" -delete

# Run all development tasks
all: clean setup code-check test run-dry-run
# Default all target - setup dependencies, check code and run in dry-run mode
all: setup code-check run-dry-run
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@
import os
import tempfile
import subprocess
import concurrent.futures
import logging

# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s - %(message)s'
)
logger = logging.getLogger('aws_resource_management')

def process_account(account_id, script_path, script_dir, args):
"""Process a single account with the main script."""
try:
cmd = [sys.executable, script_path] + args
env = os.environ.copy()
env['PYTHONPATH'] = script_dir + os.pathsep + env.get('PYTHONPATH', '')
env['AWS_ACCOUNT'] = account_id

result = subprocess.run(cmd, env=env, capture_output=True, text=True)
if result.returncode != 0:
logger.error(f"Error processing account {account_id}: {result.stderr}")
return result.returncode
except Exception as e:
logger.error(f"Error processing account {account_id}: {str(e)}")
return 1

if __name__ == "__main__":
# Get the current script directory
Expand Down Expand Up @@ -38,18 +63,34 @@
temp_file.close()

try:
# Execute the temporary file with the current script directory in PYTHONPATH
cmd = [sys.executable, temp_file.name] + sys.argv[1:]
env = os.environ.copy()
env['PYTHONPATH'] = script_dir + os.pathsep + env.get('PYTHONPATH', '')
# Get list of accounts to process
accounts = [os.environ.get('AWS_ACCOUNT_ID', '')]

# Create a thread pool for parallel execution
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
# Submit account processing tasks
futures = {
executor.submit(
process_account,
account,
temp_file.name,
script_dir,
sys.argv[1:]
): account for account in accounts if account
}

# Wait for all tasks to complete
results = []
for future in concurrent.futures.as_completed(futures):
account = futures[future]
try:
results.append(future.result())
except Exception as e:
logger.error(f"Thread execution error for account {account}: {str(e)}")
results.append(1)

try:
# Run the command and exit with its return code
result = subprocess.run(cmd, env=env)
sys.exit(result.returncode)
except KeyboardInterrupt:
print("\nOperation interrupted by user. Exiting gracefully...")
sys.exit(130) # Standard exit code for SIGINT/Ctrl+C
# Exit with error if any account processing failed
sys.exit(1 if any(result != 0 for result in results) else 0)
finally:
# Clean up the temporary file
os.unlink(temp_file.name)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
AWS Resource Management package.
This package provides tools for managing AWS resources including stopping,
starting, and listing resources across different AWS services.
"""

__version__ = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Main entry point for AWS Resource Management CLI.
"""

import sys
from aws_resource_management.cli_optimized import main

if __name__ == "__main__":
sys.exit(main())
Loading

0 comments on commit de73677

Please sign in to comment.