Exemple #1
0
func chooseAndGenSshArgs(rOpt *RnsshOption, cmdArgs []string, manager *cstore.Manager) ([]string, error) {

	// support user@host format
	sshUser, hostname, err := getSshUserAndHostname(strings.Join(cmdArgs, " "))
	if err != nil {
		return nil, fmt.Errorf("%s\n", err.Error())
	}

	hostType := HOST_TYPE_PUBLIC_IP
	if rOpt.HostType != "" {
		hostType = rOpt.HostType
	}

	var choosableList []peco.Choosable
	if rOpt.UseSshConfig {
		var err error
		choosableList, err = LoadSshConfigChoosableList()
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}

		if len(choosableList) == 0 {
			fmt.Println("ssh config does not have host settings")
			os.Exit(1)
		}
	} else {
		var err error
		handler := NewEC2Handler(manager)
		choosableList, err = handler.LoadTargetHost(hostType, rOpt.Region, rOpt.Reload)
		if err != nil {
			fmt.Printf("%s\n", err.Error())
			os.Exit(1)
		}

		if len(choosableList) == 0 {
			fmt.Printf("there is no instance. not running %s\n", rOpt.Region)
			os.Exit(1)
		}
	}

	// show ec2 instances and choose intactive
	targetHosts, err := peco.Choose("server", "which servers connect with ssh?", hostname, choosableList)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		os.Exit(1)
	}

	l := len(targetHosts) - 1
	targetHost := targetHosts[l]
	sshHost := targetHost.Value()
	sshArgs := genSshArgs(rOpt.SshUser, rOpt.IdentityFile, rOpt.Port, rOpt.StrictHostKeyCheckingNo, sshUser, sshHost)

	return sshArgs, nil
}
Exemple #2
0
func Ec2ConfigWizard() (string, string, error) {
	chosenRegion, err := peco.Choose("AWS region", "Please select default AWS region", "", AWSRegionList)
	if err != nil {
		return "", "", fmt.Errorf("region choose error:%s", err.Error())
	}

	region := ""
	for _, c := range chosenRegion {
		region = c.Value()
	}

	chosenHostType, err := peco.Choose("rnssh host type", "Please select default host type", "", HostTypeList)
	if err != nil {
		return "", "", fmt.Errorf("host type choose error:%s", err.Error())
	}

	hostType := ""
	for _, c := range chosenHostType {
		hostType = c.Value()
	}

	return region, hostType, nil
}
Exemple #3
0
func StrictHostKeyCheckingWizard() (int, error) {
	chosenStrict, err := peco.Choose("rnssh StrictHostKeyChecking option", "Please select about StrictHostKeyChecking (recommend to Not specify)", "", StrictHostKeyCheckingList)
	if err != nil {
		return -1, fmt.Errorf("StrictHostKeyChecking choose error:%s", err.Error())
	}

	var strict int
	for _, c := range chosenStrict {
		strict, err = strconv.Atoi(c.Value())
		if err != nil {
			// error then disabled
			strict = 0
		}
	}

	return strict, nil
}
Exemple #4
0
func (h *EC2Handler) ChooseEC2(region, state string, reload bool) ([]*string, error) {
	ec2list, err := h.LoadChoosableEC2List(region, state, reload)
	if err != nil {
		return nil, err
	}

	choices := ConvertChoosableList(ec2list)

	chosens, err := peco.Choose("EC2", "select instances", "", choices)
	if err != nil {
		return nil, err
	}

	ids := make([]*string, 0, len(chosens))
	for _, c := range chosens {
		if ec2, ok := c.(*ChoosableEC2); ok {
			ids = append(ids, aws.String(ec2.InstanceId))
		}
	}

	return ids, nil
}
Exemple #5
0
func ChooseEIP(region string) ([]*ChoosableEIP, error) {
	EIPs, err := LoadEIPList(region)
	if err != nil {
		return nil, err
	}

	choices := ConvertChoosableEIPList(EIPs)

	chosens, err := peco.Choose("EIP", "select EIP", "", choices)
	if err != nil {
		return nil, err
	}

	ids := make([]*ChoosableEIP, 0, len(chosens))
	for _, c := range chosens {
		if eip, ok := c.(*ChoosableEIP); ok {
			ids = append(ids, eip)
		}
	}

	return ids, nil
}
Exemple #6
0
func DoConfigWizard(cs *cstore.CStore) error {
	chosenRegion, err := peco.Choose("AWS region", "Please select default AWS region", "", AWSRegionList)
	if err != nil {
		return fmt.Errorf("region choose error:%s", err.Error())
	}

	region := ""
	for _, c := range chosenRegion {
		region = c.Value()
		break
	}

	c := &Config{
		Default: RnzooConfig{
			AWSRegion: region,
		},
	}

	if err := cs.Save(c); err != nil {
		return err
	}

	return nil
}
Exemple #7
0
func DoConfigWizard(cs *cstore.CStore) error {

	chosenResourceType, err := peco.Choose("rnssh ResourceType option", "Please select resource type", "", ResourceTypeList)
	if err != nil {
		return fmt.Errorf("ResourceType choose error:%s", err.Error())
	}

	resourceType := "ec2"
	// later win
	for _, c := range chosenResourceType {
		resourceType = c.Value()
	}

	var c *Config
	var region, hostType string
	var useSshConfig bool
	var strictHostKeyChecking int
	switch resourceType {
	case "ec2":
		region, hostType, err = Ec2ConfigWizard()
		if err != nil {
			return err
		}

		strictHostKeyChecking, err = StrictHostKeyCheckingWizard()
		if err != nil {
			return err
		}

		useSshConfig = false

	case "ssh_config":
		strictHostKeyChecking, err = StrictHostKeyCheckingWizard()
		if err != nil {
			return err
		}

		chosenContinue, err := peco.Choose("next setting", "next, continue to AWS settings?", "", ContinueList)
		if err != nil {
			return err
		}

		chosen := ""
		for _, c := range chosenContinue {
			chosen = c.Value()
		}

		if chosen == "yes" {
			region, hostType, err = Ec2ConfigWizard()
			if err != nil {
				return err
			}
		}

		useSshConfig = true
	}

	c = &Config{
		Default: RnsshConfig{
			AWSRegion:                  region,
			HostType:                   hostType,
			UseSshConfig:               useSshConfig,
			SshStrictHostKeyCheckingNo: strictHostKeyChecking,
		},
	}

	if err := cs.Save(c); err != nil {
		return err
	}

	return nil
}