Exemple #1
0
func authMethods(ctx *cli.Context) (methods []ssh.AuthMethod, err error) {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Password: "******"key")
	if keyPath == "" {
		keyPath = filepath.Join(usr.HomeDir, ".ssh", "id_rsa")
	}

	key, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return
	}

	privateKey, err := ssh.ParsePrivateKey(key)
	if err != nil {
		return
	}
	methods = append(methods, ssh.PublicKeys(privateKey))

	return
}
Exemple #2
0
func dropletActionSnapshot(ctx *cli.Context) {
	if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide ID or name for Droplet to resize.")
	}

	name := ctx.String("name")

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	id := ctx.Int("id")
	if id == 0 {
		droplet, err := FindDropletByName(client, ctx.Args()[0])
		if err != nil {
			log.Fatal(err)
		} else {
			id = droplet.ID
		}
	}

	droplet, _, err := client.Droplets.Get(id)
	if err != nil {
		log.Fatal("Unable to find Droplet: %s.", err)
	}

	action, _, err := client.DropletActions.Snapshot(droplet.ID, name)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(action)
}
Exemple #3
0
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
	var context *cli.Context

	a := cli.NewApp()
	a.Commands = []cli.Command{
		{
			Name: "foo",
			Action: func(c *cli.Context) {
				context = c
			},
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "lang",
					Value: "english",
					Usage: "language for the greeting",
				},
			},
			Before: func(_ *cli.Context) error { return nil },
		},
	}
	a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})

	expect(t, context.Args().Get(0), "abcd")
	expect(t, context.String("lang"), "spanish")
}
Exemple #4
0
func sshDestroy(ctx *cli.Context) {
	if ctx.Int("id") == 0 && ctx.String("fingerprint") == "" && len(ctx.Args()) < 1 {
		fmt.Printf("Error: Must provide ID, fingerprint or name for SSH Key to destroy.\n")
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	id := ctx.Int("id")
	fingerprint := ctx.String("fingerprint")
	var key godo.Key
	if id == 0 && fingerprint == "" {
		key, err := FindKeyByName(client, ctx.Args().First())
		if err != nil {
			fmt.Printf("%s\n", err)
			os.Exit(64)
		} else {
			id = key.ID
		}
	} else if id != 0 {
		key, _, err := client.Keys.GetByID(id)
		if err != nil {
			fmt.Printf("Unable to find SSH Key: %s\n", err)
			os.Exit(1)
		} else {
			id = key.ID
		}
	} else {
		key, _, err := client.Keys.GetByFingerprint(fingerprint)
		if err != nil {
			fmt.Printf("Unable to find SSH Key: %s\n", err)
			os.Exit(1)
		} else {
			id = key.ID
		}
	}

	_, err := client.Keys.DeleteByID(id)
	if err != nil {
		fmt.Printf("Unable to destroy SSH Key: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Key %s destroyed.\n", key.Name)
}
Exemple #5
0
func dropletActionResize(ctx *cli.Context) {
	if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
		fmt.Printf("Error: Must provide ID or name for Droplet to destroy.\n")
		os.Exit(1)
	}

	size := ctx.String("size")
	disk := ctx.Bool("disk")

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	id := ctx.Int("id")
	if id == 0 {
		droplet, err := FindDropletByName(client, ctx.Args()[0])
		if err != nil {
			fmt.Printf("%s\n", err)
			os.Exit(64)
		} else {
			id = droplet.ID
		}
	}

	droplet, _, err := client.Droplets.Get(id)
	if err != nil {
		fmt.Printf("Unable to find Droplet: %s\n", err)
		os.Exit(1)
	}

	action, _, err := client.DropletActions.Resize(droplet.ID, size, disk)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	WriteOutput(action)
}
Exemple #6
0
func domainRecordCreate(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		cli.ShowAppHelp(ctx)
		fmt.Printf("Must specify a domain name to add a record to.\n")
		os.Exit(1)
	}

	domainName := ctx.Args().First()

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	createRequest := &godo.DomainRecordEditRequest{
		Type: strings.ToUpper(ctx.String("type")),
		Name: ctx.String("name"),
		Data: ctx.String("data"),
	}

	if createRequest.Type == "MX" || createRequest.Type == "SRV" {
		createRequest.Priority = ctx.Int("priority")
	}
	if createRequest.Type == "SRV" {
		createRequest.Port = ctx.Int("port")
		createRequest.Weight = ctx.Int("weight")
	}

	domainRecord, _, err := client.Domains.CreateRecord(domainName, createRequest)
	if err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(1)
	}

	WriteOutput(domainRecord)
}
Exemple #7
0
func dropletCreate(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide name for Droplet.")
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	// Add domain to end if available.
	dropletName := ctx.Args().First()
	if ctx.Bool("add-region") {
		dropletName = fmt.Sprintf("%s.%s", dropletName, ctx.String("region"))
	}
	if ctx.String("domain") != "" {
		dropletName = fmt.Sprintf("%s.%s", dropletName, ctx.String("domain"))
	}

	// Loop through the SSH Keys and add by name. DO API should have handled
	// this case as well.
	var sshKeys []godo.DropletCreateSSHKey
	keyNames := ctx.String("ssh-keys")
	if keyNames != "" {
		for _, keyName := range strings.Split(keyNames, ",") {
			sshKey, err := FindKeyByName(client, keyName)
			if sshKey != nil && err == nil {
				sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: sshKey.ID})
			} else {
				log.Fatalf("Warning: Could not find key: %q.", keyName)
			}
		}
	}

	userData := ""
	userDataPath := ctx.String("user-data-file")
	if userDataPath != "" {
		file, err := os.Open(userDataPath)
		if err != nil {
			log.Fatalf("Error opening user data file: %s.", err)
		}

		userDataFile, err := ioutil.ReadAll(file)
		if err != nil {
			log.Fatalf("Error reading user data file: %s.", err)
		}
		userData = string(userDataFile)
	} else {
		userData = ctx.String("user-data")
	}

	createRequest := &godo.DropletCreateRequest{
		Name:   dropletName,
		Region: ctx.String("region"),
		Size:   ctx.String("size"),
		Image: godo.DropletCreateImage{
			Slug: ctx.String("image"),
		},
		SSHKeys:           sshKeys,
		Backups:           ctx.Bool("backups"),
		IPv6:              ctx.Bool("ipv6"),
		PrivateNetworking: ctx.Bool("private-networking"),
		UserData:          userData,
	}

	droplet, resp, err := client.Droplets.Create(createRequest)
	if err != nil {
		log.Fatalf("Unable to create Droplet: %s.", err)
	}

	if ctx.Bool("wait-for-active") {
		util.WaitForActive(client, resp.Links.Actions[0].HREF)
	}
	WriteOutput(droplet)
}