/
command.go
139 lines (120 loc) · 3.07 KB
/
command.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
/*
** Copyright [2013-2016] [Megam Systems]
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package run
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
"github.com/megamsys/libgo/cmd"
"launchpad.net/gnuflag"
)
type configFile struct {
value string
}
func (v *configFile) String() string {
return v.value
}
func (v *configFile) Set(value string) error {
v.value = value
return nil
}
type Start struct {
fs *gnuflag.FlagSet
dry bool
file configFile
}
func (g *Start) Info() *cmd.Info {
desc := `starts vertice.
`
return &cmd.Info{
Name: "start",
Usage: `start [--config]`,
Desc: desc,
MinArgs: 0,
}
}
func (c *Start) Run(context *cmd.Context) error {
log.Infof("vertice.")
config, err := c.ParseConfig(c.file.String())
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Failed to parse config")
return fmt.Errorf("Failed to parse config: %s", err)
}
cmd := NewCommand()
cmd.Version = "1.5.2"
if err := cmd.Megd(config, cmd.Version); err != nil {
return fmt.Errorf("run: %s", err)
}
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
select {
case <-signalCh:
log.Info("signal received, initializing clean shutdown...")
go func() {
cmd.Close()
}()
}
// Block again until another signal is received, a shutdown timeout elapses,
// or the Command is gracefully closed
log.Warn("waiting for clean shutdown...")
select {
case <-signalCh:
log.Info("second signal received, initializing hard shutdown")
case <-time.After(time.Second * 30):
log.Info("time limit reached, initializing hard shutdown")
case <-cmd.Closed:
log.Info("server shutdown completed")
}
// goodbye.
return nil
}
func (c *Start) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("vertice", gnuflag.ExitOnError)
c.fs.Var(&c.file, "config", "Path to configuration file (default to /vertice/vertice.conf)")
c.fs.Var(&c.file, "c", "Path to configuration file (default to /vertice/vertice.conf)")
}
return c.fs
}
// ParseConfig parses the config at path.
func (c *Start) ParseConfig(path string) (*Config, error) {
config := NewConfig()
if path == "" {
path = config.Meta.Dir + "/vertice.conf"
}
log.Warnf("Using configuration at: %s", path)
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
if err := toml.Unmarshal(buf, &config); err != nil {
panic(err)
}
log.Debug(config)
return config, nil
}