forked from timoreimann/pdsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
193 lines (172 loc) · 4.97 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"regexp"
"sync"
"syscall"
"time"
"github.com/urfave/cli/v2"
)
var (
pdToken string
slToken string
notAlphaNumRE = regexp.MustCompile(`[^[:alnum:]]`)
minDaemonUpdateFrequency = 1 * time.Minute
includePrivateChannels bool
)
func main() {
var (
p params
dryRun 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.BoolFlag{
Name: "include-private-channels",
Usage: "update topics from rivate channels as well",
Destination: &includePrivateChannels,
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "do not update topic",
Destination: &dryRun,
},
},
Action: func(c *cli.Context) error {
p.schedules = c.StringSlice("schedule")
if c.IsSet("dry-run") {
p.dryRun = &dryRun
}
return realMain(p)
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
// fmt.Fprintf(os.Stderr, "%+#v\n", err)
os.Exit(1)
}
}
func realMain(p params) error {
cfg, err := generateConfig(p)
if err != nil {
return err
}
if p.daemonUpdateFrequency < minDaemonUpdateFrequency {
p.daemonUpdateFrequency = minDaemonUpdateFrequency
}
sp := syncerParams{
pdClient: newPagerDutyClient(pdToken),
slClient: newSlackMetaClient(slToken, includePrivateChannels),
}
ctx := context.Background()
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(context.TODO(), cfg)
if err != nil {
return err
}
syncer := newSyncer(sp)
runFunc := func() error {
return syncer.Run(ctx, slSyncs)
}
if !p.daemon {
return runFunc()
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
stopCtx, cancel := context.WithCancel(context.Background())
go func() {
<-c
cancel()
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Starting daemon")
daemonRun(stopCtx, p.daemonUpdateFrequency, syncer, runFunc)
}()
wg.Wait()
return nil
}