Esempio n. 1
0
// AddSSHKeyToTags adds the ssh key in the tags
func AddSSHKeyToTags(ctx CommandContext, tags *[]string, image string) error {
	home, err := config.GetHomeDir()
	if err != nil {
		return fmt.Errorf("unable to find your home %v", err)
	}
	idRsa := filepath.Join(home, ".ssh", "id_rsa")
	if _, err := os.Stat(idRsa); err != nil {
		if os.IsNotExist(err) {
			logrus.Warnln("Unable to find your ~/.ssh/id_rsa")
			logrus.Warnln("Run 'ssh-keygen -t rsa'")
			return nil
		}
	}
	idRsa = strings.Join([]string{idRsa, ".pub"}, "")
	data, err := ioutil.ReadFile(idRsa)
	if err != nil {
		return fmt.Errorf("failed to read %v", err)
	}
	data[7] = '_'
	for i := range data {
		if data[i] == ' ' {
			data = data[:i]
			break
		}
	}
	*tags = append(*tags, strings.Join([]string{"AUTHORIZED_KEY", string(data[:len(data)])}, "="))
	return nil
}
Esempio n. 2
0
// selectKey allows to choice a key in ~/.ssh
func selectKey(args *LoginArgs) error {
	home, err := config.GetHomeDir()
	if err != nil {
		return err
	}
	dir := filepath.Join(home, ".ssh")
	files, err := ioutil.ReadDir(dir)
	if err != nil {
		return fmt.Errorf("Unable to open your ~/.ssh: %v", err)
	}
	var pubs []string

	for i := range files {
		if filepath.Ext(files[i].Name()) == ".pub" {
			pubs = append(pubs, files[i].Name())
		}
	}
	if len(pubs) == 0 {
		return nil
	}
	fmt.Println("Do you want to upload an SSH key ?")
	fmt.Println("[0] I don't want to upload a key !")
	for i := range pubs {
		fmt.Printf("[%d] %s\n", i+1, pubs[i])
	}
	for {
		if err := promptUser("Which [id]: ", &args.SSHKey, true); err != nil {
			return err
		}
		id, err := strconv.ParseUint(strings.TrimSpace(args.SSHKey), 10, 32)
		if err != nil {
			fmt.Println(err)
			continue
		}
		if int(id) > len(pubs) {
			fmt.Println("Out of range id must be lower than", len(pubs))
			continue
		}
		args.SSHKey = ""
		if id == 0 {
			break
		}
		buff, err := ioutil.ReadFile(filepath.Join(dir, pubs[id-1]))
		if err != nil {
			return fmt.Errorf("Unable to open your key: %v", err)
		}
		args.SSHKey = string(buff[:])
		break
	}
	return nil
}
Esempio n. 3
0
func checkVersion() {
	if os.Getenv("SCW_NOCHECKVERSION") == "1" {
		return
	}
	homeDir, err := config.GetHomeDir()
	if err != nil {
		return
	}
	updateFiles := []string{"/var/run/.scw-update", "/tmp/.scw-update", filepath.Join(homeDir, ".scw-update")}
	updateFile := ""

	callAPI := false
	for _, file := range updateFiles {
		if stat, err := os.Stat(file); err == nil {
			updateFile = file
			callAPI = stat.ModTime().Before(time.Now().AddDate(0, 0, -1))
			break
		}
	}
	if updateFile == "" {
		for _, file := range updateFiles {
			if scwupdate, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600); err == nil {
				scwupdate.Close()
				updateFile = file
				callAPI = true
				break
			}
		}
	}
	if callAPI {
		scwupdate, err := os.OpenFile(updateFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
		if err != nil {
			return
		}
		scwupdate.Close()
		req := http.Client{
			Timeout: 1 * time.Second,
		}
		resp, err := req.Get("https://fr-1.storage.online.net/scaleway/scaleway-cli/VERSION")
		if resp != nil {
			defer resp.Body.Close()
		}
		if err != nil {
			return
		}
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return
		}
		if scwversion.VERSION == "" {
			return
		}
		ver := scwversion.VERSION
		if ver[0] == 'v' {
			ver = string([]byte(ver)[1:])
		}
		actual, err1 := version.NewVersion(ver)
		update, err2 := version.NewVersion(strings.Trim(string(body), "\n"))
		if err1 != nil || err2 != nil {
			return
		}
		if actual.LessThan(update) {
			logrus.Infof("A new version of scw is available (%v), beware that you are currently running %v", update, actual)
		}
	}
}
Esempio n. 4
0
func runUserdata(cmd *Command, args []string) error {
	if userdataHelp {
		return cmd.PrintUsage()
	}
	if len(args) < 1 {
		return cmd.PrintShortUsage()
	}
	metadata := false
	ctx := cmd.GetContext(args)
	var API *api.ScalewayAPI
	var err error
	var serverID string
	if args[0] == "local" {
		API, err = api.NewScalewayAPI("", "", scwversion.UserAgent(), *flRegion)
		if err != nil {
			return err
		}
		metadata = true
	} else {
		if ctx.API == nil {
			return fmt.Errorf("You need to login first: 'scw login'")
		}
		serverID, err = ctx.API.GetServerID(args[0])
		if err != nil {
			return err
		}
		API = ctx.API
	}

	switch len(args) {
	case 1:
		// List userdata
		res, errGetUserdata := API.GetUserdatas(serverID, metadata)
		if errGetUserdata != nil {
			return errGetUserdata
		}
		for _, key := range res.UserData {
			fmt.Fprintln(ctx.Stdout, key)
		}
	default:
		parts := strings.Split(args[1], "=")
		key := parts[0]
		switch len(parts) {
		case 1:
			// Get userdatas
			res, errGetUserdata := API.GetUserdata(serverID, key, metadata)
			if errGetUserdata != nil {
				return errGetUserdata
			}
			fmt.Fprintf(ctx.Stdout, "%s\n", res.String())
		default:
			value := args[1][len(parts[0])+1:]
			if value != "" {
				var data []byte
				// Set userdata
				if value[0] == '@' {
					if len(value) > 1 && value[1] == '~' {
						home, err := config.GetHomeDir()
						if err != nil {
							return err
						}
						value = "@" + home + value[2:]
					}
					data, err = ioutil.ReadFile(value[1:])
					if err != nil {
						return err
					}
				} else {
					data = []byte(value)
				}
				err := API.PatchUserdata(serverID, key, data, metadata)
				if err != nil {
					return err
				}
				fmt.Fprintln(ctx.Stdout, key)
			} else {
				// Delete userdata
				err := API.DeleteUserdata(serverID, key, metadata)
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}