Permalink
Browse files

[cm] Adds new puppet resource provider cm_command to execute commands…

… using cm api
  • Loading branch information...
1 parent a775975 commit 38ebc8d4a156e3e5327c2e74e3291dced0aded8f @ashrithr ashrithr committed Apr 6, 2014
View
8 cm/lib/puppet/parser/functions/get_api_version.rb
@@ -0,0 +1,8 @@
+module Puppet::Parser::Functions
+ newfunction(:get_api_version, :type => :rvalue, :doc => <<-EOS
+ Puppet function to return cm api version
+ EOS
+ ) do
+ # Code here
+ end
+end
View
69 cm/lib/puppet/provider/cm_command/ruby.rb
@@ -0,0 +1,69 @@
+require 'rubygems' if RUBY_VERSION < '1.9'
+require 'open-uri'
+require 'net/http'
+require 'json'
+
+Puppet::Type.type(:cm_command).provide(:ruby) do
+ desc "Executes a cm command."
+
+ def url
+ @url
+ end
+
+ def url=(url)
+ @url = url
+ fetch_parameters
+ info "Processing command with params 'base_url': #{url}, 'post_req': #{@post}, 'params': #{@params}"
+ process_requset(@url)
+ end
+
+ private
+ def fetch_parameters
+ @post = resource[:post]
+ @wait = resource[:wait]
+ @username = resource[:username]
+ @password = resource[:password]
+ @params = resource[:params]
+ end
+
+ def process_request(url)
+ response = nil
+ cmd_ids = []
+ uri = URI.parse(url)
+ http = Net::HTTP.new(uri.host, uri.port)
+ req = Net::HTTP::Post.new(@post)
+ req.add_field('Content-Type', 'application/json')
+ req.basic_auth(@username, @password)
+ req.body = @params.to_json
+ response = http.request(req)
+ if response.code == '200'
+ pjson = JSON.parse(response.body)
+ cmd_ids << pjson['items'].first['id']
+ if @wait
+ wait_for_command(cmd_ids)
+ end
+ else
+ fail "Command execution failed, base_url: #{url} & post_req: #{@post}"
+ end
+ end
+
+ def wait_for_command(cmd_ids)
+ cmd_ids.each do |cmd_id|
+ uri = URI.parse("http://localhost:7180")
+ http = Net::HTTP.new(uri.host, uri.port)
+ req = Net::HTTP::Get.new("/api/v6/commands/27")
+ req.basic_auth('admin', 'admin')
+ res = http.request(req)
+ while JSON.parse(res.body)['active']
+ debug "Command with id: #{cmd_id} is still running, re-trying in 5 seconds"
+ sleep 5
+ res = http.request(req)
+ end
+ unless JSON.parse(res.body['success'])
+ fail "Failed command, reason: #{JSON.parse(res.body['resultMessage'])}"
+ else
+ debug "Sucessfully executed command: #{JSON.parse(res.body['name'])}"
+ end
+ end
+ end
+end
View
39 cm/lib/puppet/type/cm_command.rb
@@ -0,0 +1,39 @@
+# A type to execute scm command and wait until the command gets executed
+Puppet::Type.newtype(:cm_command) do
+ @doc = "Executes cloudera manager api command and waits till the command gets executed."
+
+ newparam :name
+
+ newproperty(:url) do
+ desc "The url to execute."
+ end
+
+ newparam(:post) do
+ desc "Post request path."
+ end
+
+ newparam(:username) do
+ desc "Username for basic authentication"
+ end
+
+ newparam(:password) do
+ desc "Password for basic authentication"
+ end
+
+ newparam(:params) do
+ desc "Parameters for the requset body"
+ munge do |value|
+ Hash.new(value)
+ end
+ end
+
+ newparam(:wait) do
+ desc "Waits until the command gets executed"
+ end
+
+ validate do
+ unless self[:url] && self[:post]
+ fail "url and post path are required."
+ end
+ end
+end

0 comments on commit 38ebc8d

Please sign in to comment.