handler

package
v0.15.14 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = cli.Command{
	Name:        "handler",
	Description: "Manage handlers",
	Usage:       "Manage handlers",
	Subcommands: []*cli.Command{
		&RegisterCommand,
		&ValidateCommand,
		&ListCommand,
		&DiagnosticCommand,
		&LogsCommand,
		&DeleteCommand,
	},
}
View Source
var DeleteCommand = cli.Command{
	Name:        "delete",
	Description: "Delete handlers",
	Usage:       "Delete handlers",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
	},
	Action: cli.ActionFunc(func(c *cli.Context) error {
		ctx := c.Context
		cfg, err := cliconfig.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}
		id := c.String("id")
		if id == "" {
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			id = h.Id
		}
		_, err = cf.AdminDeleteHandlerWithResponse(ctx, id)
		if err != nil {
			return err
		}
		clio.Success("Deleted handler ", c.String("id"))

		return nil
	}),
}
View Source
var DiagnosticCommand = cli.Command{
	Name:        "diagnostics",
	Aliases:     []string{"diagnostic"},
	Description: "List diagnostic logs for a handler",
	Usage:       "List diagnostic logs for a handler",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
	},
	Action: cli.ActionFunc(func(c *cli.Context) error {
		ctx := c.Context
		id := c.String("id")
		cfg, err := cliconfig.Load()
		if err != nil {
			return err
		}
		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}
		var handler types.TGHandler
		if id == "" {
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			handler = *h
		} else {
			res, err := cf.AdminGetHandlerWithResponse(ctx, id)
			if err != nil {
				return err
			}
			handler = *res.JSON200
		}

		health := "healthy"
		if !handler.Healthy {
			health = "unhealthy"
		}

		tbl := table.New(os.Stderr)
		clio.Log("Handler")
		tbl.Columns("ID", "Account", "Region", "Health")
		tbl.Row(handler.Id, handler.AwsAccount, handler.AwsRegion, health)

		err = tbl.Flush()
		if err != nil {
			return err
		}
		clio.NewLine()
		clio.Log("Diagnostics")
		tbl.Columns("Level", "Message")
		for _, d := range handler.Diagnostics {
			tbl.Row(string(d.Level), d.Message)
		}

		return tbl.Flush()
	}),
}
View Source
var GetCommand = cli.Command{
	Name:        "get",
	Description: "Get logs for a handler",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
		&cli.StringFlag{Name: "start", Usage: "Start time", Value: "-5m", Required: false},
		&cli.StringFlag{Name: "end", Usage: "End time", Value: "now", Required: false},
	},
	Action: func(c *cli.Context) error {

		ctx := c.Context
		cfg, err := cfaws.ConfigFromContextOrDefault(ctx)
		if err != nil {
			return err
		}
		id := c.String("id")
		if id == "" {
			cfg, err := cliconfig.Load()
			if err != nil {
				return err
			}
			cf, err := client.FromConfig(ctx, cfg)
			if err != nil {
				return err
			}
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			id = h.Id
		}
		logGroup := "/aws/lambda/" + id
		start := c.String("start")
		end := c.String("end")
		clio.Info("Starting to get logs for Health check lambda, log group id: %s", logGroup)
		hasLogs := true
		cwClient := cloudwatchlogs.NewFromConfig(cfg)

		o, _ := cwClient.DescribeLogGroups(ctx, &cloudwatchlogs.DescribeLogGroupsInput{
			LogGroupNamePrefix: &logGroup,
		})
		if o != nil && len(o.LogGroups) == 1 {
			lo, err := cwClient.DescribeLogStreams(ctx, &cloudwatchlogs.DescribeLogStreamsInput{
				LogGroupName: o.LogGroups[0].LogGroupName,
				Limit:        aws.Int32(1),
			})
			_ = err
			if lo != nil && len(lo.LogStreams) != 0 {
				hasLogs = true
			}
		}
		if hasLogs {
			getEvents(GetEventsOpts{Group: logGroup, Start: start, End: end}, cfg.Region, c.String("filter"))
		} else {
			clio.Warnf("The handler may not have been invoked yet. Log group id: %s", logGroup)
		}

		return nil
	},
}
View Source
var ListCommand = cli.Command{
	Name:        "list",
	Aliases:     []string{"ls"},
	Description: "List handlers",
	Usage:       "List handlers",
	Action: cli.ActionFunc(func(c *cli.Context) error {
		ctx := c.Context
		cfg, err := cliconfig.Load()
		if err != nil {
			return err
		}

		cfApi, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}
		res, err := cfApi.AdminListHandlersWithResponse(ctx)
		if err != nil {
			return err
		}

		tbl := table.New(os.Stderr)
		tbl.Columns("ID", "Account", "Region", "Health")
		for _, d := range res.JSON200.Res {
			health := "healthy"
			if !d.Healthy {
				health = "unhealthy"
			}
			tbl.Row(d.Id, d.AwsAccount, d.AwsRegion, health)
		}
		return tbl.Flush()
	}),
}
View Source
var LogsCommand = cli.Command{
	Name:        "logs",
	Description: "View log groups for a handler",
	Usage:       "View log groups for a handler",
	Subcommands: []*cli.Command{
		&WatchCommand,
		&GetCommand,
	},
}
View Source
var RegisterCommand = cli.Command{
	Name:        "register",
	Description: "Register a handler in Common Fate",
	Usage:       "Register a handler in Common Fate",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
		&cli.StringFlag{Name: "runtime", Value: "aws-lambda"},
		&cli.StringFlag{Name: "aws-region"},
		&cli.StringFlag{Name: "aws-account"},
	},
	Action: func(c *cli.Context) error {

		ctx := c.Context

		var handlerID = c.String("id")
		if handlerID == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter the hander ID (this should be the cloudfromation stack name)"}, &handlerID)
			if err != nil {
				return err
			}
		}
		var runtime = c.String("runtime")
		if runtime == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter the runtime", Default: "aws-lambda"}, &runtime)
			if err != nil {
				return err
			}
		}
		var awsRegion = c.String("aws-region")
		if awsRegion == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter the AWS Region that the handler is deployed in", Default: os.Getenv("AWS_REGION")}, &awsRegion)
			if err != nil {
				return err
			}
		}
		var awsAccount = c.String("aws-account")
		if awsAccount == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter the AWS Account ID that your handler is deployed in:"}, &awsAccount)
			if err != nil {
				return err
			}
		}

		cfg, err := cliconfig.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}

		_, err = cf.AdminRegisterHandlerWithResponse(ctx, types.AdminRegisterHandlerJSONRequestBody{
			AwsAccount: awsAccount,
			AwsRegion:  awsRegion,
			Runtime:    runtime,
			Id:         handlerID,
		})
		if err != nil {
			return err
		}

		clio.Successf("Successfully registered handler '%s' with Common Fate", handlerID)

		return nil
	},
}
View Source
var ValidateCommand = cli.Command{
	Name:        "validate",
	Description: "Validate a handler by invoking the handler directly",
	Usage:       "Validate a handler",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id", Required: true, Usage: "The ID of the handler, when deploying via CloudFormation this is the HandlerID parameter that you configured. e.g 'aws-sso'"},
		&cli.StringFlag{Name: "aws-region", Required: true},

		&cli.StringFlag{Name: "runtime", Required: true, Value: "aws-lambda"},
		&cli.StringFlag{Name: "cloudformation-stack-name", Usage: "If CloudFormation was used to deploy the provider, use this flag to check the status of the stack"},
	},
	Action: func(c *cli.Context) error {
		id := c.String("id")
		awsRegion := c.String("aws-region")

		if c.String("runtime") != "aws-lambda" {
			return errors.New("unsupported runtime. Supported runtimes are [aws-lambda]")
		}

		providerRuntime, err := handlerclient.NewLambdaRuntime(c.Context, id)
		if err != nil {
			return err
		}

		cfg, err := cfaws.ConfigFromContextOrDefault(c.Context)

		cfg.Region = awsRegion
		if err != nil {
			return err
		}
		if c.String("cloudformation-stack-name") != "" {
			cfnClient := cloudformation.NewFromConfig(cfg)
			stacks, err := cfnClient.DescribeStacks(c.Context, &cloudformation.DescribeStacksInput{
				StackName: aws.String(c.String("cloudformation-stack-name")),
			})
			if err != nil {
				return err
			}
			clio.Infof("cloudformation stack '%s' exists in '%s' and is in '%s' state", c.String("cloudformation-stack-name"), awsRegion, stacks.Stacks[0].StackStatus)
		}

		desc, err := providerRuntime.Describe(c.Context)
		if err != nil {
			return err
		}

		clio.Infof("Provider: %s/%s@%s\n", desc.Provider.Publisher, desc.Provider.Name, desc.Provider.Version)

		schemaBytes, err := json.Marshal(desc.Schema)
		if err != nil {
			return err
		}

		clio.Infof("Provider Schema:\n%s", string(schemaBytes))

		if len(desc.Diagnostics) > 0 {
			clio.Infow("Deployment Diagnostics", "logs", desc.Diagnostics)
		}

		if desc.Healthy {
			clio.Success("Deployment is healthy")

		} else {
			clio.Error("Deployment is unhealthy")
		}

		return nil
	},
}
View Source
var WatchCommand = cli.Command{
	Name:        "watch",
	Description: "Stream logs for a handler",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
	},
	Action: func(c *cli.Context) error {

		ctx := c.Context
		cfg, err := cfaws.ConfigFromContextOrDefault(ctx)
		if err != nil {
			return err
		}
		id := c.String("id")
		if id == "" {
			cfg, err := cliconfig.Load()
			if err != nil {
				return err
			}
			cf, err := client.FromConfig(ctx, cfg)
			if err != nil {
				return err
			}
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			id = h.Id
		}

		logGroup := "/aws/lambda/" + id
		clio.Infof("Starting to watch logs for log group id: %s", logGroup)
		watchEvents(logGroup, cfg.Region, c.String("filter"))
		return nil
	},
}

Functions

This section is empty.

Types

type GetEventsOpts

type GetEventsOpts struct {
	Group string
	Start string
	End   string
}

Jump to

Keyboard shortcuts

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