|
| 1 | +/* |
| 2 | +Copyright © 2020 Anton Kramarev |
| 3 | +Copyright © 2024 Perfana Software B.V. |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +package cmd |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + "os" |
| 31 | + "os/exec" |
| 32 | + "os/signal" |
| 33 | + "syscall" |
| 34 | + |
| 35 | + "github.com/perfana/x2i/influx" |
| 36 | + l "github.com/perfana/x2i/logger" |
| 37 | + "github.com/perfana/x2i/gatlingparser" |
| 38 | + "github.com/perfana/x2i/jmeterparser" |
| 39 | + "github.com/perfana/x2i/k6parser" |
| 40 | + "github.com/spf13/cobra" |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + ctx context.Context |
| 45 | + cancel context.CancelFunc |
| 46 | +) |
| 47 | + |
| 48 | +func preRunSetup(cmd *cobra.Command, args []string) error { |
| 49 | + |
| 50 | + // Check if InfluxDB connection is successful before going to detached mode |
| 51 | + err := influx.InitInfluxConnection(cmd) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("Failed to establish successful database connection: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // If detached state is requested, filter out corresponding flags and start new process |
| 57 | + // returning with same arguments printing its PID. Then close the initial process |
| 58 | + if d, _ := cmd.Flags().GetBool("detached"); d { |
| 59 | + newArgs := make([]string, 0, len(os.Args)-2) |
| 60 | + for _, a := range os.Args[1:] { |
| 61 | + if a == "-d" || a == "--detached" { |
| 62 | + continue |
| 63 | + } |
| 64 | + newArgs = append(newArgs, a) |
| 65 | + } |
| 66 | + |
| 67 | + command := exec.Command(os.Args[0], newArgs...) |
| 68 | + if err := command.Start(); err != nil { |
| 69 | + return fmt.Errorf("Failed to start a detached process: %w", err) |
| 70 | + } |
| 71 | + pid := command.Process.Pid |
| 72 | + fmt.Printf("[PID]\t%d\n", pid) |
| 73 | + os.Exit(0) |
| 74 | + } |
| 75 | + |
| 76 | + // catcher of SIGINT SIGTERM signals |
| 77 | + go func() { |
| 78 | + c := make(chan os.Signal, 1) |
| 79 | + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) |
| 80 | + sig := <-c |
| 81 | + l.Infof("Received signal %v. Stopping application...\n", sig) |
| 82 | + cancel() |
| 83 | + }() |
| 84 | + |
| 85 | + l.Infoln("Starting application...") |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// rootCmd represents the base command when called without any subcommands |
| 91 | +var rootCmd = &cobra.Command{ |
| 92 | + Use: "x2i [path/to/results/dir]", |
| 93 | + Example: "x2i [path]/target/gatling -i gatling -d -b gatling\nx2i [path]/target/jmeter/results -i jmeter -d -b jmeter\nx2i [path] -i k6 -d -b k6", |
| 94 | + Short: "\nWrite Gatling, JMeter or K6 logs directly to InfluxDB.\n\nMore info at https://github.com/perfana/x2i", |
| 95 | + Version: "v1.0.0", |
| 96 | + PreRunE: preRunSetup, |
| 97 | + Args: cobra.ExactArgs(1), |
| 98 | + Run: func(cmd *cobra.Command, args []string) { |
| 99 | + i, _ := cmd.Flags().GetString("testtool") |
| 100 | + if i == "jmeter" { |
| 101 | + jmeterparser.RunMain(cmd, args[0]) |
| 102 | + } else if i == "k6" { |
| 103 | + k6parser.RunMain(cmd, args[0]) |
| 104 | + } else { |
| 105 | + gatlingparser.RunMain(cmd, args[0]) |
| 106 | + } |
| 107 | + }, |
| 108 | +} |
| 109 | + |
| 110 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 111 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 112 | +func Execute() { |
| 113 | + // Initiating logger before any other processes start |
| 114 | + logPath, _ := rootCmd.Flags().GetString("log") |
| 115 | + err := l.InitLogger(logPath) |
| 116 | + if err != nil { |
| 117 | + log.Fatalf("Failed to init application logger: %v\n", err) |
| 118 | + } |
| 119 | + |
| 120 | + if err := rootCmd.ExecuteContext(ctx); err != nil { |
| 121 | + l.Errorln(err) |
| 122 | + os.Exit(1) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func init() { |
| 127 | + rootCmd.Flags().BoolP("help", "h", false, "Display this help for x2i application") |
| 128 | + rootCmd.Flags().BoolP("version", "v", false, "Display current x2i application version") |
| 129 | + rootCmd.Flags().BoolP("detached", "d", false, "Run application in background. Returns [PID] on start") |
| 130 | + rootCmd.Flags().StringP("address", "a", "http://localhost:8086", "HTTP address and port of InfluxDB instance") |
| 131 | + rootCmd.Flags().StringP("username", "u", "", "Username credential for InfluxDB instance") |
| 132 | + rootCmd.Flags().StringP("password", "p", "", "Password credential for InfluxDB instance") |
| 133 | + rootCmd.Flags().StringP("database", "b", "", "Database name in InfluxDB") |
| 134 | + rootCmd.Flags().StringP("testtool", "i", "gatling", "Testtool used, can be gatling, jmeter or k6") |
| 135 | + rootCmd.Flags().StringP("log", "l", "x2i.log", "File path to x2i log file") |
| 136 | + rootCmd.Flags().StringP("test-environment", "t", "", "Test environment identifier") |
| 137 | + rootCmd.Flags().StringP("system-under-test", "y", "", "System under test identifier") |
| 138 | + rootCmd.Flags().UintP("stop-timeout", "s", 120, "Time (seconds) to exit if no new log lines found") |
| 139 | + rootCmd.Flags().UintP("max-batch-size", "m", 1000, "Max points batch size to sent to InfluxDB") |
| 140 | + |
| 141 | + // set up global context |
| 142 | + ctx, cancel = context.WithCancel(context.Background()) |
| 143 | +} |
0 commit comments