/
launch.go
150 lines (138 loc) · 5.21 KB
/
launch.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
// Copyright © 2019 Ken'ichiro Oyama <k1lowxb@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/k1LoW/exec"
"github.com/labstack/gommon/color"
"github.com/spf13/cobra"
)
var nonInteractive bool
// launchCmd represents the launch command
var launchCmd = &cobra.Command{
Use: "launch",
Short: "Launch 'track' command in background.",
Long: `Launch 'track' command in background.`,
Run: func(cmd *cobra.Command, args []string) {
exe, err := os.Executable()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand := []string{exe, "track"}
// pid
optPID, err := optionPID(pid, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
pidStr := optPID[1]
trackCommand = append(trackCommand, optPID...)
// threshold
pidInt32, err := strconv.ParseInt(pidStr, 10, 32)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
optThreshold, err := optionThreshold(threshold, int32(pidInt32), nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optThreshold...)
// interval
optInterval, err := optionInterval(interval, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optInterval...)
// attempts
optAttempts, err := optionAttempts(attempts, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optAttempts...)
// command
optCommand, err := optionCommand(command, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optCommand...)
// times
optTimes, err := optionTimes(times, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optTimes...)
// timeout
optTimeout, err := optionTimeout(timeout, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optTimeout...)
// slack-channel
optSlackChannel, err := optionSlackChannel(slackChannel, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optSlackChannel...)
// slack-mention
if len(optSlackChannel) == 2 && optSlackChannel[1] != "" {
optSlackMention, err := optionSlackMention(slackMention, nonInteractive)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
trackCommand = append(trackCommand, optSlackMention...)
}
envs := os.Environ()
c := exec.Command(trackCommand[0], trackCommand[1:]...)
envs = append(envs, fmt.Sprintf("PID=%s", pidStr))
c.Env = envs
err = c.Start()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
fmt.Printf("%s %s\n", color.Magenta("Launched:", color.B), strings.Join(trackCommand, " "))
},
}
func init() {
rootCmd.AddCommand(launchCmd)
launchCmd.Flags().BoolVarP(&nonInteractive, "non-interactive", "", false, "Disables all interactive prompting.")
launchCmd.Flags().Int32VarP(&pid, "pid", "", 0, "PID of the process")
launchCmd.Flags().StringVarP(&threshold, "threshold", "", "cpu > 5 || mem > 10", "Threshold conditions")
launchCmd.Flags().IntVarP(&interval, "interval", "", 5, "Interval of checking if the threshold exceeded (seconds)")
launchCmd.Flags().IntVarP(&attempts, "attempts", "", 1, "Maximum number of attempts continuously exceeding the threshold")
launchCmd.Flags().StringVarP(&command, "command", "", "", "Command to execute when the maximum number of attempts is exceeded")
launchCmd.Flags().IntVarP(×, "times", "", 1, "Maximum number of command executions. If times < 1, track and execute until timeout")
launchCmd.Flags().IntVarP(&timeout, "timeout", "", 60*60*24, "Timeout of tracking (seconds)")
launchCmd.Flags().StringVarP(&slackChannel, "slack-channel", "", "", "Slack channel to notify")
launchCmd.Flags().StringVarP(&slackMention, "slack-mention", "", "", "Slack mention")
}