Skip to content

Commit

Permalink
Merge pull request #112 from trickest/feat/run-ip-addresses
Browse files Browse the repository at this point in the history
Include machine IP addresses in the JSON output of the get command
  • Loading branch information
mhmdiaa authored Feb 29, 2024
2 parents 816d325 + 8fb49a9 commit 9752fd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 1 addition & 3 deletions cmd/execute/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]o
out := ""
out += fmt.Sprintf(fmtStr, "Name:", run.WorkflowName)
out += fmt.Sprintf(fmtStr, "Status:", strings.ToLower(run.Status))
availableMachines := GetAvailableMachines(fleetName)
out += fmt.Sprintf(fmtStr, "Machines:", FormatMachines(*machines, true)+
" (currently available: "+FormatMachines(availableMachines, true)+")")
out += fmt.Sprintf(fmtStr, "Machines:", FormatMachines(*machines, true))
out += fmt.Sprintf(fmtStr, "Created:", run.CreatedDate.In(time.Local).Format(time.RFC1123)+
" ("+util.FormatDuration(time.Since(run.CreatedDate))+" ago)")
if run.Status != "PENDING" {
Expand Down
33 changes: 29 additions & 4 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package get
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/trickest/trickest-cli/client/request"
"github.com/trickest/trickest-cli/cmd/execute"
"github.com/trickest/trickest-cli/cmd/output"
"github.com/trickest/trickest-cli/types"
Expand Down Expand Up @@ -57,19 +59,27 @@ var GetCmd = &cobra.Command{
runs = []types.Run{*run}
}
if len(runs) > 0 && (runs[0].Status == "RUNNING" || runs[0].Status == "COMPLETED") {
if runs[0].Status == "COMPLETED" && runs[0].CompletedDate.IsZero() {
runs[0].Status = "RUNNING"
run := runs[0]
if run.Status == "COMPLETED" && run.CompletedDate.IsZero() {
run.Status = "RUNNING"
}

ipAddresses, err := getRunIPAddresses(*run.ID)
if err != nil {
fmt.Printf("Warning: Couldn't get the run IP addresses: %s", err)
}
run.IPAddresses = ipAddresses

if jsonOutput {
data, err := json.Marshal(runs[0])
data, err := json.Marshal(run)
if err != nil {
fmt.Println("Error marshalling project data")
return
}
output := string(data)
fmt.Println(output)
} else {
execute.WatchRun(*runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Machines, showNodeParams)
execute.WatchRun(*run.ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Machines, showNodeParams)
}
return
} else {
Expand Down Expand Up @@ -105,3 +115,18 @@ func init() {
GetCmd.Flags().StringVar(&runID, "run", "", "Get the status of a specific run")
GetCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}

func getRunIPAddresses(runID uuid.UUID) ([]string, error) {
resp := request.Trickest.Get().DoF("execution/%s/ips/", runID)
if resp == nil || resp.Status() != http.StatusOK {
return nil, fmt.Errorf("unexpected response status code: %d", resp.Status())
}

var ipAddresses []string
err := json.Unmarshal(resp.Body(), &ipAddresses)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal IP addresses response: %s", err)
}

return ipAddresses, nil
}
1 change: 1 addition & 0 deletions types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type Run struct {
Finished bool `json:"finished,omitempty"`
Author string `json:"author,omitempty"`
Fleet *uuid.UUID `json:"fleet,omitempty"`
IPAddresses []string `json:"ip_addresses,omitempty"`
}

type Machines struct {
Expand Down

0 comments on commit 9752fd3

Please sign in to comment.