forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
144 lines (120 loc) · 3.05 KB
/
start.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
// Copyright 2017 Xiaomi, Inc.
//
// 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 cmd
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/open-falcon/falcon-plus/g"
"github.com/spf13/cobra"
)
var Start = &cobra.Command{
Use: "start [Module ...]",
Short: "Start Open-Falcon modules",
Long: `
Start the specified Open-Falcon modules and run until a stop command is received.
A module represents a single node in a cluster.
Modules:
` + "all " + strings.Join(g.AllModulesInOrder, " "),
RunE: start,
SilenceUsage: true,
SilenceErrors: true,
}
var PreqOrderFlag bool
var ConsoleOutputFlag bool
func cmdArgs(name string) []string {
return []string{"-c", g.Cfg(name)}
}
func openLogFile(name string) (*os.File, error) {
logDir := g.LogDir(name)
if err := os.MkdirAll(logDir, 0755); err != nil {
return nil, err
}
logPath := g.LogPath(name)
logOutput, err := os.OpenFile(logPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
return logOutput, nil
}
func execModule(co bool, name string) error {
cmd := exec.Command(g.Bin(name), cmdArgs(name)...)
if co {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
logOutput, err := openLogFile(name)
if err != nil {
return err
}
defer logOutput.Close()
cmd.Stdout = logOutput
cmd.Stderr = logOutput
return cmd.Start()
}
func checkStartReq(name string) error {
if !g.HasModule(name) {
return fmt.Errorf("%s doesn't exist", name)
}
if !g.HasCfg(name) {
r := g.Rel(g.Cfg(name))
return fmt.Errorf("expect config file: %s", r)
}
return nil
}
func isStarted(name string) bool {
ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if g.IsRunning(name) {
return true
}
case <-time.After(time.Second):
return false
}
}
}
func start(c *cobra.Command, args []string) error {
args = g.RmDup(args)
if PreqOrderFlag {
args = g.PreqOrder(args)
}
if len(args) == 0 {
args = g.AllModulesInOrder
}
for _, moduleName := range args {
if err := checkStartReq(moduleName); err != nil {
return err
}
// Skip starting if the module is already running
if g.IsRunning(moduleName) {
fmt.Print("[", g.ModuleApps[moduleName], "] ", g.Pid(moduleName), "\n")
continue
}
if err := execModule(ConsoleOutputFlag, moduleName); err != nil {
return err
}
if isStarted(moduleName) {
fmt.Print("[", g.ModuleApps[moduleName], "] ", g.Pid(moduleName), "\n")
continue
}
return fmt.Errorf("[%s] failed to start", g.ModuleApps[moduleName])
}
return nil
}