commands

package
v2.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2016 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RegisterSelfCommand = cli.Command{
	Name:        "register-self",
	Usage:       "register-self [options]",
	Description: "Registers virtual instance that calls this script to iCE.",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "api-endpoint",
			Usage: "The iCE REST API endpoint URL",
		},
		cli.StringFlag{
			Name:  "session-id",
			Usage: "The session id",
		},
		cli.StringSliceFlag{
			Name:  "tag",
			Usage: "An instnace tag in the form of <key>=<value>",
		},
	},

	Action: func(ctx *cli.Context) error {
		apiEndpoint := ctx.String("api-endpoint")
		sessionID := ctx.String("session-id")
		tagsList := ctx.StringSlice("tag")

		tags, err := parseTags(tagsList)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}

		inst := ice.Instance{
			SessionID: sessionID,
			Tags:      tags,
		}

		sshAuthorizedFingerprint, err := ssh.AuthorizedFingerprint(context.TODO())
		if err == nil {
			inst.SSHAuthorizedFingerprint = sshAuthorizedFingerprint
		}

		iceClient := ice.NewClient(apiEndpoint)

		inst.Networks, err = network.Networks(context.TODO())
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}
		inst.PublicIPAddr, err = iceClient.MyIP(context.TODO())
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}
		publicReverseDNS, err := network.ReverseDNS(
			context.TODO(), inst.PublicIPAddr.String(),
		)
		if err == nil {
			inst.PublicReverseDNS = publicReverseDNS
		}

		instID, err := iceClient.StoreInstance(context.TODO(), inst)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}

		if err := state.WriteInstanceID(context.TODO(), instID); err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}

		inst.ID = instID
		if err := json.NewEncoder(os.Stdout).Encode(inst); err != nil {
			return cli.NewExitError(fmt.Sprintf("ERROR: %s", err), 1)
		}

		return nil
	},
}

RegisterSelfCommand implements the register-self subcommand of the agent.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL