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_loader ⇒ Object
- #environment_path ⇒ Object
- #hash_diff(hash, other) ⇒ Object
- #json_diff(a, b) ⇒ Object
- #load_cookbook(name) ⇒ Object
- #load_cookbooks(cookbook_names) ⇒ Object
- #load_databag(bag) ⇒ Object
- #load_databag_item(bag, item_name) ⇒ Object
- #load_environment(environment_name) ⇒ Object
- #load_environment_from_file(environment_name) ⇒ Object
- #load_environments_and_cookbook ⇒ Object
- #load_from_berkshelf(name) ⇒ Object
- #load_from_chef(name) ⇒ Object
- #load_from_librarian(name) ⇒ Object
- #load_node(node) ⇒ Object
- #load_remote_environment(environment_name) ⇒ Object
- #load_role(role_name) ⇒ Object
- #load_role_from_file(role_name) ⇒ Object
- #load_specified_environment_group(name) ⇒ Object
- #pretty_print_json(json) ⇒ Object
- #role_loader ⇒ Object
- #role_path ⇒ Object
- #run_plugins(hook) ⇒ Object
- #spork_config ⇒ Object
- #valid_version?(version) ⇒ Boolean
- #validate_version!(version) ⇒ Object
Instance Method Details
#all_cookbooks ⇒ Object
130 131 132 |
# File 'lib/knife-spork/runner.rb', line 130 def all_cookbooks ::Chef::CookbookLoader.new(::Chef::Config.cookbook_path) end |
#calc_diff(cookbook, version) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/knife-spork/runner.rb', line 246 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
242 243 244 |
# File 'lib/knife-spork/runner.rb', line 242 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.
117 118 119 120 |
# File 'lib/knife-spork/runner.rb', line 117 def cookbook_path ensure_cookbook_path! [config[:cookbook_path] ||= ::Chef::Config.cookbook_path].flatten[0] end |
#default_environments ⇒ Object
86 87 88 |
# File 'lib/knife-spork/runner.rb', line 86 def default_environments [ spork_config.default_environment || spork_config.default_environments ].flatten.compact end |
#ensure_cookbook_path! ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/knife-spork/runner.rb', line 263 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
79 80 81 82 83 84 |
# File 'lib/knife-spork/runner.rb', line 79 def ensure_environment_provided! if default_environments.empty? && @name_args.size < 2 ui.error('You must specify an environment or environment group and a cookbook name') exit(1) end end |
#environment_diff(local_environment, remote_environment) ⇒ Object
221 222 223 224 225 |
# File 'lib/knife-spork/runner.rb', line 221 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'] hash_diff remote_environment_versions, local_environment_versions end |
#environment_loader ⇒ Object
107 108 109 |
# File 'lib/knife-spork/runner.rb', line 107 def environment_loader @environment_loader ||= Chef::Knife::Core::ObjectLoader.new(::Chef::Environment, ui) end |
#environment_path ⇒ Object
122 123 124 |
# File 'lib/knife-spork/runner.rb', line 122 def environment_path spork_config[:environment_path] || cookbook_path.gsub("/cookbooks","/environments") end |
#hash_diff(hash, other) ⇒ Object
233 234 235 236 237 238 239 240 |
# File 'lib/knife-spork/runner.rb', line 233 def hash_diff(hash, other) hash.keys.inject({}) do |memo, key| unless hash[key] == other[key] memo[key] = "#{hash[key]} changed to #{other[key]}" end memo end end |
#json_diff(a, b) ⇒ Object
227 228 229 230 231 |
# File 'lib/knife-spork/runner.rb', line 227 def json_diff(a, b) pre_json = JSON.parse(a.respond_to?(:to_json) ? a.to_json : a) post_json = JSON.parse(b.respond_to?(:to_json) ? b.to_json : b) Diffy::Diff.new(JSON.pretty_generate(pre_json), JSON.pretty_generate(post_json), :diff=>"-U 3").to_s.gsub(/[()]/, '\\\\\0') end |
#load_cookbook(name) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/knife-spork/runner.rb', line 134 def load_cookbook(name) return name if name.is_a?(Chef::CookbookVersion) cookbook = load_from_chef(name) || load_from_berkshelf(name) || load_from_librarian(name) cookbook || raise(Chef::Exceptions::CookbookNotFound, "Could not find cookbook '#{name}' in any of the sources!") end |
#load_cookbooks(cookbook_names) ⇒ Object
178 179 180 181 |
# File 'lib/knife-spork/runner.rb', line 178 def load_cookbooks(cookbook_names) cookbook_names = [cookbook_names].flatten cookbook_names.collect{ |cookbook_name| load_cookbook(cookbook_name) } end |
#load_databag(bag) ⇒ Object
196 197 198 |
# File 'lib/knife-spork/runner.rb', line 196 def load_databag(bag) Chef::DataBag.load(bag) end |
#load_databag_item(bag, item_name) ⇒ Object
200 201 202 |
# File 'lib/knife-spork/runner.rb', line 200 def load_databag_item(bag, item_name) Chef::DataBagItem.load(bag, item_name) end |
#load_environment(environment_name) ⇒ Object
204 205 206 |
# File 'lib/knife-spork/runner.rb', line 204 def load_environment(environment_name) Chef::Environment.load(environment_name) end |
#load_environment_from_file(environment_name) ⇒ Object
208 209 210 |
# File 'lib/knife-spork/runner.rb', line 208 def load_environment_from_file(environment_name) environment_loader.object_from_file("#{environment_path}/#{environment_name}.json") end |
#load_environments_and_cookbook ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knife-spork/runner.rb', line 60 def load_environments_and_cookbook ensure_environment_provided! if @name_args.size == 2 environments = load_specified_environment_group(@name_args[0]) [ environments, @name_args[1] ] elsif @name_args.size == 1 [ [default_environments].flatten, @name_args[0] ] end end |
#load_from_berkshelf(name) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/knife-spork/runner.rb', line 150 def load_from_berkshelf(name) return unless defined?(::Berkshelf) berksfile = ::Berkshelf::Berksfile.from_file(self.config[:berksfile]) lockfile = ::Berkshelf::Lockfile.new(berksfile) raise Berkshelf::BerkshelfError, "LockFileNotFound" unless File.exists?(lockfile.filepath) cookbook = Berkshelf.ui.mute { self.config[:skip_dependencies] ||= false berksfile.resolve(lockfile.find(name), {skip_dependencies: self.config[:skip_dependencies]})[:solution].first } # convert Berkshelf::CachedCookbook to Chef::CookbookVersion by loading the cookback at the # full path returned by Berkshelf (since a cookbook of name `x` is not necessarily in a # local directory named `x`.) cookbook_path = cookbook.path chef_search_dir = ::File.dirname(cookbook_path) cookbook_dirname = ::File.basename(cookbook_path) ::Chef::CookbookLoader.new(chef_search_dir).load_cookbook(cookbook_dirname) end |
#load_from_chef(name) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/knife-spork/runner.rb', line 143 def load_from_chef(name) all_cookbooks[name] rescue Chef::Exceptions::CookbookNotFound, Chef::Exceptions::CookbookNotFoundInRepo nil end |
#load_from_librarian(name) ⇒ Object
173 174 175 176 |
# File 'lib/knife-spork/runner.rb', line 173 def load_from_librarian(name) # Your code here :) nil end |
#load_node(node) ⇒ Object
192 193 194 |
# File 'lib/knife-spork/runner.rb', line 192 def load_node(node) Chef::Node.load(node) end |
#load_remote_environment(environment_name) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/knife-spork/runner.rb', line 212 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 |
#load_role(role_name) ⇒ Object
188 189 190 |
# File 'lib/knife-spork/runner.rb', line 188 def load_role(role_name) Chef::Role.load(role_name) end |
#load_role_from_file(role_name) ⇒ Object
184 185 186 |
# File 'lib/knife-spork/runner.rb', line 184 def load_role_from_file(role_name) role_loader.object_from_file("#{role_path}/#{role_name}.json") end |
#load_specified_environment_group(name) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/knife-spork/runner.rb', line 71 def load_specified_environment_group(name) if !spork_config.environment_groups.nil? && spork_config.environment_groups.keys.include?(name) spork_config.environment_groups[name] else [name] end end |
#pretty_print_json(json) ⇒ Object
90 91 92 |
# File 'lib/knife-spork/runner.rb', line 90 def pretty_print_json(json) JSON.pretty_generate(json) end |
#role_loader ⇒ Object
111 112 113 |
# File 'lib/knife-spork/runner.rb', line 111 def role_loader @role_loader ||= Chef::Knife::Core::ObjectLoader.new(::Chef::Role, ui) end |
#role_path ⇒ Object
126 127 128 |
# File 'lib/knife-spork/runner.rb', line 126 def role_path spork_config[:role_path] || cookbook_path.gsub("/cookbooks","/roles") end |
#run_plugins(hook) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/knife-spork/runner.rb', line 33 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} # Affects promote only: # Set loaded cookbook version if the -v or --version parameter was specified # Otherwise the version on disk, often more recent will be used. # We know cookbooks will only contain one cookbook in the case of promote. cookbooks.map{|c|c.version = config[:version]} if config[:version] environments = [ @environments || @environment ].flatten.compact.collect{|environment| environment.is_a?(::Chef::Environment) ? environment : load_environment_from_file(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, :object_name => @object_name, :object_secondary_name => @object_secondary_name, :object_difference => @object_difference, :ui => ui ) end |
#spork_config ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/knife-spork/runner.rb', line 19 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
94 95 96 97 98 |
# File 'lib/knife-spork/runner.rb', line 94 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
100 101 102 103 104 105 |
# File 'lib/knife-spork/runner.rb', line 100 def validate_version!(version) if version && !valid_version?(version) ui.error("#{version} is not a valid version!") exit(1) end end |