Beispiel #1
0
 def get_by_fixed_ip_id(cls, context, fixed_ip_id):
     db_floatingips = db.floating_ip_get_by_fixed_ip_id(
         context, fixed_ip_id)
     return obj_base.obj_make_list(context, cls(), FloatingIP,
                                   db_floatingips)
Beispiel #2
0
 def get_by_fixed_ip_id(cls, context, fixed_ip_id):
     db_floatingips = db.floating_ip_get_by_fixed_ip_id(context,
                                                        fixed_ip_id)
     return obj_base.obj_make_list(context, cls(), FloatingIP,
                                   db_floatingips)
    def index(self, req):
        """Return all flavors in brief."""
        #flavors = self._get_flavors(req)
        #return self._view_builder.index(req, flavors)
	project_id=str(req.environ['HTTP_X_TENANT_ID'])
	context = req.environ['nova.context']
	context = context.elevated()

        networks = db.network_get_all(context)
        nets=[dict(network.iteritems()) for network in networks]

	virtual_interfaces = db.virtual_interface_get_all(context)
	vifs=[dict(vif.iteritems()) for vif in virtual_interfaces]
	
	#make a dict of relationships between Network_IDs and Instance_IDs {1:[1,2],...}
	net_vm_dict = {}
	for vif in vifs:
	    net_id=int(vif['network_id'])	
	    vm_id=int(vif['instance_id'])
	    if net_id in net_vm_dict:
		net_vm_dict[net_id].append(vm_id)
	    else:
		net_vm_dict[net_id] = [vm_id]
	print net_vm_dict


	#Go through the dict , filter by this project and get detailed infos	
	#instance_get(context, instance_id)
	net_list = []
	for netID in net_vm_dict:
	    try:
	        networks= db.network_get(context, netID)
	    except exception.NetworkNotFound:
		print " network not found"
		continue
	    net = dict(networks.iteritems())
	    print str(net['project_id'])
	    if net['project_id']==None or net['project_id']==project_id:
		print "my precious~~"
		net_info = {}
		net_info['id']=str(net['uuid'])
		net_info['name']=str(net['label'])
		net_info['cidr']=str(net['cidr'])
		net_info['vm']=[]
		net_list.append(net_info)	
		for vmID in  net_vm_dict[netID]:
		    vms = db.instance_get(context, vmID)
		    vm = dict(vms.iteritems())
		    if vm['project_id']==project_id:
		        print "My VM"
			vm_info = {}
			#Get vm infos for each VM
			vm_info['name']=str(vm['hostname'])
			vm_info['id']=str(vm['uuid'])
			vm_info['vm_state']=str(vm['vm_state'])
			#Get fixed_ips for each VM
			fixed_ips = db.fixed_ip_get_by_instance(context, vmID)
			fixed_ip_info = []
			for ip in fixed_ips:
			    fixed_ip_info.append(str(dict(ip.iteritems())['address']))
			vm_info['fixed_ips'] = fixed_ip_info
			#Get Floating_ips for each VM
			floating_ip_info = []
			for fixed_ip in fixed_ips:
			    
			    try:
			        floating_ips = db.floating_ip_get_by_fixed_ip_id(context, str(dict(fixed_ip.iteritems())['id']))
			    except exception.FloatingIpNotFoundForAddress:
				print "floating not found"
				continue
			    if floating_ips != None:
			        for floating_ip in floating_ips:
				    floating_ip_info.append(str(dict(floating_ip.iteritems())['address']))
			vm_info['floating_ips']=floating_ip_info
			net_info['vm'].append(vm_info)

	for net in nets:
	    if net['id'] in net_vm_dict:
		print "Existed network"
	    else:
		net_info = {}
                net_info['id']=str(net['uuid'])
                net_info['name']=str(net['label'])
                net_info['cidr']=str(net['cidr'])
                net_info['vm']=[]
                net_list.append(net_info)
	
	ret_net_list={}
	ret_net_list['networks']=net_list
	print ret_net_list

	return ret_net_list
Beispiel #4
0
    def notify(self, message):

        ctxt = context.get_admin_context()
        event_type = message.get('event_type')
        if event_type in FLAGS.wiki_eventtype_blacklist:
            return
        if event_type not in FLAGS.wiki_eventtype_whitelist:
            LOG.debug("Ignoring message type %s" % event_type)
            return

        payload = message['payload']
        instance_name = payload['display_name']
        uuid = payload['instance_id']

        if FLAGS.wiki_instance_dns_domain:
            fqdn = "%s.%s" % (instance_name, FLAGS.wiki_instance_dns_domain)
        else:
            fqdn = instance_name

        template_param_dict = {}
        for field in self.RawTemplateFields:
            template_param_dict[field] = payload[field]

        tenant_id = payload['tenant_id']
        if (FLAGS.wiki_use_keystone and
            self._keystone_login(tenant_id, ctxt)):
            tenant_obj = self.tenant_manager[tenant_id].get(tenant_id)
            user_obj = self.user_manager[tenant_id].get(payload['user_id'])
            tenant_name = tenant_obj.name
            user_name = user_obj.name
            template_param_dict['tenant'] = tenant_name
            template_param_dict['username'] = user_name

        inst = db.instance_get_by_uuid(ctxt, uuid)
        old_school_id = inst.id
        ec2_id = 'i-%08x' % old_school_id

        template_param_dict['cpu_count'] = inst.vcpus
        template_param_dict['disk_gb_current'] = inst.ephemeral_gb
        template_param_dict['host'] = inst.host
        template_param_dict['reservation_id'] = inst.reservation_id
        template_param_dict['availability_zone'] = inst.availability_zone
        template_param_dict['original_host'] = inst.launched_on
        template_param_dict['fqdn'] = fqdn
        template_param_dict['ec2_id'] = ec2_id
        template_param_dict['project_name'] = inst.project_id
        template_param_dict['region'] = FLAGS.wiki_instance_region

        try:
            fixed_ips = db.fixed_ip_get_by_instance(ctxt, old_school_id)
        except exception.FixedIpNotFoundForInstance:
            fixed_ips = []
	ips = []
	floating_ips = []
	for fixed_ip in fixed_ips:
	    ips.append(fixed_ip.address)
	    for floating_ip in db.floating_ip_get_by_fixed_ip_id(ctxt, fixed_ip.id):
	        floating_ips.append(floating_ip.address)

        template_param_dict['private_ip'] = ','.join(ips)
        template_param_dict['public_ip'] = ','.join(floating_ips)

        sec_groups = db.security_group_get_by_instance(ctxt, old_school_id)
        grps = [grp.name for grp in sec_groups]
        template_param_dict['security_group'] = ','.join(grps)

        fields_string = ""
        for key in template_param_dict:
            fields_string += "\n|%s=%s" % (key, template_param_dict[key])

        if event_type == 'compute.instance.delete.start':
            page_string = "\n%s\nThis instance has been deleted.\n%s\n" % (begin_comment,
                                                                           end_comment)
        else:
            page_string = "\n%s\n{{InstanceStatus%s}}\n%s\n" % (begin_comment,
                                                                fields_string,
                                                                end_comment)

        self._wiki_login()
        pagename = "%s%s" % (FLAGS.wiki_page_prefix, ec2_id)
        LOG.debug("wikistatus:  Writing instance info"
                  " to page http://%s/wiki/%s" %
                  (FLAGS.wiki_host, pagename))

        page = self.site.Pages[pagename]
        try:
            pText = page.edit()
            start_replace_index = pText.find(begin_comment)
            if start_replace_index == -1:
                # Just stick it at the end.
                newText = "%s%s" % (page_string, pText)
            else:
                # Replace content between comment tags.
                end_replace_index = pText.find(end_comment, start_replace_index)
                if end_replace_index == -1:
                    end_replace_index = start_replace_index + len(begin_comment)
                else:
                    end_replace_index += len(end_comment)
                newText = "%s%s%s" % (pText[:start_replace_index],
                                      page_string,
                                      pText[end_replace_index:])
            page.save(newText, "Auto update of instance info.")
        except (mwclient.errors.InsufficientPermission,
                mwclient.errors.LoginError):
            LOG.debug("Failed to update wiki page..."
                      " trying to re-login next time.")
            self._wiki_logged_in = False
Beispiel #5
0
    def notify(self, message):

        ctxt = context.get_admin_context()
        event_type = message.get('event_type')
        if event_type in FLAGS.wiki_eventtype_blacklist:
            return
        if event_type not in FLAGS.wiki_eventtype_whitelist:
            LOG.debug("Ignoring message type %s" % event_type)
            return

        payload = message['payload']
        instance_name = payload['display_name']
        uuid = payload['instance_id']

        if FLAGS.wiki_instance_dns_domain:
            fqdn = "%s.%s" % (instance_name, FLAGS.wiki_instance_dns_domain)
        else:
            fqdn = instance_name

        template_param_dict = {}
        for field in self.RawTemplateFields:
            template_param_dict[field] = payload[field]

        tenant_id = payload['tenant_id']
        if (FLAGS.wiki_use_keystone and self._keystone_login(tenant_id, ctxt)):
            tenant_obj = self.tenant_manager[tenant_id].get(tenant_id)
            user_obj = self.user_manager[tenant_id].get(payload['user_id'])
            tenant_name = tenant_obj.name
            user_name = user_obj.name
            template_param_dict['tenant'] = tenant_name
            template_param_dict['username'] = user_name

        inst = db.instance_get_by_uuid(ctxt, uuid)
        old_school_id = inst.id
        ec2_id = 'i-%08x' % old_school_id

        template_param_dict['cpu_count'] = inst.vcpus
        template_param_dict['disk_gb_current'] = inst.ephemeral_gb
        template_param_dict['host'] = inst.host
        template_param_dict['reservation_id'] = inst.reservation_id
        template_param_dict['availability_zone'] = inst.availability_zone
        template_param_dict['original_host'] = inst.launched_on
        template_param_dict['fqdn'] = fqdn
        template_param_dict['ec2_id'] = ec2_id
        template_param_dict['project_name'] = inst.project_id
        template_param_dict['region'] = FLAGS.wiki_instance_region

        try:
            fixed_ips = db.fixed_ip_get_by_instance(ctxt, old_school_id)
        except exception.FixedIpNotFoundForInstance:
            fixed_ips = []
        ips = []
        floating_ips = []
        for fixed_ip in fixed_ips:
            ips.append(fixed_ip.address)
            for floating_ip in db.floating_ip_get_by_fixed_ip_id(
                    ctxt, fixed_ip.id):
                floating_ips.append(floating_ip.address)

        template_param_dict['private_ip'] = ','.join(ips)
        template_param_dict['public_ip'] = ','.join(floating_ips)

        sec_groups = db.security_group_get_by_instance(ctxt, old_school_id)
        grps = [grp.name for grp in sec_groups]
        template_param_dict['security_group'] = ','.join(grps)

        fields_string = ""
        for key in template_param_dict:
            fields_string += "\n|%s=%s" % (key, template_param_dict[key])

        if event_type == 'compute.instance.delete.start':
            page_string = "\n%s\nThis instance has been deleted.\n%s\n" % (
                begin_comment, end_comment)
        else:
            page_string = "\n%s\n{{InstanceStatus%s}}\n%s\n" % (
                begin_comment, fields_string, end_comment)

        self._wiki_login()
        pagename = "%s%s" % (FLAGS.wiki_page_prefix, ec2_id)
        LOG.debug("wikistatus:  Writing instance info"
                  " to page http://%s/wiki/%s" % (FLAGS.wiki_host, pagename))

        page = self.site.Pages[pagename]
        try:
            pText = page.edit()
            start_replace_index = pText.find(begin_comment)
            if start_replace_index == -1:
                # Just stick it at the end.
                newText = "%s%s" % (page_string, pText)
            else:
                # Replace content between comment tags.
                end_replace_index = pText.find(end_comment,
                                               start_replace_index)
                if end_replace_index == -1:
                    end_replace_index = start_replace_index + len(
                        begin_comment)
                else:
                    end_replace_index += len(end_comment)
                newText = "%s%s%s" % (pText[:start_replace_index], page_string,
                                      pText[end_replace_index:])
            page.save(newText, "Auto update of instance info.")
        except (mwclient.errors.InsufficientPermission,
                mwclient.errors.LoginError):
            LOG.debug("Failed to update wiki page..."
                      " trying to re-login next time.")
            self._wiki_logged_in = False