squab-client 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/squab-client/auto.rb +80 -0
  2. metadata +5 -4
@@ -0,0 +1,80 @@
1
+ # squab-client/mini.rb
2
+ #
3
+ # This is meant to be require'd by various scripts and one-offs to
4
+ # easily send a message to squab upon invocation.
5
+ #
6
+ # By including "require 'squab-client/auto'" in your script
7
+ # it will fork off a process that will update squab with a message
8
+ # like: "HOSTNAME$ path/to/script arg1 arg2 ... argN" that also
9
+ # includes the username running the script
10
+ #
11
+ # To test the formatting of the message sent to squab:
12
+ # Step 1 (optional) - start squab locally
13
+ # cd ~/src/squab/squab && bundle exec bin/squab
14
+ # Step 2 - invoke this script directly
15
+ # cd ~/src/squab/squab-client/lib/squab-client && ruby ./auto.rb
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '..')
18
+
19
+ require 'squab-client'
20
+
21
+ RETRY_SLEEP = 2
22
+
23
+ def invocation_info_to_squab(sc = Squab::Client.new, debug = false, max_retries=3)
24
+ hostname = Socket.gethostname
25
+
26
+ script_name = $0
27
+ user = nil
28
+ shell = nil
29
+
30
+ # do special things if we're sudo'ing
31
+ if ENV["SUDO_USER"]
32
+ user = ENV["SUDO_USER"]
33
+ shell = "#"
34
+ else
35
+ user = Etc.getpwuid(Process.uid).name
36
+ shell = "$"
37
+ end
38
+
39
+ message = "#{hostname}#{shell} #{script_name} #{ARGV.join(' ')}"
40
+
41
+ sc.uid = user
42
+ sc.source = File.basename($0)
43
+
44
+ if debug
45
+ puts "message: #{message}"
46
+ puts "uid: #{user}"
47
+ puts "source: #{sc.source}"
48
+ end
49
+
50
+ # Send with retry
51
+ try_count = 0
52
+ begin
53
+ sc.send(message, nil)
54
+ rescue SendEventFailed
55
+ try_count += 1
56
+ if try_count > max_retries
57
+ return
58
+ else
59
+ sleep RETRY_SLEEP
60
+ retry
61
+ end
62
+ end
63
+ end
64
+
65
+ if __FILE__ == $0
66
+ # run with debug output and 0 retries
67
+ invocation_info_to_squab(
68
+ Squab::Client.new(:api => 'http://localhost:8082'), true,0)
69
+ else
70
+ # background the sending
71
+ if pid = fork
72
+ Process.detach(pid)
73
+ else
74
+ STDERR.reopen('/dev/null', 'w')
75
+ STDOUT.reopen('/dev/null', 'w')
76
+ STDIN.close
77
+
78
+ invocation_info_to_squab
79
+ end
80
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squab-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 4
9
- - 0
10
- version: 1.4.0
9
+ - 1
10
+ version: 1.4.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Grier Johnson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-12-02 00:00:00 -05:00
18
+ date: 2013-12-03 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,6 +29,7 @@ extensions: []
29
29
  extra_rdoc_files:
30
30
  - LICENSE.md
31
31
  files:
32
+ - lib/squab-client/auto.rb
32
33
  - lib/squab-client.rb
33
34
  - bin/squawk
34
35
  - bin/squab-digest