Created
January 11, 2024 19:58
-
-
Save sneal/97b614f31d2b156fa4ad7e9c3202b8ab to your computer and use it in GitHub Desktop.
CF apps running each diego cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/cloudfoundry-community/go-cfclient/v3/client" | |
"github.com/cloudfoundry-community/go-cfclient/v3/config" | |
"github.com/cloudfoundry-community/go-cfclient/v3/resource" | |
"os" | |
) | |
type HostToAppsDetail struct { | |
Host string | |
Apps []string | |
} | |
var results map[string]*HostToAppsDetail | |
func main() { | |
err := execute() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
fmt.Println("Done!") | |
} | |
func execute() error { | |
// use the CF CLI config and the stored access/refresh token | |
cfg, err := config.NewFromCFHome() | |
if err != nil { | |
return err | |
} | |
cf, err := client.New(cfg) | |
if err != nil { | |
return err | |
} | |
results = make(map[string]*HostToAppsDetail) | |
err = listApps(context.Background(), cf) | |
if err != nil { | |
return err | |
} | |
return displayResults() | |
} | |
func listApps(ctx context.Context, cf *client.Client) error { | |
opts := client.NewAppListOptions() | |
for { | |
apps, pager, err := cf.Applications.List(ctx, opts) | |
if err != nil { | |
return err | |
} | |
for _, app := range apps { | |
err := listProcessStats(ctx, cf, app) | |
if err != nil { | |
return err | |
} | |
} | |
if !pager.HasNextPage() { | |
break | |
} | |
pager.NextPage(opts) | |
} | |
return nil | |
} | |
func listProcessStats(ctx context.Context, cf *client.Client, app *resource.App) error { | |
stats, err := cf.Processes.GetStatsForApp(ctx, app.GUID, "web") | |
if err != nil { | |
return err | |
} | |
host := stats.Stats[0].Host | |
d, ok := results[host] | |
if !ok { | |
d = &HostToAppsDetail{ | |
Host: host, | |
Apps: []string{}, | |
} | |
results[host] = d | |
} | |
d.Apps = append(d.Apps, app.Name) | |
return nil | |
} | |
func displayResults() error { | |
for _, d := range results { | |
fmt.Printf("Host: %s\n", d.Host) | |
for _, a := range d.Apps { | |
fmt.Printf(" - %s\n", a) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment