예제 #1
0
파일: create.go 프로젝트: moul/scaleway-cli
// RunCreate is the handler for 'scw create'
func RunCreate(ctx CommandContext, args CreateArgs) error {
	if args.TmpSSHKey {
		err := AddSSHKeyToTags(ctx, &args.Tags, args.Image)
		if err != nil {
			return err
		}
	}

	env := strings.Join(args.Tags, " ")
	volume := strings.Join(args.Volumes, " ")
	config := api.ConfigCreateServer{
		ImageName:         args.Image,
		Name:              args.Name,
		Bootscript:        args.Bootscript,
		Env:               env,
		AdditionalVolumes: volume,
		DynamicIPRequired: false,
		IP:                args.IP,
		CommercialType:    args.CommercialType,
	}
	if args.IP == "dynamic" || args.IP == "" {
		config.DynamicIPRequired = true
		config.IP = ""
	} else if args.IP == "none" || args.IP == "no" {
		config.IP = ""
	}
	serverID, err := api.CreateServer(ctx.API, &config)
	if err != nil {
		return err
	}

	fmt.Fprintln(ctx.Stdout, serverID)
	return nil
}
예제 #2
0
파일: run.go 프로젝트: moul/scaleway-cli
// Run is the handler for 'scw run'
func Run(ctx CommandContext, args RunArgs) error {
	if args.Gateway == "" {
		args.Gateway = ctx.Getenv("SCW_GATEWAY")
	}

	if args.TmpSSHKey {
		err := AddSSHKeyToTags(ctx, &args.Tags, args.Image)
		if err != nil {
			return err
		}
	}
	env := strings.Join(args.Tags, " ")
	volume := strings.Join(args.Volumes, " ")

	// create IMAGE
	logrus.Info("Server creation ...")
	config := api.ConfigCreateServer{
		ImageName:         args.Image,
		Name:              args.Name,
		Bootscript:        args.Bootscript,
		Env:               env,
		AdditionalVolumes: volume,
		DynamicIPRequired: false,
		IP:                args.IP,
		CommercialType:    args.CommercialType,
	}
	if args.IP == "dynamic" || (args.IP == "" && args.Gateway == "") {
		config.DynamicIPRequired = true
		config.IP = ""
	} else if args.IP == "none" || args.IP == "no" || (args.IP == "" && args.Gateway != "") {
		config.IP = ""
	}
	serverID, err := api.CreateServer(ctx.API, &config)
	if err != nil {
		return fmt.Errorf("failed to create server: %v", err)
	}
	logrus.Infof("Server created: %s", serverID)

	if args.AutoRemove {
		defer ctx.API.DeleteServerSafe(serverID)
	}

	// start SERVER
	logrus.Info("Server start requested ...")
	if err = api.StartServer(ctx.API, serverID, false); err != nil {
		return fmt.Errorf("failed to start server %s: %v", serverID, err)
	}
	logrus.Info("Server is starting, this may take up to a minute ...")

	if args.Userdata != "" {
		addUserData(ctx, strings.Split(args.Userdata, " "), serverID)
	}
	// Sync cache on disk
	ctx.API.Sync()

	if args.Detach {
		fmt.Fprintln(ctx.Stdout, serverID)
		return nil
	}

	closeTimeout := make(chan struct{})
	timeoutExit := make(chan struct{})

	if args.Timeout > 0 {
		go func() {
			select {
			case <-time.After(time.Duration(args.Timeout) * time.Second):
				close(timeoutExit)
			case <-closeTimeout:
				break
			}
		}()
	}
	if args.ShowBoot {
		return runShowBoot(ctx, args, serverID, closeTimeout, timeoutExit)
	} else if args.Attach {
		// Attach to server serial
		logrus.Info("Attaching to server console ...")
		gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
		close(closeTimeout)
		if err != nil {
			return fmt.Errorf("cannot attach to server serial: %v", err)
		}
		<-done
		gottycli.Close()
	} else {
		notif, gateway, err := waitSSHConnection(ctx, args, serverID)
		if err != nil {
			close(closeTimeout)
			return err
		}
		select {
		case <-timeoutExit:
			return fmt.Errorf("Operation timed out")
		case sshConnection := <-notif:
			close(closeTimeout)
			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
			// exec -w SERVER COMMAND ARGS...
			if len(args.Command) < 1 {
				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)
				}
			} else {
				logrus.Infof("Executing command: %s ...", args.Command)
				if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, false, gateway); err != nil {
					return fmt.Errorf("command execution failed: %v", err)
				}
				logrus.Info("Command successfuly executed")
			}
		}
	}
	return nil
}