示例#1
0
// RunImageActionsTransfer an image.
func RunImageActionsTransfer(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	region, err := config.GetString(ns, doit.ArgRegionSlug)
	if err != nil {
		return err
	}

	req := &godo.ActionRequest{
		"region": region,
	}

	action, _, err := client.ImageActions.Transfer(id, req)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not transfer image")
	}

	return doit.DisplayOutput(action, out)
}
示例#2
0
文件: domains.go 项目: alexcb/doit
// RunDomainCreate runs domain create.
func RunDomainCreate(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	domainName := args[0]

	client := config.GetGodoClient()

	ipAddress, err := config.GetString(ns, "ip-address")
	if err != nil {
		return err
	}

	req := &godo.DomainCreateRequest{
		Name:      domainName,
		IPAddress: ipAddress,
	}

	d, _, err := client.Domains.Create(req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
示例#3
0
文件: domains.go 项目: alexcb/doit
// RunDomainList runs domain create.
func RunDomainList(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := client.Domains.List(opt)
		if err != nil {
			return nil, nil, err
		}

		si := make([]interface{}, len(list))
		for i := range list {
			si[i] = list[i]
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(f)
	if err != nil {
		return err
	}

	list := make([]godo.Domain, len(si))
	for i := range si {
		list[i] = si[i].(godo.Domain)
	}

	return doit.DisplayOutput(list, out)
}
示例#4
0
文件: images.go 项目: alexcb/doit
// RunImagesGet retrieves an image by id or slug.
func RunImagesGet(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawID := args[0]

	var image *godo.Image
	var err error

	if id, cerr := strconv.Atoi(rawID); cerr == nil {
		image, _, err = client.Images.GetByID(id)
	} else {
		if len(rawID) > 0 {
			image, _, err = client.Images.GetBySlug(rawID)
		} else {
			err = fmt.Errorf("image identifier is required")
		}
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(image, out)
}
示例#5
0
文件: images.go 项目: alexcb/doit
func listImages(ns string, config doit.Config, out io.Writer, lFn listFn) error {
	public, err := config.GetBool(ns, doit.ArgImagePublic)
	if err != nil {
		return err
	}

	fn := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := lFn(opt)
		if err != nil {
			return nil, nil, err
		}

		si := []interface{}{}
		for _, i := range list {
			if (public && i.Public) || !public {
				si = append(si, i)
			}
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(fn)
	if err != nil {
		return err
	}

	list := make([]godo.Image, len(si))
	for i := range si {
		list[i] = si[i].(godo.Image)
	}

	return doit.DisplayOutput(list, out)
}
示例#6
0
文件: images.go 项目: alexcb/doit
// RunImagesUpdate updates an image.
func RunImagesUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	name, err := config.GetString(ns, doit.ArgImageName)

	req := &godo.ImageUpdateRequest{
		Name: name,
	}

	image, _, err := client.Images.Update(id, req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(image, out)
}
示例#7
0
文件: sshkeys.go 项目: alexcb/doit
// RunKeyUpdate updates a key.
func RunKeyUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawKey := args[0]

	name, err := config.GetString(ns, doit.ArgKeyName)
	if err != nil {
		return err
	}

	req := &godo.KeyUpdateRequest{
		Name: name,
	}

	var key *godo.Key
	if i, aerr := strconv.Atoi(rawKey); aerr == nil {
		key, _, err = client.Keys.UpdateByID(i, req)
	} else {
		key, _, err = client.Keys.UpdateByFingerprint(rawKey, req)
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(key, out)
}
示例#8
0
文件: sshkeys.go 项目: alexcb/doit
// RunKeyGet retrieves a key.
func RunKeyGet(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawKey := args[0]

	var err error
	var key *godo.Key

	if i, aerr := strconv.Atoi(rawKey); aerr == nil {
		key, _, err = client.Keys.GetByID(i)
	} else {
		if len(rawKey) > 0 {
			key, _, err = client.Keys.GetByFingerprint(rawKey)
		} else {
			err = fmt.Errorf("missing key id or fingerprint")
		}
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(key, out)
}
示例#9
0
// RunDropletActionResize resizesx a droplet giving a size slug and
// optionally expands the disk.
func RunDropletActionResize(ns string, config doit.Config, out io.Writer, args []string) error {
	fn := func(client *godo.Client) (*godo.Action, error) {
		if len(args) != 1 {
			return nil, doit.NewMissingArgsErr(ns)
		}
		id, err := strconv.Atoi(args[0])

		if err != nil {
			return nil, err
		}

		size, err := config.GetString(ns, doit.ArgImageSlug)
		if err != nil {
			return nil, err
		}

		disk, err := config.GetBool(ns, doit.ArgResizeDisk)
		if err != nil {
			return nil, err
		}

		a, _, err := client.DropletActions.Resize(id, size, disk)
		return a, err
	}

	return performAction(out, config, fn)
}
示例#10
0
文件: sshkeys.go 项目: alexcb/doit
// RunKeyCreate uploads a SSH key.
func RunKeyCreate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	name := args[0]

	publicKey, err := config.GetString(ns, doit.ArgKeyPublicKey)
	if err != nil {
		return err
	}

	kcr := &godo.KeyCreateRequest{
		Name:      name,
		PublicKey: publicKey,
	}

	r, _, err := client.Keys.Create(kcr)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not create key")
	}

	return doit.DisplayOutput(r, out)
}
示例#11
0
// RunDropletActionRebuild rebuilds a droplet using an image id or slug.
func RunDropletActionRebuild(ns string, config doit.Config, out io.Writer, args []string) error {
	fn := func(client *godo.Client) (*godo.Action, error) {
		if len(args) != 1 {
			return nil, doit.NewMissingArgsErr(ns)
		}
		id, err := strconv.Atoi(args[0])

		if err != nil {
			return nil, err
		}

		image, err := config.GetString(ns, doit.ArgImage)
		if err != nil {
			return nil, err
		}

		var a *godo.Action
		if i, aerr := strconv.Atoi(image); aerr == nil {
			a, _, err = client.DropletActions.RebuildByImageID(id, i)
		} else {
			a, _, err = client.DropletActions.RebuildByImageSlug(id, image)
		}
		return a, err
	}

	return performAction(out, config, fn)
}
示例#12
0
func performAction(out io.Writer, config doit.Config, fn actionFn) error {
	client := config.GetGodoClient()

	a, err := fn(client)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(a, out)
}
示例#13
0
文件: images.go 项目: alexcb/doit
// RunImagesDelete deletes an image.
func RunImagesDelete(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	_, err = client.Images.Delete(id)
	return err
}
示例#14
0
文件: domains.go 项目: alexcb/doit
// RunDomainDelete deletes a domain by name.
func RunDomainDelete(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	name := args[0]

	client := config.GetGodoClient()

	if len(name) < 1 {
		return errors.New("invalid domain name")
	}

	_, err := client.Domains.Delete(name)
	return err
}
示例#15
0
// RunFloatingIPActionsUnassign unassigns a floating IP to a droplet.
func RunFloatingIPActionsUnassign(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	ip := args[0]

	client := config.GetGodoClient()

	action, _, err := client.FloatingIPActions.Unassign(ip)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not unsassign IP to droplet")
	}
	return doit.DisplayOutput(action, out)
}
示例#16
0
文件: domains.go 项目: alexcb/doit
// RunRecordDelete deletes a domain record.
func RunRecordDelete(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	domainName := args[0]

	client := config.GetGodoClient()

	recordID, err := config.GetInt(ns, doit.ArgRecordID)
	if err != nil {
		return err
	}

	_, err = client.Domains.DeleteRecord(domainName, recordID)
	return err
}
示例#17
0
文件: domains.go 项目: alexcb/doit
// RunDomainGet retrieves a domain by name.
func RunDomainGet(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	id := args[0]

	client := config.GetGodoClient()

	if len(id) < 1 {
		return errors.New("invalid domain name")
	}

	d, _, err := client.Domains.Get(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
示例#18
0
文件: sshkeys.go 项目: alexcb/doit
// RunKeyDelete deletes a key.
func RunKeyDelete(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawKey := args[0]

	var err error

	if i, aerr := strconv.Atoi(rawKey); aerr == nil {
		_, err = client.Keys.DeleteByID(i)
	} else {
		_, err = client.Keys.DeleteByFingerprint(rawKey)
	}

	return err
}
示例#19
0
文件: actions.go 项目: alexcb/doit
// RunCmdActionGet runs action get.
func RunCmdActionGet(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	client := config.GetGodoClient()

	a, _, err := client.Actions.Get(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(a, out)
}
示例#20
0
// RunDropletActionGet returns a droplet action by id.
func RunDropletActionGet(ns string, config doit.Config, out io.Writer, args []string) error {
	fn := func(client *godo.Client) (*godo.Action, error) {
		if len(args) != 1 {
			return nil, doit.NewMissingArgsErr(ns)
		}
		dropletID, err := strconv.Atoi(args[0])
		if err != nil {
			return nil, err
		}

		actionID, err := config.GetInt(ns, doit.ArgActionID)
		if err != nil {
			return nil, err
		}

		a, _, err := client.DropletActions.Get(dropletID, actionID)
		return a, err
	}

	return performAction(out, config, fn)
}
示例#21
0
// RunFloatingIPActionsGet retrieves an action for a floating IP.
func RunFloatingIPActionsGet(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	ip := args[0]

	client := config.GetGodoClient()

	actionID, err := config.GetInt(ns, doit.ArgActionID)
	if err != nil {
		return err
	}

	action, _, err := client.FloatingIPActions.Get(ip, actionID)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(action, out)
}
示例#22
0
文件: sshkeys.go 项目: alexcb/doit
// RunKeyImport imports a key from a file
func RunKeyImport(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	keyPath, err := config.GetString(ns, doit.ArgKeyPublicKeyFile)
	if err != nil {
		return err
	}

	keyName := args[0]

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

	_, comment, _, _, err := ssh.ParseAuthorizedKey(keyFile)
	if err != nil {
		return err
	}

	if len(keyName) < 1 {
		keyName = comment
	}

	kcr := &godo.KeyCreateRequest{
		Name:      keyName,
		PublicKey: string(keyFile),
	}

	r, _, err := client.Keys.Create(kcr)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(r, out)
}
示例#23
0
// RunDropletActionSnapshot creates a snapshot for a droplet.
func RunDropletActionSnapshot(ns string, config doit.Config, out io.Writer, args []string) error {
	fn := func(client *godo.Client) (*godo.Action, error) {
		if len(args) != 1 {
			return nil, doit.NewMissingArgsErr(ns)
		}
		id, err := strconv.Atoi(args[0])

		if err != nil {
			return nil, err
		}

		name, err := config.GetString(ns, doit.ArgSnapshotName)
		if err != nil {
			return nil, err
		}

		a, _, err := client.DropletActions.Snapshot(id, name)
		return a, err
	}

	return performAction(out, config, fn)
}
示例#24
0
文件: domains.go 项目: alexcb/doit
// RunRecordList list records for a domain.
func RunRecordList(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	name := args[0]

	client := config.GetGodoClient()

	if len(name) < 1 {
		return errors.New("domain name is missing")
	}

	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := client.Domains.Records(name, opt)
		if err != nil {
			return nil, nil, err
		}

		si := make([]interface{}, len(list))
		for i := range list {
			si[i] = list[i]
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(f)
	if err != nil {
		return err
	}

	list := make([]godo.DomainRecord, len(si))
	for i := range si {
		list[i] = si[i].(godo.DomainRecord)
	}

	return doit.DisplayOutput(list, out)
}
示例#25
0
// RunImageActionsGet retrieves an action for an image.
func RunImageActionsGet(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	imageID, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	actionID, err := config.GetInt(ns, doit.ArgActionID)
	if err != nil {
		return err
	}

	action, _, err := client.ImageActions.Get(imageID, actionID)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(action, out)
}
示例#26
0
文件: ssh.go 项目: alexcb/doit
// RunSSH finds a droplet to ssh to given input parameters (name or id).
func RunSSH(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) == 0 {
		return doit.NewMissingArgsErr(ns)
	}

	dropletID := args[0]

	if dropletID == "" {
		return doit.NewMissingArgsErr(ns)
	}

	user, err := config.GetString(ns, doit.ArgSSHUser)
	if err != nil {
		return err
	}

	keyPath, err := config.GetString(ns, doit.ArgsSSHKeyPath)
	if err != nil {
		return err
	}

	port, err := config.GetInt(ns, doit.ArgsSSHPort)
	if err != nil {
		return err
	}

	var droplet *godo.Droplet

	if id, err := strconv.Atoi(dropletID); err == nil {
		// dropletID is an integer
		droplet, err = getDropletByID(client, id)
	} else {
		// dropletID is a string
		var droplets []godo.Droplet
		droplets, err := listDroplets(client)
		if err != nil {
			return err
		}

		shi := extractHostInfo(dropletID)

		user = shi.user
		if i, err := strconv.Atoi(shi.port); shi.port != "" && err != nil {
			port = i
		}

		for _, d := range droplets {
			if d.Name == shi.host {
				droplet = &d
				break
			}
		}

		if droplet == nil {
			return errors.New("could not find droplet by name")
		}

	}

	if user == "" {
		user = defaultSSHUser(droplet)
	}
	publicIP := extractDropletPublicIP(droplet)

	if len(publicIP) < 1 {
		return errors.New(sshNoAddress)
	}

	runner := config.SSH(user, publicIP, keyPath, port)
	return runner.Run()
}
示例#27
0
文件: domains.go 项目: alexcb/doit
// RunRecordUpdate updates a domain record.
func RunRecordUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	domainName := args[0]

	client := config.GetGodoClient()

	recordID, err := config.GetInt(ns, doit.ArgRecordID)
	if err != nil {
		return err
	}

	rType, err := config.GetString(ns, doit.ArgRecordType)
	if err != nil {
		return err
	}

	rName, err := config.GetString(ns, doit.ArgRecordName)
	if err != nil {
		return err
	}

	rData, err := config.GetString(ns, doit.ArgRecordData)
	if err != nil {
		return err
	}

	rPriority, err := config.GetInt(ns, doit.ArgRecordPriority)
	if err != nil {
		return err
	}

	rPort, err := config.GetInt(ns, doit.ArgRecordPort)
	if err != nil {
		return err
	}

	rWeight, err := config.GetInt(ns, doit.ArgRecordWeight)
	if err != nil {
		return err
	}

	drcr := &godo.DomainRecordEditRequest{
		Type:     rType,
		Name:     rName,
		Data:     rData,
		Priority: rPriority,
		Port:     rPort,
		Weight:   rWeight,
	}

	r, _, err := client.Domains.EditRecord(domainName, recordID, drcr)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(r, out)
}
示例#28
0
文件: images.go 项目: alexcb/doit
// RunImagesListUser lists user images.
func RunImagesListUser(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()
	return listImages(ns, config, out, client.Images.ListUser)
}