-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (126 loc) · 3.99 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
package main
import (
"fmt"
"log"
"os"
"strings"
"tailscale-route-tiller/config"
"tailscale-route-tiller/slack"
"tailscale-route-tiller/tailscale"
"tailscale-route-tiller/utils"
"tailscale-route-tiller/worker"
"github.com/spf13/cobra"
)
var subnets []string
var (
version = "dev"
commit = "none"
date = "unknown"
)
func runUpdates(testMode bool, config config.Config) {
resolvedSubnets, _, err := utils.PerformDNSLookupsWithTTL(config.Sites, config.EnableIpv6)
if err != nil {
log.Println("Error: ", err.Error())
slack.PostError(err)
os.Exit(1)
}
resolvedSubnets = append(resolvedSubnets, config.Subnets...)
// we might have some overlap, so let's dedupe
resolvedSubnets = utils.Unique(resolvedSubnets)
subnetsString := strings.Join(resolvedSubnets, ",")
fullCommand := fmt.Sprintf(config.TailscaleCommand, subnetsString)
if !testMode {
output := utils.RunShellCommand(fullCommand, testMode)
log.Println(output)
log.Println("Trying to update Approved Subnets...")
err = tailscale.SetTailscaleApprovedSubnets(resolvedSubnets)
if err != nil {
log.Println("Error: ", err.Error())
slack.PostError(err)
os.Exit(1)
}
slack.PostRouteUpdate(resolvedSubnets, config.TailscaleclientId)
} else {
slack.PostRouteUpdate(resolvedSubnets, config.TailscaleclientId)
log.Println("In test mode, not running command")
}
}
func runGetTailsScaleClientRouteSettings(config config.Config) {
output, err := tailscale.GetTailsScaleClientRouteSettings()
if err != nil {
log.Println("Error: ", err.Error())
slack.PostError(err)
os.Exit(1)
}
log.Println(string(output))
}
func initConfig(configFile string) {
config.ReadYAML(configFile)
slack.WebhookURL = config.ActiveConfig.Slack.WebhookURL
slack.Enabled = config.ActiveConfig.Slack.Enabled
tailscale.TailscaleKey = config.ActiveConfig.TailscaleKey
tailscale.TailScaleClientId = config.ActiveConfig.TailscaleclientId
}
func main() {
log.Println("Starting tailscale-route-tiler", "version", version, "commit", commit, "date", date)
rootCmd := &cobra.Command{
Use: "tailscale-route-tiler",
Long: `This is a helper tool to getnerate a list of subnets for tailscale and the run the tailscale
command to update the routes.
`,
Short: "A tailscale helper tool for subnets",
Run: func(cmd *cobra.Command, args []string) {
// This is the default action when no command is provided
cmd.Help()
},
}
// Flags
var ConfigFile string
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config", "c", "", "Specify the configuration file")
// version command
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print the version number of tailscale-route-tiler",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("tailscale-route-tiler %s, commit %s, built at %s\n", version, commit, date)
},
})
// Run Command
var testMode bool
runCmd := &cobra.Command{
Use: "run",
Short: "Run the tailscale command to update the routes",
Run: func(cmd *cobra.Command, args []string) {
initConfig(ConfigFile)
runUpdates(testMode, *config.ActiveConfig)
},
}
runCmd.Flags().BoolVarP(&testMode, "test", "t", false, "Run in test mode")
rootCmd.AddCommand(runCmd)
// worker Command
workerCmd := &cobra.Command{
Use: "worker",
Short: "Run in worker mode, waits for an SQS messages, then runs the tailscale command to update the routes",
Run: func(cmd *cobra.Command, args []string) {
initConfig(ConfigFile)
worker.Run(testMode, *config.ActiveConfig)
},
}
workerCmd.Flags().BoolVarP(&testMode, "test", "t", false, "Run in test mode")
rootCmd.AddCommand(workerCmd)
// Get Client Routes Command
getClientRoutes := &cobra.Command{
Use: "get-client-routes",
Short: "Get the current routes for the client",
Run: func(cmd *cobra.Command, args []string) {
initConfig(ConfigFile)
runGetTailsScaleClientRouteSettings(*config.ActiveConfig)
},
}
rootCmd.AddCommand(getClientRoutes)
// Execute the CLI
if err := rootCmd.Execute(); err != nil {
log.Println(err)
os.Exit(1)
}
}