-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsft-util.rb
executable file
·46 lines (39 loc) · 1.14 KB
/
sft-util.rb
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
#!/usr/bin/env ruby
require 'open3'
require 'optparse'
require 'shellwords'
team = ""
slack_webhook = ""
cmd = ""
regex = //
msg = ""
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] <command>"
opts.on("--team TEAM", "Team name") {|o| team = Shellwords.escape(o) }
opts.on("--slack-webhook URL", "Slack webhook url") {|o| slack_webhook = Shellwords.escape(o) }
opts.on("--help", "Help") {|o| puts opts }
opts.on_tail("Available commands:\n enroll\n login")
end.parse!
command = ARGV[0]
hostname = `hostname`.strip
case command
when "enroll"
cmd = "sft enroll --team #{team} 2>&1"
regex = /https:\/\/.*scaleft.*approve/
msg = "[#{hostname}] Scaleft Client Enrollment Requested"
when "login"
cmd = "sft login --team #{team} 2>&1"
regex = /https:\/\/.*scaleft.*client_logins[^" ]*/
msg = "[#{hostname}] Scaleft Client Login Requested"
else
abort "invalid command"
end
Open3.popen3(cmd) do |stdin, stdout, stderr|
match = stdout.gets.match(regex)
url = match[0]
`curl -s #{slack_webhook} -d '{"text": "#{msg}: #{url}"}'`
puts "Sent message to Slack!"
puts "Waiting on approval..."
end
puts "Done."