Main function to demonstrate GitHub to EC2 deployment.
()
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | def main(): |
| 31 | """Main function to demonstrate GitHub to EC2 deployment.""" |
| 32 | |
| 33 | # Load configuration |
| 34 | config = get_config() |
| 35 | |
| 36 | # Get credentials manager |
| 37 | cred_manager = get_credential_manager() |
| 38 | |
| 39 | # Initialize AWS credentials (will try environment variables or AWS profile) |
| 40 | aws_creds = cred_manager.get_aws_credentials( |
| 41 | region=os.environ.get('AWS_REGION', 'us-east-1') |
| 42 | ) |
| 43 | |
| 44 | # Initialize GitHub credentials (will try environment variables or keyring) |
| 45 | github_creds = cred_manager.get_github_credentials() |
| 46 | |
| 47 | # Initialize EC2 service |
| 48 | ec2 = EC2Service(credentials=aws_creds) |
| 49 | |
| 50 | # Initialize GitHub service |
| 51 | github = GitHubService(token=github_creds.token) |
| 52 | |
| 53 | # Configuration variables (replace with your actual values) |
| 54 | repository = "owner/repo" # GitHub repository in format "owner/repo" |
| 55 | branch = "main" # Branch to deploy |
| 56 | instance_id = "i-0123456789abcdef0" # EC2 instance ID |
| 57 | deploy_path = "/var/www/html" # Path on the instance to deploy to |
| 58 | |
| 59 | # 1. Get information about the repository |
| 60 | try: |
| 61 | repo_info = github.get_repository(repository) |
| 62 | logger.info(f"Repository: {repo_info['full_name']}") |
| 63 | logger.info(f"Description: {repo_info['description']}") |
| 64 | logger.info(f"Default branch: {repo_info['default_branch']}") |
| 65 | |
| 66 | # Use the default branch if none specified |
| 67 | if not branch: |
| 68 | branch = repo_info['default_branch'] |
| 69 | logger.info(f"Using default branch: {branch}") |
| 70 | except Exception as e: |
| 71 | logger.error(f"Failed to get repository information: {e}") |
| 72 | return 1 |
| 73 | |
| 74 | # 2. List running EC2 instances if no instance_id provided |
| 75 | if not instance_id: |
| 76 | try: |
| 77 | instances = ec2.list_instances(filters=[{ |
| 78 | 'Name': 'instance-state-name', |
| 79 | 'Values': ['running'] |
| 80 | }]) |
| 81 | |
| 82 | if not instances: |
| 83 | logger.error("No running EC2 instances found") |
| 84 | return 1 |
| 85 | |
| 86 | logger.info(f"Found {len(instances)} running instances:") |
| 87 | for i, instance in enumerate(instances): |
no test coverage detected