Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qclaogui committed Jan 17, 2024
1 parent 4603bc4 commit 9faec40
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 62 deletions.
48 changes: 48 additions & 0 deletions internal/cmd/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <[email protected]>
//
// Licensed under the Apache License 2.0.

package all

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode"}
var mode string

func NewCmdAll() *cobra.Command {
var allCmd = &cobra.Command{
Short: "Run Grafana LGTMP Stack All-in-one.",
Use: "all",
Example: heredoc.Doc(`
# Start up all in monolithic-mode
$ lgtmp up all
# Start up all in microservices-mode
$ lgtmp up all --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-all-in-one Run monolithic-mode all-in-one
// deploy-monolithic-mode-all-in-one Deploy monolithic-mode all-in-one
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-all-in-one", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

allCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for all-in-one. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return allCmd
}
10 changes: 5 additions & 5 deletions internal/cmd/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)
Expand All @@ -29,12 +30,11 @@ func NewCmdLogs() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// TODO(qc)
// up-monolithic-mode-logs Run monolithic-mode logs
// deploy-monolithic-mode-logs Deploy monolithic-mode logs
action := cmd.Parent().Use
fullCmd := fmt.Sprintf("%s-%s-logs", action, mode)
fmt.Printf("🔥\x1b[91m make %s \x1b[39m\n", fullCmd)

if err := cmd.Help(); err != nil {
target := fmt.Sprintf("%s-%s-logs", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
Expand Down
44 changes: 4 additions & 40 deletions internal/cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ package metrics

import (
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)
Expand All @@ -33,46 +30,13 @@ func NewCmdMetrics() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-metrics Run monolithic-mode metrics
// deploy-monolithic-mode-metrics Deploy monolithic-mode metrics
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-metrics", action, mode)

command := exec.Command("make", "-C", ".", target)
command.Stderr = os.Stderr
command.Stdout = os.Stdout
err := command.Start()
if err != nil {
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
if action == "down" || action == "delete" {
err := command.Wait()
if err != nil {
return err
}
} else {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

done := make(chan error, 1)
go func() { done <- command.Wait() }()

select {
case <-signalChan:
fmt.Printf("Received interrupt signal, stopping %s...\n", target)
_ = command.Process.Signal(os.Interrupt)
select {
case <-signalChan:
fmt.Printf("Forcefully stopping %s...\n", target)
_ = command.Process.Kill()
os.Exit(1) // Exit with a status code of 1
case <-done:
os.Exit(0) // Exit with a status code of 0
}
case err := <-done:
if err != nil {
os.Exit(1) // Exit with a status code of 1 upon failure
}
}
}
return nil
},
}
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/profiles/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <[email protected]>
//
// Licensed under the Apache License 2.0.

package profiles

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdProfiles() *cobra.Command {
var profilesCmd = &cobra.Command{
Short: "Run Pyroscope for Profiles.",
Use: "profiles",
Example: heredoc.Doc(`
# Start up profiles in monolithic-mode
$ lgtmp up profiles
# Start up profiles in microservices-mode
$ lgtmp up profiles --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-profiles Run monolithic-mode profiles
// deploy-monolithic-mode-profiles Deploy monolithic-mode profiles
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-profiles", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

profilesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for profiles. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return profilesCmd
}
48 changes: 48 additions & 0 deletions internal/cmd/traces/traces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <[email protected]>
//
// Licensed under the Apache License 2.0.

package traces

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdTraces() *cobra.Command {
var tracesCmd = &cobra.Command{
Short: "Run Tempo for Traces.",
Use: "traces",
Example: heredoc.Doc(`
# Start up traces in monolithic-mode
$ lgtmp up traces
# Start up traces in microservices-mode
$ lgtmp up traces --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-traces Run monolithic-mode traces
// deploy-monolithic-mode-traces Deploy monolithic-mode traces
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-traces", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

tracesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for traces. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return tracesCmd
}
61 changes: 61 additions & 0 deletions internal/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © Weifeng Wang <[email protected]>
//
// Licensed under the Apache License 2.0.

package internal

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)

func ExecuteCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
var stderr bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
log.Fatalf("Error: %s\n", stderr.String())
}

// make target is the last argment
target := args[len(args)-1]
if strings.HasPrefix(target, "down-") || strings.HasPrefix(target, "delete-") {
err := cmd.Wait()
if err != nil {
return err
}
} else {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

done := make(chan error, 1)
go func() { done <- cmd.Wait() }()

select {
case <-signalChan:
fmt.Printf("Received interrupt signal, stopping %s...\n", target)
_ = cmd.Process.Signal(os.Interrupt)
select {
case <-signalChan:
fmt.Printf("Force stopping %s...\n", target)
_ = cmd.Process.Kill()
os.Exit(1) // Exit with a status code of 1
case <-done:
os.Exit(0) // Exit with a status code of 0
}
case err := <-done:
if err != nil {
os.Exit(1) // Exit with a status code of 1 upon failure
}
}
}

return nil
}
10 changes: 8 additions & 2 deletions lgtmp/cmd/delete/delete.go → lgtmp/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
//
// Licensed under the Apache License 2.0.

package delete
package cmd

import (
"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal/cmd/all"
"github.com/qclaogui/codelab-monitoring/internal/cmd/logs"
"github.com/qclaogui/codelab-monitoring/internal/cmd/metrics"
"github.com/qclaogui/codelab-monitoring/internal/cmd/profiles"
"github.com/qclaogui/codelab-monitoring/internal/cmd/traces"

"github.com/spf13/cobra"
)
Expand All @@ -23,8 +26,11 @@ func NewCmdDelete() *cobra.Command {
`),
}

cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(all.NewCmdAll())
cmd.AddCommand(logs.NewCmdLogs())
cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(profiles.NewCmdProfiles())
cmd.AddCommand(traces.NewCmdTraces())

return cmd
}
10 changes: 8 additions & 2 deletions lgtmp/cmd/deploy/deploy.go → lgtmp/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
//
// Licensed under the Apache License 2.0.

package deploy
package cmd

import (
"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal/cmd/all"
"github.com/qclaogui/codelab-monitoring/internal/cmd/logs"
"github.com/qclaogui/codelab-monitoring/internal/cmd/metrics"
"github.com/qclaogui/codelab-monitoring/internal/cmd/profiles"
"github.com/qclaogui/codelab-monitoring/internal/cmd/traces"

"github.com/spf13/cobra"
)
Expand All @@ -23,8 +26,11 @@ func NewCmdDeploy() *cobra.Command {
`),
}

cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(all.NewCmdAll())
cmd.AddCommand(logs.NewCmdLogs())
cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(profiles.NewCmdProfiles())
cmd.AddCommand(traces.NewCmdTraces())

return cmd
}
10 changes: 8 additions & 2 deletions lgtmp/cmd/down/down.go → lgtmp/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
//
// Licensed under the Apache License 2.0.

package down
package cmd

import (
"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal/cmd/all"
"github.com/qclaogui/codelab-monitoring/internal/cmd/logs"
"github.com/qclaogui/codelab-monitoring/internal/cmd/metrics"
"github.com/qclaogui/codelab-monitoring/internal/cmd/profiles"
"github.com/qclaogui/codelab-monitoring/internal/cmd/traces"

"github.com/spf13/cobra"
)
Expand All @@ -23,8 +26,11 @@ func NewCmdDown() *cobra.Command {
`),
}

cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(all.NewCmdAll())
cmd.AddCommand(logs.NewCmdLogs())
cmd.AddCommand(metrics.NewCmdMetrics())
cmd.AddCommand(profiles.NewCmdProfiles())
cmd.AddCommand(traces.NewCmdTraces())

return cmd
}
Loading

0 comments on commit 9faec40

Please sign in to comment.