def getRunningInstancesTree(self, req): euca = None try : euca = Euca2ool() except Exception, e: print e
def getMachineImageTree(self,req): euca = Euca2ool() image_ids = euca.process_args() image_json_string = "" euca_conn = euca.make_connection() images = euca_conn.get_all_images() image_no = 0 for image in images : if image.type == "machine" : # atmosphere info try : machine_image_name = Machine_images.objects.get(image_id = image.id).image_name except : machine_image_name = "" # end of atmos info image_no = image_no + 1 #image.id, image.location, image.ownerId, image.state, image.is_public, image.product_codes, image.architecture, image.type, image.ramdisk_id, image.kernel_id if image.is_public: image.is_public = "public" else : image.is_puiblic = "private" image_json_string = image_json_string +""" { "id" : "%s", "text" : "%s (%s)", "leaf" : "false" }, """ % ( image_no, machine_image_name, image.id ) r = "[%s]" % image_json_string[0:-2] return r
def _upload_file_to_s3(self, bucket_name, keyname, filename, s3_key, s3_secret, s3_url, canned_acl='aws-exec-read'): if not has_euca: raise Exception("Euca2ools missing.. Required to run this function") s3euca = Euca2ool(is_s3=True) s3euca.ec2_user_access_key = s3_key s3euca.ec2_user_secret_key = s3_secret s3euca.url = s3_url conn = s3euca.make_connection() bucket_instance = _ensure_bucket(conn, bucket_name, canned_acl) k = Key(bucket_instance) k.key = keyname with open(filename, "rb") as the_file: try: logger.debug("Uploading File:%s to bucket:%s // key:%s" % (filename, bucket_name, keyname)) k.set_contents_from_file(the_file, policy=canned_acl) logger.debug("File Upload complete") except S3ResponseError, s3error: s3error_string = '%s' % (s3error) if s3error_string.find("403") >= 0: logger.exception("Permission denied while writing : %s\n%s" % (k.key, s3error))
def getVolumeListTree(self, reg) : logging.debug("_seungjin_ getVolumeListTree called") euca = Euca2ool() euca_conn = euca.make_connection() volumes = euca_conn.get_all_volumes() volume_json_string = u'' volume_no = 0 for volume in volumes: volume_no = volume_no + 1 try : volume_name = Machine_volumes.objects.get(volume_id = volume.id).volume_name except : volume_name = "" volume_json_string = volume_json_string + """ { "id" : "%s", "text" : "%s (%s)", "leaf" : "false" }, """ % ( volume_no , volume_name , volume.id , ) return "["+volume_json_string[0:-2]+"]"
def getKeyPairsList(self, req) : euca = Euca2ool() euca_conn = euca.make_connection() keypairs = euca_conn.get_all_key_pairs() keypairs_list = '' for keypair in keypairs : keypair_string = """{'keypair_name' : '%s', 'keypair_fingerprint' : '%s'},""" % (keypair.name, keypair.fingerprint) keypairs_list = keypairs_list + keypair_string return "[%s]" % keypairs_list[0:-1]
def _upload_bundle(self, bucket_name, manifest_path, ec2cert_path=None, directory=None, part=None, canned_acl='aws-exec-read', skipmanifest=False): """ upload_bundle - Read the manifest and upload the entire bundle (In parts) to the S3 Bucket (bucket_name) Required Args: bucket_name - The name of the S3 Bucket to be created manifest_path - The absolute path to the XML manifest Optional Args: ec2cert_path = (Default:os.environ['EC2_CERT']) The absolute path to the Admin EC2 Cert (For S3 Access) canned_acl - (Default:'aws-exec-read') skipmanifest - (Default:False) Skip manifest upload directory - Select directory of parts (If different than values in XML) part - """ if not has_euca: raise Exception( "Euca2ools missing.. Required to run this function") from euca2ools import Euca2ool, FileValidationError logger.debug("Validating the manifest") try: self.euca.validate_file(manifest_path) except FileValidationError: print 'Invalid manifest' logger.error("Invalid manifest file provided. Check path") raise s3euca = Euca2ool(is_s3=True) s3euca.ec2_user_access_key = self.euca.ec2_user_access_key s3euca.ec2_user_secret_key = self.euca.ec2_user_secret_key s3euca.url = self.s3_url conn = s3euca.make_connection() bucket_instance = _ensure_bucket(conn, bucket_name, canned_acl) logger.debug("S3 Bucket %s Created. Retrieving Parts from manifest" % bucket_name) parts = self._get_parts(manifest_path) if not directory: manifest_path_parts = manifest_path.split('/') directory = manifest_path.replace( manifest_path_parts[len(manifest_path_parts) - 1], '') if not skipmanifest and not part: _upload_manifest(bucket_instance, manifest_path, canned_acl) logger.debug("Uploading image in parts to S3 Bucket %s." % bucket_name) _upload_parts(bucket_instance, directory, parts, part, canned_acl) return "%s/%s" % \ (bucket_name, self.euca.get_relative_filename(manifest_path))
def getImages(self, req): # list image list euca = Euca2ool() image_ids = euca.process_args() image_json_string = "" euca_conn = euca.make_connection() images = euca_conn.get_all_images() for image in images : if image.type == "machine" : # get atmosphere info try : image_name = Machine_images.objects.get(image_id = image.id).image_name except : image_name = "" try : image_description = Machine_images.objects.get(image_id = image.id).image_description if image_description == None : image_descriontino = "" except: image_description = "" try : image_tags = Machine_images.objects.get(image_id = image.id).image_tags if image_tags == None : image_tags = "" except : image_tags = "no tags" #image.id, image.location, image.ownerId, image.state, image.is_public, image.product_codes, image.architecture, image.type, image.ramdisk_id, image.kernel_id if image.is_public: image.is_public = "public" else : image.is_puiblic = "private" image_json_string = image_json_string +""" { "image_name" : "%s", "image_description" : "%s", "image_tags" : "%s", "image_id" : "%s" , "image_location" : "%s" , "image_ownerid" : "%s" , "image_state" : "%s" , "image_is_public" : "%s" , "image_product_codes" : "%s" , "image_architecture" : "%s" , "image_type" : "%s" , "image_ramdisk_id" : "%s" , "image_kernel_id" : "%s" }, """ % ( image_name, image_description.replace("\n", "<br>"), image_tags, image.id, image.location, image.ownerId, image.state, image.is_public, image.product_codes, image.architecture, image.type, image.ramdisk_id, image.kernel_id ) r = "[%s]" % image_json_string[0:-2] return r
def getKeyPairsListTree(self, req) : euca = Euca2ool() euca_conn = euca.make_connection() keypairs = euca_conn.get_all_key_pairs() keypairs_list = '' keypair_id = 0 for keypair in keypairs : keypair_id = keypair_id + 1 keypair_string = """{'id' : '%s', 'text' : '%s', 'leaf' : 'false'},""" % (keypair_id,keypair.name) keypairs_list = keypairs_list + keypair_string return "[%s]" % keypairs_list[0:-1]
def detachVolume(self,req) : if req.method == "POST": instance_id = req.POST['instance_id'] volume_id = req.POST['volume_id'] try: euca = Euca2ool('i:d:', ['instance=', 'device=']) except Exception, e: print e euca_conn = euca.make_connection() try: return_code = euca_conn.detach_volume(volume_id = volume_id) except Exception, ex: logging.error(ex)
def getVolumeList(self,req) : logging.debug("_seungjin_ method call : getVolumeList") euca = Euca2ool() euca_conn = euca.make_connection() volumes = euca_conn.get_all_volumes() volume_json_string = u'' volume_no = 0 for volume in volumes: volume_no = volume_no + 1 try : volume_name = Machine_volumes.objects.get(volume_id = volume.id).volume_name except : volume_name = "" try : volume_description = Machine_volumes.objects.get(volume_id = volume.id).volume_description except : volume_description = "" try : volume_tags = Machine_volumes.objects.get(volume_id = volume.id).volume_tags except : volume_tags = "" volume_json_string = volume_json_string + """ { "no" : "%s", "name" : "%s", "description" : "%s", "tags" : "%s" , "id" : "%s", "size" : "%s", "snapshot_id" : "%s", "status" : "%s", "create_time" : "%s" , "attach_data_instance_id" : "%s" , "attach_data_device" : "%s" , "attach_data_attach_time" : "%s" }, """ % ( volume_no , volume_name , volume_description.replace("\n", "<br>"), volume_tags, volume.id , volume.size , volume.snapshot_id, volume.status, volume.create_time, volume.attach_data.instance_id, volume.attach_data.device, volume.attach_data.attach_time ) logging.debug("_seungjin_ 2" + "["+volume_json_string[0:-2]+"]") return "["+volume_json_string[0:-2]+"]"
def __init__(self, ec2_access_key, ec2_secret_key, ec2_url, s3_url): self.euca = Euca2ool() self.euca.ec2_user_access_key = ec2_access_key self.euca.ec2_user_secret_key = ec2_secret_key self.euca.ec2_url = ec2_url self.euca.s3_url = s3_url self.ec2_access_key = ec2_access_key self.ec2_secret_key = ec2_secret_key self.ec2_url = ec2_url self.s3_url = s3_url self.userid = Ec2_keys.objects.get( ec2_access_key=self.euca.ec2_user_access_key).username
def attachVolume(self, req): if req.method == "POST": instance_id = req.POST['instance_id'] device = req.POST['device'] volume_id = req.POST['volume_id'] try: euca = Euca2ool('i:d:', ['instance=', 'device=']) except Exception, e: logging.error(e) #euca_conn = euca.make_connection() euca_conn = self.euca.make_connection() try: return_code = euca_conn.attach_volume(volume_id=volume_id, instance_id=instance_id, device=device) except Exception, ex: logging.error(ex)
def launchInstance(self, req) : logging.error("_seungjin_ launch call") if req.method == "POST": #print req.POST #print req.POST['image_id'] #req.POST['instance_name'] #req.POST['instance_description'] #req.POST['instance_tags'] image_id = req.POST['image_id'] keyname = req.POST['auth_key'] kernel_id = None ramdisk_id = None min_count = req.POST['num_of_instances'] max_count = req.POST['num_of_instances'] instance_type = req.POST['instance_size'] group_names = [ ] user_data = None user_data_file = None addressing_type = None zone = None try : euca = Euca2ool('k:n:t:g:d:f:z:', ['key=', 'kernel=', 'ramdisk=', 'instance-count=', 'instance-type=', 'group=', 'user-data=', 'user-data-file=', 'addressing=', 'availability-zone=']) except Exception, e: logging.error("euca tool error: "+e) euca_conn = euca.make_connection() try : reservation = euca_conn.run_instances ( image_id = image_id, min_count = min_count, max_count = max_count, key_name = keyname, security_groups = group_names, user_data = user_data, addressing_type = addressing_type, instance_type = instance_type, placement = zone, kernel_id = kernel_id, ramdisk_id = ramdisk_id ) except Exception, e: logging.error("cloud error 272")
def getInstanceTypeSize(self): #to use atmosphere for amazonaws #os.environ['EC2_URL'] = 'https://' + region + ".ec2.amazonaws.com" euca = Euca2ool('', ['region=']) euca.ec2_user_access_key = self.access_key euca.ec2_user_secret_key = self.secret_key euca.ec2_url = self.ec2_url zone_ids = "verbose" euca_conn = euca.make_connection() zones = euca_conn.get_all_zones(zone_ids) typeSize = [] zonename = zones[0].name for zone in zones[2:]: typeSize.append((zonename,zone.name[3:],zone.state.split()[3],zone.state.split()[4],zone.state.split()[5])) #zone_string = '%s\t%s' % (zone.name, zone.state) #if region: # zone_string += '\t%s' % region # print 'AVAILABILITYZONE\t%s' % (zone_string) return typeSize
def terminateInstance(self, req) : if req.method == "POST" : euca = Euca2ool() euca_conn = euca.make_connection() instance_ids = [] instance_ids.append(req.POST['instance_id']) print instance_ids try : euca_conn.terminate_instances(instance_ids) except Exception, ex: print ex # update status in my db try: e = Instances.objects.get(instance_id = req.POST['instance_id']) e.state = 'terminated' e.save() except : pass
def _init_euca2ool(self, key, secret, url, is_s3=False): # Argv must be reset to stop euca from gobbling bad sys.argv's if not has_euca: raise Exception("Euca2ools missing.. Required to run this function") sys.argv = [] euca = Euca2ool( short_opts=None, long_opts=None, is_s3=is_s3, compat=False) #Prepare euca environment euca.ec2_user_access_key = key euca.ec2_user_secret_key = secret euca.url = url euca.environ['EC2_CERT'] = self.ec2_cert_path euca.environ['EUCALYPTUS_CERT'] = self.euca_cert_path euca.environ['EC2_PRIVATE_KEY'] = self.pk_path return euca
def createVolume(self, req): #euca-create-volume -S, --size size | --snapshot snapshot_id -z zone # for current system. zone is fixed. zone=iplant size = None zone = "iplant01" snaphost_id = None name = None description = None tags = None volume_id = None if req.method == "POST": size = req.POST['size'] name = req.POST['name'] description = req.POST['description'] tags = req.POST['tags'] else: return "method not valid" euca_local = Euca2ool('s:z:', ['zone=', 'snapshot=', 'size='], compat=True) euca_local.ec2_user_access_key = self.ec2_access_key euca_local.ec2_user_secret_key = self.ec2_secret_key euca_local.ec2_url = self.ec2_url euca_local.s3_url = self.s3_url euca_conn = euca_local.make_connection() try: euca_local.validate_volume_size(int(size)) except SizeValidationError: print 'Invalid volume size' try: volume = euca_conn.create_volume(size, zone, snaphost_id) if volume: volume_id = str(volume).split(":")[1] except Exception, ex: print ex
def openConnection(self): euca = Euca2ool() euca_conn = euca.make_connection() return euca_conn
def createKeyPair(self, req) : euca = Euca2ool() euca_conn = euca.make_connection() keypair = euca_conn.create_key_pair(req.POST['keypair_name']) print keypair.material
def getInstanceList(self, req) : euca = None try : euca = Euca2ool() except Exception, e: print e
def getInstanceInfo(self,req) : euca = None try : euca = Euca2ool() except Exception, e: logging.error(e)