-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
83 lines (76 loc) · 3.16 KB
/
setup.py
File metadata and controls
83 lines (76 loc) · 3.16 KB
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
import boto3
import botocore.exceptions
import subprocess
import platform
import os
import time
import logging
from datetime import datetime
# Configure logging
logging.basicConfig(
filename="aws_connection_setup.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger()
def log(message):
"""Log messages with a timestamp."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] {message}")
logger.info(message)
def install_aws_cli():
"""Install the AWS CLI if not already installed."""
try:
subprocess.run(["aws", "--version"], check=True)
log("AWS CLI is already installed.")
except FileNotFoundError:
log("AWS CLI is not installed. Installing...")
system = platform.system().lower()
if "windows" in system:
installer_url = "https://awscli.amazonaws.com/AWSCLIV2.msi"
installer_path = "AWSCLIV2.msi"
subprocess.run(["msiexec", "/i", installer_path, "/quiet", "/norestart"], check=True)
elif "linux" in system:
subprocess.run(["curl", "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip", "-o", "awscliv2.zip"], check=True)
subprocess.run(["unzip", "awscliv2.zip"], check=True)
subprocess.run(["sudo", "./aws/install"], check=True)
elif "darwin" in system:
installer_url = "https://awscli.amazonaws.com/AWSCLIV2.pkg"
subprocess.run(["curl", "-o", "AWSCLIV2.pkg", installer_url], check=True)
subprocess.run(["sudo", "installer", "-pkg", "AWSCLIV2.pkg", "-target", "/"], check=True)
else:
raise OSError("Unsupported Operating System")
log("AWS CLI installed successfully.")
def validate_aws_credentials():
"""Validate AWS credentials."""
try:
sts = boto3.client('sts')
response = sts.get_caller_identity()
log(f"Validated AWS credentials for account: {response['Account']}")
except botocore.exceptions.NoCredentialsError:
log("AWS credentials not found. Please configure them.")
raise
except botocore.exceptions.PartialCredentialsError:
log("Incomplete AWS credentials. Please verify configuration.")
raise
def validate_cloudwatch_connection(region):
"""Validate connection to CloudWatch."""
cloudwatch = boto3.client('cloudwatch', region_name=region)
try:
cloudwatch.list_metrics()
log(f"Successfully connected to CloudWatch in region {region}.")
except botocore.exceptions.EndpointConnectionError:
log("Failed to connect to CloudWatch endpoint.")
raise
except Exception as e:
log(f"Unexpected error while connecting to CloudWatch: {e}")
raise
def setup_aws_connection(region):
"""Set up and validate all AWS connection requirements."""
install_aws_cli()
validate_aws_credentials()
validate_cloudwatch_connection(region)
log("AWS connection setup complete.")
if __name__ == "__main__":
REGION = "us-east-1" # Specify your AWS region
setup_aws_connection(REGION)