github.com/didip/deis@v1.4.1/deisctl/backend/fleet/list_units.go (about)

     1  package fleet
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"text/tabwriter"
     8  
     9  	"github.com/coreos/fleet/machine"
    10  	"github.com/coreos/fleet/schema"
    11  )
    12  
    13  // initialize tabwriter on stdout
    14  func init() {
    15  	out = new(tabwriter.Writer)
    16  	out.Init(os.Stdout, 0, 8, 1, '\t', 0)
    17  }
    18  
    19  const (
    20  	//defaultListUnitFields  = "unit,state,load,active,sub,machine"
    21  	defaultListUnitsFields = "unit,machine,load,active,sub"
    22  )
    23  
    24  type usToField func(us *schema.UnitState, full bool) string
    25  
    26  var (
    27  	out             *tabwriter.Writer
    28  	listUnitsFields = map[string]usToField{
    29  		"unit": func(us *schema.UnitState, full bool) string {
    30  			if us == nil {
    31  				return "-"
    32  			}
    33  			return us.Name
    34  		},
    35  		"load": func(us *schema.UnitState, full bool) string {
    36  			if us == nil {
    37  				return "-"
    38  			}
    39  			return us.SystemdLoadState
    40  		},
    41  		"active": func(us *schema.UnitState, full bool) string {
    42  			if us == nil {
    43  				return "-"
    44  			}
    45  			return us.SystemdActiveState
    46  		},
    47  		"sub": func(us *schema.UnitState, full bool) string {
    48  			if us == nil {
    49  				return "-"
    50  			}
    51  			return us.SystemdSubState
    52  		},
    53  		"machine": func(us *schema.UnitState, full bool) string {
    54  			if us == nil || us.MachineID == "" {
    55  				return "-"
    56  			}
    57  			ms := cachedMachineState(us.MachineID)
    58  			if ms == nil {
    59  				ms = &machine.MachineState{ID: us.MachineID}
    60  			}
    61  			return machineFullLegend(*ms, full)
    62  		},
    63  		"hash": func(us *schema.UnitState, full bool) string {
    64  			if us == nil || us.Hash == "" {
    65  				return "-"
    66  			}
    67  			if !full {
    68  				return us.Hash[:7]
    69  			}
    70  			return us.Hash
    71  		},
    72  	}
    73  )
    74  
    75  // ListUnits prints all Deis-related units to Stdout
    76  func (c *FleetClient) ListUnits() (err error) {
    77  	var states []*schema.UnitState
    78  
    79  	unitStates, err := cAPI.UnitStates()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	for _, us := range unitStates {
    85  		if strings.HasPrefix(us.Name, "deis-") {
    86  			states = append(states, us)
    87  		}
    88  	}
    89  	printUnits(states)
    90  	return
    91  }
    92  
    93  // printUnits writes units to stdout using a tabwriter
    94  func printUnits(states []*schema.UnitState) {
    95  	cols := strings.Split(defaultListUnitsFields, ",")
    96  	for _, s := range cols {
    97  		if _, ok := listUnitsFields[s]; !ok {
    98  			fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s)
    99  		}
   100  	}
   101  	fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t")))
   102  	for _, us := range states {
   103  		var f []string
   104  		for _, c := range cols {
   105  			f = append(f, listUnitsFields[c](us, false))
   106  		}
   107  		fmt.Fprintln(out, strings.Join(f, "\t"))
   108  	}
   109  	out.Flush()
   110  }
   111  
   112  func machineIDLegend(ms machine.MachineState, full bool) string {
   113  	legend := ms.ID
   114  	if !full {
   115  		legend = fmt.Sprintf("%s...", ms.ShortID())
   116  	}
   117  	return legend
   118  }
   119  
   120  func machineFullLegend(ms machine.MachineState, full bool) string {
   121  	legend := machineIDLegend(ms, full)
   122  	if len(ms.PublicIP) > 0 {
   123  		legend = fmt.Sprintf("%s/%s", legend, ms.PublicIP)
   124  	}
   125  	return legend
   126  }