lita-puppet 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9097b96f123e1d9952226626448af5b9162f131d
4
- data.tar.gz: 2f729905e6c7ba910923eeaf048fa4483161812f
3
+ metadata.gz: 57bbb8a20245057c75fdbc4b930e35f0402bc53d
4
+ data.tar.gz: 246146da0825efad3b35fc05838a7a5ab1186bd0
5
5
  SHA512:
6
- metadata.gz: 9ab0ca71632fe5e9ea5e7f617f83b3dbf2d6d9f6d359b41eec56159529a6acab40e9aef93e824f4bc1e9ac89bd3017c897061c0a7745ef44fdd279618865585d
7
- data.tar.gz: b09ed799fc39ed282e2b2b88421a2c7a6b1a73b50414aa2f653bc3ff44e547ac5792083041f3819bfb391f8daee484e348c3cf8c478cfccf883fa41c19669acf
6
+ metadata.gz: 41b578974acfb904c33f5042936e7b2812e6f1de86d2462355fcfb4cbefa003878e817eff8ad65da6b7f7abb633ebd5e252b743fd353f1cddfbf1c33e2dd195d
7
+ data.tar.gz: d63d1ccce2367c89758658154daba0993be51e0cf6095fdd0181b92db6b12374f415409d9e08545a74a0e7a41b864b9ae56d84a7ffafac4b843c3a75daefb342
@@ -24,106 +24,86 @@ module Lita
24
24
  }
25
25
  )
26
26
 
27
+ include ::Utils::SSH
28
+ include ::Utils::Text
29
+
27
30
  def r10k_deploy(response)
28
31
  environment = response.matches[0][3]
29
32
  control_repo = config.control_repo_path || '/opt/puppet/control'
30
33
  user = config.ssh_user || 'lita'
31
- username = response.user.name
34
+ username = friendly_name(response.user.name)
32
35
 
33
36
  response.reply("#{username}, I'll get right on that. Give me a moment and I'll let you know how it went.")
34
37
 
35
- ret = nil
36
-
37
- # TODO: better error handling
38
- puppet_master = Rye::Box.new(
39
- config.master_hostname,
40
- user: user,
41
- auth_methods: ['publickey'],
42
- password_prompt: false
43
- )
44
-
45
- begin
46
- Timeout::timeout(600) do
47
- puppet_master.cd control_repo
48
-
49
- # Need to use sudo from here on
50
- puppet_master.enable_sudo
51
-
52
- puppet_master.git :pull
53
-
54
- # scary...
55
- puppet_master.disable_safe_mode
56
- command = "r10k deploy environment"
57
- command << " #{environment}" if environment
58
- command << ' -pv'
59
- ret = puppet_master.execute command
60
- end
61
- rescue Exception => e
62
- exception = e
63
- ensure
64
- puppet_master.disconnect
38
+ result1 = over_ssh(host: config.master_hostname, user: user, timeout: 120) do |server|
39
+ # Need to use sudo
40
+ server.enable_sudo
41
+ server[control_repo].git :pull
65
42
  end
66
43
 
67
- # build a reply
68
- if ret
69
- response.reply("#{username}, your r10k deployment is done!")
70
- response.reply "/code " + ret.stderr.join("\n")
71
- else
72
- response.reply "#{username}, your r10k run didn't seem to work... I think it may have timed out."
73
- response.reply "/code " + exception.message
44
+ if result1[:exception]
45
+ response.reply "#{username}, your r10k run didn't seem to work. Looks like there was a problem with Git:"
46
+ response.reply "/code " + result1[:exception].message
47
+ raise result1[:exception]
48
+ end
49
+
50
+ result2 = over_ssh(host: config.master_hostname, user: user) do |server|
51
+ # Need to use sudo
52
+ server.enable_sudo
53
+ # scary...
54
+ server.disable_safe_mode
55
+
56
+ command = "r10k deploy environment"
57
+ command << " #{environment}" if environment
58
+ command << ' -pv'
59
+ server.execute command
60
+ end
61
+
62
+ if result2[:exception]
63
+ response.reply "#{username}, your r10k run didn't seem to work... Maybe it timed out?"
64
+ response.reply "/code " + result2[:exception].message
65
+ raise result2[:exception]
74
66
  end
67
+
68
+ # build a reply
69
+ response.reply("#{username}, your r10k deployment is done!")
70
+ reply_content = [result1[:stdout].join("\n"), result2[:stderr].join("\n")].join("\n")
71
+ response.reply "/code " + sanitze_for_chat(reply_content)
75
72
  end
76
73
 
77
74
  def puppet_agent_run(response)
78
75
  host = response.matches[0][4]
79
76
  user = config.ssh_user || 'lita'
80
- username = response.user.name
77
+ username = friendly_name(response.user.name)
81
78
 
82
79
  response.reply("#{username}, I'll run puppet right away. Give me a sec and I'll let you know how it goes.")
83
80
 
84
- ret = nil
85
- exception = nil
86
-
87
- # TODO: better error handling
88
- remote = Rye::Box.new(
89
- host,
90
- user: user,
91
- auth_methods: ['publickey'],
92
- password_prompt: false
93
- )
94
-
95
- begin
96
- Timeout::timeout(300) do
97
- remote.cd '/tmp'
98
-
99
- # Need to use sudo from here on
100
- remote.enable_sudo
101
-
102
- # scary...
103
- remote.disable_safe_mode
104
-
105
- # build up the command
106
- command = 'puppet agent'
107
- command << ' --verbose --no-daemonize'
108
- command << ' --no-usecacheonfailure'
109
- command << ' --no-splay --show_diff'
110
-
111
- ret = remote.execute command
112
- end
113
- rescue Exception => e
114
- exception = e
115
- ensure
116
- remote.disconnect
81
+ result = over_ssh(host: host, user: user) do |server|
82
+ server.cd '/tmp'
83
+
84
+ # Need to use sudo from here on
85
+ server.enable_sudo
86
+
87
+ # scary...
88
+ server.disable_safe_mode
89
+
90
+ # build up the command
91
+ command = 'puppet agent'
92
+ command << ' --verbose --no-daemonize'
93
+ command << ' --no-usecacheonfailure'
94
+ command << ' --no-splay --show_diff'
95
+
96
+ server.execute command
117
97
  end
118
98
 
119
99
  # build a reply
120
- if ret
121
- response.reply "#{username}, that puppet run is complete! It exited with status #{ret.exit_status}."
100
+ if !result[:exception]
101
+ response.reply "#{username}, that puppet run is complete! It exited with status #{result[:exit_status]}."
122
102
  # Send the standard out, but strip off the bash color code stuff...
123
- response.reply "/code " + ret.stdout.join("\n").gsub(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/, '')
103
+ response.reply "/code " + sanitze_for_chat(result[:stdout].join("\n"))
124
104
  else
125
105
  response.reply "#{username}, your puppet run is done, but didn't seem to work... I think it may have timed out."
126
- response.reply "/code " + exception.message
106
+ response.reply "/code " + result[:exception].message
127
107
  end
128
108
  end
129
109
 
data/lib/lita-puppet.rb CHANGED
@@ -6,6 +6,8 @@ Lita.load_locales Dir[File.expand_path(
6
6
  File.join("..", "..", "locales", "*.yml"), __FILE__
7
7
  )]
8
8
 
9
+ require 'utils/ssh'
10
+ require 'utils/text'
9
11
  require "lita/handlers/puppet"
10
12
 
11
13
  Lita::Handlers::Puppet.template_root File.expand_path(
data/lib/utils/ssh.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Utils
2
+ # Utility methods for doing things over SSH
3
+ module SSH
4
+ # Intelligently do some things over SSH
5
+ def over_ssh(opts = {})
6
+ result = {}
7
+ fail "MissingSSHHost" unless opts[:host]
8
+ fail "MissingSSHUser" unless opts[:user]
9
+ opts[:timeout] ||= 300 # default to a 5 minute timeout
10
+
11
+ remote = Rye::Box.new(
12
+ opts[:host],
13
+ user: opts[:user],
14
+ auth_methods: ['publickey'],
15
+ password_prompt: false
16
+ )
17
+
18
+ exception = nil
19
+
20
+ output = begin
21
+ Timeout::timeout(opts[:timeout]) do
22
+ yield remote # pass our host back to the user to work with
23
+ end
24
+ rescue Exception => e
25
+ exception = e
26
+ ensure
27
+ remote.disconnect
28
+ end
29
+
30
+ result[:exception] = exception
31
+ result[:exit_status] = output.exit_status
32
+ result[:stdout] = output.stdout
33
+ result[:stderr] = output.stderr
34
+ return result
35
+ end
36
+ end
37
+ end
data/lib/utils/text.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Utils
2
+ # Utility methods for manipulating text
3
+ module Text
4
+ # Try to make names more friendly
5
+ def friendly_name(long_name)
6
+ long_name.split(/\s/).first
7
+ end
8
+
9
+ # Strip off bad characters
10
+ def sanitze_for_chat(text)
11
+ # Remove bash colorings
12
+ text.gsub(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/, '')
13
+ end
14
+ end
15
+ end
data/lita-puppet.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-puppet"
3
- spec.version = "0.2.5"
3
+ spec.version = "0.3.0"
4
4
  spec.authors = ["Jonathan Gnagy"]
5
5
  spec.email = ["jgnagy@knuedge.com"]
6
6
  spec.description = "Some basic Puppet interactions for Lita"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-puppet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Gnagy
@@ -123,6 +123,8 @@ files:
123
123
  - Rakefile
124
124
  - lib/lita-puppet.rb
125
125
  - lib/lita/handlers/puppet.rb
126
+ - lib/utils/ssh.rb
127
+ - lib/utils/text.rb
126
128
  - lita-puppet.gemspec
127
129
  - locales/en.yml
128
130
  - spec/lita/handlers/puppet_spec.rb