monitor

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "monitor",
	Short: "(Used when installed in fly.io env) Monitors flycd apps, listens to webhooks, grabs new states from git, etc",
	Args:  cobra.RangeArgs(0, 1),
	Run: func(cmd *cobra.Command, args []string) {
		path, err := os.Getwd()
		if err != nil {
			fmt.Printf("Error getting current working directory: %v\n", err)
			os.Exit(1)
		}

		if len(args) > 0 {
			path = args[0]
		}

		fmt.Printf("Monitoring: %s\n", path)

		ctx := context.Background()

		accessToken := os.Getenv("FLY_ACCESS_TOKEN")
		if accessToken == "" {
			fmt.Printf("WARNING: FLY_ACCESS_TOKEN env var not set. Proceeding and assuming you are running locally logged in...\n")
		} else {
			ctx = context.WithValue(ctx, "FLY_ACCESS_TOKEN", accessToken)
		}

		fmt.Printf("Checking if to store ssh... \n")
		sshKey := os.Getenv("FLY_SSH_PRIVATE_KEY")
		sshKeyName := os.Getenv("FLY_SSH_PRIVATE_KEY_NAME")
		if sshKey == "" {
			fmt.Printf("WARNING: FLY_SSH_PRIVATE_KEY env var not set. Proceeding and assuming you only want to access public repos, or you have magically solved git auth in some other way...\n")
		} else {

			fmt.Printf("FLY_SSH_PRIVATE_KEY env var is set, so we probably want o do something... \n")

			if sshKeyName == "" {
				fmt.Printf("FLY_SSH_PRIVATE_KEY_NAME env var not set, so just guessing we want 'id_rsa'\n")
				sshKeyName = "id_rsa"
			}

			fmt.Printf("Checking if to store ssh key: %s\n", sshKeyName)

			homeDir, err := os.UserHomeDir()
			if err != nil {
				fmt.Printf("Error getting user home directory: %v\n", err)
				os.Exit(1)
			}

			sshDir := homeDir + "/.ssh"
			sshKeyPath := sshDir + "/" + sshKeyName

			if _, err := os.Stat(sshDir); os.IsNotExist(err) {
				fmt.Printf("ssh dir does not exist: %s\n", sshDir)
				os.Exit(1)
			}

			if _, err := os.Stat(sshKeyPath); !os.IsNotExist(err) {
				fmt.Printf("ssh key already exists. Skipping copy from env var -> %s\n", sshKeyPath)
			} else {

				err = os.WriteFile(sshKeyPath, []byte(sshKey), 0600)
				if err != nil {
					fmt.Printf("Error writing ssh key to file: %v\n", err)
					os.Exit(1)
				}

				fmt.Printf("Stored ssh key: %s\n", sshKeyPath)
			}
		}

		res, err := util_cmd.NewCommand("fly", "apps", "list").Run(ctx)
		if err != nil {
			fmt.Printf("Error getting apps list. Do you have a token loaded?: %v\n", err)
			os.Exit(1)
		}

		appsTable, err := util_tab_table.ParseTable(res.StdOut)
		if err != nil {
			fmt.Printf("Error parsing apps list: %v\n", err)
			os.Exit(1)
		}

		fmt.Printf("Currently deployed apps: \n")
		for _, appRow := range appsTable.RowMaps {
			name := appRow["NAME"]
			org := appRow["OWNER"]

			fmt.Printf(" - name=%s, org=%s\n", name, org)
		}

		if *flags.startupSync {
			fmt.Printf("Syncing/Deploying all apps in %s\n", path)

			deployCfg := model.
				NewDeployConfig().
				WithAbortOnFirstError(false)

			_, err := flycd.DeployAll(ctx, path, deployCfg)
			if err != nil {
				fmt.Printf("Error deploying: %v\n", err)
				return
			}

		}

		e := echo.New()

		e.Use(middleware.Logger())
		e.Use(middleware.Recover())

		whPath := *flags.whPath
		if whPath == "" {
			whPath = "/webhook"
		}
		if !strings.HasPrefix(whPath, "/") {
			whPath = "/" + whPath
		}

		fmt.Printf("Listening on webhook path: %s\n", whPath)
		fmt.Printf("Listening on webhook port: %d\n", *flags.whPort)

		e.GET("/", processHealth)
		e.POST(whPath, func(c echo.Context) error {
			return processWebhook(c, path)
		})

		e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", *flags.whIfc, *flags.whPort)))
	},
}

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