github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/cmd/logs.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/emc-advanced-dev/pkg/errors"
    12  	"github.com/solo-io/unik/pkg/client"
    13  )
    14  
    15  var follow, deleteOnDisconnect bool
    16  
    17  var logsCmd = &cobra.Command{
    18  	Use:   "logs",
    19  	Short: "retrieve the logs (stdout) of a unikernel instance",
    20  	Long: `Retrieves logs from a running unikernel instance.
    21  
    22  Cannot be used on an instance in powered-off state.
    23  Use the --follow flag to attach to the instance's stdout
    24  Use --delete in combination with --follow to force automatic instance
    25  deletion when the HTTP connection to the instance is broken (by client
    26  disconnect). The --delete flag is typically intended for use with
    27  orchestration software such as cluster managers which may require
    28  a persistent http connection managed instances.
    29  
    30  You may specify the instance by name or id.
    31  
    32  Example usage:
    33  	unik logs --instancce myInstance
    34  
    35  	# will return captured stdout from myInstance since boot time
    36  
    37  	unik logs --instance myInstance --follow --delete
    38  
    39  	# will open an http connection between the cli and unik
    40  	backend which streams stdout from the instance to the client
    41  	# when the client disconnects (i.e. with Ctrl+C) unik will
    42  	automatically power down and terminate the instance
    43  `,
    44  	Run: func(cmd *cobra.Command, args []string) {
    45  		if err := func() error {
    46  			if err := readClientConfig(); err != nil {
    47  				return err
    48  			}
    49  			if host == "" {
    50  				host = clientConfig.Host
    51  			}
    52  			if instanceName == "" {
    53  				return errors.New("must specify --instance", nil)
    54  			}
    55  			if follow {
    56  				logrus.WithFields(logrus.Fields{"host": host, "instance": instanceName}).Info("attaching to instance")
    57  				r, err := client.UnikClient(host).Instances().AttachLogs(instanceName, deleteOnDisconnect)
    58  				if err != nil {
    59  					return err
    60  				}
    61  				reader := bufio.NewReader(r)
    62  				for {
    63  					line, err := reader.ReadString('\n')
    64  					if err != nil {
    65  						return err
    66  					}
    67  					if line != "\n" {
    68  						fmt.Printf(line)
    69  					}
    70  				}
    71  			} else {
    72  				logrus.WithFields(logrus.Fields{"host": host, "instance": instanceName}).Info("getting instance logs")
    73  				data, err := client.UnikClient(host).Instances().GetLogs(instanceName)
    74  				if err != nil {
    75  					return err
    76  				}
    77  				fmt.Printf("%s\n", string(data))
    78  			}
    79  			return nil
    80  		}(); err != nil {
    81  			logrus.Errorf("failed retrieving instance logs: %v", err)
    82  			os.Exit(-1)
    83  		}
    84  	},
    85  }
    86  
    87  func init() {
    88  	RootCmd.AddCommand(logsCmd)
    89  	logsCmd.Flags().StringVar(&instanceName, "instance", "", "<string,required> name or id of instance. unik accepts a prefix of the name or id")
    90  	logsCmd.Flags().BoolVar(&follow, "follow", false, "<bool,optional> follow stdout of instance as it is printed")
    91  	logsCmd.Flags().BoolVar(&deleteOnDisconnect, "delete", false, "<bool,optional> use this flag with the --follow flag to trigger automatic deletion of instance after client closes the http connection")
    92  }