Exemple #1
0
// SSHAddress returns a function that can be given to the SSH communicator
// for determining the SSH address based on the server AccessIPv4 setting..
func SSHAddress(csp gophercloud.CloudServersProvider, port int) func(multistep.StateBag) (string, error) {
	return func(state multistep.StateBag) (string, error) {
		s := state.Get("server").(*gophercloud.Server)

		if ip := state.Get("access_ip").(gophercloud.FloatingIp); ip.Ip != "" {
			return fmt.Sprintf("%s:%d", ip.Ip, port), nil
		}

		ip_pools, err := s.AllAddressPools()
		if err != nil {
			return "", errors.New("Error parsing SSH addresses")
		}
		for pool, addresses := range ip_pools {
			if pool != "" {
				for _, address := range addresses {
					if address.Addr != "" && address.Version == 4 {
						return fmt.Sprintf("%s:%d", address.Addr, port), nil
					}
				}
			}
		}

		serverState, err := csp.ServerById(s.Id)

		if err != nil {
			return "", err
		}

		state.Put("server", serverState)
		time.Sleep(1 * time.Second)

		return "", errors.New("couldn't determine IP address for server")
	}
}
func withServer(api gophercloud.CloudServersProvider, f func(string)) {
	id, err := createServer(api, "", "", "", "")
	if err != nil {
		panic(err)
	}

	for {
		s, err := api.ServerById(id)
		if err != nil {
			panic(err)
		}
		if s.Status == "ACTIVE" {
			break
		}
		time.Sleep(10 * time.Second)
	}

	f(id)

	// I've learned that resizing an instance can fail if a delete request
	// comes in prior to its completion.  This ends up leaving the server
	// in an error state, and neither the resize NOR the delete complete.
	// This is a bug in OpenStack, as far as I'm concerned, but thankfully,
	// there's an easy work-around -- just wait for your server to return to
	// active state first!
	waitForServerState(api, id, "ACTIVE")
	err = api.DeleteServerById(id)
	if err != nil {
		panic(err)
	}
}
Exemple #3
0
// ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch
// an openstacn server.
func ServerStateRefreshFunc(csp gophercloud.CloudServersProvider, s *gophercloud.Server) StateRefreshFunc {
	return func() (interface{}, string, int, error) {
		resp, err := csp.ServerById(s.Id)
		if err != nil {
			log.Printf("Error on ServerStateRefresh: %s", err)
			return nil, "", 0, err
		}

		return resp, resp.Status, resp.Progress, nil
	}
}
func WaitForServerState(api gophercloud.CloudServersProvider, id string) resource.StateRefreshFunc {

	return func() (interface{}, string, error) {
		s, err := api.ServerById(id)
		if err != nil {
			return nil, "", err
		}

		return nil, s.Status, nil

	}
}
// waitForServerState polls, every 10 seconds, for a given server to appear in the indicated state.
// This call will block forever if it never appears in the desired state, so if a timeout is required,
// make sure to call this function in a goroutine.
func waitForServerState(api gophercloud.CloudServersProvider, id, state string) error {
	for {
		s, err := api.ServerById(id)
		if err != nil {
			return err
		}
		if s.Status == state {
			return nil
		}
		time.Sleep(10 * time.Second)
	}
	panic("Impossible")
}
Exemple #6
0
// ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch
// an openstack server.
func ServerStateRefreshFunc(csp gophercloud.CloudServersProvider, s *gophercloud.Server) StateRefreshFunc {
	return func() (interface{}, string, int, error) {
		resp, err := csp.ServerById(s.Id)
		if err != nil {
			urce, ok := err.(*perigee.UnexpectedResponseCodeError)
			if ok && (urce.Actual == 404) {
				log.Printf("404 on ServerStateRefresh, returning DELETED")

				return nil, "DELETED", 0, nil
			} else {
				log.Printf("Error on ServerStateRefresh: %s", err)
				return nil, "", 0, err
			}
		}
		return resp, resp.Status, resp.Progress, nil
	}
}
Exemple #7
0
// SSHAddress returns a function that can be given to the SSH communicator
// for determining the SSH address based on the server AccessIPv4 setting..
func SSHAddress(csp gophercloud.CloudServersProvider, port int) func(multistep.StateBag) (string, error) {
	return func(state multistep.StateBag) (string, error) {
		for j := 0; j < 2; j++ {
			s := state.Get("server").(*gophercloud.Server)
			if s.AccessIPv4 != "" {
				return fmt.Sprintf("%s:%d", s.AccessIPv4, port), nil
			}
			serverState, err := csp.ServerById(s.Id)

			if err != nil {
				return "", err
			}

			state.Put("server", serverState)
			time.Sleep(1 * time.Second)
		}

		return "", errors.New("couldn't determine IP address for server")
	}
}
func tryAllAddresses(id string, api gophercloud.CloudServersProvider) {
	log("Getting the server instance")
	s, err := api.ServerById(id)
	if err != nil {
		panic(err)
	}

	log("Getting the complete set of pools")
	ps, err := s.AllAddressPools()
	if err != nil {
		panic(err)
	}

	log("Listing IPs for each pool")
	for k, v := range ps {
		log(fmt.Sprintf("  Pool %s", k))
		for _, a := range v {
			log(fmt.Sprintf("    IP: %s, Version: %d", a.Addr, a.Version))
		}
	}
}