Beispiel #1
0
func (command *commandRebuild) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsRebuild)
	opts := params.opts
	serverID := params.serverID
	server, err := servers.Rebuild(command.Ctx.ServiceClient, serverID, opts).Extract()
	if err != nil {
		resource.Err = err
		return
	}

	if params.wait {
		err = osServers.WaitForStatus(command.Ctx.ServiceClient, serverID, "ACTIVE", 600)
		if err != nil {
			resource.Err = err
			return
		}

		server, err = servers.Get(command.Ctx.ServiceClient, serverID).Extract()
		if err != nil {
			resource.Err = err
			return
		}
	}

	resource.Result = serverSingle(server)
}
Beispiel #2
0
func (command *commandResize) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsResize)
	err := servers.Resize(command.Ctx.ServiceClient, params.serverID, params.opts).ExtractErr()
	if err != nil {
		resource.Err = err
		return
	}

	if params.wait {
		err = osServers.WaitForStatus(command.Ctx.ServiceClient, params.serverID, "VERIFY_RESIZE", 600)
		if err != nil {
			resource.Err = err
			return
		}

		resource.Result = fmt.Sprintf("Instance [%s] awaiting confirmation of to be resized to flavor [%s]\n", params.serverID, params.opts.FlavorRef)
	} else {
		resource.Result = fmt.Sprintf("Transitioning instance [%s] to a status of VERIFY_RESIZE\n", params.serverID)
	}
}
Beispiel #3
0
func (command *commandReboot) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsReboot)
	serverID := params.serverID
	err := servers.Reboot(command.Context().ServiceClient, serverID, params.how).ExtractErr()
	if err != nil {
		resource.Err = err
		return
	}

	if params.wait {
		err = osServers.WaitForStatus(command.Ctx.ServiceClient, serverID, "ACTIVE", 600)
		if err != nil {
			resource.Err = err
			return
		}

		resource.Result = fmt.Sprintf("Rebooted instance [%s]\n", serverID)
	} else {
		resource.Result = fmt.Sprintf("Rebooting instance [%s]\n", serverID)
	}
}
Beispiel #4
0
func (command *commandCreate) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsCreate).opts

	var server *osServers.Server
	var err error
	if len(opts.BlockDevice) > 0 {
		server, err = bfv.Create(command.Ctx.ServiceClient, opts).Extract()
	} else {
		server, err = servers.Create(command.Ctx.ServiceClient, opts).Extract()
	}

handleErr:
	if err != nil {
		switch err.(type) {
		case *osServers.ErrNeitherImageIDNorImageNameProvided:
			err = errors.New("One and only one of the --image-id and the --image-name flags must be provided.")
		case *osServers.ErrNeitherFlavorIDNorFlavorNameProvided:
			err = errors.New("One and only one of the --flavor-id and the --flavor-name flags must be provided.")
		case *gophercloud.ErrErrorAfterReauthentication:
			err = err.(*gophercloud.ErrErrorAfterReauthentication).UnexpectedResponseCodeError
			goto handleErr
		case *gophercloud.UnexpectedResponseCodeError:
			switch err.(*gophercloud.UnexpectedResponseCodeError).Actual {
			case 403:
				imageID := opts.ImageRef
				if imageID == "" {
					id, err := osImages.IDFromName(command.Ctx.ServiceClient, opts.ImageName)
					if err != nil {
						resource.Err = err
						return
					}
					imageID = id
				}
				flavorLabel := "id"
				flavorID := opts.FlavorRef
				if flavorID == "" {
					flavorLabel = "name"
					flavorID = opts.FlavorName
				}
				err = fmt.Errorf(strings.Join([]string{"The flavor you've chosen has a disk size of 0, so an image can't be created on it directly.\n",
					"To boot with this flavor, creating a 100 GB volume and not deleting that volume when the server is deleted, run this command:\n",
					fmt.Sprintf("rack servers instance create --name %s --flavor-%s %s \\", opts.Name, flavorLabel, flavorID),
					fmt.Sprintf("--block-device \"source-type=image,source-id=%s,volume-size=100,destination-type=volume,delete-on-termination=false\"\n", imageID),
					"For more information please run: rack servers instance create --help",
				}, "\n"))
			}
		}
		resource.Err = err
		return
	}

	if resource.Params.(*paramsCreate).wait {
		err = osServers.WaitForStatus(command.Ctx.ServiceClient, server.ID, "ACTIVE", 600)
		if err != nil {
			resource.Err = err
			return
		}

		server, err = servers.Get(command.Ctx.ServiceClient, server.ID).Extract()
		if err != nil {
			resource.Err = err
			return
		}
	}

	resource.Result = serverSingle(server)
}
Beispiel #5
0
// WaitForStatus will continually poll a server until it successfully transitions to a specified
// status. It will do this for at most the number of seconds specified.
func WaitForStatus(c *gophercloud.ServiceClient, id, status string, secs int) error {
	return os.WaitForStatus(c, id, status, secs)
}