Skip to content

Commit a04d7fe

Browse files
authored
add tailing for onprem stats (#1645)
1 parent 09674f0 commit a04d7fe

19 files changed

+91
-62
lines changed

cmd/cmd_instance.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ func instanceListCommand() *cobra.Command {
148148
}
149149

150150
func instanceStatsCommand() *cobra.Command {
151+
var watch bool
151152
var cmdInstanceStats = &cobra.Command{
152153
Use: "stats",
153154
Short: "list instance stats",
154155
Run: instanceStatsCommandHandler,
155156
}
156157
cmdInstanceStats.PersistentFlags().StringP("instance-name", "i", "", "instance name")
158+
cmdInstanceStats.PersistentFlags().BoolVarP(&watch, "watch", "w", false, "watch stats")
157159

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

183+
watch, err := strconv.ParseBool(cmd.Flag("watch").Value.String())
184+
if err != nil {
185+
fmt.Printf(err.Error())
186+
os.Exit(1)
187+
}
188+
181189
c, err := getInstanceCommandDefaultConfig(cmd)
182190
if err != nil {
183191
exitWithError(err.Error())
@@ -188,7 +196,7 @@ func instanceStatsCommandHandler(cmd *cobra.Command, args []string) {
188196
exitForCmd(cmd, err.Error())
189197
}
190198

191-
err = p.InstanceStats(ctx, iname)
199+
err = p.InstanceStats(ctx, iname, watch)
192200
if err != nil {
193201
exitWithError(err.Error())
194202
}

lepton/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Provider interface {
3333

3434
CreateInstance(ctx *Context) error
3535
ListInstances(ctx *Context) error
36-
InstanceStats(ctx *Context, instancename string) error
36+
InstanceStats(ctx *Context, instancename string, watch bool) error
3737
GetInstances(ctx *Context) ([]CloudInstance, error)
3838
GetInstanceByName(ctx *Context, name string) (*CloudInstance, error)
3939
DeleteInstance(ctx *Context, instancename string) error

provider/aws/aws_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ func (p *AWS) PrintInstanceLogs(ctx *lepton.Context, instancename string, watch
587587
}
588588

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

provider/azure/azure_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,6 @@ func (a *Azure) GetInstanceLogs(ctx *lepton.Context, instancename string) (strin
569569
}
570570

571571
// InstanceStats show metrics for instances on azure.
572-
func (a *Azure) InstanceStats(ctx *lepton.Context, instancename string) error {
572+
func (a *Azure) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
573573
return errors.New("currently not avilable")
574574
}

provider/digitalocean/digital_ocean_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (do *DigitalOcean) GetInstanceLogs(ctx *lepton.Context, instancename string
264264
}
265265

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

provider/gcp/gcp_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,6 @@ func (p *GCloud) getLogs(ctx *lepton.Context, instancename string, start int64)
558558
}
559559

560560
// InstanceStats show metrics for instances on gcp.
561-
func (p *GCloud) InstanceStats(ctx *lepton.Context, instancename string) error {
561+
func (p *GCloud) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
562562
return errors.New("currently not avilable")
563563
}

provider/hyperv/hyperv_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,6 @@ func (p *Provider) PrintInstanceLogs(ctx *lepton.Context, instancename string, w
255255
}
256256

257257
// InstanceStats show metrics for instances on hyperv
258-
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
258+
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
259259
return errors.New("currently not avilable")
260260
}

provider/ibm/ibm_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,6 @@ func (v *IBM) GetInstanceLogs(ctx *lepton.Context, instancename string) (string,
277277
}
278278

279279
// InstanceStats show metrics for instances on ibm.
280-
func (v *IBM) InstanceStats(ctx *lepton.Context, instancename string) error {
280+
func (v *IBM) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
281281
return errors.New("currently not avilable")
282282
}

provider/linode/linode_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,6 @@ func (v *Linode) GetInstanceLogs(ctx *lepton.Context, instancename string) (stri
293293
}
294294

295295
// InstanceStats show metrics for instances on Linnode
296-
func (v *Linode) InstanceStats(ctx *lepton.Context, instancename string) error {
296+
func (v *Linode) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
297297
return errors.New("currently not avilable")
298298
}

provider/oci/oci_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,6 @@ func (p *Provider) PrintInstanceLogs(ctx *lepton.Context, instancename string, w
282282
}
283283

284284
// InstanceStats show metrics for instances on provider.
285-
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
285+
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
286286
return errors.New("currently not avilable")
287287
}

provider/onprem/onprem_instance.go

+65-44
Original file line numberDiff line numberDiff line change
@@ -504,31 +504,11 @@ func (p *OnPrem) GetInstances(ctx *lepton.Context) (instances []lepton.CloudInst
504504
return
505505
}
506506

507-
// InstanceStats shows metrics for instance onprem .
508-
func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string) error {
509-
instances, err := p.GetInstances(ctx)
510-
if err != nil {
511-
return err
512-
}
513-
514-
rinstances := []lepton.CloudInstance{}
515-
516-
for i := 0; i < len(instances); i++ {
517-
if iname != "" {
518-
if iname != instances[i].Name {
519-
continue
520-
} else {
521-
rinstances = append(rinstances, instances[i])
522-
}
523-
} else {
524-
rinstances = append(rinstances, instances[i])
525-
}
526-
}
527-
507+
func (p *OnPrem) getInstancesStats(ctx *lepton.Context, rinstances []lepton.CloudInstance) ([]lepton.CloudInstance, error) {
528508
for i := 0; i < len(rinstances); i++ {
529509
instance, err := p.GetMetaInstanceByName(ctx, rinstances[i].Name)
530510
if err != nil {
531-
return err
511+
fmt.Println(err)
532512
}
533513

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

560-
// perhaps this could be a new type
561-
if ctx.Config().RunConfig.JSON {
562-
if len(rinstances) == 0 {
563-
fmt.Println("[]")
564-
return nil
540+
return rinstances, nil
541+
}
542+
543+
// InstanceStats shows metrics for instance onprem .
544+
func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string, watch bool) error {
545+
instances, err := p.GetInstances(ctx)
546+
if err != nil {
547+
return err
548+
}
549+
550+
rinstances := []lepton.CloudInstance{}
551+
552+
for i := 0; i < len(instances); i++ {
553+
if iname != "" {
554+
if iname != instances[i].Name {
555+
continue
556+
} else {
557+
rinstances = append(rinstances, instances[i])
558+
}
559+
} else {
560+
rinstances = append(rinstances, instances[i])
565561
}
566-
return json.NewEncoder(os.Stdout).Encode(rinstances)
567562
}
568563

569-
table := tablewriter.NewWriter(os.Stdout)
570-
table.SetHeader([]string{"PID", "Name", "Memory"})
571-
table.SetHeaderColor(
572-
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
573-
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
574-
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
564+
// FIXME: if we're watching no need to establish a connection
565+
// everytime
566+
if watch {
567+
for {
568+
rinstances, err = p.getInstancesStats(ctx, rinstances)
569+
if err != nil {
570+
fmt.Println(err)
571+
}
575572

576-
table.SetRowLine(true)
573+
json.NewEncoder(os.Stdout).Encode(rinstances)
574+
time.Sleep(500 * time.Millisecond)
575+
}
576+
} else {
577+
rinstances, err = p.getInstancesStats(ctx, rinstances)
578+
if err != nil {
579+
fmt.Println(err)
580+
}
577581

578-
for _, i := range rinstances {
579-
var rows []string
582+
if ctx.Config().RunConfig.JSON {
583+
if len(rinstances) == 0 {
584+
fmt.Println("[]")
585+
return nil
586+
}
587+
return json.NewEncoder(os.Stdout).Encode(rinstances)
588+
}
580589

581-
rows = append(rows, i.ID)
582-
rows = append(rows, i.Name)
583-
rows = append(rows, i.HumanMem())
590+
table := tablewriter.NewWriter(os.Stdout)
591+
table.SetHeader([]string{"PID", "Name", "Memory"})
592+
table.SetHeaderColor(
593+
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
594+
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
595+
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
584596

585-
table.Append(rows)
586-
}
597+
table.SetRowLine(true)
587598

588-
table.Render()
599+
for _, i := range rinstances {
600+
var rows []string
589601

590-
return nil
602+
rows = append(rows, i.ID)
603+
rows = append(rows, i.Name)
604+
rows = append(rows, i.HumanMem())
591605

606+
table.Append(rows)
607+
}
608+
609+
table.Render()
610+
611+
return nil
612+
}
592613
}
593614

594615
// ListInstances on premise

provider/openshift/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ func (oc *OpenShift) DetachVolume(ctx *lepton.Context, instanceName, volumeName
163163
}
164164

165165
// InstanceStats show metrics for instances on openshift.
166-
func (oc *OpenShift) InstanceStats(ctx *lepton.Context, instancename string) error {
166+
func (oc *OpenShift) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
167167
return errors.New("currently not avilable")
168168
}

provider/openstack/openstack_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,6 @@ func (o *OpenStack) GetInstanceLogs(ctx *lepton.Context, instancename string) (s
394394
}
395395

396396
// InstanceStats show metrics for instances on openstack
397-
func (o *OpenStack) InstanceStats(ctx *lepton.Context, instancename string) error {
397+
func (o *OpenStack) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
398398
return errors.New("currently not avilable")
399399
}

provider/proxmox/proxmox_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,6 @@ func (p *ProxMox) GetInstanceLogs(ctx *lepton.Context, instancename string) (str
578578
}
579579

580580
// InstanceStats show metrics for instances on proxmox
581-
func (p *ProxMox) InstanceStats(ctx *lepton.Context, instancename string) error {
581+
func (p *ProxMox) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
582582
return errors.New("currently not avilable")
583583
}

provider/relayered/relayered_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,6 @@ func (v *Relayered) GetInstanceLogs(ctx *lepton.Context, instancename string) (s
240240
}
241241

242242
// InstanceStats show metrics for instances on relayered
243-
func (v *Relayered) InstanceStats(ctx *lepton.Context, instancename string) error {
243+
func (v *Relayered) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
244244
return errors.New("currently not avilable")
245245
}

provider/upcloud/upcloud_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,6 @@ func (p *Provider) waitForServerState(uuid, state string) (err error) {
363363
}
364364

365365
// InstanceStats show metrics for instances on Provider.
366-
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
366+
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
367367
return errors.New("currently not avilable")
368368
}

provider/vbox/vbox_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,6 @@ func findOrCreateVmsDir() (string, error) {
296296
}
297297

298298
// InstanceStats show metrics for instances on Provider
299-
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string) error {
299+
func (p *Provider) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
300300
return errors.New("currently not avilable")
301301
}

provider/vsphere/vsphere_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,6 @@ func (v *Vsphere) GetInstanceLogs(ctx *lepton.Context, instancename string) (str
605605
}
606606

607607
// InstanceStats show metrics for instances on vsphere.
608-
func (v *Vsphere) InstanceStats(ctx *lepton.Context, instancename string) error {
608+
func (v *Vsphere) InstanceStats(ctx *lepton.Context, instancename string, watch bool) error {
609609
return errors.New("currently not avilable")
610610
}

provider/vultr/vultr_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,6 @@ func (v *Vultr) GetInstanceLogs(ctx *lepton.Context, instancename string) (strin
253253
}
254254

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

0 commit comments

Comments
 (0)