Skip to content

Commit

Permalink
add tailing for onprem stats (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyberg authored Aug 1, 2024
1 parent 09674f0 commit a04d7fe
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 62 deletions.
10 changes: 9 additions & 1 deletion cmd/cmd_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ func instanceListCommand() *cobra.Command {
}

func instanceStatsCommand() *cobra.Command {
var watch bool
var cmdInstanceStats = &cobra.Command{
Use: "stats",
Short: "list instance stats",
Run: instanceStatsCommandHandler,
}
cmdInstanceStats.PersistentFlags().StringP("instance-name", "i", "", "instance name")
cmdInstanceStats.PersistentFlags().BoolVarP(&watch, "watch", "w", false, "watch stats")

return cmdInstanceStats
}
Expand All @@ -178,6 +180,12 @@ func instanceListCommandHandler(cmd *cobra.Command, args []string) {
func instanceStatsCommandHandler(cmd *cobra.Command, args []string) {
iname, _ := cmd.Flags().GetString("instance-name")

watch, err := strconv.ParseBool(cmd.Flag("watch").Value.String())
if err != nil {
fmt.Printf(err.Error())
os.Exit(1)
}

c, err := getInstanceCommandDefaultConfig(cmd)
if err != nil {
exitWithError(err.Error())
Expand All @@ -188,7 +196,7 @@ func instanceStatsCommandHandler(cmd *cobra.Command, args []string) {
exitForCmd(cmd, err.Error())
}

err = p.InstanceStats(ctx, iname)
err = p.InstanceStats(ctx, iname, watch)
if err != nil {
exitWithError(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion lepton/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Provider interface {

CreateInstance(ctx *Context) error
ListInstances(ctx *Context) error
InstanceStats(ctx *Context, instancename string) error
InstanceStats(ctx *Context, instancename string, watch bool) error
GetInstances(ctx *Context) ([]CloudInstance, error)
GetInstanceByName(ctx *Context, name string) (*CloudInstance, error)
DeleteInstance(ctx *Context, instancename string) error
Expand Down
2 changes: 1 addition & 1 deletion provider/aws/aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func (p *AWS) PrintInstanceLogs(ctx *lepton.Context, instancename string, watch
}

// InstanceStats show metrics for instances on aws.
func (p *AWS) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *AWS) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}

Expand Down
2 changes: 1 addition & 1 deletion provider/azure/azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,6 @@ func (a *Azure) GetInstanceLogs(ctx *lepton.Context, instancename string) (strin
}

// InstanceStats show metrics for instances on azure.
func (a *Azure) InstanceStats(ctx *lepton.Context, instancename string) error {
func (a *Azure) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/digitalocean/digital_ocean_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (do *DigitalOcean) GetInstanceLogs(ctx *lepton.Context, instancename string
}

// InstanceStats show metrics for instances on digitalocean.
func (do *DigitalOcean) InstanceStats(ctx *lepton.Context, instancename string) error {
func (do *DigitalOcean) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}

Expand Down
2 changes: 1 addition & 1 deletion provider/gcp/gcp_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,6 @@ func (p *GCloud) getLogs(ctx *lepton.Context, instancename string, start int64)
}

// InstanceStats show metrics for instances on gcp.
func (p *GCloud) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *GCloud) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/hyperv/hyperv_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,6 @@ func (p *Provider) PrintInstanceLogs(ctx *lepton.Context, instancename string, w
}

// InstanceStats show metrics for instances on hyperv
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/ibm/ibm_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,6 @@ func (v *IBM) GetInstanceLogs(ctx *lepton.Context, instancename string) (string,
}

// InstanceStats show metrics for instances on ibm.
func (v *IBM) InstanceStats(ctx *lepton.Context, instancename string) error {
func (v *IBM) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/linode/linode_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,6 @@ func (v *Linode) GetInstanceLogs(ctx *lepton.Context, instancename string) (stri
}

// InstanceStats show metrics for instances on Linnode
func (v *Linode) InstanceStats(ctx *lepton.Context, instancename string) error {
func (v *Linode) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/oci/oci_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,6 @@ func (p *Provider) PrintInstanceLogs(ctx *lepton.Context, instancename string, w
}

// InstanceStats show metrics for instances on provider.
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
109 changes: 65 additions & 44 deletions provider/onprem/onprem_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,31 +504,11 @@ func (p *OnPrem) GetInstances(ctx *lepton.Context) (instances []lepton.CloudInst
return
}

// InstanceStats shows metrics for instance onprem .
func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string) error {
instances, err := p.GetInstances(ctx)
if err != nil {
return err
}

rinstances := []lepton.CloudInstance{}

for i := 0; i < len(instances); i++ {
if iname != "" {
if iname != instances[i].Name {
continue
} else {
rinstances = append(rinstances, instances[i])
}
} else {
rinstances = append(rinstances, instances[i])
}
}

func (p *OnPrem) getInstancesStats(ctx *lepton.Context, rinstances []lepton.CloudInstance) ([]lepton.CloudInstance, error) {
for i := 0; i < len(rinstances); i++ {
instance, err := p.GetMetaInstanceByName(ctx, rinstances[i].Name)
if err != nil {
return err
fmt.Println(err)
}

last := instance.Mgmt
Expand Down Expand Up @@ -557,38 +537,79 @@ func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string) error {
rinstances[i].TotalMemory = (lr.qmpReturn.Stats.TotalMemory / int64(1000000))
}

// perhaps this could be a new type
if ctx.Config().RunConfig.JSON {
if len(rinstances) == 0 {
fmt.Println("[]")
return nil
return rinstances, nil
}

// InstanceStats shows metrics for instance onprem .
func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string, watch bool) error {
instances, err := p.GetInstances(ctx)
if err != nil {
return err
}

rinstances := []lepton.CloudInstance{}

for i := 0; i < len(instances); i++ {
if iname != "" {
if iname != instances[i].Name {
continue
} else {
rinstances = append(rinstances, instances[i])
}
} else {
rinstances = append(rinstances, instances[i])
}
return json.NewEncoder(os.Stdout).Encode(rinstances)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PID", "Name", "Memory"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
// FIXME: if we're watching no need to establish a connection
// everytime
if watch {
for {
rinstances, err = p.getInstancesStats(ctx, rinstances)
if err != nil {
fmt.Println(err)
}

table.SetRowLine(true)
json.NewEncoder(os.Stdout).Encode(rinstances)
time.Sleep(500 * time.Millisecond)
}
} else {
rinstances, err = p.getInstancesStats(ctx, rinstances)
if err != nil {
fmt.Println(err)
}

for _, i := range rinstances {
var rows []string
if ctx.Config().RunConfig.JSON {
if len(rinstances) == 0 {
fmt.Println("[]")
return nil
}
return json.NewEncoder(os.Stdout).Encode(rinstances)
}

rows = append(rows, i.ID)
rows = append(rows, i.Name)
rows = append(rows, i.HumanMem())
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PID", "Name", "Memory"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})

table.Append(rows)
}
table.SetRowLine(true)

table.Render()
for _, i := range rinstances {
var rows []string

return nil
rows = append(rows, i.ID)
rows = append(rows, i.Name)
rows = append(rows, i.HumanMem())

table.Append(rows)
}

table.Render()

return nil
}
}

// ListInstances on premise
Expand Down
2 changes: 1 addition & 1 deletion provider/openshift/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ func (oc *OpenShift) DetachVolume(ctx *lepton.Context, instanceName, volumeName
}

// InstanceStats show metrics for instances on openshift.
func (oc *OpenShift) InstanceStats(ctx *lepton.Context, instancename string) error {
func (oc *OpenShift) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/openstack/openstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,6 @@ func (o *OpenStack) GetInstanceLogs(ctx *lepton.Context, instancename string) (s
}

// InstanceStats show metrics for instances on openstack
func (o *OpenStack) InstanceStats(ctx *lepton.Context, instancename string) error {
func (o *OpenStack) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/proxmox/proxmox_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,6 @@ func (p *ProxMox) GetInstanceLogs(ctx *lepton.Context, instancename string) (str
}

// InstanceStats show metrics for instances on proxmox
func (p *ProxMox) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *ProxMox) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/relayered/relayered_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,6 @@ func (v *Relayered) GetInstanceLogs(ctx *lepton.Context, instancename string) (s
}

// InstanceStats show metrics for instances on relayered
func (v *Relayered) InstanceStats(ctx *lepton.Context, instancename string) error {
func (v *Relayered) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/upcloud/upcloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,6 @@ func (p *Provider) waitForServerState(uuid, state string) (err error) {
}

// InstanceStats show metrics for instances on Provider.
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/vbox/vbox_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,6 @@ func findOrCreateVmsDir() (string, error) {
}

// InstanceStats show metrics for instances on Provider
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/vsphere/vsphere_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,6 @@ func (v *Vsphere) GetInstanceLogs(ctx *lepton.Context, instancename string) (str
}

// InstanceStats show metrics for instances on vsphere.
func (v *Vsphere) InstanceStats(ctx *lepton.Context, instancename string) error {
func (v *Vsphere) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}
2 changes: 1 addition & 1 deletion provider/vultr/vultr_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,6 @@ func (v *Vultr) GetInstanceLogs(ctx *lepton.Context, instancename string) (strin
}

// InstanceStats show metrics for instances on vultr.
func (v *Vultr) InstanceStats(ctx *lepton.Context, instancename string) error {
func (v *Vultr) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("currently not avilable")
}

0 comments on commit a04d7fe

Please sign in to comment.