Module: KnifeSpork::Runner::InstanceMethods
- Defined in:
- lib/knife-spork/runner.rb
Instance Method Summary collapse
- #all_cookbooks ⇒ Object
- #calc_diff(cookbook, version) ⇒ Object
- #constraints_diff(environment_diff) ⇒ Object
-
#cookbook_path ⇒ Object
It’s not feasible to try and “guess” which cookbook path to use, so we will always just use the first one in the path.
- #default_environments ⇒ Object
- #ensure_cookbook_path! ⇒ Object
- #ensure_environment_provided! ⇒ Object
- #environment_diff(local_environment, remote_environment) ⇒ Object
- #environment_path ⇒ Object
- #load_cookbook(cookbook_name) ⇒ Object
- #load_cookbooks(cookbook_names) ⇒ Object
- #load_environment(environment_name) ⇒ Object
- #load_environments_and_cookbook ⇒ Object
- #load_remote_environment(environment_name) ⇒ Object
- #loader ⇒ Object
- #pretty_print_json(json) ⇒ Object
- #run_plugins(hook) ⇒ Object
- #spork_config ⇒ Object
- #valid_version?(version) ⇒ Boolean
- #validate_version!(version) ⇒ Object
Instance Method Details
#all_cookbooks ⇒ Object
97 98 99 |
# File 'lib/knife-spork/runner.rb', line 97 def all_cookbooks ::Chef::CookbookLoader.new(::Chef::Config.cookbook_path) end |
#calc_diff(cookbook, version) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/knife-spork/runner.rb', line 151 def calc_diff(cookbook, version) components = version.map{|v|v.split(".")} if components.length < 2 ui.warn "#{cookbook} has no remote version to diff against!" return 0 end if components[1][0].to_i != components[0][0].to_i return (components[1][0].to_i - components[0][0].to_i)*100 elsif components[1][1].to_i != components[0][1].to_i return (components[1][1].to_i - components[0][1].to_i)*10 else return (components[1][2].to_i - components[0][2].to_i) end end |
#constraints_diff(environment_diff) ⇒ Object
147 148 149 |
# File 'lib/knife-spork/runner.rb', line 147 def constraints_diff (environment_diff) Hash[Hash[environment_diff.map{|k,v| [k, v.split(" changed to ").map{|x|x.gsub("= ","")}]}].map{|k,v|[k,calc_diff(k,v)]}] end |
#cookbook_path ⇒ Object
It’s not feasible to try and “guess” which cookbook path to use, so we will always just use the first one in the path.
88 89 90 91 |
# File 'lib/knife-spork/runner.rb', line 88 def cookbook_path ensure_cookbook_path! [config[:cookbook_path] ||= ::Chef::Config.cookbook_path].flatten[0] end |
#default_environments ⇒ Object
61 62 63 |
# File 'lib/knife-spork/runner.rb', line 61 def default_environments [ spork_config.default_environment || spork_config.default_environments ].flatten.compact end |
#ensure_cookbook_path! ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/knife-spork/runner.rb', line 168 def ensure_cookbook_path! if !config.has_key?(:cookbook_path) ui.fatal "No default cookbook_path; Specify with -o or fix your knife.rb." show_usage exit(1) end end |
#ensure_environment_provided! ⇒ Object
54 55 56 57 58 59 |
# File 'lib/knife-spork/runner.rb', line 54 def ensure_environment_provided! if default_environments.empty? && @name_args.size < 2 ui.error('You must specify a cookbook name and an environment') exit(1) end end |
#environment_diff(local_environment, remote_environment) ⇒ Object
141 142 143 144 145 |
# File 'lib/knife-spork/runner.rb', line 141 def environment_diff(local_environment, remote_environment) local_environment_versions = local_environment.to_hash['cookbook_versions'] remote_environment_versions = remote_environment.to_hash['cookbook_versions'] remote_environment_versions.diff(local_environment_versions) end |
#environment_path ⇒ Object
93 94 95 |
# File 'lib/knife-spork/runner.rb', line 93 def environment_path spork_config[:environment_path] || cookbook_path.gsub("/cookbooks","/environments") end |
#load_cookbook(cookbook_name) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/knife-spork/runner.rb', line 101 def load_cookbook(cookbook_name) return cookbook_name if cookbook_name.is_a?(::Chef::CookbookVersion) # Search the local chef repo first loader = ::Chef::CookbookLoader.new(Chef::Config.cookbook_path) if loader.has_key?(cookbook_name) return loader[cookbook_name] end # We didn't find the cookbook in our local repo, so check Berkshelf if defined?(::Berkshelf) berksfile = ::Berkshelf::Berksfile.from_file(self.config[:berksfile]) if cookbook = berksfile.sources.find{ |source| source.name == cookbook_name } return cookbook end end # TODO: add librarian support here raise ::Chef::Exceptions::CookbookNotFound, "Could not find cookbook '#{cookbook_name}' in any of the sources!" end |
#load_cookbooks(cookbook_names) ⇒ Object
123 124 125 126 |
# File 'lib/knife-spork/runner.rb', line 123 def load_cookbooks(cookbook_names) cookbook_names = [cookbook_names].flatten cookbook_names.collect{ |cookbook_name| load_cookbook(cookbook_name) } end |
#load_environment(environment_name) ⇒ Object
128 129 130 |
# File 'lib/knife-spork/runner.rb', line 128 def load_environment(environment_name) loader.object_from_file("#{environment_path}/#{environment_name}.json") end |
#load_environments_and_cookbook ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/knife-spork/runner.rb', line 44 def load_environments_and_cookbook ensure_environment_provided! if @name_args.size == 2 [ [@name_args[0]].flatten, @name_args[1] ] elsif @name_args.size == 1 [ [default_environments].flatten, @name_args[0] ] end end |
#load_remote_environment(environment_name) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/knife-spork/runner.rb', line 132 def load_remote_environment(environment_name) begin Chef::Environment.load(environment_name) rescue Net::HTTPServerException => e ui.error "Could not load #{environment_name} from Chef Server. You must upload the environment manually the first time." exit(1) end end |
#loader ⇒ Object
82 83 84 |
# File 'lib/knife-spork/runner.rb', line 82 def loader @loader ||= Chef::Knife::Core::ObjectLoader.new(::Chef::Environment, ui) end |
#pretty_print_json(json) ⇒ Object
65 66 67 |
# File 'lib/knife-spork/runner.rb', line 65 def pretty_print_json(json) JSON.pretty_generate(json) end |
#run_plugins(hook) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/knife-spork/runner.rb', line 27 def run_plugins(hook) cookbooks = [ @cookbooks || @cookbook ].flatten.compact.collect{|cookbook| cookbook.is_a?(::Chef::CookbookVersion) ? cookbook : load_cookbook(cookbook)}.sort{|a,b| a.name.to_s <=> b.name.to_s} environments = [ @environments || @environment ].flatten.compact.collect{|environment| environment.is_a?(::Chef::Environment) ? environment : load_environment(environment)}.sort{|a,b| a.name.to_s <=> b.name.to_s} environment_diffs = @environment_diffs KnifeSpork::Plugins.run( :config => spork_config, :hook => hook.to_sym, :cookbooks => cookbooks, :environments => environments, :environment_diffs => environment_diffs, :environment_path => environment_path, :cookbook_path => cookbook_path, :ui => ui ) end |
#spork_config ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/knife-spork/runner.rb', line 13 def spork_config return @spork_config unless @spork_config.nil? @spork_config = AppConf.new load_paths = [ File.("#{cookbook_path.gsub('cookbooks','')}/config/spork-config.yml"), File.('config/spork-config.yml'), '/etc/spork-config.yml', File.('~/.chef/spork-config.yml') ] load_paths.each do |load_path| if File.exists?(load_path) @spork_config.load(load_path) end end @spork_config end |
#valid_version?(version) ⇒ Boolean
69 70 71 72 73 |
# File 'lib/knife-spork/runner.rb', line 69 def valid_version?(version) version_keys = version.split('.') return false unless version_keys.size == 3 && version_keys.any?{ |k| begin Float(k); rescue false; else true; end } true end |
#validate_version!(version) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/knife-spork/runner.rb', line 75 def validate_version!(version) if version && !valid_version?(version) ui.error("#{version} is not a valid version!") exit(1) end end |