|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/coreos/fleet/job" |
| 11 | + "github.com/coreos/fleet/machine" |
| 12 | +) |
| 13 | + |
| 14 | +// initialize tabwriter on stdout |
| 15 | +func init() { |
| 16 | + out = new(tabwriter.Writer) |
| 17 | + out.Init(os.Stdout, 0, 8, 1, '\t', 0) |
| 18 | +} |
| 19 | + |
| 20 | +const ( |
| 21 | + defaultListUnitFields = "unit,state,load,active,sub,desc,machine" |
| 22 | +) |
| 23 | + |
| 24 | +type jobToField func(j *job.Job, full bool) string |
| 25 | + |
| 26 | +var ( |
| 27 | + out *tabwriter.Writer |
| 28 | + listUnitsFields = map[string]jobToField{ |
| 29 | + "unit": func(j *job.Job, full bool) string { |
| 30 | + return j.Name |
| 31 | + }, |
| 32 | + "state": func(j *job.Job, full bool) string { |
| 33 | + js := j.State |
| 34 | + if js != nil { |
| 35 | + return string(*js) |
| 36 | + } |
| 37 | + return "-" |
| 38 | + }, |
| 39 | + "dstate": func(j *job.Job, full bool) string { |
| 40 | + return string(j.TargetState) |
| 41 | + }, |
| 42 | + "load": func(j *job.Job, full bool) string { |
| 43 | + us := j.UnitState |
| 44 | + if us == nil { |
| 45 | + return "-" |
| 46 | + } |
| 47 | + return us.LoadState |
| 48 | + }, |
| 49 | + "active": func(j *job.Job, full bool) string { |
| 50 | + us := j.UnitState |
| 51 | + if us == nil { |
| 52 | + return "-" |
| 53 | + } |
| 54 | + return us.ActiveState |
| 55 | + }, |
| 56 | + "sub": func(j *job.Job, full bool) string { |
| 57 | + us := j.UnitState |
| 58 | + if us == nil { |
| 59 | + return "-" |
| 60 | + } |
| 61 | + return us.SubState |
| 62 | + }, |
| 63 | + "desc": func(j *job.Job, full bool) string { |
| 64 | + d := j.Unit.Description() |
| 65 | + if d == "" { |
| 66 | + return "-" |
| 67 | + } |
| 68 | + return d |
| 69 | + }, |
| 70 | + "machine": func(j *job.Job, full bool) string { |
| 71 | + us := j.UnitState |
| 72 | + if us == nil || us.MachineID == "" { |
| 73 | + return "-" |
| 74 | + } |
| 75 | + ms := cachedMachineState(us.MachineID) |
| 76 | + if ms == nil { |
| 77 | + ms = &machine.MachineState{ID: us.MachineID} |
| 78 | + } |
| 79 | + return machineFullLegend(*ms, full) |
| 80 | + }, |
| 81 | + "tmachine": func(j *job.Job, full bool) string { |
| 82 | + if j.TargetMachineID == "" { |
| 83 | + return "-" |
| 84 | + } |
| 85 | + ms := cachedMachineState(j.TargetMachineID) |
| 86 | + if ms == nil { |
| 87 | + ms = &machine.MachineState{ID: j.TargetMachineID} |
| 88 | + } |
| 89 | + return machineFullLegend(*ms, full) |
| 90 | + }, |
| 91 | + "hash": func(j *job.Job, full bool) string { |
| 92 | + us := j.UnitState |
| 93 | + if us == nil || us.UnitHash == "" { |
| 94 | + return "-" |
| 95 | + } |
| 96 | + if !full { |
| 97 | + return us.UnitHash[:7] |
| 98 | + } |
| 99 | + return us.UnitHash |
| 100 | + }, |
| 101 | + } |
| 102 | +) |
| 103 | + |
| 104 | +// List prints all Deis-related units to Stdout |
| 105 | +func (c *FleetClient) List() (err error) { |
| 106 | + |
| 107 | + var jobs map[string]job.Job |
| 108 | + var sortable sort.StringSlice |
| 109 | + |
| 110 | + jobs = make(map[string]job.Job, 0) |
| 111 | + jj, err := c.Fleet.Jobs() |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + for _, j := range jj { |
| 116 | + if strings.HasPrefix(j.Name, "deis-") { |
| 117 | + jobs[j.Name] = j |
| 118 | + sortable = append(sortable, j.Name) |
| 119 | + } |
| 120 | + } |
| 121 | + sortable.Sort() |
| 122 | + printList(jobs, sortable) |
| 123 | + return |
| 124 | +} |
| 125 | + |
| 126 | +// printList writes units to stdout using a tabwriter |
| 127 | +func printList(jobs map[string]job.Job, sortable sort.StringSlice) { |
| 128 | + cols := strings.Split(defaultListUnitFields, ",") |
| 129 | + for _, s := range cols { |
| 130 | + if _, ok := listUnitsFields[s]; !ok { |
| 131 | + fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s) |
| 132 | + } |
| 133 | + } |
| 134 | + fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t"))) |
| 135 | + for _, name := range sortable { |
| 136 | + var f []string |
| 137 | + j := jobs[name] |
| 138 | + for _, c := range cols { |
| 139 | + f = append(f, listUnitsFields[c](&j, false)) |
| 140 | + } |
| 141 | + fmt.Fprintln(out, strings.Join(f, "\t")) |
| 142 | + } |
| 143 | + out.Flush() |
| 144 | +} |
| 145 | + |
| 146 | +func machineIDLegend(ms machine.MachineState, full bool) string { |
| 147 | + legend := ms.ID |
| 148 | + if !full { |
| 149 | + legend = fmt.Sprintf("%s...", ms.ShortID()) |
| 150 | + } |
| 151 | + return legend |
| 152 | +} |
| 153 | + |
| 154 | +func machineFullLegend(ms machine.MachineState, full bool) string { |
| 155 | + legend := machineIDLegend(ms, full) |
| 156 | + if len(ms.PublicIP) > 0 { |
| 157 | + legend = fmt.Sprintf("%s/%s", legend, ms.PublicIP) |
| 158 | + } |
| 159 | + return legend |
| 160 | +} |
0 commit comments