Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generate_types to Mcollective R10K plugin #458

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions files/agent/r10k.ddl
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,36 @@ action 'deploy_module', :description => "Deploy a specific module" do

display :always
end

action 'generate_types', :description => "Generate metadata files of resource types" do
input :environment,
:prompt => "Specific environment",
:description => "Generate metadata files for a particular environment",
:type => :string,
:validation => '.',
:optional => true,
:maxlength => 256

input :user,
:prompt => "User",
:description => "User to run as",
:type => :string,
:validation => '\w+',
:optional => true,
:maxlength => 32

output :environment,
:description => "Generate metadata files for a particular environment",
:display_as => "Specific environment"

output :output,
:description => "Output from r10k",
:display_as => "Output"

output :error,
:description => "Error from r10k",
:display_as => "Errors"

display :always
end
# vim: set syntax=ruby:
34 changes: 28 additions & 6 deletions files/application/r10k.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
class MCollective::Application::R10k<MCollective::Application
description 'R10K interface for MCO'

usage <<-END_OF_USAGE
mco r10k <ACTION> <ARGUMENTS>
Usage: mco r10k <push|pull|status|deploy|deploy_module|generate_types>

The ACTION can be one of the following:

push - Git push a module
pull - Git pull a module
status - Git status of a module
deploy - Deploy a specific environment, and its Puppetfile specified modules
deploy_module - Deploy a specific module
generate_types - Generate metadata file of resource types

END_OF_USAGE

def post_option_parser(configuration)
if ARGV.length >= 1
configuration[:command] = ARGV.shift
case configuration[:command]
when 'push','pull','status'
configuration[:path] = ARGV.shift || docs
when 'deploy'
when 'deploy', 'generate_types'
configuration[:environment] = ARGV.shift || docs
when 'deploy_module'
configuration[:module_name] = ARGV.shift || docs
end
else
else
docs
end
end

def docs
puts "Usage: #{$0} push | pull | status | deploy | deploy_module"
puts "Usage: #{$0} push | pull | status | deploy | deploy_module | generate_types"
end

def main
mc = rpcclient("r10k", :chomp => true)
options = {:path => configuration[:path]} if ['push','pull','status'].include? configuration[:command]
options = {:environment => configuration[:environment]} if ['deploy'].include? configuration[:command]
options = {:path => configuration[:path]} if ['push','pull','status']
.include? configuration[:command]
options = {:environment => configuration[:environment]} if [
'deploy', 'generate_types'
].include? configuration[:command]
options = {:module_name => configuration[:module_name]} if ['deploy_module'].include? configuration[:command]
mc.send(configuration[:command], options).each do |resp|
puts "#{resp[:sender]}:"
if resp[:statuscode] == 0
responses = resp[:data][:output]
puts responses if responses and ['push','pull','status','deploy','deploy_module'].include? configuration[:command]
puts responses if responses and [
'push','pull','status','deploy','deploy_module', 'generate_types'
].include? configuration[:command]
else
puts resp[:statusmsg]
end
Expand Down
14 changes: 10 additions & 4 deletions templates/agent/r10k.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ module MCollective
'synchronize',
'deploy',
'deploy_module',
'sync'].each do |act|
'sync',
'generate_types'].each do |act|
action act do
if act == 'deploy'
if ['deploy', 'generate_types'].include? act
validate :environment, :shellsafe
environment = request[:environment]
lock do
Expand Down Expand Up @@ -76,8 +77,8 @@ module MCollective
output = ''
git = ['/usr/bin/env', 'git']
r10k = ['/usr/bin/env', 'r10k']
# Given most people using this are using Puppet Enterprise, add the PE Path
environment = {"LC_ALL" => "C","PATH" => "#{ENV['PATH']}:<%= if @is_pe == true or @is_pe == 'true' then '/opt/puppet/bin' else '/usr/local/bin' end %>", "http_proxy" => "<%= @http_proxy %>", "https_proxy" => "<%= @http_proxy %>", "GIT_SSL_NO_VERIFY" => "<%= @git_ssl_no_verify %>" }
puppet = ['/usr/bin/env', 'puppet']
environment = {"LC_ALL" => "C","PATH" => "#{ENV['PATH']}:/opt/puppetlabs/bin", "http_proxy" => "<%= @http_proxy %>", "https_proxy" => "<%= @http_proxy %>", "GIT_SSL_NO_VERIFY" => "<%= @git_ssl_no_verify %>" }
case action
when 'push','pull','status'
cmd = git
Expand All @@ -94,6 +95,11 @@ module MCollective
elsif action == 'deploy_module'
cmd << 'deploy' << 'module' << arg
end
reply[:status] = run(cmd_as_user(cmd), :stderr => :error, :stdout => :output, :chomp => true, :environment => environment)
when 'generate_types'
cmd = puppet
cmd << 'generate' << 'types' << '--environment' << arg

reply[:status] = run(cmd_as_user(cmd), :stderr => :error, :stdout => :output, :chomp => true, :environment => environment)
end
end
Expand Down
17 changes: 12 additions & 5 deletions templates/webhook.bin.erb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Server < Sinatra::Base

module_name = sanitize_input(module_name)
$logger.info("Deploying module #{module_name} in the background.")
Process.detach(fork { deploy_module(module_name) })
deploy_module(module_name)
end

# Simulate a github post:
Expand Down Expand Up @@ -209,7 +209,7 @@ class Server < Sinatra::Base
return 200
else
$logger.info("Deploying environment #{env} in the background")
Process.detach(fork { deploy(env, deleted) })
deploy(env, deleted)
end
end

Expand Down Expand Up @@ -256,9 +256,9 @@ class Server < Sinatra::Base
# more or less simultaneously. To mitigate, we just lock on a file and wait for
# the other one to complete.
file.flock(File::LOCK_EX)

message = "triggered: #{command}"
exec "#{command} &"
fork { exec "#{command}" }
exit_status = 0
raise "#{stdout}\n#{stderr}" if exit_status != 0
end
Expand All @@ -267,7 +267,14 @@ class Server < Sinatra::Base

def generate_types(environment)
begin
command = "#{$command_prefix} /opt/puppetlabs/bin/puppet generate types --environment #{environment.gsub(/\W/, '_')}"
if $config['use_mcollective']
command = "#{$command_prefix} mco r10k generate_types #{environment.gsub(/\W/, '_')}"
# because of the r10k racing condition issue wait for other r10k processes to finish
# https://github.com/voxpupuli/puppet-r10k/pull/268
Process.waitall
else
command = "#{$command_prefix} /opt/puppetlabs/bin/puppet generate types --environment #{environment.gsub(/\W/, '_')}"
end

message = run_command(command)
$logger.info("message: #{message} environment: #{environment}")
Expand Down