示例#1
0
文件: run.go 项目: pkgr/scaleway-cli
func runRun(cmd *types.Command, args []string) {
	if runHelpFlag {
		cmd.PrintUsage()
	}
	if len(args) < 1 {
		cmd.PrintShortUsage()
	}
	if runAttachFlag && len(args) > 1 {
		log.Fatalf("Cannot use '--attach' and 'COMMAND [ARG...]' at the same time. See 'scw run --help'")
	}

	// create IMAGE
	log.Debugf("Creating a new server")
	serverID, err := api.CreateServer(cmd.API, args[0], runCreateName, runCreateBootscript, runCreateEnv, runCreateVolume)
	if err != nil {
		log.Fatalf("Failed to create server: %v", err)
	}
	log.Debugf("Created server: %s", serverID)

	// start SERVER
	log.Debugf("Starting server")
	err = api.StartServer(cmd.API, serverID, false)
	if err != nil {
		log.Fatalf("Failed to start server %s: %v", serverID, err)
	}
	log.Debugf("Server is booting")

	if runAttachFlag {
		// Attach to server serial
		log.Debugf("Attaching to server console")
		err = utils.AttachToSerial(serverID, cmd.API.Token, true)
		if err != nil {
			log.Fatalf("Cannot attach to server serial: %v", err)
		}
	} else {
		// waiting for server to be ready
		log.Debugf("Waiting for server to be ready")
		// We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
		time.Sleep(30 * time.Second)
		server, err := api.WaitForServerReady(cmd.API, serverID)
		if err != nil {
			log.Fatalf("Cannot get access to server %s: %v", serverID, err)
		}
		log.Debugf("Server is ready: %s", server.PublicAddress.IP)

		// exec -w SERVER COMMAND ARGS...
		log.Debugf("Executing command")
		if len(args) < 2 {
			err = utils.SSHExec(server.PublicAddress.IP, []string{"if [ -x /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi"}, false)
		} else {
			err = utils.SSHExec(server.PublicAddress.IP, args[1:], false)
		}
		if err != nil {
			log.Debugf("Command execution failed: %v", err)
			os.Exit(1)
		}
		log.Debugf("Command successfuly executed")
	}
}
示例#2
0
文件: exec.go 项目: pkgr/scaleway-cli
func runExec(cmd *types.Command, args []string) {
	if execHelp {
		cmd.PrintUsage()
	}
	if len(args) < 2 {
		cmd.PrintShortUsage()
	}

	serverID := cmd.API.GetServerID(args[0])

	var server *api.ScalewayServer
	var err error
	if execW {
		// --wait
		server, err = api.WaitForServerReady(cmd.API, serverID)
		if err != nil {
			log.Fatalf("Failed to wait for server to be ready, %v", err)
		}
	} else {
		// no --wait
		server, err = cmd.API.GetServer(serverID)
		if err != nil {
			log.Fatalf("Failed to get server information for %s: %v", serverID, err)
		}
	}

	if execTimeout > 0 {
		go func() {
			time.Sleep(time.Duration(execTimeout*1000) * time.Millisecond)
			log.Fatalf("Operation timed out")
		}()
	}

	err = utils.SSHExec(server.PublicAddress.IP, args[1:], !execW)
	if err != nil {
		log.Fatalf("%v", err)
		os.Exit(1)
	}
	log.Debugf("Command successfuly executed")
}