forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
162 lines (131 loc) · 3.57 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path"
"strings"
"text/tabwriter"
"github.com/coreos/fleet/third_party/github.com/codegangsta/cli"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
"github.com/coreos/fleet/third_party/github.com/rakyll/globalconf"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/ssh"
"github.com/coreos/fleet/unit"
"github.com/coreos/fleet/version"
)
var out *tabwriter.Writer
var flagset *flag.FlagSet = flag.NewFlagSet("fleetctl", flag.ExitOnError)
func init() {
out = new(tabwriter.Writer)
out.Init(os.Stdout, 0, 8, 1, '\t', 0)
cli.CommandHelpTemplate = `NAME:
fleetctl {{.Name}} - {{.Usage}}
DESCRIPTION:
{{.Description}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
}
func getRegistry(context *cli.Context) *registry.Registry {
tun := getTunnelFlag()
endpoint := getEndpointFlag()
machines := []string{endpoint}
client := etcd.NewClient(machines)
if tun != "" {
sshClient, err := ssh.NewSSHClient("core", tun)
if err != nil {
log.Fatalf("Failed initializing SSH client: %v", err)
}
dial := func(network, addr string) (net.Conn, error) {
tcpaddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return sshClient.DialTCP(network, nil, tcpaddr)
}
tr := http.Transport{
Dial: dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client.SetTransport(&tr)
}
return registry.New(client)
}
func main() {
app := cli.NewApp()
app.Name = "fleetctl"
app.Usage = "fleetctl is a command line driven interface to the cluster wide CoreOS init system."
app.Flags = []cli.Flag{
cli.StringFlag{"endpoint", "http://127.0.0.1:4001", "Fleet Engine API endpoint (etcd)"},
cli.StringFlag{"tunnel", "", "Establish an SSH tunnel through the provided address for communication with fleet and etcd."},
}
app.Commands = []cli.Command{
newListUnitsCommand(),
newSubmitUnitCommand(),
newDestroyUnitCommand(),
newStartUnitCommand(),
newStopUnitCommand(),
newStatusUnitsCommand(),
newCatUnitCommand(),
newListMachinesCommand(),
newJournalCommand(),
newSSHCommand(),
}
for _, f := range app.Flags {
f.Apply(flagset)
}
flagset.Bool("version", false, "Print the version and exit")
flagset.Parse(os.Args[1:])
if (*flagset.Lookup("version")).Value.(flag.Getter).Get().(bool) {
fmt.Println("fleetctl version", version.Version)
os.Exit(0)
}
globalconf.Register("fleetctl", flagset)
opts := globalconf.Options{
EnvPrefix: "FLEETCTL_",
}
gconf, _ := globalconf.NewWithOptions(opts)
gconf.ParseSet("", flagset)
app.Run(os.Args)
}
func ellipsize(field string, n int) string {
// When ellipsing a field, we want to display the first n
// characters. We check for n+3 so we don't inadvertently
// make fields with fewer than n+3 characters even longer
// by adding unnecessary ellipses.
if len(field) > n+3 {
return fmt.Sprintf("%s...", field[0:n])
} else {
return field
}
}
func getJobPayloadFromFile(file string) (*job.JobPayload, error) {
out, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
unitFile := unit.NewSystemdUnitFile(string(out))
name := path.Base(file)
payload := job.NewJobPayload(name, *unitFile)
return payload, nil
}
func getTunnelFlag() string {
tun := (*flagset.Lookup("tunnel")).Value.(flag.Getter).Get().(string)
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func getEndpointFlag() string {
return (*flagset.Lookup("endpoint")).Value.(flag.Getter).Get().(string)
}