-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
213 lines (193 loc) · 6.02 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"context"
"fmt"
"os"
"os/signal"
"regexp"
"syscall"
"time"
"github.com/urfave/cli/v2"
)
var (
pdToken string
slToken string
notAlphaNumRE = regexp.MustCompile(`[^[:alnum:]]`)
daemonMinUpdateFrequency = 1 * time.Minute
daemonMaxExecutionTime time.Duration
includePrivateChannels bool
)
func main() {
var (
p params
dryRun bool
pretendUsers bool
)
app := &cli.App{
Name: "pdsync",
Usage: "sync PagerDuty on-call schedules to Slack",
UsageText: `Poll a list of PagerDuty schedules for on-call personnel and update a Slack channel's topic using a predefined template
Schedules can be given as names or IDs. Similarly, the channel to update the topic for can be specified by name or ID.
Optionally, a set of Slack user groups can be kept in sync. This can be used to manage on-call handles.
By default, the program will terminate after a single run. Use the --daemon flag to keep running in the background and synchronize schedule changes periodically.
`,
Authors: []*cli.Author{
{
Name: "Timo Reimann",
Email: "[email protected]",
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pagerduty-token",
Usage: "the PagerDuty token",
Destination: &pdToken,
EnvVars: []string{"PAGERDUTY_TOKEN"},
Required: true,
},
&cli.StringFlag{
Name: "slack-token",
Usage: "the Slack token",
Destination: &slToken,
EnvVars: []string{"SLACK_TOKEN"},
Required: true,
},
&cli.StringFlag{
Name: "config",
Usage: "config file to use",
Destination: &p.config,
},
&cli.StringSliceFlag{
Name: "schedule",
Usage: "name of a PageDuty schedule to sync periodically (can be repeated to define several schedules); syntax: id|name=<schedule reference>[;userGroup=id|name|handle=<user group reference>..]",
},
&cli.StringFlag{
Name: "channel-name",
Usage: "the name of the channel to post topic updates to",
Destination: &p.channelName,
},
&cli.StringFlag{
Name: "channel-id",
Usage: "the ID of the channel to post topic updates to",
Destination: &p.channelID,
},
&cli.StringFlag{
Name: "template",
Usage: "the literal Go template for the channel topic to apply; variable like {{.<ScheduleName>}} are replaced by the on-caller's Slack handle",
Destination: &p.tmplString,
},
&cli.StringFlag{
Name: "template-file",
Usage: "a template file `FILE` describing the Go template for the channel topic to apply",
Destination: &p.tmplFile,
},
&cli.BoolFlag{
Name: "daemon",
Usage: "run as daemon in the background and update periodically",
Destination: &p.daemon,
},
&cli.DurationFlag{
Name: "daemon-update-frequency",
Value: 5 * time.Minute,
Usage: "how often on-call schedules should be checked for changes (minimum is 1 minute)",
Destination: &p.daemonUpdateFrequency,
},
&cli.DurationFlag{
Name: "daemon-max-execution-time",
Usage: "time after which the daemon should self-terminate (default: never)",
Destination: &daemonMaxExecutionTime,
},
&cli.BoolFlag{
Name: "include-private-channels",
Usage: "update topics from rivate channels as well",
Destination: &includePrivateChannels,
},
&cli.BoolFlag{
Name: "pretend-users",
Usage: "escape Slack user IDs to prevent tagging",
Destination: &pretendUsers,
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "do not update topic",
Destination: &dryRun,
},
&cli.BoolFlag{
Name: "fail-fast",
Usage: "fail on the first schedule that cannot be synced, and otherwise handle failures gracefully (defaults to false when running in daemon mode, otherwise true)",
Destination: &p.failFast,
},
},
Action: func(c *cli.Context) error {
p.schedules = c.StringSlice("schedule")
if c.IsSet("pretend-users") {
p.pretendUsers = &pretendUsers
}
if c.IsSet("dry-run") {
p.dryRun = &dryRun
}
if !c.IsSet("fail-fast") {
p.failFast = !p.daemon
}
return realMain(p)
},
}
err := app.Run(os.Args)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func realMain(p params) error {
cfg, err := generateConfig(p)
if err != nil {
return err
}
if p.daemonUpdateFrequency < daemonMinUpdateFrequency {
p.daemonUpdateFrequency = daemonMinUpdateFrequency
}
sp := syncerParams{
pdClient: newPagerDutyClient(pdToken),
slClient: newSlackMetaClient(slToken, includePrivateChannels),
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
fmt.Println("Getting Slack users")
sp.slackUsers, err = sp.slClient.getSlackUsers(ctx)
if err != nil {
return fmt.Errorf("failed to get Slack users: %s", err)
}
fmt.Printf("Found %d Slack user(s)\n", len(sp.slackUsers))
fmt.Println("Getting Slack user groups")
sp.slackUserGroups, err = sp.slClient.getUserGroups(ctx)
if err != nil {
return fmt.Errorf("failed to get Slack user groups: %s", err)
}
fmt.Printf("Found %d Slack user group(s)\n", len(sp.slackUserGroups))
slSyncs, err := sp.createSlackSyncs(ctx, cfg)
if err != nil {
return fmt.Errorf("failed to create Slack syncs: %s", err)
}
syncer := newSyncer(sp)
runFunc := func() error {
return syncer.Run(ctx, slSyncs, p.failFast)
}
if !p.daemon {
return runFunc()
}
daemonCtx := ctx
if daemonMaxExecutionTime > 0 {
fmt.Printf("Setting maximum daemon execution time to %s\n", daemonMaxExecutionTime)
var cancel context.CancelFunc
daemonCtx, cancel = context.WithTimeout(ctx, daemonMaxExecutionTime)
defer cancel()
}
fmt.Println("Starting daemon")
startDaemon(daemonCtx, p.daemonUpdateFrequency, runFunc)
termMessage := "Daemon terminated"
if daemonCtx.Err() != nil && ctx.Err() == nil {
termMessage += " (maximum execution time has elapsed)"
}
fmt.Println(termMessage)
return nil
}