aws-janitor.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:AWS-Demos 作者: miztiik 项目源码 文件源码
def main():
    argparser = argparse.ArgumentParser()
    argparser.add_argument('--site', help='Name of the Site VPC', required=True)
    argparser.add_argument('--cidr', help='CIDR to add route for', required=True)
    argparser.add_argument('--peeringid', help='Peering connection we are going to use', required=True)
    argparser.add_argument('--profile', help='AWS Profile to use', required=True)
    args = argparser.parse_args()
    cidr = args.cidr
    site = args.site
    peeringid = args.peeringid
    profile = args.profile

    if profile != None: boto3.setup_default_session(profile_name=profile)
    ec2resource = boto3.resource('ec2', region_name='eu-central-1')
    ec2Client = boto3.client('ec2', region_name='eu-central-1')


    vpcid = get_vpcid(site, client = ec2Client)
    print('Looking up route tables for %s' % vpcid)
    route_tables = get_route_tables(vpcid, ec2resource)
    print ('Retrieved %s tables, proceeding to add tables' % len(route_tables))
    for table_id in route_tables:
        print('Adding route for %s to %s' % (cidr, peeringid))
        try:
            response = add_peering_route(table_id, cidr, peeringid, ec2resource)
            if response:
                continue
            else:
                print('Error occurred adding route to %s' % table_id)
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == 'RouteAlreadyExists':
                print('Route already exists on %s, continuing' % table_id)
                continue
            else:
                print('Unexpected error: %s' % e)



    print('Routes have been added!')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号