Exemple #1
0
// deleteRunningServer terminates the server and waits until it is removed.
func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
	err := scaleway.PostServerAction(server.Identifier, "terminate")

	if err != nil {
		if serr, ok := err.(api.ScalewayAPIError); ok {
			if serr.StatusCode == 404 {
				return nil
			}
		}

		return err
	}

	return resource.Retry(20*time.Minute, func() *resource.RetryError {
		_, err := scaleway.GetServer(server.Identifier)

		if err == nil {
			return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier))
		}

		if serr, ok := err.(api.ScalewayAPIError); ok {
			if serr.StatusCode == 404 {
				return nil
			}
		}

		return resource.RetryableError(err)
	})
}
Exemple #2
0
func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
	pending := []string{}
	for _, state := range allStates {
		if state != targetState {
			pending = append(pending, state)
		}
	}
	stateConf := &resource.StateChangeConf{
		Pending: pending,
		Target:  []string{targetState},
		Refresh: func() (interface{}, string, error) {
			s, err := scaleway.GetServer(serverID)

			if err == nil {
				return 42, s.State, nil
			}

			if serr, ok := err.(api.ScalewayAPIError); ok {
				if serr.StatusCode == 404 {
					return 42, "stopped", nil
				}
			}

			return 42, s.State, err
		},
		Timeout:    60 * time.Minute,
		MinTimeout: 5 * time.Second,
		Delay:      5 * time.Second,
	}
	_, err := stateConf.WaitForState()
	return err
}
Exemple #3
0
func deleteServerSafe(s *api.ScalewayAPI, serverID string) error {
	server, err := s.GetServer(serverID)
	if err != nil {
		return err
	}

	if server.State != "stopped" {
		if err := s.PostServerAction(serverID, "poweroff"); err != nil {
			return err
		}
		if err := waitForServerState(s, serverID, "stopped"); err != nil {
			return err
		}
	}

	if err := s.DeleteServer(serverID); err != nil {
		return err
	}
	if rootVolume, ok := server.Volumes["0"]; ok {
		if err := s.DeleteVolume(rootVolume.Identifier); err != nil {
			return err
		}
	}

	return nil
}
Exemple #4
0
func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
	return resource.Retry(20*time.Minute, func() *resource.RetryError {
		s, err := scaleway.GetServer(serverID)

		if err != nil {
			return resource.NonRetryableError(err)
		}

		if s.State != targetState {
			return resource.RetryableError(fmt.Errorf("Waiting for server to enter %q state", targetState))
		}

		return nil
	})
}
Exemple #5
0
func waitForServerState(s *api.ScalewayAPI, serverID string, targetState string) error {
	var server *api.ScalewayServer
	var err error

	var currentState string

	for {
		server, err = s.GetServer(serverID)
		if err != nil {
			return err
		}
		if currentState != server.State {
			log.Printf("[DEBUG] Server changed state to %q\n", server.State)
			currentState = server.State
		}
		if server.State == targetState {
			break
		}
		time.Sleep(1 * time.Second)
	}

	return nil
}