github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/deisctl/backend/fleet/list_unit_files.go (about)

     1  package fleet
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/coreos/fleet/job"
    10  	"github.com/coreos/fleet/machine"
    11  	"github.com/coreos/fleet/schema"
    12  )
    13  
    14  const (
    15  	defaultListUnitFilesFields = "unit,hash,dstate,state,tmachine"
    16  )
    17  
    18  var (
    19  	listUnitFilesFields = map[string]unitToField{
    20  		"unit": func(c *FleetClient, u schema.Unit, full bool) string {
    21  			return u.Name
    22  		},
    23  		"global": func(c *FleetClient, u schema.Unit, full bool) string {
    24  			return strconv.FormatBool(suToGlobal(u))
    25  		},
    26  		"dstate": func(c *FleetClient, u schema.Unit, full bool) string {
    27  			if u.DesiredState == "" {
    28  				return "-"
    29  			}
    30  			return u.DesiredState
    31  		},
    32  		"tmachine": func(c *FleetClient, u schema.Unit, full bool) string {
    33  			if suToGlobal(u) || u.MachineID == "" {
    34  				return "-"
    35  			}
    36  			ms := c.cachedMachineState(u.MachineID)
    37  			if ms == nil {
    38  				ms = &machine.MachineState{ID: u.MachineID}
    39  			}
    40  
    41  			return machineFullLegend(*ms, full)
    42  		},
    43  		"state": func(c *FleetClient, u schema.Unit, full bool) string {
    44  			if suToGlobal(u) || u.CurrentState == "" {
    45  				return "-"
    46  			}
    47  			return u.CurrentState
    48  		},
    49  		"hash": func(c *FleetClient, u schema.Unit, full bool) string {
    50  			uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options)
    51  			if !full {
    52  				return uf.Hash().Short()
    53  			}
    54  			return uf.Hash().String()
    55  		},
    56  		"desc": func(c *FleetClient, u schema.Unit, full bool) string {
    57  			uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options)
    58  			d := uf.Description()
    59  			if d == "" {
    60  				return "-"
    61  			}
    62  			return d
    63  		},
    64  	}
    65  )
    66  
    67  type unitToField func(c *FleetClient, u schema.Unit, full bool) string
    68  
    69  // ListUnitFiles prints all Deis-related unit files to Stdout
    70  func (c *FleetClient) ListUnitFiles() (err error) {
    71  	var sortable sort.StringSlice
    72  	units := make(map[string]*schema.Unit, 0)
    73  
    74  	us, err := c.Fleet.Units()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	for _, u := range us {
    80  		if strings.HasPrefix(u.Name, "deis-") {
    81  			units[u.Name] = u
    82  			sortable = append(sortable, u.Name)
    83  		}
    84  	}
    85  	sortable.Sort()
    86  	c.printUnitFiles(units, sortable)
    87  	return
    88  }
    89  
    90  // printUnitFiles writes unit files to stdout using a tabwriter
    91  func (c *FleetClient) printUnitFiles(units map[string]*schema.Unit, sortable sort.StringSlice) {
    92  	cols := strings.Split(defaultListUnitFilesFields, ",")
    93  	fmt.Fprintln(c.out, strings.ToUpper(strings.Join(cols, "\t")))
    94  	for _, name := range sortable {
    95  		var f []string
    96  		u := units[name]
    97  		for _, col := range cols {
    98  			f = append(f, listUnitFilesFields[col](c, *u, false))
    99  		}
   100  		fmt.Fprintln(c.out, strings.Join(f, "\t"))
   101  	}
   102  	c.out.Flush()
   103  }
   104  
   105  // suToGlobal returns whether or not a schema.Unit refers to a global unit
   106  func suToGlobal(su schema.Unit) bool {
   107  	u := job.Unit{
   108  		Unit: *schema.MapSchemaUnitOptionsToUnitFile(su.Options),
   109  	}
   110  	return u.IsGlobal()
   111  }