cmd

package
v0.1.1-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InitCmd = &cobra.Command{
	Use:   "init",
	Short: "Setup Actions in Kubernetes or Standalone modes",
	Run: func(cmd *cobra.Command, args []string) {
		print.PendingStatusEvent(os.Stdout, "Making the jump to hyperspace...")

		if kubernetesMode {
			err := kubernetes.Init()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				return
			}
		} else {
			err := standalone.Init()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				return
			}
		}

		print.SuccessStatusEvent(os.Stdout, "Success! Get ready to rumble")
	},
}
View Source
var ListCmd = &cobra.Command{
	Use:   "list",
	Short: "List all Actions",
	Run: func(cmd *cobra.Command, args []string) {
		if kubernetesMode {
			list, err := kubernetes.List()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				os.Exit(1)
			}

			table, err := gocsv.MarshalString(list)
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				os.Exit(1)
			}

			utils.PrintTable(table)
		} else {
			list, err := standalone.List()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				os.Exit(1)
			}

			if len(list) == 0 {
				println("No Actions found.")
				return
			}

			table, err := gocsv.MarshalString(list)
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				os.Exit(1)
			}

			utils.PrintTable(table)
		}
	},
}
View Source
var PublishCmd = &cobra.Command{
	Use:   "publish",
	Short: "publish an event to multiple consumers",
	Run: func(cmd *cobra.Command, args []string) {
		err := publish.PublishTopic(publishAppID, publishTopic, publishPayload)
		if err != nil {
			print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error publishing topic %s: %s", publishTopic, err))
			return
		}

		print.SuccessStatusEvent(os.Stdout, "Event published successfully")
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "actions",
	Short: "Actions CLI",
	Long: `
   ___  ___________________  _  ______
  / _ |/ ___/_  __/  _/ __ \/ |/ / __/
 / __ / /__  / / _/ // /_/ /    /\ \  
/_/ |_\___/ /_/ /___/\____/_/|_/___/  								
======================================================
A serverless runtime for hyperscale, distributed systems`,
}
View Source
var RunCmd = &cobra.Command{
	Use:   "run",
	Short: "Launches Actions and your app side by side",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		uuid, err := uuid.NewRandom()
		if err != nil {
			print.FailureStatusEvent(os.Stdout, err.Error())
			return
		}

		actionsRunID := uuid.String()

		if kubernetesMode {
			output, err := kubernetes.Run(&kubernetes.RunConfig{
				AppID:         appID,
				AppPort:       appPort,
				Port:          port,
				Arguments:     args,
				Image:         image,
				CodeDirectory: args[0],
			})
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				return
			}

			print.InfoStatusEvent(os.Stdout, output.Message)
		} else {
			output, err := standalone.Run(&standalone.RunConfig{
				AppID:     appID,
				AppPort:   appPort,
				Port:      port,
				Arguments: args,
			})
			if err != nil {
				print.FailureStatusEvent(os.Stdout, err.Error())
				return
			}

			var sigCh = make(chan os.Signal)
			signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)

			actionsRunning := make(chan bool, 1)
			appRunning := make(chan bool, 1)
			actionsRunCreatedTime := time.Now()

			go func() {
				print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Starting Actions with id %s on port %v", output.AppID, output.ActionsPort))

				stdErrPipe, err := output.ActionsCMD.StderrPipe()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error creating stderr for Actions: %s", err.Error()))
					os.Exit(1)
				}

				stdOutPipe, err := output.ActionsCMD.StdoutPipe()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error creating stdout for Actions: %s", err.Error()))
					os.Exit(1)
				}

				errScanner := bufio.NewScanner(stdErrPipe)
				outScanner := bufio.NewScanner(stdOutPipe)
				go func() {
					for errScanner.Scan() {
						fmt.Printf(print.Yellow(fmt.Sprintf("== ACTIONS == %s\n", errScanner.Text())))
					}
				}()

				go func() {
					for outScanner.Scan() {
						fmt.Printf(print.Yellow(fmt.Sprintf("== ACTIONS == %s\n", outScanner.Text())))
					}
				}()

				err = output.ActionsCMD.Start()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, err.Error())
					os.Exit(1)
				}

				actionsRunning <- true
			}()

			<-actionsRunning

			go func() {
				stdErrPipe, err := output.AppCMD.StderrPipe()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error creating stderr for App: %s", err.Error()))
					os.Exit(1)
				}

				stdOutPipe, err := output.AppCMD.StdoutPipe()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error creating stdout for App: %s", err.Error()))
					os.Exit(1)
				}

				errScanner := bufio.NewScanner(stdErrPipe)
				outScanner := bufio.NewScanner(stdOutPipe)
				go func() {
					for errScanner.Scan() {
						fmt.Printf(print.Blue(fmt.Sprintf("== APP == %s\n", errScanner.Text())))
					}
				}()

				go func() {
					for outScanner.Scan() {
						fmt.Printf(print.Blue(fmt.Sprintf("== APP == %s\n", outScanner.Text())))
					}
				}()

				err = output.AppCMD.Start()
				if err != nil {
					print.FailureStatusEvent(os.Stdout, err.Error())
					os.Exit(1)
				}

				appRunning <- true
			}()

			<-appRunning

			rundata.AppendRunData(&rundata.RunData{
				ActionsRunId: actionsRunID,
				AppId:        output.AppID,
				ActionsPort:  output.ActionsPort,
				AppPort:      appPort,
				Command:      strings.Join(args, " "),
				Created:      actionsRunCreatedTime,
			})

			print.SuccessStatusEvent(os.Stdout, "You're up and running! Both Actions and your app logs will appear here.\n")

			<-sigCh
			print.InfoStatusEvent(os.Stdout, "\nterminated signal recieved: shutting down")

			rundata.ClearRunData(actionsRunID)

			err = output.ActionsCMD.Process.Kill()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting Actions: %s", err))
			} else {
				print.SuccessStatusEvent(os.Stdout, "Exited Actions successfully")
			}

			err = output.AppCMD.Process.Kill()
			if err != nil {
				print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting App: %s", err))
			} else {
				print.SuccessStatusEvent(os.Stdout, "Exited App successfully")
			}
		}
	},
}
View Source
var SendCmd = &cobra.Command{
	Use:   "send",
	Short: "invoke an Actions app or actor with an optional payload",
	Run: func(cmd *cobra.Command, args []string) {
		response, err := send.InvokeApp(sendAppID, sendAppMethod, sendPayload)
		if err != nil {
			print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error invoking app %s: %s", sendAppID, err))
			return
		}

		if response != "" {
			fmt.Println(response)
		}

		print.SuccessStatusEvent(os.Stdout, "App invoked successfully")
	},
}

Functions

func Execute

func Execute(version, apiVersion string)

Execute adds all child commands to the root command

Types

This section is empty.

Jump to

Keyboard shortcuts

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