Permalink
Browse files

Refactorings:

- using modules properly, for the logger
- tweak to configs for the puppetcode option
- delete the util file as it's now a module method
  • Loading branch information...
1 parent a1aaa4f commit 16accb2a5ecf9b0f0824589537ca130aa5a6905b @simpsonjulian simpsonjulian committed May 17, 2012
View
@@ -22,8 +22,7 @@ program_desc 'Runs Puppet on cloud machines, with no PuppetMaster'
version Skewer::VERSION
desc 'Location of your Puppet Codebase'
-switch [:pc,:puppetcode]
-default_value '../infrastructure'
+flag [:pc,:puppetcode]
desc 'Hook script to invoke, after the Puppet run success'
flag [:hk,:hook]
@@ -52,6 +51,8 @@ command :update do |c|
c.flag [:r, :role]
c.action do |global_options,options,args|
+ puts global_options.inspect
+ puts options.inspect
require 'skewer/command/update'
Skewer::Command::Update.new(global_options, options).execute
end
@@ -22,7 +22,7 @@ Scenario: config in local file
"""
{"puppet_repo":"/tmp/skewer_test_code"}
"""
- When I run `./bin/skewer provision --cloud=stub --role=foobar --image=ami-deadbeef`
+ And I run `./bin/skewer provision --cloud=stub --role=foobar --image=ami-deadbeef`
Then the stdout should contain "Using Puppet Code from /tmp/skewer_test_code"
And the exit status should be 0
View
@@ -1,5 +1,4 @@
require 'config'
-require 'util'
require 'skewer'
module Skewer
@@ -25,7 +24,7 @@ def add_ssh_hostkey
unless self.host_key_exists(location)
system "ssh -o 'StrictHostKeyChecking no' -o 'PasswordAuthentication no' no_such_user@#{location} >/dev/null 2>&1"
else
- Skewer.logger.debug("SSH Host Key exists; not making it again")
+ logger.debug("SSH Host Key exists; not making it again")
end
end
@@ -40,7 +39,7 @@ def execute(file_name)
end
def install_gems
- Skewer.logger.debug "Installing Gems"
+ logger.debug "Installing Gems"
assets = File.join(File.dirname(__FILE__), '..', 'assets')
@node.scp File.join(File.expand_path(assets), 'Gemfile'), 'infrastructure'
command = ". /etc/profile.d/rubygems.sh && cd infrastructure && bundle install"
@@ -51,7 +50,7 @@ def add_key_to_agent(executor = Kernel, homedir = ENV['HOME'])
config = SkewerConfig.instance
key_name = config.get('key_name')
key_path = File.join(homedir, '.ssh', "#{key_name}.pem")
- Skewer.logger.debug "****Looking for #{key_path}"
+ logger.debug "****Looking for #{key_path}"
if File.exists?(key_path)
executor.system("ssh-add #{key_path}")
end
@@ -62,14 +61,14 @@ def sync_source()
require 'puppet_node'
config = SkewerConfig.instance
source_dir = config.get(:puppet_repo)
- Skewer.logger.debug "Using Puppet Code from #{source_dir}"
+ logger.debug "Using Puppet Code from #{source_dir}"
role = @options[:role]
if role
PuppetNode.new({:default => role.to_sym}).render
end
# TODO: if there's no role, it should look it up from an external source
if @mock
- Skewer.logger.debug "Mock: would normally rsync now"
+ logger.debug "Mock: would normally rsync now"
else
Source.new(source_dir).rsync(@node)
end
View
@@ -3,7 +3,6 @@
require 'skewer'
require 'bootstrapper'
-require 'util'
require 'hooks'
module Skewer
@@ -16,38 +15,36 @@ def initialize(options)
@options = options
@config = SkewerConfig.instance
@config.slurp_options(options)
- #@util = Util.new
- @config.set(:logger, Skewer.logger)
end
def select_node(cloud)
- Skewer.logger.debug "Evaluating cloud #{cloud}"
+ logger.debug "Evaluating cloud #{cloud}"
image = @options[:image]
case cloud.to_sym
when :ec2
require 'aws/security_group'
require 'aws/node'
require 'aws/service'
- Skewer.logger.debug 'Launching an EC2 node'
+ logger.debug 'Launching an EC2 node'
aws_group = @options[:group]
group = aws_group ? aws_group : 'default'
node = AWS::Node.new(image, [group]).node
when :rackspace
require 'rackspace/node'
- Skewer.logger.debug 'Launching a Rackspace node'
+ logger.debug 'Launching a Rackspace node'
node = Rackspace::Node.new(@config.get('flavor_id'), image, 'default').node
when :linode
raise "not implemented"
when :eucalyptus
- Skewer.logger.debug 'Using the EC2 API'
+ logger.debug 'Using the EC2 API'
require 'eucalyptus'
node = Eucalyptus.new
when :vagrant
- Skewer.logger.debug 'Launching a local vagrant node'
+ logger.debug 'Launching a local vagrant node'
require 'ersatz/ersatz_node.rb'
node = ErsatzNode.new('default', 'vagrant')
when :stub
- Skewer.logger.debug "Launching stubbed node for testing"
+ logger.debug "Launching stubbed node for testing"
require 'stub_node'
node = StubNode.new
when :ersatz
@@ -81,9 +78,9 @@ def go
Hooks.new(location).run
Cuke.new(@config.get(:cuke_dir), location).run if @config.get(:cuke_dir)
- Skewer.logger.debug "Node ready\n open http://#{location} or \n ssh -l #{node.username} #{location}"
+ logger.info "Node ready\n open http://#{location} or \n ssh -l #{node.username} #{location}"
rescue Exception => exception
- Skewer.logger.debug exception
+ logger.debug exception
raise "Something went wrong, and we killed the node"
ensure
destroy
View
@@ -4,6 +4,7 @@ module Skewer
# responsible for all configuration, once I move all the options in
class SkewerConfig
include Singleton
+ include Skewer
attr_accessor :aws_service, :puppet_repo, :region, :flavor_id, :aws_username, :flavor_id
def initialize
@@ -19,11 +20,11 @@ def reset
end
def read_config_file(config_file)
+ logger.debug "Looking for #{config_file}"
if File.exists?(config_file)
- Skewer.logger.debug "reading #{config_file}"
+ logger.debug "Reading #{config_file}"
config = File.read(config_file)
parse(config)
- Skewer.logger.debug self.inspect
end
end
@@ -35,7 +36,10 @@ def read_config_files
def parse(config)
require 'json'
configz = JSON.parse(config)
- configz.each { |key,value| set(key,value) }
+ configz.each do |key,value|
+ puts("Setting #{key}, #{value} from config")
+ set(key,value)
+ end
end
def set(attribute, value)
@@ -58,9 +62,13 @@ def self.get(key)
self.instance.get(key)
end
+ def translate_key(key)
+ key == :puppetcode ? :puppet_repo : key
+ end
+
def slurp_options(options)
options.each_pair do |key, value|
- self.set(key, value)
+ self.set(self.translate_key(key), value) unless value.nil?
end
end
end
View
@@ -15,7 +15,7 @@ def initialize(dir = nil, host = nil)
end
def run
- Skewer.logger.debug("Running cucumber hook")
+ logger.debug("Running cucumber hook")
`cd #{@dir}/.. && bundle install` if File.join(@dir, '..', 'Gemfile')
result = `cucumber #{@dir} SKEWER_HOST=#{@host}`
parsed = result.match(/failed/)[0] rescue false
@@ -3,6 +3,7 @@
module Skewer
# fakes a fog node
class ErsatzNode
+ include Skewer
attr_accessor :username, :dns_name
def initialize(hostname, user)
@@ -12,7 +13,7 @@ def initialize(hostname, user)
def ssh(command)
full_ssh_command = "ssh -l #{@username} #{@dns_name} '#{command}'"
- Skewer.logger.debug full_ssh_command
+ logger.debug full_ssh_command
stdout = `#{full_ssh_command}`
result = ErsatzSSHResult.new(command, stdout, $?.exitstatus)
[result]
View
@@ -3,13 +3,14 @@
module Skewer
# fakes a fog SSH result
class ErsatzSSHResult
+ include Skewer
attr_accessor :command, :stdout, :status
def initialize(command, stdout, status)
@command = command
@stdout = stdout
@status = status
- Skewer.logger.debug self.stdout
+ logger.debug stdout
end
end
end
View
@@ -3,6 +3,7 @@ module Skewer
# responsible for calling post-run hooks
class Hooks
+ include Skewer
attr_writer :command
@@ -14,7 +15,7 @@ def initialize(host_name)
def run
return_code = false
unless @command.nil?
- Skewer.logger.debug "Running hooks ..."
+ logger.debug "Running hooks ..."
`#{@command} #{@host_name}`
return_code = $? == 0 ? true : false
end
View
@@ -34,9 +34,9 @@ def destroy
def ssh(commands)
results = @ssh.run(commands)
if results.is_a?(Array)
- results.each {|result| Skewer.logger.debug result.stdout }
+ results.each {|result| logger.debug result.stdout }
else
- Skewer.logger.debug results.stdout
+ logger.debug results.stdout
end
end
end
View
@@ -4,6 +4,7 @@ module Skewer
require 'puppet_runtime_error'
# responsible for executing puppet
class Puppet
+ include Skewer
def arguments
[
"--modulepath modules",
@@ -37,10 +38,10 @@ def run(node, options)
command = command_string(node.username, options)
result = node.ssh(command)[0]
if result.status != 0
- Skewer.logger.debug result.stdout
+ logger.debug result.stdout
raise PuppetRuntimeError, "Puppet failed"
else
- Skewer.logger.debug "Puppet run succeeded"
+ logger.info "Puppet run succeeded"
end
result
end
View
@@ -1,13 +1,16 @@
module Skewer
require 'logger'
- class << self
- def logger
- @log = @log ? @log : Logger.new(STDOUT)
- end
+ def logger
+ @log = Logger.new(STDOUT)
+ end
- def logger=(logger)
- @log = logger
- end
+ # Get the location (dns name, or IP) for a given Fog server. Cloud
+ # provider agnostic.
+ def get_location(server = nil)
+ @has_dns_name = server.respond_to?(:dns_name)
+ return nil unless (@has_dns_name or server.respond_to?(:public_ip_address))
+ @has_dns_name ? server.dns_name : server.public_ip_address
end
+
end
View
@@ -1,4 +1,4 @@
-require 'util'
+require 'skewer'
module Skewer
# responsible for moving source to remote nodes
@@ -8,7 +8,7 @@ def initialize(path = nil)
#@util = Util.new
raise "I can't see the path #{path}" unless File.exists?(path)
@path = path.sub(/\/$/, '')
- @logger = Skewer.logger
+
end
def excludes
@@ -30,7 +30,7 @@ def rsync_command(node)
end
def mock_rsync(command)
- @logger.debug "MOCK: #{command}"
+ logger.debug "MOCK: #{command}"
end
def real_rsync(node, command)
@@ -40,11 +40,11 @@ def real_rsync(node, command)
def rsync(node)
location = get_location(node)
- @logger.debug "Copying code to #{location} ..."
+ logger.debug "Copying code to #{location} ..."
create_destination(node)
command = self.rsync_command(node)
node.class.to_s =~ /StubNode/ ? mock_rsync(command) : real_rsync(node, command)
- @logger.debug " Done."
+ logger.debug " Done."
end
end
end
View
@@ -1,23 +1,23 @@
module Skewer
# test stub for pretending to be a real node
class StubNode
+ include Skewer
attr_reader :dns_name, :username, :public_ip_address
def initialize
@dns_name = 'com.doodoo'
@public_ip_address = '192.168.0.1'
@username = 'imabirdbrain'
- @debug = false
end
def announce(return_type)
- Skewer.logger.debug "#{self.class} will return #{return_type}" if @debug
+ logger.debug "#{self.class} will return #{return_type}"
return_type
end
def method_missing(name, *args)
require 'ersatz/ssh_result.rb'
- Skewer.logger.debug "#{self.class}.#{name} called with #{args.join(',')}" if @debug
+ logger.debug "#{self.class}.#{name} called with #{args.join(',')}"
return announce([ErsatzSSHResult.new('foo', 'success', 0)]) if name == :ssh
announce true
end
View
@@ -1,11 +0,0 @@
-module Skewer
-
- # Get the location (dns name, or IP) for a given Fog server. Cloud
- # provider agnostic.
- def get_location(server = nil)
- @has_dns_name = server.respond_to?(:dns_name)
- return nil unless (@has_dns_name or server.respond_to?(:public_ip_address))
- @has_dns_name ? server.dns_name : server.public_ip_address
- end
-
-end
View
@@ -65,4 +65,11 @@
config = Skewer::SkewerConfig.instance
config.region.should == 'us-east-1'
end
+
+ it "should support some old options" do
+ config = Skewer::SkewerConfig.instance
+ config.get(:puppet_repo).should == '../infrastructure'
+ config.slurp_options({:puppetcode => '../foo'})
+ config.get(:puppet_repo).should == '../foo'
+ end
end
Oops, something went wrong.

0 comments on commit 16accb2

Please sign in to comment.