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

Adding an option to get only envs and nodes. #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ begin
gem.email = "[email protected]"
gem.homepage = "http://github.com/opscode/chef-rundeck"
gem.authors = ["Adam Jacob"]
gem.add_dependency "sinatra"
gem.add_dependency "chef"
gem.add_dependency "mixlib-cli"
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
Expand All @@ -19,15 +22,15 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.rspec_opts = [ '-I', 'lib', '-I', 'spec' ]
spec.pattern = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rspec_opts = [ '-I', 'lib', '-I', 'spec' ]
spec.pattern = FileList['spec/**/*_spec.rb']
spec.rcov = true
end

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0
8 changes: 8 additions & 0 deletions bin/chef-rundeck
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class ChefRundeckCLI
:long => "--port PORT",
:description => "The port to run on, default 9980",
:default => 9980

option :env_node_only,
:short => "-e",
:long => "--env-node-only",
:description => "Get nodes and their envs only",
:boolean => true

end

cli = ChefRundeckCLI.new
Expand All @@ -54,6 +61,7 @@ cli.parse_options
ChefRundeck.config_file = cli.config[:config_file]
ChefRundeck.username = cli.config[:username]
ChefRundeck.web_ui_url = cli.config[:web_ui_url]
ChefRundeck.env_node_only = cli.config[:env_node_only]
ChefRundeck.configure

ChefRundeck.run! :host => 'localhost', :port => cli.config[:port]
Expand Down
62 changes: 62 additions & 0 deletions chef-rundeck.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = "chef-rundeck"
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Adam Jacob"]
s.date = "2011-11-07"
s.description = "Provides a resource endpoint for RunDeck from a Chef Server"
s.email = "[email protected]"
s.executables = ["chef-rundeck"]
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
]
s.files = [
".document",
"LICENSE",
"NOTICE",
"README.rdoc",
"Rakefile",
"VERSION",
"bin/chef-rundeck",
"lib/chef-rundeck.rb",
"spec/chef-rundeck_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = "http://github.com/opscode/chef-rundeck"
s.require_paths = ["lib"]
s.rubygems_version = "1.8.10"
s.summary = "Integrates Chef with RunDeck"

if s.respond_to? :specification_version then
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<sinatra>, [">= 0"])
s.add_runtime_dependency(%q<chef>, [">= 0"])
s.add_runtime_dependency(%q<mixlib-cli>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
s.add_development_dependency(%q<yard>, [">= 0"])
else
s.add_dependency(%q<sinatra>, [">= 0"])
s.add_dependency(%q<chef>, [">= 0"])
s.add_dependency(%q<mixlib-cli>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<yard>, [">= 0"])
end
else
s.add_dependency(%q<sinatra>, [">= 0"])
s.add_dependency(%q<chef>, [">= 0"])
s.add_dependency(%q<mixlib-cli>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<yard>, [">= 0"])
end
end

75 changes: 58 additions & 17 deletions lib/chef-rundeck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,74 @@ class << self
attr_accessor :config_file
attr_accessor :username
attr_accessor :web_ui_url
attr_accessor :env_node_only

def configure
Chef::Config.from_file(ChefRundeck.config_file)
Chef::Log.level = Chef::Config[:log_level]
end
end

set :environment, :production
set :lock, true
get '/' do

content_type 'text/xml'
response = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE project PUBLIC "-//DTO Labs Inc.//DTD Resources Document 1.0//EN" "project.dtd"><project>'
Chef::Node.list(true).each do |node_array|
node = node_array[1]
response << <<-EOH
<node name="#{xml_escape(node[:fqdn])}"
type="Node"
description="#{xml_escape(node.name)}"
osArch="#{xml_escape(node[:kernel][:machine])}"
osFamily="#{xml_escape(node[:kernel][:name])}"
osName="#{xml_escape(node[:platform])}"
osVersion="#{xml_escape(node[:platform_version])}"
tags="#{xml_escape([node.chef_environment, node.run_list.roles.join(',')].join(','))}"
username="#{xml_escape(ChefRundeck.username)}"
hostname="#{xml_escape(node[:fqdn])}"
editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node.name)}/edit"/>
if ChefRundeck.env_node_only
#Get all Chef envs, get all nodes for each env
#Use inflate = false to reduce load time
#Tag nodes with their envs in a hash
#Expand hash into response
node_hash = {}
Chef::Environment.list(false).each do |envr|
Chef::Node.list_by_environment(envr[0], false).each do |node_info|
#print envr[0], "->", node_arr[0], "->", node_arr[1], "\n"
node = node_info[0]
if node_hash.has_key?(node)
node_hash[node] = "#{node_hash[node]} , #{envr[0]}"
else
node_hash[node] = envr[0]
end
end
end
node_hash.sort_by{ |envs, nodes| nodes }
node_hash.each_pair do |nodename, nodeenv|
nodename = xml_escape(nodename)
response << <<-EOH
<node name="#{nodename}"
type="Node"
osFamily="unix"
tags="#{xml_escape(nodeenv)}"
username="#{xml_escape(ChefRundeck.username)}"
hostname="#{nodename}"/>
EOH
end
response << "</project>"
response
else
Chef::Node.list(true).each do |node_array|
node = node_array[1]
#--
# Certain features in Rundeck require the osFamily value to be set to 'unix' to work appropriately. - SRK
#++
os_family = node[:kernel][:os] =~ /windows/i ? 'windows' : 'unix'
response << <<-EOH
<node name="#{xml_escape(node[:fqdn])}"
type="Node"
description="#{xml_escape(node.name)}"
osArch="#{xml_escape(node[:kernel][:machine])}"
osFamily="#{xml_escape(os_family)}"
osName="#{xml_escape(node[:platform])}"
osVersion="#{xml_escape(node[:platform_version])}"
tags="#{xml_escape([node.chef_environment, node.run_list.roles.join(',')].join(','))}"
username="#{xml_escape(ChefRundeck.username)}"
hostname="#{xml_escape(node[:fqdn])}"
editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node.name)}/edit"/>
EOH
end
response << "</project>"
response
end
response << "</project>"
response
end
end