Example #1
0
def main():
    my_logger.debug("In main module()")
    
    """Validate Winsys"""
    
    ttWin.validateWin(host_winsys_password, host_winsys_username, host_winsys)
    ttWin.findWinZones(zone_NYC_long, host_windns,host_winsys_password, host_winsys_username, host_winsys)
    """
    #This module finds the Zone ID (Z12...) for the given Zone Name (say, 3top.com)
    """

    my_logger.debug("In dnsOps...calling ttAWS.zoneR53 for collecting Zone ID information...")
    zone_Id = ttAWS.zoneR53(zone_AWS_long)
    my_logger.debug("Zone ID is: %s " % zone_Id)
    
    """
    print "In dnsOps: zone53:"
    print("zoneId = ",zone_Id)
    """
    
    """
                    #------BOTO AUTHENTICATION------
    #The auth_func in ttAWS checks if the import of boto packages
    (ec2, rds2 etc) is successful by checking
    #if the required packages have been installed
    and authentication information is accurately stored
    """
    my_logger.debug("Initializing Boto authentication....calling ttAWS.auth_func!")
    ttAWS.auth_func(REGION_NAME)
    """
                    #-----SELECT ENVIRONMENT-----
    #The selectEnviron module helps in finding the Env name for which the
    #DNS Operations have to be performed
    """
    my_logger.debug("In dnsOps...calling ttAWS.selectEnviron!")
    ENV_NAME = ttAWS.selectEnviron(REGION_NAME)
    my_logger.debug("The envName selected is: %s" % ENV_NAME)
    """#ENV_NAME is a.prod, b.prod, a.dev etc.."""
    """
                    #for h in my_logger.handlers:
    my_logger.removeHandler(h)----COLLECT EC2 DNS RECORDS-----
    #dnsNameMap function in dnsEc2 collects the following records:
    #fp-ec2-1,     mongodb,     mq,     search
    """
    my_logger.debug("Collecting DNS records from the new environment....")
    my_logger.debug("In dnsOps...calling ttAWS.dnsEc2ToDict for collecting DNS records from EC2...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsEc2ToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """Append the DNS records collected from EC2 to the global dictionaries"""
    print("Updating dict_NYC and dict_AWS with EC2 records...")
    my_logger.debug("Updating dict_NYC and dict_AWS with EC2 records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from EC2: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from EC2: %s" % str(dict_AWS))
    my_logger.debug("\nIn dnsOps: EC2:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    
    """
                   # -----COLLECT BEANSTALK DNS RECORDS------
    #cnameEndpoint function in dnsBeanstalk collects the following records:
    #cname and endpointURL
    """
    my_logger.debug("In dnsOps...calling ttAWS.dnsBeanstalkToDict for collecting DNS records from BEANSTALK...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsBeanstalkToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """
    #Append the DNS records collected from BEANSTALK to the global dictionaries
    """
    my_logger.debug("Updating dict_NYC and dict_AWS with Beanstalk records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from Beanstalk: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from Beanstalk: %s" % str(dict_AWS))
    
    """
    print "\nIn dnsOps:Beanstalk:"
    print "\ndict_NYC:"
    print dict_NYC
    print "\ndict_AWS:"
    print dict_AWS
    """
    
    """
                   # -----COLLECT RDS DNS RECORDS------
    #rdsImport function in dnsRds collects the following records:
    #fp-rds-1
    """
    my_logger.debug("In dnsOps...calling ttAWS.dnsRdsToDict for collecting DNS records from RDS...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsRdsToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """
   # Append the DNS records collected from RDS to the global dictionaries
    """
    ("Updating dict_NYC and dict_AWS with RDS records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from RDS: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from RDS: %s" % str(dict_AWS))
    
    my_logger.debug( "\nIn dnsOps:RDS:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
       

    """
    #This will print the current status of the records
    """
    my_logger.debug("In dnsOps...calling ttAWS.currRecords!")
    currAws = ttAWS.route53Records(zone_Id, ENV_NAME)
    print("\nCurrent status of AWS Route53 records:\n ")
    for k, v in currAws.items():
        if ENV_NAME in k:
            print '{:30s} {:85s}'.format(str(k), str(v[0]))
            
    
    my_logger.debug("Current status of AWS Route53 records: %s"%currAws)
   
    list_dict_AWS = list()
    list_currAws = list()
    
    for k,v in currAws.items():
        list_currAws.append(k)
    for k,v in dict_AWS.items():
        list_dict_AWS.append(k)
    
    
    temp1 = dict()
    for k, v in currAws.items():
        if ENV_NAME in k:
            for k1, v1 in dict_AWS.items():
                if (k1 not in list_currAws):
                    temp1.update({k1: ("No Current Record", v1)})
                    #print temp1.update({k1: ("No Current Record", v1)})
                elif k in list_currAws:
                    if k == k1:
                        if ("fp-lb-1" in k):
                            rr_type = ttAWS.find_rrtype(zone_AWS_long, k)
                            if rr_type == "ALIAS":
                                if str(v).upper() != str(v1).upper():
                                    print "Deleting old ALIAS"
                                    ttAWS.del_cname(zone_AWS_long, k, rr_type)
                                    temp1.update({k: ("No Current Record- Deleted old ALIAS", v1)})
                            elif rr_type == "CNAME":
                                #print "rr_type found as %s"%rr_type 
                                print "Deleting CNAME"
                                ttAWS.del_cname(zone_AWS_long, k, rr_type)
                                temp1.update({k1: ("No Current Record- Deleted CNAME", v1)})
            if k not in list_dict_AWS:
                temp1.update({k: (v, "No Change")})
    
    #print temp1
    
    """
    #Check the current records on NYC DNS
    """
    my_logger.debug("Checking for current NYC records status with respect to the NYC records imported...")
    my_logger.debug(sorted(dict_NYC.keys()))
       
    print ("\n")
    for k,v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum, currRecWinEnum) = ttWin.enumRecords(k, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s"%(v,currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum:
                recordCheck.update({k: 'Record not found'})
            elif v.lower() in outWinEnum.lower():
                recordCheck.update({k: 'Record status current'})
            elif v.lower() not in outWinEnum.lower():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum) or ("FAILED" in errWinEnum) or ("FAILED" in currRecWinEnum):
                my_logger.info(sys.exit("Error encountered: %s"%(outWinEnum, errWinEnum, currRecWinEnum)))
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)
    
    dictTemp = dict([(k , [dictTemp[k], dict_NYC[k]]) for k in dictTemp])
    my_logger.debug("new dictTemp:%s " % dictTemp)
    
    for k,v in dictTemp.items():
        if 'Record not found' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status old' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status current' in v[0]:
            dictRecCorrect[k] = v[1]
    my_logger.debug("dictRecAdd:", dictRecAdd)
    my_logger.debug("dictRecCorrect:", dictRecCorrect)
    my_logger.debug("dictRecDelete",dictRecDelete)
    r53_update_count = 0
    for k in sorted(temp1):
        if ENV_NAME in k:
            temp_val = str(temp1[k][0]).split("\'")
            if str(temp_val[len(temp_val)-2]).lower() == str(temp1[k][1]).lower():
                continue
            else:
                r53_update_count = r53_update_count + 1
    
    if r53_update_count>0:
        print "Route53 updates required. Find the current and future values as follows:"
        my_logger.debug("Route53 updates required. Find the current and future values as follows:")
    else:
        print "No Route53 updates required. Find the current and future values as follows:"
        my_logger.debug("No Route53 updates required. Find the current and future values as follows:")
            
    if temp1:
        print "\nRoute 53 Updates:"
        
        my_logger.debug("\nRoute 53 Updates:")
        my_logger.debug("Records Name")
        for k in sorted(temp1):
            if (ENV_NAME in k) and ("fp-lb-1" not in k):
                temp_val = str(temp1[k][0]).split("\'")
                print "[CNAME RECORD]::{:40s}  [CURRENT VALUE]::{:85s}  [FUTURE VALUE]::{:85s}" .format(str(k), str(temp_val[len(temp_val)-2]), str(temp1[k][1]))
                str_k = str(k)
                str_v0 = str(v[0])
                str_v1 = str(v[1])
                my_logger.debug(str_k + str_v0 + str_v1)
                my_logger.debug(str_k + str_v0 + str_v1)
            if (ENV_NAME in k) and ("fp-lb-1" in k):
                temp_val = str(temp1[k][0]).split("\'")
                print "[ALIAS RECORD]::{:40s}  [CURRENT VALUE]::{:85s}  [FUTURE VALUE]::{:85s}" .format(str(k), str(temp_val[len(temp_val)-2]), str(temp1[k][1]))
                str_k = str(k)
                str_v0 = str(v[0])
                str_v1 = str(v[1])
                my_logger.debug(str_k + str_v0 + str_v1)
                my_logger.debug(str_k + str_v0 + str_v1)
                
                     
    my_logger.debug("NYC DNS Updates:: %s" % str(dictRecCorrect))
    
    if dictRecCorrect:
        print "\nNYC DNS Updates:"
        my_logger.debug("\nNYC DNS Updates: \nThe following records are up-to-date for NYC DNS.")
        my_logger.debug("dictRecCorrect: %s" %dictRecCorrect)
        print "\nThe following records are up-to-date for NYC DNS:"
        for k in sorted(dictRecCorrect):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [VALUE]::{:85s}" .format(str(k), str(dictRecCorrect[k]))
            elif "fp-lb-1" in k:
                print "[ALIAS RECORD]::{:40s}  [VALUE]::{:85s}" .format(str(k), str(dictRecCorrect[k]))
    
        
    my_logger.debug("\nDelete the following records for NYC DNS: %s" % str(sorted(dictRecDelete)))
    if dictRecDelete:
        print "\nDelete the following records for NYC DNS:"
        for k in sorted(dictRecDelete):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [VALUE]::{:65s}" .format(str(k), str(dictRecDelete[k]))
            elif "fp-lb-1" not in k:
                print "[ALIAS RECORD]::{:40s}  [VALUE]::{:65s}" .format(str(k), str(dictRecDelete[k]))
                
            
    my_logger.debug("\nAdd the following records for NYC DNS:%s" % str(sorted(dictRecAdd)))
    if dictRecAdd:
        print "\nAdd the following records for NYC DNS:" 
        for k in sorted(dictRecAdd):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [FUTURE VALUE]::{:85s}" .format(str(k), str(dictRecAdd[k]))
            elif "fp-lb-1" not in k:
                print "[ALIAS RECORD]::{:40s}  [FUTURE VALUE]::{:85s}" .format(str(k), str(dictRecAdd[k]))
    
    confirm_backup = raw_input("\nUpdate %s DNS records above in AWS Route 53 DNS and NYC Office DNS [y/n]:"%ENV_NAME)
    if confirm_backup == 'y' or confirm_backup == 'Y':
        
        
        #This will export the current AWS records into a file to allow for reverting in future 
        
       
        print("In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        my_logger.debug("In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/beforeUpdate.txt')
    
        print("Before Update Backup Successful")
        my_logger.debug("Before Update Backup Successful")
        my_logger.info("NYC DNS Records:%s" % str(dict_NYC))
        
           # Update route53 DNS records (AWS)- take confirmation before updating
        
        try:
            my_logger.info( "In dnsOps...calling ttAWS.dnsUpdate!")
            out_aws_update = ttAWS.dnsUpdate(ENV_NAME, zone_Id, dict_AWS, zone_AWS_long)
            my_logger.info(out_aws_update)
            my_logger.info("AWS Import successful")
            print("AWS Import successful")
            
        except:
            e = sys.exc_info()[0]
            my_logger.info(sys.exit("Error Encountered: %s. AWS Import failed! Exiting..." % e ))
    
        my_logger.info("Backing up current records...")
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.winDnsBackup!")
            ttWin.winDnsBackup("beforeUpdate", host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.info("Back up of current records succeeded...")
            print("Back up of current records succeeded...")
            
        except:
            e = sys.exc_info()[0]
            my_logger.info(sys.exit("Error Encountered: %s.Backup of records failed! Exiting..." % e))
            
        try:
            my_logger.info( "In dnsOps...calling ttWin.deleteRecords!")
            (cmd, out_del_rec) = ttWin.deleteRecords(dictRecDelete, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_del_rec = str(out_del_rec)
            my_logger.info("Command: %s, Out_del_rec: %s"%(cmd,out_del_rec))
            my_logger.info("Delete of old records complete...")
            print("Delete of old records complete...")
            
        except:
            e = sys.exc_info()[0]
            my_logger.info(sys.exit("Error Encountered: %s.Deleting records failed! Exiting..." % e))
            #sys.exit(-1)
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.addRecords for adding records...")
            (cmd, out_add_rec) = ttWin.addRecords(dictRecAdd, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_add_rec = str(out_add_rec)
            my_logger.info("Command: %s, Out_del_rec: %s"%(cmd,out_add_rec))
            my_logger.info("Adding records complete....")
            print("Adding records complete....")
        except:
            e = sys.exc_info()[0]
            my_logger.info(sys.exit("Error Encountered: %s.Adding records failed! Exiting..." % e))
            #sys.exit(-1)   
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.winDnsBackup for completing post update backup...")
            ttWin.winDnsBackup("afterUpdate", host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.info("Post update win_dns_backup successful...")
            print("Post update win_dns_backup successful...")
        except:
            e = sys.exc_info()[0]
            my_logger.info(sys.exit("Error Encountered: %s.Adding records failed! Exiting..." % e))
            #sys.exit(-1) 
       
        my_logger.info( "In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/afterUpdate.txt')
    
        my_logger.info("After Update cli53 Backup Successful")
        print("Logfile location: %s"%dir_backup + "/dnsOps.log")
        
    else:
        my_logger.error(sys.exit("Confirmation not received. Received information: %s. Exiting..." % confirm_backup))
        print("Logfile location: %s"%dir_backup + "/dnsOps.log")
Example #2
0
def main():
    my_logger.debug("In main module()")
    """Validate Winsys"""

    ttWin.validateWin(host_winsys_password, host_winsys_username, host_winsys)
    ttWin.findWinZones(zone_NYC_long, host_windns, host_winsys_password,
                       host_winsys_username, host_winsys)
    """
    #This module finds the Zone ID (Z12...) for the given Zone Name (say, 3top.com)
    """

    my_logger.debug(
        "In dnsOps...calling ttAWS.zoneR53 for collecting Zone ID information..."
    )
    zone_Id = ttAWS.zoneR53(zone_AWS_long)
    my_logger.debug("Zone ID is: %s " % zone_Id)
    """
    print "In dnsOps: zone53:"
    print("zoneId = ",zone_Id)
    """
    """
                    #------BOTO AUTHENTICATION------
    #The auth_func in ttAWS checks if the import of boto packages
    (ec2, rds2 etc) is successful by checking
    #if the required packages have been installed
    and authentication information is accurately stored
    """
    my_logger.debug(
        "Initializing Boto authentication....calling ttAWS.auth_func!")
    ttAWS.auth_func(REGION_NAME)
    """
                    #-----SELECT ENVIRONMENT-----
    #The selectEnviron module helps in finding the Env name for which the
    #DNS Operations have to be performed
    """
    my_logger.debug("In dnsOps...calling ttAWS.selectEnviron!")
    ENV_NAME = ttAWS.selectEnviron(REGION_NAME)
    my_logger.debug("The envName selected is: %s" % ENV_NAME)
    """#ENV_NAME is a.prod, b.prod, a.dev etc.."""
    """
                    #for h in my_logger.handlers:
    my_logger.removeHandler(h)----COLLECT EC2 DNS RECORDS-----
    #dnsNameMap function in dnsEc2 collects the following records:
    #fp-ec2-1,     mongodb,     mq,     search
    """
    my_logger.debug("Collecting DNS records from the new environment....")
    my_logger.debug(
        "In dnsOps...calling ttAWS.dnsEc2ToDict for collecting DNS records from EC2..."
    )
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsEc2ToDict(REGION_NAME, ENV_NAME,
                                                    zone_AWS_long)
    """Append the DNS records collected from EC2 to the global dictionaries"""
    print("Updating dict_NYC and dict_AWS with EC2 records...")
    my_logger.debug("Updating dict_NYC and dict_AWS with EC2 records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from EC2: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from EC2: %s" % str(dict_AWS))
    my_logger.debug("\nIn dnsOps: EC2:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    """
                   # -----COLLECT BEANSTALK DNS RECORDS------
    #cnameEndpoint function in dnsBeanstalk collects the following records:
    #cname and endpointURL
    """
    my_logger.debug(
        "In dnsOps...calling ttAWS.dnsBeanstalkToDict for collecting DNS records from BEANSTALK..."
    )
    (dictNycTemp,
     dictAwsTemp) = ttAWS.dnsBeanstalkToDict(REGION_NAME, ENV_NAME,
                                             zone_AWS_long)
    """
    #Append the DNS records collected from BEANSTALK to the global dictionaries
    """
    my_logger.debug("Updating dict_NYC and dict_AWS with Beanstalk records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from Beanstalk: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from Beanstalk: %s" % str(dict_AWS))
    """
    print "\nIn dnsOps:Beanstalk:"
    print "\ndict_NYC:"
    print dict_NYC
    print "\ndict_AWS:"
    print dict_AWS
    """
    """
                   # -----COLLECT RDS DNS RECORDS------
    #rdsImport function in dnsRds collects the following records:
    #fp-rds-1
    """
    my_logger.debug(
        "In dnsOps...calling ttAWS.dnsRdsToDict for collecting DNS records from RDS..."
    )
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsRdsToDict(REGION_NAME, ENV_NAME,
                                                    zone_AWS_long)
    """
   # Append the DNS records collected from RDS to the global dictionaries
    """
    ("Updating dict_NYC and dict_AWS with RDS records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from RDS: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from RDS: %s" % str(dict_AWS))

    my_logger.debug("\nIn dnsOps:RDS:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    """
    #This will print the current status of the records
    """
    my_logger.debug("In dnsOps...calling ttAWS.currRecords!")
    currAws = ttAWS.route53Records(zone_Id, ENV_NAME)
    print("\nCurrent status of AWS Route53 records:\n ")
    for k, v in currAws.items():
        if ENV_NAME in k:
            print '{:30s} {:85s}'.format(str(k), str(v[0]))

    my_logger.debug("Current status of AWS Route53 records: %s" % currAws)

    list_dict_AWS = list()
    list_currAws = list()

    for k, v in currAws.items():
        list_currAws.append(k)
    for k, v in dict_AWS.items():
        list_dict_AWS.append(k)

    temp1 = dict()
    for k, v in currAws.items():
        if ENV_NAME in k:
            for k1, v1 in dict_AWS.items():
                if (k1 not in list_currAws):
                    temp1.update({k1: ("No Current Record", v1)})
                    #print temp1.update({k1: ("No Current Record", v1)})
                elif k in list_currAws:
                    if k == k1:
                        if ("fp-lb-1" in k):
                            rr_type = ttAWS.find_rrtype(zone_AWS_long, k)
                            if rr_type == "ALIAS":
                                if str(v).upper() != str(v1).upper():
                                    print "Deleting old ALIAS"
                                    ttAWS.del_cname(zone_AWS_long, k, rr_type)
                                    temp1.update({
                                        k:
                                        ("No Current Record- Deleted old ALIAS",
                                         v1)
                                    })
                            elif rr_type == "CNAME":
                                #print "rr_type found as %s"%rr_type
                                print "Deleting CNAME"
                                ttAWS.del_cname(zone_AWS_long, k, rr_type)
                                temp1.update({
                                    k1:
                                    ("No Current Record- Deleted CNAME", v1)
                                })
            if k not in list_dict_AWS:
                temp1.update({k: (v, "No Change")})

    #print temp1
    """
    #Check the current records on NYC DNS
    """
    my_logger.debug(
        "Checking for current NYC records status with respect to the NYC records imported..."
    )
    my_logger.debug(sorted(dict_NYC.keys()))

    print("\n")
    for k, v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum, currRecWinEnum) = ttWin.enumRecords(
                k, host_windns, zone_NYC_long, host_winsys_password,
                host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s" % (v, currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum:
                recordCheck.update({k: 'Record not found'})
            elif v.lower() in outWinEnum.lower():
                recordCheck.update({k: 'Record status current'})
            elif v.lower() not in outWinEnum.lower():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum) or ("FAILED" in errWinEnum) or (
                    "FAILED" in currRecWinEnum):
                my_logger.info(
                    sys.exit("Error encountered: %s" %
                             (outWinEnum, errWinEnum, currRecWinEnum)))
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)

    dictTemp = dict([(k, [dictTemp[k], dict_NYC[k]]) for k in dictTemp])
    my_logger.debug("new dictTemp:%s " % dictTemp)

    for k, v in dictTemp.items():
        if 'Record not found' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status old' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status current' in v[0]:
            dictRecCorrect[k] = v[1]
    my_logger.debug("dictRecAdd:", dictRecAdd)
    my_logger.debug("dictRecCorrect:", dictRecCorrect)
    my_logger.debug("dictRecDelete", dictRecDelete)
    r53_update_count = 0
    for k in sorted(temp1):
        if ENV_NAME in k:
            temp_val = str(temp1[k][0]).split("\'")
            if str(temp_val[len(temp_val) - 2]).lower() == str(
                    temp1[k][1]).lower():
                continue
            else:
                r53_update_count = r53_update_count + 1

    if r53_update_count > 0:
        print "Route53 updates required. Find the current and future values as follows:"
        my_logger.debug(
            "Route53 updates required. Find the current and future values as follows:"
        )
    else:
        print "No Route53 updates required. Find the current and future values as follows:"
        my_logger.debug(
            "No Route53 updates required. Find the current and future values as follows:"
        )

    if temp1:
        print "\nRoute 53 Updates:"

        my_logger.debug("\nRoute 53 Updates:")
        my_logger.debug("Records Name")
        for k in sorted(temp1):
            if (ENV_NAME in k) and ("fp-lb-1" not in k):
                temp_val = str(temp1[k][0]).split("\'")
                print "[CNAME RECORD]::{:40s}  [CURRENT VALUE]::{:85s}  [FUTURE VALUE]::{:85s}".format(
                    str(k), str(temp_val[len(temp_val) - 2]), str(temp1[k][1]))
                str_k = str(k)
                str_v0 = str(v[0])
                str_v1 = str(v[1])
                my_logger.debug(str_k + str_v0 + str_v1)
                my_logger.debug(str_k + str_v0 + str_v1)
            if (ENV_NAME in k) and ("fp-lb-1" in k):
                temp_val = str(temp1[k][0]).split("\'")
                print "[ALIAS RECORD]::{:40s}  [CURRENT VALUE]::{:85s}  [FUTURE VALUE]::{:85s}".format(
                    str(k), str(temp_val[len(temp_val) - 2]), str(temp1[k][1]))
                str_k = str(k)
                str_v0 = str(v[0])
                str_v1 = str(v[1])
                my_logger.debug(str_k + str_v0 + str_v1)
                my_logger.debug(str_k + str_v0 + str_v1)

    my_logger.debug("NYC DNS Updates:: %s" % str(dictRecCorrect))

    if dictRecCorrect:
        print "\nNYC DNS Updates:"
        my_logger.debug(
            "\nNYC DNS Updates: \nThe following records are up-to-date for NYC DNS."
        )
        my_logger.debug("dictRecCorrect: %s" % dictRecCorrect)
        print "\nThe following records are up-to-date for NYC DNS:"
        for k in sorted(dictRecCorrect):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [VALUE]::{:85s}".format(
                    str(k), str(dictRecCorrect[k]))
            elif "fp-lb-1" in k:
                print "[ALIAS RECORD]::{:40s}  [VALUE]::{:85s}".format(
                    str(k), str(dictRecCorrect[k]))

    my_logger.debug("\nDelete the following records for NYC DNS: %s" %
                    str(sorted(dictRecDelete)))
    if dictRecDelete:
        print "\nDelete the following records for NYC DNS:"
        for k in sorted(dictRecDelete):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [VALUE]::{:65s}".format(
                    str(k), str(dictRecDelete[k]))
            elif "fp-lb-1" not in k:
                print "[ALIAS RECORD]::{:40s}  [VALUE]::{:65s}".format(
                    str(k), str(dictRecDelete[k]))

    my_logger.debug("\nAdd the following records for NYC DNS:%s" %
                    str(sorted(dictRecAdd)))
    if dictRecAdd:
        print "\nAdd the following records for NYC DNS:"
        for k in sorted(dictRecAdd):
            if "fp-lb-1" not in k:
                print "[CNAME RECORD]::{:40s}  [FUTURE VALUE]::{:85s}".format(
                    str(k), str(dictRecAdd[k]))
            elif "fp-lb-1" not in k:
                print "[ALIAS RECORD]::{:40s}  [FUTURE VALUE]::{:85s}".format(
                    str(k), str(dictRecAdd[k]))

    confirm_backup = raw_input(
        "\nUpdate %s DNS records above in AWS Route 53 DNS and NYC Office DNS [y/n]:"
        % ENV_NAME)
    if confirm_backup == 'y' or confirm_backup == 'Y':

        #This will export the current AWS records into a file to allow for reverting in future

        print(
            "In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data..."
        )
        my_logger.debug(
            "In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data..."
        )
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/beforeUpdate.txt')

        print("Before Update Backup Successful")
        my_logger.debug("Before Update Backup Successful")
        my_logger.info("NYC DNS Records:%s" % str(dict_NYC))

        # Update route53 DNS records (AWS)- take confirmation before updating

        try:
            my_logger.info("In dnsOps...calling ttAWS.dnsUpdate!")
            out_aws_update = ttAWS.dnsUpdate(ENV_NAME, zone_Id, dict_AWS,
                                             zone_AWS_long)
            my_logger.info(out_aws_update)
            my_logger.info("AWS Import successful")
            print("AWS Import successful")

        except:
            e = sys.exc_info()[0]
            my_logger.info(
                sys.exit(
                    "Error Encountered: %s. AWS Import failed! Exiting..." %
                    e))

        my_logger.info("Backing up current records...")

        try:
            my_logger.info("In dnsOps...calling ttWin.winDnsBackup!")
            ttWin.winDnsBackup("beforeUpdate", host_windns, zone_NYC_long,
                               host_winsys_password, host_winsys_username,
                               host_winsys)
            my_logger.info("Back up of current records succeeded...")
            print("Back up of current records succeeded...")

        except:
            e = sys.exc_info()[0]
            my_logger.info(
                sys.exit(
                    "Error Encountered: %s.Backup of records failed! Exiting..."
                    % e))

        try:
            my_logger.info("In dnsOps...calling ttWin.deleteRecords!")
            (cmd, out_del_rec) = ttWin.deleteRecords(
                dictRecDelete, host_windns, zone_NYC_long,
                host_winsys_password, host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_del_rec = str(out_del_rec)
            my_logger.info("Command: %s, Out_del_rec: %s" % (cmd, out_del_rec))
            my_logger.info("Delete of old records complete...")
            print("Delete of old records complete...")

        except:
            e = sys.exc_info()[0]
            my_logger.info(
                sys.exit(
                    "Error Encountered: %s.Deleting records failed! Exiting..."
                    % e))
            #sys.exit(-1)

        try:
            my_logger.info(
                "In dnsOps...calling ttWin.addRecords for adding records...")
            (cmd, out_add_rec) = ttWin.addRecords(dictRecAdd, host_windns,
                                                  zone_NYC_long,
                                                  host_winsys_password,
                                                  host_winsys_username,
                                                  host_winsys)
            cmd = str(cmd)
            out_add_rec = str(out_add_rec)
            my_logger.info("Command: %s, Out_del_rec: %s" % (cmd, out_add_rec))
            my_logger.info("Adding records complete....")
            print("Adding records complete....")
        except:
            e = sys.exc_info()[0]
            my_logger.info(
                sys.exit(
                    "Error Encountered: %s.Adding records failed! Exiting..." %
                    e))
            #sys.exit(-1)

        try:
            my_logger.info(
                "In dnsOps...calling ttWin.winDnsBackup for completing post update backup..."
            )
            ttWin.winDnsBackup("afterUpdate", host_windns, zone_NYC_long,
                               host_winsys_password, host_winsys_username,
                               host_winsys)
            my_logger.info("Post update win_dns_backup successful...")
            print("Post update win_dns_backup successful...")
        except:
            e = sys.exc_info()[0]
            my_logger.info(
                sys.exit(
                    "Error Encountered: %s.Adding records failed! Exiting..." %
                    e))
            #sys.exit(-1)

        my_logger.info(
            "In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data..."
        )
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/afterUpdate.txt')

        my_logger.info("After Update cli53 Backup Successful")
        print("Logfile location: %s" % dir_backup + "/dnsOps.log")

    else:
        my_logger.error(
            sys.exit(
                "Confirmation not received. Received information: %s. Exiting..."
                % confirm_backup))
        print("Logfile location: %s" % dir_backup + "/dnsOps.log")
Example #3
0
def main():
    my_logger.debug("In main module()")
    
    """Validate Winsys"""
    
    ttWin.validateWin(host_winsys_password, host_winsys_username, host_winsys)
    ttWin.findWinZones(zone_NYC_long, host_windns, host_winsys_password, host_winsys_username, host_winsys)
    """
    #This module finds the Zone ID (Z12...) for the given Zone Name (say, 3top.com)
    """

    my_logger.debug("In dnsOps...calling ttAWS.zoneR53 for collecting Zone ID information...")
    zone_Id = ttAWS.zoneR53(zone_AWS_long)
    my_logger.debug("Zone ID is: %s " % zone_Id)
    
    """
    print "In dnsOps: zone53:"
    print("zoneId = ",zone_Id)
    """
    
    """
                    #------BOTO AUTHENTICATION------
    #The auth_func in ttAWS checks if the import of boto packages
    (ec2, rds2 etc) is successful by checking
    #if the required packages have been installed
    and authentication information is accurately stored
    """
    my_logger.debug("Initializing Boto authentication....calling ttAWS.auth_func!")
    ttAWS.auth_func(REGION_NAME)
    """
                    #-----SELECT ENVIRONMENT-----
    #The selectEnviron module helps in finding the Env name for which the
    #DNS Operations have to be performed
    """
    my_logger.debug("In dnsOps...calling ttAWS.selectEnviron!")
    ENV_NAME = ttAWS.selectEnviron(REGION_NAME)
    my_logger.debug("The envName selected is: %s" % ENV_NAME)
    """#ENV_NAME is a.prod, b.prod, a.dev etc.."""
    """
                    #for h in my_logger.handlers:
    my_logger.removeHandler(h)----COLLECT EC2 DNS RECORDS-----
    #dnsNameMap function in dnsEc2 collects the following records:
    #fp-ec2-1,     mongodb,     mq,     search
    """
    my_logger.debug("Collecting DNS records from the new environment....")
    my_logger.debug("In dnsOps...calling ttAWS.dnsEc2ToDict for collecting DNS records from EC2...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsEc2ToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """Append the DNS records collected from EC2 to the global dictionaries"""
    print("Updating dict_NYC and dict_AWS with EC2 records...")
    my_logger.debug("Updating dict_NYC and dict_AWS with EC2 records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from EC2: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from EC2: %s" % str(dict_AWS))
    my_logger.debug("\nIn dnsOps: EC2:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    
    """
                   # -----COLLECT BEANSTALK DNS RECORDS------
    #cnameEndpoint function in dnsBeanstalk collects the following records:
    #cname and endpointURL
    """
    my_logger.debug("In dnsOps...calling ttAWS.dnsBeanstalkToDict for collecting DNS records from BEANSTALK...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsBeanstalkToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """
    #Append the DNS records collected from BEANSTALK to the global dictionaries
    """
    my_logger.debug("Updating dict_NYC and dict_AWS with Beanstalk records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from Beanstalk: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from Beanstalk: %s" % str(dict_AWS))
    
    """
    print "\nIn dnsOps:Beanstalk:"
    print "\ndict_NYC:"
    print dict_NYC
    print "\ndict_AWS:"
    print dict_AWS
    """
    
    """
                   # -----COLLECT RDS DNS RECORDS------
    #rdsImport function in dnsRds collects the following records:
    #fp-rds-1
    """
    my_logger.debug("In dnsOps...calling ttAWS.dnsRdsToDict for collecting DNS records from RDS...")
    (dictNycTemp, dictAwsTemp) = ttAWS.dnsRdsToDict(REGION_NAME, ENV_NAME, zone_AWS_long)
    """
   # Append the DNS records collected from RDS to the global dictionaries
    """
    ("Updating dict_NYC and dict_AWS with RDS records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from RDS: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from RDS: %s" % str(dict_AWS))
    
    my_logger.debug( "\nIn dnsOps:RDS:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
       

    """
    #This will print the current status of the records
    """
    
    my_logger.debug("In dnsOps...calling ttAWS.currRecords!")
    currAws = ttAWS.route53Records(zone_Id, ENV_NAME)
    
    """Collect of list of items in currAWS dict and Future Aws Dict"""
    
    list_dict_AWS = list()
    list_currAws = list()
    for k,v in currAws.items():
        list_currAws.append(k)
    for k,v in dict_AWS.items():
        list_dict_AWS.append(k)
    
    
    """Create a consolidated list of records from current and future lists""" 
  
    list_total=list()
    for l in list_currAws:
        if l not in list_total:
            list_total.append(l)
    
    for l in list_dict_AWS:
        if l not in list_total:
            list_total.append(l)
    
    """Append Dummy entries for missing entries in curr AWS"""
    
    for l in list_total:
        if l not in currAws:
            currAws[l] = "No current value" 
    
    
            
    """Readable format dict"""
    
    read_currAws = {}
    
    for k, v in currAws.items():
        k1 = ''.join(str(e1) for e1 in k)
        v1 = ''.join(str(e2) for e2 in v)
        read_currAws[k1] = v1
        
    read_dict_AWS = {}
    
    for k, v in dict_AWS.items():
        k1 = ''.join(str(e1) for e1 in k)
        v1 = ''.join(str(e2) for e2 in v)
        read_dict_AWS[k1] = v1
    
    """Generate a comparison dictionary and set values as array"""
    
    compare_aws_dict={}
    dict_curr_rr_type={}
    dict_future_rr_type={}
    
    for l in list_total:
        dict_curr_rr_type[l] = ttAWS.find_rrtype(zone_AWS_long, l)
    
    for l in list_total:
        if "fp-lb-1" in l:
            dict_future_rr_type[l] = "ALIAS"
        else:
            dict_future_rr_type[l] = "CNAME"
    
    #print "Dict_rrtype:%s"%dict_rr_type 
    
    for l in list_total:
        l1 = ''.join(str(e1) for e1 in l)
        compare_aws_dict.setdefault(l1, [])
    
    for k, v in dict_curr_rr_type.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)
    
    for k, v in read_currAws.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)
    
    for k, v in dict_future_rr_type.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)
    
    #print read_dict_AWS
                
    for k1, v1 in compare_aws_dict.items():
        if k1 in list_dict_AWS:
            for k, v in read_dict_AWS.items():
                if k == k1:
                    compare_aws_dict[k1].append(v)
        elif k1 not in list_dict_AWS:
            compare_aws_dict[k1].append(v1[1])
    
    compare_nyc_dict = dict()
    list_dict_NYC = list()
    for k,v in dict_NYC.items():
        list_dict_NYC.append(k)
    
    for l in list_dict_NYC:
        l1 = ''.join(str(e1) for e1 in l)
        compare_nyc_dict.setdefault(l1, [])
    
    for k,v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum, currRecWinEnum) = ttWin.enumRecords(k, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s"%(v,currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum.upper():
                recordCheck.update({k: 'Record not found'})
            elif v.upper() in outWinEnum.upper():
                recordCheck.update({k: 'Record status current'})
            elif v.upper() not in outWinEnum.upper():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum.upper()) or ("FAILED" in errWinEnum.upper()) or ("FAILED" in currRecWinEnum.upper()):
                my_logger.error("Error encountered in Enum Records. Exiting!")
                sys.exit("Error encountered in Enum Records. Exiting!")
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)
    
    dict_add_aws_record = {}
    dict_del_aws_record = {}
    
    for k, v in compare_aws_dict.items():
        if ("fp-lb-1" in k):
            if (v[0] == "CNAME"):
                dict_add_aws_record[k] = v[3]
                dict_del_aws_record[k] = v[0]
            elif (v[0] == "ALIAS") and (v[1].upper() != v[3].upper()):
                dict_add_aws_record[k] = v[3]
                dict_del_aws_record[k] = v[0]
            elif (v[0] == None):
                dict_add_aws_record[k] = v[3]
        elif (v[0] == "CNAME") and (v[1].upper() != v[3].upper()):
            dict_add_aws_record[k] = v[3]
        elif ("fp-lb-1" not in k) and (v[0]) == None:
            dict_add_aws_record[k] = v[3]
    
    """
    #Check the current records on NYC DNS
    """
    my_logger.debug("Checking for current NYC records status with respect to the NYC records imported...")
    my_logger.debug(sorted(dict_NYC.keys()))
       
    print ("\n")
    for k,v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum, currRecWinEnum) = ttWin.enumRecords(k, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s"%(v,currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum:
                recordCheck.update({k: 'Record not found'})
            elif v.lower() in outWinEnum.lower():
                recordCheck.update({k: 'Record status current'})
            elif v.lower() not in outWinEnum.lower():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum) or ("FAILED" in errWinEnum) or ("FAILED" in currRecWinEnum):
                my_logger.error("Error encountered in Enum Records. Exiting!")
                sys.exit("Error encountered in Enum Records. Exiting!")
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)
    
    dictTemp = dict([(k , [dictTemp[k], dict_NYC[k]]) for k in dictTemp])
    my_logger.debug("new dictTemp:%s " % dictTemp)
    
    for k,v in dictTemp.items():
        if 'Record not found' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status old' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status current' in v[0]:
            dictRecCorrect[k] = v[1]
    my_logger.debug("dictRecAdd:%s"% str(dictRecAdd))
    my_logger.debug("dictRecCorrect: %s"%str(dictRecCorrect))
    my_logger.debug("dictRecDelete: %s"%str(dictRecDelete))
    
    if (dict_del_aws_record) or (dict_add_aws_record):
        print "Route53 updates required. Find the current and future values as follows:"
        my_logger.debug("Route53 updates required. Find the current and future values as follows:")
    else:
        print "No Route53 updates required. Find the current and future values as follows:"
        my_logger.debug("No Route53 updates required. Find the current and future values as follows:")
    
    print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format("RECORD NAME", "CURR R_TYPE", "CURRENT VALUE", "FUTURE R_TYPE", "FUTURE VALUE")
    print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format("-----------", "-----------", "-------------", "-------------", "------------")
    for k, v in compare_aws_dict.items():
        print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format(str(k), str(v[0]), str(v[1]), str(v[2]), str(v[3]))
    print "\n"    
    
    my_logger.debug("NYC DNS Updates:: %s" % str(dictRecCorrect))
    
    if dictRecCorrect:
        print "\nNYC DNS Updates:"
        my_logger.debug("\nNYC DNS Updates: \nThe following records are up-to-date for NYC DNS.")
        my_logger.debug("dictRecCorrect: %s" %dictRecCorrect)
        print "\nThe following records are up-to-date for NYC DNS:"
        for k in sorted(dictRecCorrect):
            print "[CNAME RECORD]::{:40s}  [VALUE]::{:85s}" .format(str(k), str(dictRecCorrect[k]))
            
        
    my_logger.debug("\nDelete the following records for NYC DNS: %s" % str(sorted(dictRecDelete)))
    if dictRecDelete:
        print "\nDelete the following records for NYC DNS:"
        for k in sorted(dictRecDelete):
            print "[CNAME RECORD]::{:40s}  [VALUE]::{:65s}" .format(str(k), str(dictRecDelete[k]))
               
            
    my_logger.debug("\nAdd the following records for NYC DNS:%s" % str(sorted(dictRecAdd)))
    if dictRecAdd:
        print "\nAdd the following records for NYC DNS:" 
        for k in sorted(dictRecAdd):
            print "[CNAME RECORD]::{:40s}  [FUTURE VALUE]::{:85s}" .format(str(k), str(dictRecAdd[k]))
            
            
    confirm_backup = raw_input("\nUpdate %s DNS records above in AWS Route 53 DNS and NYC Office DNS [y/n]:"%ENV_NAME)
    if confirm_backup == 'y' or confirm_backup == 'Y':
        
        print("In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        my_logger.debug("In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/beforeUpdate.txt')
    
        print("Before Update AWS Backup Successful")
        my_logger.debug("Before Update AWS Backup Successful")
        
        
        try:
            print "Delete AWS Records: "
            pprint(dict_del_aws_record)
            for k,v in dict_del_aws_record.items():
                print "Deleting old %s record"%v
                ttAWS.del_cname(zone_AWS_long, k, v)
        except:
            my_logger.info("Error Encountered. Delete AWS Records failed! Exiting..." )
            sys.exit("Error Encountered. Delete AWS Records failed! Exiting..." )
            
        try:
            print "dict_add_aws_record: "
            pprint(dict_add_aws_record)
            my_logger.info( "In dnsOps...calling ttAWS.dnsUpdate!")
            out_aws_update = ttAWS.dnsUpdate(ENV_NAME, zone_Id, dict_add_aws_record, zone_AWS_long)
            my_logger.info(out_aws_update)
            my_logger.info("AWS Import successful")
            print("AWS Import successful")
        except:
            my_logger.info("Error Encountered. AWS Import failed! Exiting..." )
            sys.exit("Error Encountered. AWS Import failed! Exiting..." )
    
        my_logger.info("Backing up NYC current records...")
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.winDnsBackup!")
            ttWin.winDnsBackup("beforeUpdate", host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.info("Back up of current NYC records succeeded...")
            print("Back up of current NYC records succeeded...")
        except:
            my_logger.info("Error Encountered.Backup of records failed! Exiting..." )
            sys.exit("Error Encountered.Backup of records failed! Exiting..." )
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.deleteRecords!")
            (cmd, out_del_rec) = ttWin.deleteRecords(dictRecDelete, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_del_rec = str(out_del_rec)
            my_logger.info("Command: %s, Out_del_rec: %s"%(cmd,out_del_rec))
            my_logger.info("Delete of old records complete...")
            print("Delete of old records complete...")
            
        except:
            
            my_logger.info("Error Encountered.Deleting records failed! Exiting...")
            sys.exit("Error Encountered.Deleting records failed! Exiting...")
            #sys.exit(-1)
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.addRecords for adding records...")
            (cmd, out_add_rec) = ttWin.addRecords(dictRecAdd, host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_add_rec = str(out_add_rec)
            my_logger.info("Command: %s, Out_del_rec: %s"%(cmd,out_add_rec))
            my_logger.info("Adding records complete....")
            print("Adding records complete....")
        except:
            
            my_logger.info("Error Encountered.Adding records failed! Exiting...")
            sys.exit("Error Encountered.Adding records failed! Exiting...")
            #sys.exit(-1)   
        
        try:
            my_logger.info( "In dnsOps...calling ttWin.winDnsBackup for completing post update backup...")
            ttWin.winDnsBackup("afterUpdate", host_windns, zone_NYC_long, host_winsys_password, host_winsys_username, host_winsys)
            my_logger.info("Post update win_dns_backup successful...")
            print("Post update win_dns_backup successful...")
        except:
            
            my_logger.info("Error Encountered.Adding records failed! Exiting...")
            sys.exit("Error Encountered.Adding records failed! Exiting...")
            #sys.exit(-1) 
       
        my_logger.info( "In dnsOps...calling ttAWS.cli53_export for backing up current AWS DNS Data...")
        ttAWS.cli53_export(zone_AWS_long, dir_backup, '/afterUpdate.txt')
    
        my_logger.info("After Update cli53 Backup Successful")
        print("Logfile location: %s"%dir_backup + "/dnsOps.log")
        
    else:
        my_logger.error("Confirmation not received. Received information: %s. Exiting..." % confirm_backup)
        sys.exit("Confirmation not received. Received information: %s. Exiting..." % confirm_backup)
        print("Logfile location: %s"%dir_backup + "/dnsOps.log")
Example #4
0
def main():
    my_logger.debug("In main module()")
    """Validate Winsys"""

    validateWin(host_winsys_password, host_winsys_username, host_winsys)
    findWinZones(zone_NYC_long, host_windns, host_winsys_password,
                 host_winsys_username, host_winsys)
    """
    #This module finds the Zone ID (Z12...) for the given Zone Name (say, 3top.com)
    """

    my_logger.debug(
        "In dnsOps...calling zoneR53 for collecting Zone ID information...")
    zone_Id = zoneR53(zone_AWS_long)
    my_logger.debug("Zone ID is: %s " % zone_Id)
    """
    print "In dnsOps: zone53:"
    print("zoneId = ",zone_Id)
    """
    """
                    #------BOTO AUTHENTICATION------
    #The auth_func in ttAWS checks if the import of boto packages
    (ec2, rds2 etc) is successful by checking
    #if the required packages have been installed
    and authentication information is accurately stored
    """
    my_logger.debug("Initializing Boto authentication....calling auth_func!")
    auth_func(REGION_NAME)
    """
                    #-----SELECT ENVIRONMENT-----
    #The selectEnviron module helps in finding the Env name for which the
    #DNS Operations have to be performed
    """
    if len(sys.argv) == 3:
        if sys.argv[1] == "-p":
            m_arg = sys.argv
            m_arg.pop(0)
        else:
            sys.exit(
                "The parameter passed cannot be identified. Use \"-p <active_env_name>\" in maintenance mode (to set production environment)"
            )
    elif (len(sys.argv) == 1) and (sys.argv[0] == "dnsOps.py"):
        m_arg = "NA"
    else:
        sys.exit(
            "Format mismatch. Run the program as \"python dnsOps.py\" or \"python dnsOps.py -p <active_env_name>\" in maintenance mode (to set production environment)\". EXITING!"
        )
    my_logger.debug("In dnsOps...calling selectEnviron!")
    ENV_NAME = selectEnviron(REGION_NAME, m_arg)
    my_logger.debug("The envName selected is: %s" % ENV_NAME)
    """#ENV_NAME is a.prod, b.prod, a.dev etc.."""
    """
                    #for h in my_logger.handlers:
    my_logger.removeHandler(h)----COLLECT EC2 DNS RECORDS-----
    #dnsNameMap function in dnsEc2 collects the following records:
    #fp-ec2-1,     mongodb,     mq,     search
    """
    my_logger.debug("Collecting DNS records from the new environment....")
    my_logger.debug(
        "In dnsOps...calling dnsEc2ToDict for collecting DNS records from EC2..."
    )
    (dictNycTemp, dictAwsTemp) = dnsEc2ToDict(REGION_NAME, ENV_NAME,
                                              zone_AWS_long)
    """Append the DNS records collected from EC2 to the global dictionaries"""
    print("Updating dict_NYC and dict_AWS with EC2 records...")
    my_logger.debug("Updating dict_NYC and dict_AWS with EC2 records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from EC2: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from EC2: %s" % str(dict_AWS))
    my_logger.debug("\nIn dnsOps: EC2:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    """
                   # -----COLLECT BEANSTALK DNS RECORDS------
    #cnameEndpoint function in dnsBeanstalk collects the following records:
    #cname and endpointURL
    """
    my_logger.debug(
        "In dnsOps...calling dnsBeanstalkToDict for collecting DNS records from BEANSTALK..."
    )
    (dictNycTemp, dictAwsTemp) = dnsBeanstalkToDict(REGION_NAME, ENV_NAME,
                                                    zone_AWS_long)
    """
    #Append the DNS records collected from BEANSTALK to the global dictionaries
    """
    my_logger.debug("Updating dict_NYC and dict_AWS with Beanstalk records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from Beanstalk: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from Beanstalk: %s" % str(dict_AWS))
    """
    print "\nIn dnsOps:Beanstalk:"
    print "\ndict_NYC:"
    print dict_NYC
    print "\ndict_AWS:"
    print dict_AWS
    """
    """
                   # -----COLLECT RDS DNS RECORDS------
    #rdsImport function in dnsRds collects the following records:
    #fp-rds-1
    """
    my_logger.debug(
        "In dnsOps...calling dnsRdsToDict for collecting DNS records from RDS..."
    )
    (dictNycTemp, dictAwsTemp) = dnsRdsToDict(REGION_NAME, ENV_NAME,
                                              zone_AWS_long)
    """
   # Append the DNS records collected from RDS to the global dictionaries
    """
    ("Updating dict_NYC and dict_AWS with RDS records...")
    dict_NYC.update(dictNycTemp)
    dict_AWS.update(dictAwsTemp)
    my_logger.debug("NYC DNS records from RDS: %s" % str(dict_NYC))
    my_logger.debug("AWS DNS records from RDS: %s" % str(dict_AWS))

    my_logger.debug("\nIn dnsOps:RDS:")
    my_logger.debug("\ndict_NYC:")
    my_logger.debug(dict_NYC)
    my_logger.debug("\ndict_AWS:")
    my_logger.debug(dict_AWS)
    """
    #This will print the current status of the records
    """

    my_logger.debug("In dnsOps...calling currRecords!")
    currAws = route53Records(zone_Id, ENV_NAME)
    """Collect of list of items in currAWS dict and Future Aws Dict"""

    list_dict_AWS = list()
    list_currAws = list()
    for k, v in currAws.items():
        list_currAws.append(k)
    for k, v in dict_AWS.items():
        list_dict_AWS.append(k)
    """Create a consolidated list of records from current and future lists"""

    list_total = list()
    for l in list_currAws:
        if l not in list_total:
            list_total.append(l)

    for l in list_dict_AWS:
        if l not in list_total:
            list_total.append(l)
    """Append Dummy entries for missing entries in curr AWS"""

    for l in list_total:
        if l not in currAws:
            currAws[l] = "No current value"
    """Readable format dict"""

    read_currAws = {}

    for k, v in currAws.items():
        k1 = ''.join(str(e1) for e1 in k)
        v1 = ''.join(str(e2) for e2 in v)
        read_currAws[k1] = v1

    read_dict_AWS = {}

    for k, v in dict_AWS.items():
        k1 = ''.join(str(e1) for e1 in k)
        v1 = ''.join(str(e2) for e2 in v)
        read_dict_AWS[k1] = v1
    """Generate a comparison dictionary and set values as array"""

    compare_aws_dict = {}
    dict_curr_rr_type = {}
    dict_future_rr_type = {}

    for l in list_total:
        dict_curr_rr_type[l] = find_rrtype(zone_AWS_long, l)

    for l in list_total:
        if "fp-lb-1" in l:
            dict_future_rr_type[l] = "ALIAS"
        else:
            dict_future_rr_type[l] = "CNAME"

    #print "Dict_rrtype:%s"%dict_rr_type

    for l in list_total:
        l1 = ''.join(str(e1) for e1 in l)
        compare_aws_dict.setdefault(l1, [])

    for k, v in dict_curr_rr_type.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)

    for k, v in read_currAws.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)

    for k, v in dict_future_rr_type.items():
        for k1, v1 in compare_aws_dict.items():
            if k == k1:
                compare_aws_dict[k1].append(v)

    #print read_dict_AWS

    for k1, v1 in compare_aws_dict.items():
        if k1 in list_dict_AWS:
            for k, v in read_dict_AWS.items():
                if k == k1:
                    compare_aws_dict[k1].append(v)
        elif k1 not in list_dict_AWS:
            compare_aws_dict[k1].append(v1[1])

    compare_nyc_dict = dict()
    list_dict_NYC = list()
    for k, v in dict_NYC.items():
        list_dict_NYC.append(k)

    for l in list_dict_NYC:
        l1 = ''.join(str(e1) for e1 in l)
        compare_nyc_dict.setdefault(l1, [])

    for k, v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum,
             currRecWinEnum) = enumRecords(k, host_windns, zone_NYC_long,
                                           host_winsys_password,
                                           host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s" % (v, currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum.upper():
                recordCheck.update({k: 'Record not found'})
            elif v.upper() in outWinEnum.upper():
                recordCheck.update({k: 'Record status current'})
            elif v.upper() not in outWinEnum.upper():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum.upper()) or (
                    "FAILED"
                    in errWinEnum.upper()) or ("FAILED"
                                               in currRecWinEnum.upper()):
                my_logger.error("Error encountered in Enum Records. Exiting!")
                sys.exit("Error encountered in Enum Records. Exiting!")
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)

    dict_add_aws_record = {}
    dict_del_aws_record = {}

    for k, v in compare_aws_dict.items():
        if ("fp-lb-1" in k):
            if (v[0] == "CNAME"):
                dict_add_aws_record[k] = v[3]
                dict_del_aws_record[k] = v[0]
            elif (v[0] == "ALIAS") and (v[1].upper() != v[3].upper()):
                dict_add_aws_record[k] = v[3]
                dict_del_aws_record[k] = v[0]
            elif (v[0] == None):
                dict_add_aws_record[k] = v[3]
        elif (v[0] == "CNAME") and (v[1].upper() != v[3].upper()):
            dict_add_aws_record[k] = v[3]
        elif ("fp-lb-1" not in k) and (v[0]) == None:
            dict_add_aws_record[k] = v[3]
    """
    #Check the current records on NYC DNS
    """
    my_logger.debug(
        "Checking for current NYC records status with respect to the NYC records imported..."
    )
    my_logger.debug(sorted(dict_NYC.keys()))

    print("\n")
    for k, v in dict_NYC.items():
        if ENV_NAME in k:
            (outWinEnum, errWinEnum,
             currRecWinEnum) = enumRecords(k, host_windns, zone_NYC_long,
                                           host_winsys_password,
                                           host_winsys_username, host_winsys)
            my_logger.debug("Output: %s: %s" % (v, currRecWinEnum))
            if "DNS_ERROR_NAME_DOES_NOT_EXIST" in outWinEnum:
                recordCheck.update({k: 'Record not found'})
            elif v.lower() in outWinEnum.lower():
                recordCheck.update({k: 'Record status current'})
            elif v.lower() not in outWinEnum.lower():
                recordCheck.update({k: 'Record status old'})
                dictRecDelete[k] = currRecWinEnum
            elif ("FAILED" in outWinEnum) or ("FAILED" in errWinEnum) or (
                    "FAILED" in currRecWinEnum):
                my_logger.error("Error encountered in Enum Records. Exiting!")
                sys.exit("Error encountered in Enum Records. Exiting!")
    dictTemp = recordCheck.copy()
    my_logger.debug("Current records status:%s" % recordCheck)

    dictTemp = dict([(k, [dictTemp[k], dict_NYC[k]]) for k in dictTemp])
    my_logger.debug("new dictTemp:%s " % dictTemp)

    for k, v in dictTemp.items():
        if 'Record not found' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status old' in v[0]:
            dictRecAdd[k] = v[1]
        elif 'Record status current' in v[0]:
            dictRecCorrect[k] = v[1]
    my_logger.debug("dictRecAdd:%s" % str(dictRecAdd))
    my_logger.debug("dictRecCorrect: %s" % str(dictRecCorrect))
    my_logger.debug("dictRecDelete: %s" % str(dictRecDelete))

    if (dict_del_aws_record) or (dict_add_aws_record):
        print "Route53 updates required. Find the current and future values as follows:"
        my_logger.debug(
            "Route53 updates required. Find the current and future values as follows:"
        )
    else:
        print "No Route53 updates required. Find the current and future values as follows:"
        my_logger.debug(
            "No Route53 updates required. Find the current and future values as follows:"
        )

    print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format("RECORD NAME",
                                                      "CURR R_TYPE",
                                                      "CURRENT VALUE",
                                                      "FUTURE R_TYPE",
                                                      "FUTURE VALUE")
    print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format("-----------",
                                                      "-----------",
                                                      "-------------",
                                                      "-------------",
                                                      "------------")
    for k, v in compare_aws_dict.items():
        print '{:30s} {:15s} {:85s} {:15s} {:85s}'.format(
            str(k), str(v[0]), str(v[1]), str(v[2]), str(v[3]))
    print "\n"

    my_logger.debug("NYC DNS Updates:: %s" % str(dictRecCorrect))

    if dictRecCorrect:
        print "\nNYC DNS Updates:"
        my_logger.debug(
            "\nNYC DNS Updates: \nThe following records are up-to-date for NYC DNS."
        )
        my_logger.debug("dictRecCorrect: %s" % dictRecCorrect)
        print "\nThe following records are up-to-date for NYC DNS:"
        for k in sorted(dictRecCorrect):
            print "[CNAME RECORD]::{:40s}  [VALUE]::{:85s}".format(
                str(k), str(dictRecCorrect[k]))

    my_logger.debug("\nDelete the following records for NYC DNS: %s" %
                    str(sorted(dictRecDelete)))
    if dictRecDelete:
        print "\nDelete the following records for NYC DNS:"
        for k in sorted(dictRecDelete):
            print "[CNAME RECORD]::{:40s}  [VALUE]::{:65s}".format(
                str(k), str(dictRecDelete[k]))

    my_logger.debug("\nAdd the following records for NYC DNS:%s" %
                    str(sorted(dictRecAdd)))
    if dictRecAdd:
        print "\nAdd the following records for NYC DNS:"
        for k in sorted(dictRecAdd):
            print "[CNAME RECORD]::{:40s}  [FUTURE VALUE]::{:85s}".format(
                str(k), str(dictRecAdd[k]))

    confirm_backup = raw_input(
        "\nUpdate %s DNS records above in AWS Route 53 DNS and NYC Office DNS [y/n]:"
        % ENV_NAME)
    if confirm_backup == 'y' or confirm_backup == 'Y':

        print(
            "In dnsOps...calling cli53_export for backing up current AWS DNS Data..."
        )
        my_logger.debug(
            "In dnsOps...calling cli53_export for backing up current AWS DNS Data..."
        )
        cli53_export(zone_AWS_long, dir_backup, '/beforeUpdate.txt')

        print("Before Update AWS Backup Successful")
        my_logger.debug("Before Update AWS Backup Successful")

        try:
            print "Delete AWS Records: "
            pprint(dict_del_aws_record)
            for k, v in dict_del_aws_record.items():
                print "Deleting old %s record" % v
                del_cname(zone_AWS_long, k, v)
        except:
            my_logger.info(
                "Error Encountered. Delete AWS Records failed! Exiting...")
            sys.exit(
                "Error Encountered. Delete AWS Records failed! Exiting...")

        try:
            print "dict_add_aws_record: "
            pprint(dict_add_aws_record)
            my_logger.info("In dnsOps...calling dnsUpdate!")
            out_aws_update = dnsUpdate(ENV_NAME, zone_Id, dict_add_aws_record,
                                       zone_AWS_long)
            my_logger.info(out_aws_update)
            my_logger.info("AWS Import successful")
            print("AWS Import successful")
        except:
            my_logger.info("Error Encountered. AWS Import failed! Exiting...")
            sys.exit("Error Encountered. AWS Import failed! Exiting...")

        my_logger.info("Backing up NYC current records...")

        try:
            my_logger.info("In dnsOps...calling winDnsBackup!")
            winDnsBackup("beforeUpdate", host_windns, zone_NYC_long,
                         host_winsys_password, host_winsys_username,
                         host_winsys)
            my_logger.info("Back up of current NYC records succeeded...")
            print("Back up of current NYC records succeeded...")
        except:
            my_logger.info(
                "Error Encountered.Backup of records failed! Exiting...")
            sys.exit("Error Encountered.Backup of records failed! Exiting...")

        try:
            my_logger.info("In dnsOps...calling deleteRecords!")
            (cmd,
             out_del_rec) = deleteRecords(dictRecDelete, host_windns,
                                          zone_NYC_long, host_winsys_password,
                                          host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_del_rec = str(out_del_rec)
            my_logger.info("Command: %s, Out_del_rec: %s" % (cmd, out_del_rec))
            my_logger.info("Delete of old records complete...")
            print("Delete of old records complete...")

        except:

            my_logger.info(
                "Error Encountered.Deleting records failed! Exiting...")
            sys.exit("Error Encountered.Deleting records failed! Exiting...")
            #sys.exit(-1)

        try:
            my_logger.info(
                "In dnsOps...calling addRecords for adding records...")
            (cmd, out_add_rec) = addRecords(dictRecAdd, host_windns,
                                            zone_NYC_long,
                                            host_winsys_password,
                                            host_winsys_username, host_winsys)
            cmd = str(cmd)
            out_add_rec = str(out_add_rec)
            my_logger.info("Command: %s, Out_del_rec: %s" % (cmd, out_add_rec))
            my_logger.info("Adding records complete....")
            print("Adding records complete....")
        except:

            my_logger.info(
                "Error Encountered.Adding records failed! Exiting...")
            sys.exit("Error Encountered.Adding records failed! Exiting...")
            #sys.exit(-1)

        try:
            my_logger.info(
                "In dnsOps...calling winDnsBackup for completing post update backup..."
            )
            winDnsBackup("afterUpdate", host_windns, zone_NYC_long,
                         host_winsys_password, host_winsys_username,
                         host_winsys)
            my_logger.info("Post update win_dns_backup successful...")
            print("Post update win_dns_backup successful...")
        except:

            my_logger.info(
                "Error Encountered.Adding records failed! Exiting...")
            sys.exit("Error Encountered.Adding records failed! Exiting...")
            #sys.exit(-1)

        my_logger.info(
            "In dnsOps...calling cli53_export for backing up current AWS DNS Data..."
        )
        cli53_export(zone_AWS_long, dir_backup, '/afterUpdate.txt')

        my_logger.info("After Update cli53 Backup Successful")
        print("Logfile location: %s" % dir_backup + "/dnsOps.log")

    else:
        print("Confirmation not received. Received choice for update: %s." %
              confirm_backup)
        print("Deleting TimestampDir: %s" % dir_backup)
        try:
            shutil.rmtree(dir_backup)
        except:
            sys.exit("Deleting %s failed. Exiting!" % dir_backup)
        sys.exit("Deleting %s successful. Exiting!" % dir_backup)