Пример #1
0
/*
	Build all translation maps for the given project.
	Does NOT replace a map with a nil map; we assume this is an openstack glitch.

	CAUTION:  ip2 maps are complete, where vm2 or vmid2 maps are not because
			they only reference one of the VMs IP addresses where there might
			be many.
*/
func (p *osif_project) refresh_maps(creds *ostack.Ostack) (rerr error) {

	if p == nil {
		return
	}
	if creds == nil {
		osif_sheep.Baa(1, "IER: refresh_maps given nil creds")
		rerr = fmt.Errorf("creds were nil")
		return
	}

	if *p.name != "_ref_" { // we don't fetch maps from the ref since it's not real
		olastfetch := p.lastfetch // last fetch -- ensure it wasn't fetched while we waited
		p.rwlock.Lock()           // wait for a write lock
		defer p.rwlock.Unlock()   // ensure unlocked on return

		if olastfetch != p.lastfetch { // assume read done while we waited
			return
		}

		osif_sheep.Baa(2, "refresh: creating VM maps from: %s", creds.To_str())
		vmid2ip, ip2vmid, vm2ip, vmid2host, vmip2vm, err := creds.Mk_vm_maps(nil, nil, nil, nil, nil, true)
		if err != nil {
			osif_sheep.Baa(2, "WRN: unable to map VM info (vm): %s; %s   [TGUOSI003]", creds.To_str(), err)
			rerr = err
			creds.Expire() // force re-auth next go round
		} else {

			osif_sheep.Baa(2, "%s map sizes: vmid2ip=%d ip2vmid=%d vm2ip=%d vmid2host=%d vmip2vm=%d",
				*p.name, len(vmid2ip), len(ip2vmid), len(vm2ip), len(vmid2host), len(vmip2vm))
			if len(vmip2vm) > 0 && len(vmid2ip) > 0 && len(ip2vmid) > 0 && len(vm2ip) > 0 && len(vmid2host) > 0 { // don't refresh unless all are good
				p.vmid2ip = vmid2ip // id and vm name map to just ONE ip address
				p.vm2ip = vm2ip
				p.ip2vmid = ip2vmid     // the only complete list of ips
				p.vmid2host = vmid2host // id to physical host
				p.ip2vm = vmip2vm
			}
		}

		fip2ip, ip2fip, err := creds.Mk_fip_maps(nil, nil, true)
		if err != nil {
			osif_sheep.Baa(2, "WRN: unable to map VM info (fip): %s; %s   [TGUOSI004]", creds.To_str(), err)
			rerr = err
			creds.Expire() // force re-auth next go round
		} else {
			osif_sheep.Baa(2, "%s map sizes: ip2fip=%d fip2ip=%d", *p.name, len(ip2fip), len(fip2ip))
			if len(ip2fip) > 0 && len(fip2ip) > 0 {
				p.ip2fip = ip2fip
				p.ip2fip = fip2ip
			}
		}

		ip2mac, _, err := creds.Mk_mac_maps(nil, nil, true)
		if err != nil {
			osif_sheep.Baa(2, "WRN: unable to map MAC info: %s; %s   [TGUOSI005]", creds.To_str(), err)
			rerr = err
			creds.Expire() // force re-auth next go round
		} else {
			osif_sheep.Baa(2, "%s map sizes: ip2mac=%d", *p.name, len(ip2mac))
			if len(ip2mac) > 0 {
				p.ip2mac = ip2mac
			}
		}

		gwmap, _, gwmac2id, _, _, gwip2phost, err := creds.Mk_gwmaps(nil, nil, nil, nil, nil, nil, true, false) // gwmap is mac2ip
		if err != nil {
			osif_sheep.Baa(2, "WRN: unable to map gateway info: %s; %s   [TGUOSI006]", creds.To_str(), err)
			creds.Expire() // force re-auth next go round
		} else {
			osif_sheep.Baa(2, "%s map sizes: gwmap=%d", *p.name, len(gwmap))
			if len(gwmap) > 0 {
				p.gwmap = gwmap
			}

			for mac, id := range gwmac2id { // run the gateway info and insert as though they were first class VMs
				ip := gwmap[mac]
				p.vmid2ip[*id] = ip
				p.ip2vmid[*ip] = id
				p.vmid2host[*id] = gwip2phost[*ip]
				p.vm2ip[*ip] = ip // gw is nameless, so use the ip address
			}
		}

		_, gw2cidr, err := creds.Mk_snlists() // get list of gateways and their subnet cidr
		if err == nil && gw2cidr != nil {
			p.gw2cidr = gw2cidr
		} else {
			if err != nil {
				osif_sheep.Baa(1, "WRN: unable to create gateway to cidr map: %s; %s   [TGUOSI007]", creds.To_str(), err)
			} else {
				osif_sheep.Baa(1, "WRN: unable to create gateway to cidr map: %s  no reason given   [TGUOSI007]", creds.To_str())
			}
			creds.Expire() // force re-auth next go round
		}

		p.lastfetch = time.Now().Unix()
	}

	return
}