def getAllInstances(self, kInstanceId=""): import boto self.doAwsConnection() InstancesCollection = [] self.printMsg("", "[BOMBO] Getting instance details...") if kInstanceId != "": reservations = self.__awsConnection.get_all_instances( instance_ids=[kInstanceId]) if not reservations[0].instances: self.printMsg( "", "Ooh no, i can't find the instance -> " + kInstanceId + "!", True, True) InstancesCollection.append( clsInstance(reservations[0].instances[0].id, self, reservations[0].instances[0])) self.printMsg("", "---> " + reservations[0].instances[0].id) else: reservations = self.__awsConnection.get_all_instances() for inst in reservations: InstancesCollection.append( clsInstance(inst.instances[0].id, self, inst.instances[0])) self.printMsg( "", "---> Found " + str(len(InstancesCollection)) + " instances") return InstancesCollection
def getAllInstances(self,kInstanceId=""): import boto self.doAwsConnection() InstancesCollection = [] self.printMsg ("","[BOMBO] Getting instance details...") if kInstanceId != "": reservations = self.__awsConnection.get_all_instances(instance_ids=[kInstanceId]) if not reservations[0].instances: self.printMsg ("","Ooh no, i can't find the instance -> " + kInstanceId + "!",True,True) InstancesCollection.append( clsInstance( reservations[0].instances[0].id, self, reservations[0].instances[0] ) ) self.printMsg ("","---> " + reservations[0].instances[0].id) else: reservations = self.__awsConnection.get_all_instances() for inst in reservations: InstancesCollection.append( clsInstance( inst.instances[0].id, self, inst.instances[0] ) ) #Update Instances count if BOMBO_REDIS_HOST: self.__redisdb.set("hset","",len(reservations),"instances") self.printMsg ("","---> Found " + str(len(InstancesCollection)) + " instances") return InstancesCollection
def __runProcess(self, kIsTest): import time import boto.ec2 ObjTmp = None self.printMsg("", "[AWS] Connecting ...") #Connecting to region self.__awsConnection = boto.ec2.connect_to_region( self.__ObjLaunchConfig.Region, aws_access_key_id=self.__ObjLaunchConfig.Customer.Access_key, aws_secret_access_key=self.__ObjLaunchConfig.Customer.Secret_key) for singleLaunch in self.__ObjLaunchConfig.LaunchList: #Return Reservation tmpReservation = self.__singleLaunch(kIsTest, singleLaunch, self.__ObjLaunchConfig) #Sleep to avoid INSTANCE NOT FOUND error time.sleep(4) if (self.__checkLaunchStatus(tmpReservation)): self.printMsg( "", "Ooh no, I can't believe there was an error during the launch..... you made a mistake as usual!", True, True) for inst in tmpReservation.instances: ObjTmp = clsInstance(inst.id, self.__ObjLaunchConfig.Customer, inst) ObjTmp.Infom_dns = singleLaunch.Hostname + "." + self.__ObjLaunchConfig.Customer.Dns_domain #Load data from AWS in memory ObjTmp.refreshAWS() #Add DNS record if DNS domain specified if self.__ObjLaunchConfig.Customer.Dns_domain: ObjTmp.setDns(singleLaunch.Hostname) self.printMsg("", "[BOMBO] Instance" + inst.id + " saved") #Tagging instances launched self.__setTag(singleLaunch, self.__ObjLaunchConfig, tmpReservation)
def __runProcess(self,kIsTest): import time import boto.ec2 ObjTmp = None self.printMsg ("","[AWS] Connecting ...") #Connecting to region self.__awsConnection = boto.ec2.connect_to_region( self.__ObjLaunchConfig.Region, aws_access_key_id=self.__ObjLaunchConfig.Customer.Access_key, aws_secret_access_key=self.__ObjLaunchConfig.Customer.Secret_key) for singleLaunch in self.__ObjLaunchConfig.LaunchList: #Return Reservation tmpReservation=self.__singleLaunch(kIsTest,singleLaunch,self.__ObjLaunchConfig) #Sleep to avoid INSTANCE NOT FOUND error time.sleep(4) if (self.__checkLaunchStatus(tmpReservation)): self.printMsg ("","Ooh no, I can't believe there was an error during the launch..... you made a mistake as usual!",True,True) for inst in tmpReservation.instances: ObjTmp = clsInstance(inst.id,self.__ObjLaunchConfig.Customer,inst) ObjTmp.Infom_dns = singleLaunch.Hostname + "." + self.__ObjLaunchConfig.Customer.Dns_domain #Load data from AWS in memory ObjTmp.refreshAWS() #Add DNS record if DNS domain specified if self.__ObjLaunchConfig.Customer.Dns_domain: ObjTmp.setDns(singleLaunch.Hostname) self.printMsg ("","[BOMBO] Instance" + inst.id + " saved") #Tagging instances launched self.__setTag(singleLaunch,self.__ObjLaunchConfig,tmpReservation)
def CopyInstance(self,kCustomerId,kRegion,kInstanceId,kSubnet,kKeepInstanceOn = False): from datetime import datetime import sys, time import boto.ec2 import boto.vpc self.showInitialMsg() if kKeepInstanceOn: self.printMsg ("","###> You choose to copy the instance without switching it off, please check when done if it was completed successfully!") ObjCustomer = clsCustomer(kCustomerId) self.printMsg ("","Connecting ...") self.__awsConnection = boto.ec2.connect_to_region( kRegion, aws_access_key_id=ObjCustomer.Access_key, aws_secret_access_key=ObjCustomer.Secret_key) self.printMsg ("","---> " + "Connected") self.printMsg ("","Getting instance details...") reservations = self.__awsConnection.get_all_instances(instance_ids=[kInstanceId]) instance = reservations[0].instances[0] self.printMsg ("","---> " + instance.id) self.printMsg ("","Getting subnet details...") self.__awsVpcConnection = boto.vpc.connect_to_region( kRegion, aws_access_key_id=ObjCustomer.Access_key, aws_secret_access_key=ObjCustomer.Secret_key) subnets = self.__awsVpcConnection.get_all_subnets(subnet_ids=[kSubnet]) old_vpc_id = subnets[0].vpc_id old_azone = subnets[0].availability_zone self.printMsg ("","---> Found " + kSubnet + " linked to " + old_vpc_id + " in " + old_azone) if kKeepInstanceOn == False: self.printMsg ("","Stopping the instance...") instances_to_stop = self.__awsConnection.stop_instances(instance_ids=kInstanceId) counter = 0 spinner = self.spinning_cursor() while counter != len(instances_to_stop): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in instances_to_stop: if instance.update() == "stopped": counter += 1 time.sleep(2) self.printMsg ("","---> Done") self.printMsg ("","Getting volumes...") vols = self.__awsConnection.get_all_volumes(filters={'attachment.instance-id': instance.id}) self.printMsg ("","---> " + str(len(vols)) + " Volumes found") self.printMsg ("","Tagging instance...") self.__awsConnection.create_tags([instance.id], {"bombo_moving": kSubnet + " - " + datetime.today().strftime('%d-%m-%Y %H:%M:%S')}) self.printMsg ("","---> " + kSubnet + " - " + datetime.today().strftime('%d-%m-%Y %H:%M:%S')) #self.printMsg ("","Copying tags...") #TagsList = [] #TagsList = setTagsToInstance([instance.id]) #self.printMsg ("","Finished copying tags") VolumesSnapshotMatchList = [] for vol in vols: self.printMsg ("","Tagging volume " + vol.id + "...") if instance.tags.get('Name') : vol.add_tag("Name", "### Copy of :" + instance.tags.get('Name') + " ###") else: vol.add_tag("Name", "### Copied ###") vol.add_tag("bombo_moving:INSTANCE", instance.id) vol.add_tag("bombo_moving:STATUS", vol.attach_data.status) vol.add_tag("bombo_moving:DEVICE", vol.attach_data.device) self.printMsg ("","Snapshot volume " + vol.id + "...") snapshot = self.__awsConnection.create_snapshot(vol.id, "INSTANCE: " + instance.id + " - DEVICE:" + vol.attach_data.device) VolumesSnapshotMatchList.append([vol,snapshot]) self.printMsg ("","---> Done with [" + str(vol.id) + "] => " + "INSTANCE: " + instance.id + " - DEVICE:" + vol.attach_data.device) self.printMsg ("","Waiting for the snapshots to be ready...") counter = 0 progress = 0 singleValue = "" spinner = self.spinning_cursor() while progress < (100 * len(VolumesSnapshotMatchList)): progress = 0 for VolumesSnapshotMatch in VolumesSnapshotMatchList: singleValue = VolumesSnapshotMatch[1].update() if len(singleValue)==0: singleValue="0%" progress = progress + int(singleValue[:-1]) time.sleep(2) sys.stdout.flush() sys.stdout.write('\r') sys.stdout.write(spinner.next() + " Waiting for " + str(len(VolumesSnapshotMatchList)) + " snapshots => Progress: " + str(progress / len(VolumesSnapshotMatchList)) + "%") sys.stdout.write('\n') self.printMsg ("","---> Snapshots ready...") NewInstanceVolumeList = [] self.printMsg ("","Creating volumes from snapshot...") for VolumesSnapshotMatch in VolumesSnapshotMatchList: new_vol_tmp = self.__awsConnection.create_volume( size=VolumesSnapshotMatch[0].size, volume_type = VolumesSnapshotMatch[0].type, snapshot=VolumesSnapshotMatch[1], zone=old_azone ) new_vol_tmp.add_tag("bombo_moving:DATE", datetime.today().strftime('%d-%m-%Y %H:%M:%S')) new_vol_tmp.add_tag("bombo_moving:SOURCE", kInstanceId + " -> " + VolumesSnapshotMatch[0].attach_data.device) NewInstanceVolumeList.append([VolumesSnapshotMatch[0].attach_data.device,new_vol_tmp]) self.printMsg ("","---> [" + VolumesSnapshotMatch[0].attach_data.device + "] - " + VolumesSnapshotMatch[0].type + ", " + str(VolumesSnapshotMatch[0].size) + " GB created") self.printMsg ("","---> Volumes ready...") self.printMsg ("","Check AMI availability " + instance.image_id + "...") ObjInstance = clsInstance(instance.id,ObjCustomer,instance) if ObjInstance.checkAmiAvailility(self.__awsConnection): self.printMsg ("","---> AMI Available") ObjInstance.Ami = instance.image_id else: self.printMsg ("","###> AMI NOT Available, searching for a similar AMI [" + instance.architecture +" - " + instance.virtualization_type +" - " + instance.hypervisor +" - " + instance.root_device_type + "]") ObjInstance.Ami = self.searchSimilarAMI(self.__awsConnection,instance) self.printMsg ("","Launching new instance on " + kSubnet + "...") reservation = self.__awsConnection.run_instances( image_id = ObjInstance.Ami, instance_type = instance.instance_type, key_name = instance.key_name, #private_ip_address = "10.0.34.100", #security_group_ids = kSingleLaunch.SecGroups, subnet_id=kSubnet, dry_run=False, ) counter = 0 spinner = self.spinning_cursor() while counter != len(reservation.instances): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in reservation.instances: if instance.update() != "pending": counter += 1 time.sleep(2) #Must wait even when the instance is ready, otherwise we got instance not found by AWS self.printMsg ("","---> [" + reservation.instances[0].id + "] => Instance launched") self.printMsg ("","Tagging new instance...") self.__awsConnection.create_tags([instance.id], {"bombo_moving:SOURCE_INSTANCE":kInstanceId}) self.__awsConnection.create_tags([instance.id], {"bombo_moving:DATE":datetime.today().strftime('%d-%m-%Y %H:%M:%S')}) # self.printMsg ("","Extracting tags from source instance...") # TagsList = self.getTagsFromInstance(kInstanceId) # self.printMsg ("","Applying tags to the new instance...") # self.setTagsToInstance(TagsList, instance.id) self.printMsg ("","---> New instance tagged...") self.printMsg ("","Stopping NEW instance...") self.__awsConnection.stop_instances( instance_ids=reservation.instances[0].id, force=True) counter = 0 spinner = self.spinning_cursor() while counter != len(reservation.instances): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in reservation.instances: if instance.update() == "stopped": counter += 1 time.sleep(2) self.printMsg ("","---> Done") self.printMsg ("","Getting volumes list of the new instance...") new_instance_vols = self.__awsConnection.get_all_volumes(filters={'attachment.instance-id': reservation.instances[0].id}) self.printMsg ("","---> " + str(len(vols)) + " volumes found, deleting all..") for new_instance_vol in new_instance_vols: new_instance_vol.detach( force=True) self.printMsg ("","---> " + str(new_instance_vol.id) + " detached....") new_instance_vol.delete self.printMsg ("","---> " + str(new_instance_vol.id) + " and now deleted!") counter = 0 spinner = self.spinning_cursor() while counter < len(new_instance_vols): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for vol_check_detach in new_instance_vols: if vol_check_detach.update() == "available": counter += 1 time.sleep(2) self.printMsg ("","Attaching volumes to the new instance...") for NewInstanceVolume in NewInstanceVolumeList: self.__awsConnection.attach_volume( instance_id = reservation.instances[0].id, volume_id = NewInstanceVolume[1].id, device = NewInstanceVolume[0]) self.printMsg ("","---> " + NewInstanceVolume[0] + " attached to " + str(reservation.instances[0].id)) self.printMsg ("","Starting the NEW instance...") self.__awsConnection.start_instances(instance_ids=reservation.instances[0].id) self.printMsg ("","---> Done") self.printMsg ("","Hopefully everything went well......")
def CopyInstance(self, kCustomerId, kRegion, kInstanceId, kSubnet, kKeepInstanceOn=False): from datetime import datetime import sys, time import boto.ec2 import boto.vpc self.showInitialMsg() if kKeepInstanceOn: self.printMsg( "", "###> You choose to copy the instance without switching it off, please check when done if it was completed successfully!" ) ObjCustomer = clsCustomer(kCustomerId) self.printMsg("", "Connecting ...") self.__awsConnection = boto.ec2.connect_to_region( kRegion, aws_access_key_id=ObjCustomer.Access_key, aws_secret_access_key=ObjCustomer.Secret_key) self.printMsg("", "---> " + "Connected") self.printMsg("", "Getting instance details...") reservations = self.__awsConnection.get_all_instances( instance_ids=[kInstanceId]) instance = reservations[0].instances[0] self.printMsg("", "---> " + instance.id) self.printMsg("", "Getting subnet details...") self.__awsVpcConnection = boto.vpc.connect_to_region( kRegion, aws_access_key_id=ObjCustomer.Access_key, aws_secret_access_key=ObjCustomer.Secret_key) subnets = self.__awsVpcConnection.get_all_subnets(subnet_ids=[kSubnet]) old_vpc_id = subnets[0].vpc_id old_azone = subnets[0].availability_zone self.printMsg( "", "---> Found " + kSubnet + " linked to " + old_vpc_id + " in " + old_azone) if kKeepInstanceOn == False: self.printMsg("", "Stopping the instance...") instances_to_stop = self.__awsConnection.stop_instances( instance_ids=kInstanceId) counter = 0 spinner = self.spinning_cursor() while counter != len(instances_to_stop): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in instances_to_stop: if instance.update() == "stopped": counter += 1 time.sleep(2) self.printMsg("", "---> Done") self.printMsg("", "Getting volumes...") vols = self.__awsConnection.get_all_volumes( filters={'attachment.instance-id': instance.id}) self.printMsg("", "---> " + str(len(vols)) + " Volumes found") self.printMsg("", "Tagging instance...") self.__awsConnection.create_tags( [instance.id], { "bombo_moving": kSubnet + " - " + datetime.today().strftime('%d-%m-%Y %H:%M:%S') }) self.printMsg( "", "---> " + kSubnet + " - " + datetime.today().strftime('%d-%m-%Y %H:%M:%S')) #self.printMsg ("","Copying tags...") #TagsList = [] #TagsList = setTagsToInstance([instance.id]) #self.printMsg ("","Finished copying tags") VolumesSnapshotMatchList = [] for vol in vols: self.printMsg("", "Tagging volume " + vol.id + "...") if instance.tags.get('Name'): vol.add_tag( "Name", "### Copy of :" + instance.tags.get('Name') + " ###") else: vol.add_tag("Name", "### Copied ###") vol.add_tag("bombo_moving:INSTANCE", instance.id) vol.add_tag("bombo_moving:STATUS", vol.attach_data.status) vol.add_tag("bombo_moving:DEVICE", vol.attach_data.device) self.printMsg("", "Snapshot volume " + vol.id + "...") snapshot = self.__awsConnection.create_snapshot( vol.id, "INSTANCE: " + instance.id + " - DEVICE:" + vol.attach_data.device) VolumesSnapshotMatchList.append([vol, snapshot]) self.printMsg( "", "---> Done with [" + str(vol.id) + "] => " + "INSTANCE: " + instance.id + " - DEVICE:" + vol.attach_data.device) self.printMsg("", "Waiting for the snapshots to be ready...") counter = 0 progress = 0 singleValue = "" spinner = self.spinning_cursor() while progress < (100 * len(VolumesSnapshotMatchList)): progress = 0 for VolumesSnapshotMatch in VolumesSnapshotMatchList: singleValue = VolumesSnapshotMatch[1].update() if len(singleValue) == 0: singleValue = "0%" progress = progress + int(singleValue[:-1]) time.sleep(2) sys.stdout.flush() sys.stdout.write('\r') sys.stdout.write(spinner.next() + " Waiting for " + str(len(VolumesSnapshotMatchList)) + " snapshots => Progress: " + str(progress / len(VolumesSnapshotMatchList)) + "%") sys.stdout.write('\n') self.printMsg("", "---> Snapshots ready...") NewInstanceVolumeList = [] self.printMsg("", "Creating volumes from snapshot...") for VolumesSnapshotMatch in VolumesSnapshotMatchList: new_vol_tmp = self.__awsConnection.create_volume( size=VolumesSnapshotMatch[0].size, volume_type=VolumesSnapshotMatch[0].type, snapshot=VolumesSnapshotMatch[1], zone=old_azone) new_vol_tmp.add_tag("bombo_moving:DATE", datetime.today().strftime('%d-%m-%Y %H:%M:%S')) new_vol_tmp.add_tag( "bombo_moving:SOURCE", kInstanceId + " -> " + VolumesSnapshotMatch[0].attach_data.device) NewInstanceVolumeList.append( [VolumesSnapshotMatch[0].attach_data.device, new_vol_tmp]) self.printMsg( "", "---> [" + VolumesSnapshotMatch[0].attach_data.device + "] - " + VolumesSnapshotMatch[0].type + ", " + str(VolumesSnapshotMatch[0].size) + " GB created") self.printMsg("", "---> Volumes ready...") self.printMsg("", "Check AMI availability " + instance.image_id + "...") ObjInstance = clsInstance(instance.id, ObjCustomer, instance) if ObjInstance.checkAmiAvailility(self.__awsConnection): self.printMsg("", "---> AMI Available") ObjInstance.Ami = instance.image_id else: self.printMsg( "", "###> AMI NOT Available, searching for a similar AMI [" + instance.architecture + " - " + instance.virtualization_type + " - " + instance.hypervisor + " - " + instance.root_device_type + "]") ObjInstance.Ami = self.searchSimilarAMI(self.__awsConnection, instance) self.printMsg("", "Launching new instance on " + kSubnet + "...") reservation = self.__awsConnection.run_instances( image_id=ObjInstance.Ami, instance_type=instance.instance_type, key_name=instance.key_name, #private_ip_address = "10.0.34.100", #security_group_ids = kSingleLaunch.SecGroups, subnet_id=kSubnet, dry_run=False, ) counter = 0 spinner = self.spinning_cursor() while counter != len(reservation.instances): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in reservation.instances: if instance.update() != "pending": counter += 1 time.sleep( 2 ) #Must wait even when the instance is ready, otherwise we got instance not found by AWS self.printMsg( "", "---> [" + reservation.instances[0].id + "] => Instance launched") self.printMsg("", "Tagging new instance...") self.__awsConnection.create_tags( [instance.id], {"bombo_moving:SOURCE_INSTANCE": kInstanceId}) self.__awsConnection.create_tags([instance.id], { "bombo_moving:DATE": datetime.today().strftime('%d-%m-%Y %H:%M:%S') }) # self.printMsg ("","Extracting tags from source instance...") # TagsList = self.getTagsFromInstance(kInstanceId) # self.printMsg ("","Applying tags to the new instance...") # self.setTagsToInstance(TagsList, instance.id) self.printMsg("", "---> New instance tagged...") self.printMsg("", "Stopping NEW instance...") self.__awsConnection.stop_instances( instance_ids=reservation.instances[0].id, force=True) counter = 0 spinner = self.spinning_cursor() while counter != len(reservation.instances): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for instance in reservation.instances: if instance.update() == "stopped": counter += 1 time.sleep(2) self.printMsg("", "---> Done") self.printMsg("", "Getting volumes list of the new instance...") new_instance_vols = self.__awsConnection.get_all_volumes( filters={'attachment.instance-id': reservation.instances[0].id}) self.printMsg( "", "---> " + str(len(vols)) + " volumes found, deleting all..") for new_instance_vol in new_instance_vols: new_instance_vol.detach(force=True) self.printMsg("", "---> " + str(new_instance_vol.id) + " detached....") new_instance_vol.delete self.printMsg( "", "---> " + str(new_instance_vol.id) + " and now deleted!") counter = 0 spinner = self.spinning_cursor() while counter < len(new_instance_vols): sys.stdout.write(spinner.next()) sys.stdout.flush() sys.stdout.write('\b') for vol_check_detach in new_instance_vols: if vol_check_detach.update() == "available": counter += 1 time.sleep(2) self.printMsg("", "Attaching volumes to the new instance...") for NewInstanceVolume in NewInstanceVolumeList: self.__awsConnection.attach_volume( instance_id=reservation.instances[0].id, volume_id=NewInstanceVolume[1].id, device=NewInstanceVolume[0]) self.printMsg( "", "---> " + NewInstanceVolume[0] + " attached to " + str(reservation.instances[0].id)) self.printMsg("", "Starting the NEW instance...") self.__awsConnection.start_instances( instance_ids=reservation.instances[0].id) self.printMsg("", "---> Done") self.printMsg("", "Hopefully everything went well......")