Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from mattdbridges/refactoring-commands

Reducing duplication for all commands
  • Loading branch information...
commit edfc1d6de4c86baa2c6eb3cb5f49aad28c869dfc 2 parents 73aab04 + a8ca571
@jjasghar authored
Showing with 26 additions and 69 deletions.
  1. +26 −69 src/chef.coffee
View
95 src/chef.coffee
@@ -16,112 +16,69 @@
# hubot knife client show <name> - chef: Display client configurations et al
# hubot node list - chef: Lists all nodes on chef server
# hubot node status - chef: Get knife status of all nodes
+# hubot node show <node> - chef: Get knife status of all nodes
# hubot uptime <server> - chef: Prints uptime per node
#
# Author:
# jjasghar
+# mattdbridges
+#
+
+exec = require('child_process').exec
+execCommand = (msg, cmd) ->
+ exec cmd, (error, stdout, stderr) ->
+ msg.send error
+ msg.send stdout
+ msg.send stderr
module.exports = (robot) ->
- robot.respond /node list$/i, (msg) ->
- spawn = require('child_process').spawn
- server = msg.match[1]
- command = "knife node list"
+ robot.respond /node (list|status)$/i, (msg) ->
+ subcmd = msg.match[1]
+ command = "knife node #{subcmd}"
msg.send "Listing nodes..."
+ execCommand msg, command
- @exec = require('child_process').exec
+ robot.respond /node show (.*)$/i, (msg) ->
+ nodeName = msg.match[1]
+ command = "knife node show #{nodeName}"
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
-
- robot.respond /node status$/i, (msg) ->
- spawn = require('child_process').spawn
-
- server = msg.match[1]
- command = "knife status"
-
- msg.send "Getting statuses of all nodes..."
-
- @exec = require('child_process').exec
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
+ msg.send "Showing node for #{nodeName}..."
+ execCommand msg, command
robot.respond /environment list$/i, (msg) ->
- spawn = require('child_process').spawn
-
- server = msg.match[1]
command = "knife environment list"
msg.send "Listing environments..."
-
- @exec = require('child_process').exec
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
-
+ execCommand msg, command
robot.respond /knife (node|role|client) show (.*)$/i, (msg) ->
- spawn = require('child_process').spawn
-
subcmd = msg.match[1]
name = msg.match[2]
command = "knife #{subcmd} show #{name}"
msg.send "Running: #{command}"
-
- @exec = require('child_process').exec
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
+ execCommand msg, command
robot.respond /uptime (.*)$/i, (msg) ->
- spawn = require('child_process').spawn
server = msg.match[1]
-
command = "knife ssh name:#{server} 'uptime'"
msg.send "Checking #{server} for uptime..."
-
- @exec = require('child_process').exec
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
+ execCommand msg, command
robot.respond /converge (.*)$/i, (msg) ->
server = msg.match[1]
-
- @exec = require('child_process').exec
-
command = "knife ssh --attribute ipaddress --no-color name:#{server} 'sudo chef-client'"
msg.send "Converging #{server}."
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
+ execCommand msg, command
robot.respond /converge-environment (.*)$/i, (msg) ->
- # environment = msg.match[1]
- # @exec = require('child_process').exec
- # command = "knife ssh --no-color --attribute ipaddress chef_environment:#{environment} 'sudo chef-client'"
+ environment = msg.match[1]
+ command = "knife ssh --no-color --attribute ipaddress chef_environment:#{environment} 'sudo chef-client'"
msg.send "Configuring #{environment}....nope just kidding man, you have balls..."
-
- @exec command, (error, stdout, stderr) ->
- msg.send error
- msg.send stdout
- msg.send stderr
+ # execCommand msg, command
Please sign in to comment.
Something went wrong with that request. Please try again.