Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implemented deployment status check #88

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions cmd/world/forge/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"

"github.com/rotisserie/eris"

"pkg.world.dev/world-cli/common/globalconfig"
)

var statusFailRegEx = regexp.MustCompile(`[^a-zA-Z0-9\. ]+`)

// Deploy a project
func deploy(ctx context.Context) error {
globalConfig, err := globalconfig.GetGlobalConfig()
Expand Down Expand Up @@ -128,3 +133,130 @@

return nil
}

func status(ctx context.Context) error {
globalConfig, err := globalconfig.GetGlobalConfig()
if err != nil {
return eris.Wrap(err, "Failed to get global config")
}
projectID := globalConfig.ProjectID
if projectID == "" {
printNoSelectedProject()
return nil
}
// Get project details
prj, err := getSelectedProject(ctx)
if err != nil {
return eris.Wrap(err, "Failed to get project details")
}

statusURL := fmt.Sprintf("%s/api/deployment/%s", baseURL, projectID)
result, err := sendRequest(ctx, http.MethodGet, statusURL, nil)
if err != nil {
return eris.Wrap(err, "Failed to get deployment status")
}
var response map[string]any
err = json.Unmarshal(result, &response)
if err != nil {
return eris.Wrap(err, "Failed to unmarshal deployment status")
}
var data map[string]any
if response["data"] != nil {
data = response["data"].(map[string]any)

Check failure on line 165 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
}
ezavada marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Deployment Status")
fmt.Println("-----------------")
fmt.Printf("Project: %s\n", prj.Name)
fmt.Printf("Project Slug: %s\n", prj.Slug)
fmt.Printf("Repository: %s\n", prj.RepoURL)
if data == nil {
fmt.Printf("\n** Project has not been deployed **\n")
return nil
}
if data["project_id"] != projectID {
return eris.Errorf("Deployment status does not match project id %s", projectID)
}
if data["type"] != "deploy" {
ezavada marked this conversation as resolved.
Show resolved Hide resolved
return eris.Errorf("Deployment status does not match type %s", data["type"])
}
executorID := data["executor_id"].(string)

Check failure on line 182 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
dt, dte := time.Parse(time.RFC3339, data["execution_time"].(string))
if dte != nil {
return eris.Wrapf(dte, "Failed to parse execution time %s", dt)
}
buildNumber := int(data["build_number"].(float64))
bt, bte := time.Parse(time.RFC3339, data["build_start_time"].(string))
ezavada marked this conversation as resolved.
Show resolved Hide resolved
if bte != nil {
return eris.Wrapf(bte, "Failed to parse build start time %s", bt)
}
ezavada marked this conversation as resolved.
Show resolved Hide resolved
buildState := data["build_state"].(string)

Check failure on line 192 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
if buildState != "finished" {
ezavada marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Build: #%d started %s by %s - %s\n", buildNumber, dt.Format(time.RFC822), executorID, buildState)
return nil
}
fmt.Printf("Build: #%d on %s by %s\n", buildNumber, dt.Format(time.RFC822), executorID)
fmt.Print("Health: ")

// fmt.Println()
// fmt.Println(string(result))

healthURL := fmt.Sprintf("%s/api/health/%s", baseURL, projectID)
result, err = sendRequest(ctx, http.MethodGet, healthURL, nil)
if err != nil {
return eris.Wrap(err, "Failed to get health")
}
err = json.Unmarshal(result, &response)
if err != nil {
return eris.Wrap(err, "Failed to unmarshal deployment status")
}
instances := response["data"].([]any)

Check failure on line 212 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
if len(instances) == 0 {
fmt.Println("** No deployed instances found **")
return nil
}
fmt.Printf("(%d deployed instances)\n", len(instances))
currRegion := ""
for _, instance := range instances {
info := instance.(map[string]any)

Check failure on line 220 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
region := info["region"].(string)

Check failure on line 221 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
instanceNum := int(info["instance"].(float64))
cardinalInfo := info["cardinal"].(map[string]any)

Check failure on line 223 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
nakamaInfo := info["nakama"].(map[string]any)

Check failure on line 224 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
cardinalURL := cardinalInfo["url"].(string)

Check failure on line 225 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
cardinalHost := strings.Split(cardinalURL, "/")[2]
cardinalOK := cardinalInfo["ok"].(bool)

Check failure on line 227 in cmd/world/forge/deployment.go

View workflow job for this annotation

GitHub Actions / Go

Error return value is not checked (errcheck)
cardinalResultCode := int(cardinalInfo["result_code"].(float64))
cardinalResultStr := cardinalInfo["result_str"].(string)
nakamaURL := nakamaInfo["url"].(string)
nakamaHost := strings.Split(nakamaURL, "/")[2]
nakamaOK := nakamaInfo["ok"].(bool)
nakamaResultCode := int(nakamaInfo["result_code"].(float64))
nakamaResultStr := nakamaInfo["result_str"].(string)

ezavada marked this conversation as resolved.
Show resolved Hide resolved
if region != currRegion {
currRegion = region
fmt.Printf("• %s\n", currRegion)
}
fmt.Printf(" %d)", instanceNum)
fmt.Printf("\tCardinal: %s - ", cardinalHost)
if cardinalOK {
fmt.Print("OK\n")
} else if cardinalResultCode == 0 {
fmt.Printf("FAIL %s\n", statusFailRegEx.ReplaceAllString(cardinalResultStr, ""))
} else {
fmt.Printf("FAIL %d %s\n", cardinalResultCode, statusFailRegEx.ReplaceAllString(cardinalResultStr, ""))
}
fmt.Printf("\tNakama: %s - ", nakamaHost)
if nakamaOK {
fmt.Print("OK\n")
} else if nakamaResultCode == 0 {
fmt.Printf("FAIL %s\n", statusFailRegEx.ReplaceAllString(nakamaResultStr, ""))
} else {
fmt.Printf("FAIL %d %s\n", nakamaResultCode, statusFailRegEx.ReplaceAllString(nakamaResultStr, ""))
}
}
//fmt.Println()
//fmt.Println(string(result))

return nil
}
14 changes: 13 additions & 1 deletion cmd/world/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
// For local development
worldForgeBaseURLLocal = "http://localhost:8081"
worldForgeBaseURLLocal = "http://localhost:8001"
ezavada marked this conversation as resolved.
Show resolved Hide resolved

// For production
worldForgeBaseURLProd = "https://forge.world.dev"
Expand Down Expand Up @@ -173,6 +173,17 @@ var (
return destroy(cmd.Context())
},
}

statusCmd = &cobra.Command{
Use: "status",
Short: "Show status of a project",
RunE: func(cmd *cobra.Command, _ []string) error {
if !checkLogin() {
return nil
}
return status(cmd.Context())
},
}
)

func init() {
Expand Down Expand Up @@ -206,5 +217,6 @@ func init() {
// Add deployment commands
deploymentCmd.AddCommand(deployCmd)
deploymentCmd.AddCommand(destroyCmd)
deploymentCmd.AddCommand(statusCmd)
BaseCmd.AddCommand(deploymentCmd)
}
Loading