예제 #1
0
// createServer creates a new server in a manner compatible with acceptance testing.
// In particular, it ensures that the name of the server always starts with "ACPTTEST--",
// which the delete servers acceptance test relies on to identify servers to delete.
// Passing in empty image and flavor references will force the use of reasonable defaults.
// An empty name string will result in a dynamically created name prefixed with "ACPTTEST--".
// A blank admin password will cause a password to be automatically generated; however,
// at present no means of recovering this password exists, as no acceptance tests yet require
// this data.
func createServer(servers gophercloud.CloudServersProvider, imageRef, flavorRef, name, adminPass string) (string, error) {
	if imageRef == "" {
		imageRef = aSuitableImage(servers)
	}

	if flavorRef == "" {
		flavorRef = aSuitableFlavor(servers)
	}

	if len(name) < 1 {
		name = randomString("ACPTTEST", 16)
	}

	if (len(name) < 8) || (name[0:8] != "ACPTTEST") {
		name = fmt.Sprintf("ACPTTEST--%s", name)
	}

	newServer, err := servers.CreateServer(gophercloud.NewServer{
		Name:      name,
		ImageRef:  imageRef,
		FlavorRef: flavorRef,
		AdminPass: adminPass,
	})

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

	return newServer.Id, nil
}
예제 #2
0
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)
	}
}
예제 #3
0
파일: ssh.go 프로젝트: JNPRAutomate/packer
// 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 tryLinksOnly(api gophercloud.CloudServersProvider) {
	servers, err := api.ListServersLinksOnly()
	if err != nil {
		panic(err)
	}

	if !*quiet {
		fmt.Println("Id,Name")
		for _, s := range servers {
			if s.AccessIPv4 != "" {
				panic("IPv4 not expected")
			}

			if s.Status != "" {
				panic("Status not expected")
			}

			if s.Progress != 0 {
				panic("Progress not expected")
			}

			fmt.Printf("%s,\"%s\"\n", s.Id, s.Name)
		}
	}
}
예제 #5
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

	}
}
예제 #7
0
// waitForImageState polls, every 10 seconds, for a given image 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 waitForImageState(api gophercloud.CloudServersProvider, id, state string) error {
	for {
		s, err := api.ImageById(id)
		if err != nil {
			return err
		}
		if s.Status == state {
			return nil
		}
		time.Sleep(10 * time.Second)
	}
	panic("Impossible")
}
예제 #8
0
func tryAddressesByNetwork(networkLabel string, id string, api gophercloud.CloudServersProvider) {
	log("Getting list of addresses on", networkLabel, "network...")
	network, err := api.ListAddressesByNetwork(id, networkLabel)
	if (err != nil) && (err != gophercloud.WarnUnauthoritative) {
		panic(err)
	}
	if err == gophercloud.WarnUnauthoritative {
		log("Uh oh -- got a response back, but it's not authoritative for some reason.")
	}
	for _, addr := range network[networkLabel] {
		log("Address:", addr.Addr, "  IPv", addr.Version)
	}
}
func tryFullDetails(api gophercloud.CloudServersProvider) {
	servers, err := api.ListServers()
	if err != nil {
		panic(err)
	}

	if !*quiet {
		fmt.Println("Id,Name,AccessIPv4,Status,Progress")
		for _, s := range servers {
			fmt.Printf("%s,\"%s\",%s,%s,%d\n", s.Id, s.Name, s.AccessIPv4, s.Status, s.Progress)
		}
	}
}
예제 #10
0
func tryAllAddresses(id string, api gophercloud.CloudServersProvider) {
	log("Getting list of all addresses...")
	addresses, err := api.ListAddresses(id)
	if (err != nil) && (err != gophercloud.WarnUnauthoritative) {
		panic(err)
	}
	if err == gophercloud.WarnUnauthoritative {
		log("Uh oh -- got a response back, but it's not authoritative for some reason.")
	}
	if !*quiet {
		fmt.Println("Addresses:")
		fmt.Printf("%+v\n", addresses)
	}
}
예제 #11
0
// WaitForImage waits for the given Image ID to become ready.
func WaitForImage(csp gophercloud.CloudServersProvider, imageId string) error {
	for {
		image, err := csp.ImageById(imageId)
		if err != nil {
			return err
		}

		if image.Status == "ACTIVE" {
			return nil
		}

		log.Printf("Waiting for image creation status: %s (%d%%)", image.Status, image.Progress)
		time.Sleep(2 * time.Second)
	}
}
예제 #12
0
// Perform the resize test, but accept the resize request.
func resizeAcceptTest(api gophercloud.CloudServersProvider, done chan bool) {
	withServer(api, func(id string) {
		newFlavorId := findAlternativeFlavor()
		err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "")
		if err != nil {
			panic(err)
		}

		waitForServerState(api, id, "VERIFY_RESIZE")

		err = api.ConfirmResize(id)
		if err != nil {
			panic(err)
		}
	})
	done <- true
}
예제 #13
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
	}
}
예제 #14
0
// aSuitableImage finds a minimal image for use in dynamically creating servers.
// If none can be found, this function will panic.
func aSuitableImage(api gophercloud.CloudServersProvider) string {
	images, err := api.ListImages()
	if err != nil {
		panic(err)
	}

	// TODO(sfalvo):
	// Works for Rackspace, might not work for your provider!
	// Need to figure out why ListImages() provides 0 values for
	// Ram and Disk fields.
	//
	// Until then, just return Ubuntu 12.04 LTS.
	for i := 0; i < len(images); i++ {
		if strings.Contains(images[i].Name, "Ubuntu 12.04 LTS") {
			return images[i].Id
		}
	}
	panic("Image for Ubuntu 12.04 LTS not found.")
}
예제 #15
0
// aSuitableImage finds a minimal image for use in dynamically creating servers.
// If none can be found, this function will panic.
func aSuitableImage(api gophercloud.CloudServersProvider) string {
	images, err := api.ListImages()
	if err != nil {
		panic(err)
	}

	// TODO(sfalvo):
	// Works for Rackspace, might not work for your provider!
	// Need to figure out why ListImages() provides 0 values for
	// Ram and Disk fields.
	//
	// Until then, just return Ubuntu 12.04 LTS.
	for i := 0; i < len(images); i++ {
		if images[i].Id == "23b564c9-c3e6-49f9-bc68-86c7a9ab5018" {
			return images[i].Id
		}
	}
	panic("Image 23b564c9-c3e6-49f9-bc68-86c7a9ab5018 (Ubuntu 12.04 LTS) not found.")
}
예제 #16
0
// aSuitableFlavor finds the minimum flavor capable of running the test image
// chosen by aSuitableImage.  If none can be found, this function will panic.
func aSuitableFlavor(api gophercloud.CloudServersProvider) string {
	flavors, err := api.ListFlavors()
	if err != nil {
		panic(err)
	}

	// TODO(sfalvo):
	// Works for Rackspace, might not work for your provider!
	// Need to figure out why ListFlavors() provides 0 values for
	// Ram and Disk fields.
	//
	// Until then, just return Ubuntu 12.04 LTS.
	for i := 0; i < len(flavors); i++ {
		if flavors[i].Id == "2" {
			return flavors[i].Id
		}
	}
	panic("Flavor 2 (512MB 1-core 20GB machine) not found.")
}
예제 #17
0
파일: ssh.go 프로젝트: prognostikos/packer
// 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")
	}
}
// locateAServer queries the set of servers owned by the user.  If at least one
// exists, the first found is picked, and its ID is returned.  Otherwise, a new
// server will be created, and its ID returned.
//
// deleteAfter will be true if the caller should schedule a call to DeleteServerById()
// to clean up.
func locateAServer(servers gophercloud.CloudServersProvider) (deleteAfter bool, id string, err error) {
	ss, err := servers.ListServers()
	if err != nil {
		return false, "", err
	}

	if len(ss) > 0 {
		// We could just cheat and dump the server details from ss[0].
		// But, that tests ListServers(), and not ServerById().  So, we
		// elect not to cheat.
		return false, ss[0].Id, nil
	}

	serverId, err := createServer(servers, "", "", "", "")
	if err != nil {
		return false, "", err
	}
	err = waitForServerState(servers, serverId, "ACTIVE")
	return true, serverId, err
}
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))
		}
	}
}
예제 #20
0
func getServerByName(api gophercloud.CloudServersProvider, name string) (*gophercloud.Server, error) {
	filter := url.Values{}
	filter.Set("name", fmt.Sprintf("^%s$", regexp.QuoteMeta(name)))
	filter.Set("status", "ACTIVE")

	servers, err := api.ListServersByFilter(filter)
	if err != nil {
		return nil, err
	}

	if len(servers) == 0 {
		return nil, ErrServerNotFound
	} else if len(servers) > 1 {
		return nil, ErrMultipleServersFound
	}

	return &servers[0], nil
}