Skip to content

Commit

Permalink
Merge pull request #262 from tobigiwa/tobytobias#221
Browse files Browse the repository at this point in the history
Resolves #221, show currently running stacks.
  • Loading branch information
nguyer committed Jan 12, 2024
2 parents b041c90 + e7623ee commit 311edc5
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
74 changes: 74 additions & 0 deletions cmd/ps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright © 2023 Giwa Oluwatobi <giwaoluwatobi@gmial.com>
*/
package cmd

import (
"context"
"fmt"
"strings"

"github.com/hyperledger/firefly-cli/internal/log"
"github.com/hyperledger/firefly-cli/internal/stacks"
"github.com/spf13/cobra"
)

// psCmd represents the ps command
var psCmd = &cobra.Command{
Use: "ps [a stack name]...",
Short: "Returns information on running stacks",
Long: `ps returns currently running stacks on your local machine.
It also takes a continuous list of whitespace optional arguement - stack name.`,
Aliases: []string{"process"},
RunE: func(cmd *cobra.Command, args []string) error {

ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

allStacks, err := stacks.ListStacks()
if err != nil {
return err
}

if len(args) > 0 {
namedStacks := make([]string, 0, len(args))
for _, stackName := range args {
if contains(allStacks, strings.TrimSpace(stackName)) {
namedStacks = append(namedStacks, stackName)
} else {
fmt.Printf("stack name - %s, is not present on your local machine. Run `ff ls` to see all available stacks.\n", stackName)
}
}

allStacks = namedStacks // replace only the user specified stacks in the slice instead.
}

stackManager := stacks.NewStackManager(ctx)
for _, stackName := range allStacks {
if err := stackManager.LoadStack(stackName); err != nil {
return err
}

if err := stackManager.IsRunning(); err != nil {
return err
}
}
return nil
},
}

func init() {
rootCmd.AddCommand(psCmd)
}

// contains can be removed if the go mod version is bumped to version Go 1.18
// and replaced with slices.Contains().
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
6 changes: 6 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func RunDockerCommandBuffered(ctx context.Context, workingDir string, command ..
return runCommand(ctx, dockerCmd)
}

func RunDockerComposeCommandReturnsStdout(workingDir string, command ...string) ([]byte, error) {
dockerCmd := exec.Command("docker", append([]string{"compose"}, command...)...)
dockerCmd.Dir = workingDir
return dockerCmd.Output()
}

func runCommand(ctx context.Context, cmd *exec.Cmd) (string, error) {
verbose := log.VerbosityFromContext(ctx)
isLogCmd, _ := ctx.Value(CtxIsLogCmdKey{}).(bool)
Expand Down
34 changes: 31 additions & 3 deletions internal/stacks/stack_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"text/tabwriter"
"time"

"github.com/hyperledger/firefly-cli/internal/blockchain"
Expand Down Expand Up @@ -58,6 +60,7 @@ type StackManager struct {
blockchainProvider blockchain.IBlockchainProvider
tokenProviders []tokens.ITokensProvider
IsOldFileStructure bool
once sync.Once
}

func ListStacks() ([]string, error) {
Expand All @@ -66,13 +69,11 @@ func ListStacks() ([]string, error) {
return nil, err
}

stacks := make([]string, 0)
i := 0
stacks := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
if exists, err := CheckExists(f.Name()); err == nil && exists {
stacks = append(stacks, f.Name())
i++
}
}
}
Expand Down Expand Up @@ -1106,6 +1107,33 @@ func (s *StackManager) PrintStackInfo() error {
return nil
}

// IsRunning prints to the stdout, the stack name and it status as "running" or "not_running".
func (s *StackManager) IsRunning() error {
output, err := docker.RunDockerComposeCommandReturnsStdout(s.Stack.StackDir, "ps")
if err != nil {
return err
}

formatHeader := "\n %s\t%s\t "
formatBody := "\n %s\t%s\t"

w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 8, '\t', 0)

s.once.Do(func() {
fmt.Fprintf(w, formatHeader, "STACK", "STATUS")
})

if strings.Contains(string(output), s.Stack.Name) { // if the output contains the stack name, it means the container is running.
fmt.Fprintf(w, formatBody, s.Stack.Name, "running")
} else {
fmt.Fprintf(w, formatBody, s.Stack.Name, "not_running")
}
fmt.Fprintln(w)
w.Flush()
return nil
}

func (s *StackManager) disableFireflyCoreContainers() error {
compose := s.buildDockerCompose()
for _, member := range s.Stack.Members {
Expand Down

0 comments on commit 311edc5

Please sign in to comment.