Ejemplo n.º 1
0
func runShowBoot(ctx CommandContext, args RunArgs, serverID string, closeTimeout chan struct{}, timeoutExit chan struct{}) error {
	// Attach to server serial
	logrus.Info("Attaching to server console ...")
	gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
	if err != nil {
		close(closeTimeout)
		return fmt.Errorf("cannot attach to server serial: %v", err)
	}
	utils.Quiet(true)
	notif, gateway, err := waitSSHConnection(ctx, args, serverID)
	if err != nil {
		close(closeTimeout)
		gottycli.ExitLoop()
		<-done
		return err
	}
	select {
	case <-timeoutExit:
		gottycli.ExitLoop()
		<-done
		utils.Quiet(false)
		return fmt.Errorf("Operation timed out")
	case sshConnection := <-notif:
		close(closeTimeout)
		gottycli.ExitLoop()
		<-done
		utils.Quiet(false)
		if sshConnection.err != nil {
			return sshConnection.err
		}
		if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 {
			for i := range fingerprints {
				fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
			}
		}
		server := sshConnection.server
		logrus.Info("Connecting to server ...")
		if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil {
			return fmt.Errorf("Connection to server failed: %v", err)
		}
	}
	return nil
}
Ejemplo n.º 2
0
// Start is the entrypoint
func Start(rawArgs []string, streams *commands.Streams) (int, error) {
	checkVersion()
	if streams == nil {
		streams = &commands.Streams{
			Stdin:  os.Stdin,
			Stdout: os.Stdout,
			Stderr: os.Stderr,
		}
	}
	flag.CommandLine.Parse(rawArgs)

	config, cfgErr := config.GetConfig()
	if cfgErr != nil && !os.IsNotExist(cfgErr) {
		return 1, fmt.Errorf("unable to open .scwrc config file: %v", cfgErr)
	}

	if *flVersion {
		fmt.Fprintf(streams.Stderr, "scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT)
		return 0, nil
	}

	if *flSensitive {
		os.Setenv("SCW_SENSITIVE", "1")
	}

	if *flDebug {
		os.Setenv("DEBUG", "1")
	}

	utils.Quiet(*flQuiet)
	initLogging(os.Getenv("DEBUG") != "", *flVerbose, streams)

	args := flag.Args()
	if len(args) < 1 {
		CmdHelp.Exec(CmdHelp, []string{})
		return 1, nil
	}
	name := args[0]

	args = args[1:]

	// Apply default values
	for _, cmd := range Commands {
		cmd.streams = streams
	}

	for _, cmd := range Commands {
		if cmd.Name() == name {
			cmd.Flag.SetOutput(ioutil.Discard)
			err := cmd.Flag.Parse(args)
			if err != nil {
				return 1, fmt.Errorf("usage: scw %s", cmd.UsageLine)
			}
			switch cmd.Name() {
			case "login", "help", "version":
				// commands that don't need API
			case "_userdata":
				// commands that may need API
				api, _ := getScalewayAPI()
				cmd.API = api
			default:
				// commands that do need API
				if cfgErr != nil {
					if name != "login" && config == nil {
						logrus.Debugf("cfgErr: %v", cfgErr)
						fmt.Fprintf(streams.Stderr, "You need to login first: 'scw login'\n")
						return 1, nil
					}
				}
				api, errGet := getScalewayAPI()
				if errGet != nil {
					return 1, fmt.Errorf("unable to initialize scw api: %v", errGet)
				}
				cmd.API = api
			}
			// clean cache between versions
			if cmd.API != nil && config.Version != scwversion.VERSION {
				cmd.API.ClearCache()
				config.Save()
			}
			err = cmd.Exec(cmd, cmd.Flag.Args())
			switch err {
			case nil:
			case ErrExitFailure:
				return 1, nil
			case ErrExitSuccess:
				return 0, nil
			default:
				return 1, fmt.Errorf("cannot execute '%s': %v", cmd.Name(), err)
			}
			if cmd.API != nil {
				cmd.API.Sync()
			}
			return 0, nil
		}
	}
	return 1, fmt.Errorf("scw: unknown subcommand %s\nRun 'scw help' for usage", name)
}
Ejemplo n.º 3
0
func Start(rawArgs []string, streams *commands.Streams) (int, error) {
	if streams == nil {
		streams = &commands.Streams{
			Stdin:  os.Stdin,
			Stdout: os.Stdout,
			Stderr: os.Stderr,
		}
	}

	flag.CommandLine.Parse(rawArgs)

	config, cfgErr := config.GetConfig()
	if cfgErr != nil && !os.IsNotExist(cfgErr) {
		return 1, fmt.Errorf("unable to open .scwrc config file: %v", cfgErr)
	}

	if config != nil {
		defaultComputeAPI := os.Getenv("scaleway_api_endpoint")
		if defaultComputeAPI == "" {
			defaultComputeAPI = config.ComputeAPI
		}
		if flAPIEndPoint == nil {
			flAPIEndPoint = flag.String([]string{"-api-endpoint"}, defaultComputeAPI, "Set the API endpoint")
		}
	}

	if *flVersion {
		fmt.Fprintf(streams.Stderr, "scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT)
		return 0, nil
	}

	if flAPIEndPoint != nil {
		os.Setenv("scaleway_api_endpoint", *flAPIEndPoint)
	}

	if *flSensitive {
		os.Setenv("SCW_SENSITIVE", "1")
	}

	if *flDebug {
		os.Setenv("DEBUG", "1")
	}

	utils.Quiet(*flQuiet)
	initLogging(os.Getenv("DEBUG") != "", *flVerbose, streams)

	args := flag.Args()
	if len(args) < 1 {
		CmdHelp.Exec(CmdHelp, []string{})
		return 1, nil
	}
	name := args[0]

	args = args[1:]

	// Apply default values
	for _, cmd := range Commands {
		cmd.streams = streams
	}

	for _, cmd := range Commands {
		if cmd.Name() == name {
			cmd.Flag.SetOutput(ioutil.Discard)
			err := cmd.Flag.Parse(args)
			if err != nil {
				return 1, fmt.Errorf("usage: scw %s", cmd.UsageLine)
			}
			if cmd.Name() != "login" && cmd.Name() != "help" && cmd.Name() != "version" {
				if cfgErr != nil {
					if name != "login" && config == nil {
						logrus.Debugf("cfgErr: %v", cfgErr)
						fmt.Fprintf(streams.Stderr, "You need to login first: 'scw login'\n")
						return 1, nil
					}
				}
				api, err := getScalewayAPI()
				if err != nil {
					return 1, fmt.Errorf("unable to initialize scw api: %s", err)
				}
				cmd.API = api
			}
			err = cmd.Exec(cmd, cmd.Flag.Args())
			switch err {
			case nil:
			case ErrExitFailure:
				return 1, nil
			case ErrExitSuccess:
				return 0, nil
			default:
				return 1, fmt.Errorf("cannot execute '%s': %v", cmd.Name(), err)
			}
			if cmd.API != nil {
				cmd.API.Sync()
			}
			return 0, nil
		}
	}

	return 1, fmt.Errorf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name)
}