Exemplo n.º 1
0
// RunWait is the handler for 'scw wait'
func RunWait(ctx CommandContext, args WaitArgs) error {
	hasError := false
	for _, needle := range args.Servers {
		serverIdentifier := ctx.API.GetServerID(needle)

		_, err := api.WaitForServerStopped(ctx.API, serverIdentifier)
		if err != nil {
			logrus.Errorf("failed to wait for server %s: %v", serverIdentifier, err)
			hasError = true
		}
	}

	if hasError {
		return fmt.Errorf("at least 1 server failed to be stopped")
	}
	return nil
}
Exemplo n.º 2
0
func (vp *scalewayProvider) deleteServer(s api.ScalewayServer, dnsProvider providers.DnsProvider, domain string) error {
	if s.State == "running" {
		vp.Logger.Infof("Stopping server %s", s.Name)
		if err := vp.client.PostServerAction(s.Identifier, "terminate"); err != nil {
			return maskAny(err)
		}
		api.WaitForServerStopped(vp.client, s.Identifier)
	} else {
		vp.Logger.Infof("Server %s is at state '%s'", s.Name, s.State)
	}

	// Delete DNS instance records
	vp.Logger.Infof("Unregistering DNS for %s", s.Name)
	instance := vp.clusterInstance(s, false)
	if err := providers.UnRegisterInstance(vp.Logger, dnsProvider, instance, domain); err != nil {
		return maskAny(err)
	}

	// Delete server
	/*err := vp.client.DeleteServer(s.Identifier)
	if err != nil {
		vp.Logger.Errorf("Failed to delete server %s: %#v", s.Name, err)
		return maskAny(err)
	}*/

	// Delete volume
	/*for _, v := range s.Volumes {
		if err := vp.client.DeleteVolume(v.Identifier); err != nil {
		vp.Logger.Errorf("Failed to delete volume %s: %#v", v.Identifier, err)
			return maskAny(err)
		}
	}*/

	// Delete IP
	if (s.PublicAddress.Dynamic != nil) && !(*s.PublicAddress.Dynamic) {
		if err := vp.client.DeleteIP(s.PublicAddress.Identifier); err != nil {
			vp.Logger.Errorf("Failed to delete IP %s: %#v", s.PublicAddress.Identifier, err)
			return maskAny(err)
		}
	}

	return nil
}
Exemplo n.º 3
0
// RunStop is the handler for 'scw stop'
func RunStop(ctx CommandContext, args StopArgs) error {
	// FIXME: parallelize stop when stopping multiple servers
	hasError := false
	for _, needle := range args.Servers {
		serverID, err := ctx.API.GetServerID(needle)
		if err != nil {
			return err
		}
		action := "poweroff"
		if args.Terminate {
			action = "terminate"
		}
		if err = ctx.API.PostServerAction(serverID, action); err != nil {
			if err.Error() != "server should be running" && err.Error() != "server is being stopped or rebooted" {
				logrus.Warningf("failed to stop server %s: %s", serverID, err)
				hasError = true
			}
		} else {
			if args.Wait {
				// We wait for 10 seconds which is the minimal amount of time needed for a server to stop
				time.Sleep(10 * time.Second)
				if _, err = api.WaitForServerStopped(ctx.API, serverID); err != nil {
					logrus.Errorf("failed to wait for server %s: %v", serverID, err)
					hasError = true
				}
			}
			if args.Terminate {
				ctx.API.Cache.RemoveServer(serverID)
			}
			fmt.Fprintln(ctx.Stdout, needle)
		}
	}

	if hasError {
		return fmt.Errorf("at least 1 server failed to be stopped")
	}
	return nil
}