Skip to content

Commit

Permalink
Separate printing logs and getting logs concerns (closes nanovms#657)
Browse files Browse the repository at this point in the history
* Add safeguards to azure getIstances

* Add safeguard to openstack getInstances
  • Loading branch information
fabioDMFerreira committed Oct 12, 2020
1 parent 40a3a75 commit 49f7a94
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 77 deletions.
2 changes: 1 addition & 1 deletion cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func instanceLogsCommandHandler(cmd *cobra.Command, args []string) {
c.CloudConfig.ProjectID = projectID
c.CloudConfig.Zone = zone
ctx := api.NewContext(&c, &p)
err = p.GetInstanceLogs(ctx, args[0], watch)
err = p.PrintInstanceLogs(ctx, args[0], watch)
if err != nil {
exitWithError(err.Error())
}
Expand Down
19 changes: 14 additions & 5 deletions lepton/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,18 @@ func (p *AWS) DeleteInstance(ctx *Context, instancename string) error {
return nil
}

// PrintInstanceLogs writes instance logs to console
func (p *AWS) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := p.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs
func (p *AWS) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
func (p *AWS) GetInstanceLogs(ctx *Context, instancename string) (string, error) {
svc, err := session.NewSession(&aws.Config{
Region: aws.String(ctx.config.CloudConfig.Zone)},
)
Expand All @@ -695,18 +705,17 @@ func (p *AWS) GetInstanceLogs(ctx *Context, instancename string, watch bool) err
} else {
fmt.Println(err.Error())
}
return err
return "", err
}

data, err := base64.StdEncoding.DecodeString(aws.StringValue(result.Output))
if err != nil {
return err
return "", err
}

l := string(data)
fmt.Printf(l)

return nil
return l, nil
}

func (p *AWS) customizeImage(ctx *Context) (string, error) {
Expand Down
56 changes: 34 additions & 22 deletions lepton/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,28 +538,32 @@ func (a *Azure) GetInstances(ctx *Context) ([]CloudInstance, error) {
privateIP := ""
publicIP := ""

nifs := *((*(*instance.VirtualMachineProperties).NetworkProfile).NetworkInterfaces)
if instance.VirtualMachineProperties != nil {
nifs := *((*(*instance.VirtualMachineProperties).NetworkProfile).NetworkInterfaces)

for i := 0; i < len(nifs); i++ {
nicClient := a.getNicClient()
nic, err := nicClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}

ipconfig := *(*nic.InterfacePropertiesFormat).IPConfigurations
for x := 0; x < len(ipconfig); x++ {
format := *ipconfig[x].InterfaceIPConfigurationPropertiesFormat
privateIP = *format.PrivateIPAddress

ipClient := a.getIPClient()
pubip, err := ipClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
for i := 0; i < len(nifs); i++ {
nicClient := a.getNicClient()
nic, err := nicClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
publicIP = *(*pubip.PublicIPAddressPropertiesFormat).IPAddress
}

if nic.InterfacePropertiesFormat != nil {
ipconfig := *(*nic.InterfacePropertiesFormat).IPConfigurations
for x := 0; x < len(ipconfig); x++ {
format := *ipconfig[x].InterfaceIPConfigurationPropertiesFormat
privateIP = *format.PrivateIPAddress

ipClient := a.getIPClient()
pubip, err := ipClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
publicIP = *(*pubip.PublicIPAddressPropertiesFormat).IPAddress
}
}

}
}
cinstance.PrivateIps = []string{privateIP}
cinstance.PublicIps = []string{publicIP}
Expand Down Expand Up @@ -651,8 +655,18 @@ func (a *Azure) StopInstance(ctx *Context, instancename string) error {
return nil
}

// PrintInstanceLogs writes instance logs to console
func (a *Azure) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := a.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs
func (a *Azure) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
func (a *Azure) GetInstanceLogs(ctx *Context, instancename string) (string, error) {
// this is basically 2 calls
// 1) grab the log location
// 2) grab it from storage
Expand Down Expand Up @@ -700,17 +714,15 @@ func (a *Azure) GetInstanceLogs(ctx *Context, instancename string, watch bool) e

get, err := blobURL.Download(context.TODO(), 0, 0, azblob.BlobAccessConditions{}, false)
if err != nil {
fmt.Println(err)
return "", err
}

downloadedData := &bytes.Buffer{}
reader := get.Body(azblob.RetryReaderOptions{})
downloadedData.ReadFrom(reader)
reader.Close()

fmt.Println(downloadedData.String())

return nil
return downloadedData.String(), nil
}

// ResizeImage is not supported on azure.
Expand Down
14 changes: 12 additions & 2 deletions lepton/digital_ocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,21 @@ func (do *DigitalOcean) StopInstance(ctx *Context, instancename string) error {
return nil
}

// GetInstanceLogs gets instance related logs
func (do *DigitalOcean) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
// PrintInstanceLogs writes instance logs to console
func (do *DigitalOcean) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := do.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs
func (do *DigitalOcean) GetInstanceLogs(ctx *Context, instancename string) (string, error) {
return "", nil
}

func (do *DigitalOcean) customizeImage(ctx *Context) (string, error) {
imagePath := ctx.config.RunConfig.Imagename
return imagePath, nil
Expand Down
38 changes: 23 additions & 15 deletions lepton/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,27 +556,35 @@ func (p *GCloud) StopInstance(ctx *Context, instancename string) error {
return nil
}

// PrintInstanceLogs writes instance logs to console
func (p *GCloud) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := p.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs
func (p *GCloud) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
func (p *GCloud) GetInstanceLogs(ctx *Context, instancename string) (string, error) {
context := context.TODO()

cloudConfig := ctx.config.CloudConfig
lastPos := int64(0)
for {
resp, err := p.Service.Instances.GetSerialPortOutput(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Start(lastPos).Context(context).Do()
if err != nil {
return err
}
if resp.Contents != "" {
fmt.Printf("%s", resp.Contents)
}
if lastPos == resp.Next && !watch {
break
}
lastPos = resp.Next
time.Sleep(time.Second)

resp, err := p.Service.Instances.GetSerialPortOutput(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Start(lastPos).Context(context).Do()
if err != nil {
return "", err
}
return nil
if resp.Contents != "" {
return resp.Contents, nil
}

lastPos = resp.Next
time.Sleep(time.Second)

return "", nil
}

func createArchive(archive string, files []string) error {
Expand Down
16 changes: 12 additions & 4 deletions lepton/onprem.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,26 @@ func (p *OnPrem) DeleteInstance(ctx *Context, instancename string) error {
return nil
}

// PrintInstanceLogs writes instance logs to console
func (p *OnPrem) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := p.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs for onprem instance logs
func (p *OnPrem) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
func (p *OnPrem) GetInstanceLogs(ctx *Context, instancename string) (string, error) {

body, err := ioutil.ReadFile("/tmp/" + instancename + ".log")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(string(body))

return nil
return string(body), nil
}

// Initialize on prem provider
Expand Down
28 changes: 19 additions & 9 deletions lepton/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package lepton
import (
"errors"
"fmt"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
"github.com/olekukonko/tablewriter"
"os"
"strconv"
"strings"
"time"

"github.com/olekukonko/tablewriter"

"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"

"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"

"github.com/gophercloud/gophercloud"
Expand Down Expand Up @@ -545,32 +547,40 @@ func (o *OpenStack) findInstance(name string) (id string, err error) {
return sid, errors.New("could not find server")
}

// PrintInstanceLogs writes instance logs to console
func (o *OpenStack) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := o.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs.
func (o *OpenStack) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
func (o *OpenStack) GetInstanceLogs(ctx *Context, instancename string) (string, error) {

client, err := openstack.NewComputeV2(o.provider, gophercloud.EndpointOpts{
Region: os.Getenv("OS_REGION_NAME"),
})
if err != nil {
fmt.Println(err)
return "", err
}

sid, err := o.findInstance(instancename)
if err != nil {
fmt.Println(err)
return "", err
}

outputOpts := &servers.ShowConsoleOutputOpts{
Length: 100,
}
output, err := servers.ShowConsoleOutput(client, sid, outputOpts).Extract()
if err != nil {
fmt.Println(err)
return "", err
}

fmt.Println(output)

return nil
return output, nil
}

func (o *OpenStack) customizeImage(ctx *Context) (string, error) {
Expand Down
3 changes: 2 additions & 1 deletion lepton/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type Provider interface {
DeleteInstance(ctx *Context, instancename string) error
StopInstance(ctx *Context, instancename string) error
StartInstance(ctx *Context, instancename string) error
GetInstanceLogs(ctx *Context, instancename string, watch bool) error
GetInstanceLogs(ctx *Context, instancename string) (string, error)
PrintInstanceLogs(ctx *Context, instancename string, watch bool) error

VolumeService

Expand Down
33 changes: 18 additions & 15 deletions lepton/vsphere.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lepton

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -746,42 +747,44 @@ func (v *Vsphere) StopInstance(ctx *Context, instancename string) error {
return err
}

// PrintInstanceLogs writes instance logs to console
func (v *Vsphere) PrintInstanceLogs(ctx *Context, instancename string, watch bool) error {
l, err := v.GetInstanceLogs(ctx, instancename)
if err != nil {
return err
}
fmt.Printf(l)
return nil
}

// GetInstanceLogs gets instance related logs.
// govc datastore.tail -n 100 gtest/serial.out
// logs don't appear until you spin up the instance.
func (v *Vsphere) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {

func (v *Vsphere) GetInstanceLogs(ctx *Context, instancename string) (string, error) {
f := find.NewFinder(v.client, true)
ds, err := f.DatastoreOrDefault(context.TODO(), v.datastore)
if err != nil {
fmt.Println(err)
return err
}

_, err = f.DefaultHostSystem(context.TODO())
if err != nil {
fmt.Println(err)
return "", err
}

serialFile := instancename + "/console.log"

file, err := ds.Open(context.TODO(), serialFile)
if err != nil {
return err
return "", err
}

var reader io.ReadCloser = file

err = file.Tail(100)
if err != nil {
return err
return "", err
}

_, err = io.Copy(os.Stdout, reader)
buf := new(bytes.Buffer)
_, err = io.Copy(buf, reader)

_ = reader.Close()

return nil
return buf.String(), nil
}

func (v *Vsphere) customizeImage(ctx *Context) (string, error) {
Expand Down
Loading

0 comments on commit 49f7a94

Please sign in to comment.