Exemplo n.º 1
0
    def launch_instance(self):
        """ Launch an instance need to clean up the exception logic """

        try:
            img = CloudTools.findBy("id", self.conn.list_images(), self.options.id)
            sze = CloudTools.findBy("id", self.conn.list_sizes(), self.options.size)
            if hasattr(self.options, "keyname") and self.options.keyname is not None:
                if hasattr(self.options, "userdata") and self.options.userdata is not None:
                    self.conn.create_node(
                        name="",
                        image=img,
                        size=sze,
                        ex_keyname=self.options.keyname,
                        ex_mincount=self.options.number,
                        ex_userdata=base64.b64decode(self.options.userdata),
                    )
                else:
                    self.conn.create_node(
                        name="", image=img, size=sze, ex_keyname=self.options.keyname, ex_mincount=self.options.number
                    )

            return CloudTools.SUCCESS
        except InvalidCredsError:
            raise InvalidCredsError()
        except:
            return '[{"message": "Quota exceeded: code=InstanceLimitExceeded", "code": 413, "retryAfter": 0}]'
Exemplo n.º 2
0
def resource_by_id(id, resources):
    image = CloudTools.findBy("id", resources, id)
    if image is None:
        return "[]"
    i_dict = image.__dict__
    del i_dict["driver"]
    return json.dumps([i_dict])
Exemplo n.º 3
0
    def delete_instance(self, instance_id):
        """ Delete the instance with id instance_id"""

        ins = CloudTools.findBy("id", self.conn.list_nodes(), instance_id)
        self.conn.destroy_node(ins)
        return CloudTools.SUCCESS
Exemplo n.º 4
0
def api_request(options, conn):
    try:
        # LIST
        if options.list == "images":
            if options.id:
                json_results = resource_by_id(options.id, conn.list_images())
            else:
                json_results = CloudTools.jsonifyLList(conn.list_images())
            json_results = json_results.replace(
                '"imagetype": "ramdisk"',
                '"imagetype":"ramdisk","container_format":"ari"')
            json_results = json_results.replace(
                '"imagetype": "kernel"',
                '"imagetype":"kernel","container_format":"aki"')
            json_results = json_results.replace(
                '"imagetype": "machine"',
                '"imagetype":"machine","container_format":"ami"')

            json_results = json_results.replace('"state": "available"','"state": "active"')

            if options.limit:
                start = 0
                if options.marker:
                    logger.debug("marker is set")
                    logger.debug(options.marker)

                    # I think my logic here was that if the marker
                    # was an OpenStack image than we aren't even looking
                    # at these images yet however this means that either
                    # we have looked at all of them or none of them yet
                    # in the latter case we will never be able to look
                    # at them so I would rather choose the less wrong
                    # option of always showing them.
#                    if not options.marker.startswith(('emi','eki','eri')):
#                        start = limit
#                    else:
                    for index, image in enumerate(json.loads(json_results)):
                        if image['id'] == options.marker:
                            start = index + 1
                     
                logger.debug("the limit: %s", options.limit)
                logger.debug(len(json.loads(json_results)))
                logger.debug(start + options.limit)
                json_results = json.dumps(json.loads(json_results)[start:start + options.limit])
		

            print json_results

        elif options.list == "instances":
            if options.id:
        	json_results = resource_by_id(options.id, conn.list_nodes())
            else:
        	json_results = CloudTools.jsonifyLList(conn.list_nodes())
            print json_results.replace('"status": "running"','"status": "active"')

        elif options.list == "keys":
            aws_schema = "http://ec2.amazonaws.com/doc/2010-08-31/"
            xml_as_dict = xmldict.xml_to_dict(conn.list_keys().__dict__['body'].replace(aws_schema, ''))
            if xml_as_dict["DescribeKeyPairsResponse"]["keySet"] is None:
        	print []
            else:
        	result =  xml_as_dict["DescribeKeyPairsResponse"]["keySet"]["item"]


        	if 'keyName' in result:
        	    result['keyMaterial'] = ""
        	    result = [result]
        	else:
        	    for item in result:
        	        if 'keyName' in result:
        			item['keyMaterial'] = ""

        	if options.id:
        	    result = keypair_by_name(options.id, result)
                keys_json = json.dumps(result)
                print(keys_json)

        # ACTIONS
        elif options.action == "launch":
            try:
        	img = CloudTools.findBy("id", conn.list_images(), options.id)
                sze = CloudTools.findBy("id", conn.list_sizes(), options.size)
        	if hasattr(options, 'keyname') and options.keyname is not None:
        	    if hasattr(options, 'userdata') and options.userdata is not None:
        		conn.create_node(name='', image=img, size=sze,
                            ex_keyname=options.keyname,
                            ex_mincount=options.number,
        		    ex_userdata=base64.b64decode(options.userdata))
        	    else:			
        		conn.create_node(name='', image=img, size=sze, 
        		    ex_keyname=options.keyname, 
        		    ex_mincount=options.number)

        	print(CloudTools.SUCCESS)
            except InvalidCredsError:
                raise InvalidCredsError()
            except:
        	print '[{"message": "Quota exceeded: code=InstanceLimitExceeded", "code": 413, "retryAfter": 0}]'

        elif options.action == "kill":
            ins = CloudTools.findBy("id", conn.list_nodes(), options.id)
            conn.destroy_node(ins)
            print(CloudTools.SUCCESS)

        elif options.action == "create_keypair":
            try:
        	resp = conn.ex_create_keypair(name=options.keyname)
    		for keys in resp:
        	    resp[keys] = resp[keys].replace('\n','\\n')
        	resp['keyName'] = options.keyname
        	print json.dumps([resp])
            except InvalidCredsError:
        	raise InvalidCredsError()
            except:
        	print '[{"message": "Key pair \'' + options.keyname + '\' already exists.", "code": 409}]'

        elif options.action == "import_keypair":
            resp = conn.ex_import_keypair(name=options.keyname, keyfile=options.keyfile)
            for keys in resp:
                resp[keys] = resp[keys].replace('\n','\\n')
            print json.dumps([resp])
            
    except InvalidCredsError:
        time.sleep(2)
        api_request(options, conn)