Class: KitchenHooks::App
- Inherits:
-
Sinatra::Application
- Object
- Sinatra::Application
- KitchenHooks::App
- Defined in:
- lib/kitchen_hooks/app.rb,
lib/kitchen_hooks/helpers.rb
Class Method Summary collapse
- .apply_constraints(constraints, environment, knife) ⇒ Object
- .author(event) ⇒ Object
- .backlog! ⇒ Object
- .berks_install(berksfile, knife = nil) ⇒ Object
- .berks_upload(berksfile, knife, options = {}) ⇒ Object
- .berkshelf_config(knife) ⇒ Object
- .close! ⇒ Object
- .commit_to_data_bags?(event) ⇒ Boolean
- .commit_to_environments?(event) ⇒ Boolean
- .commit_to_kitchen?(event) ⇒ Boolean
- .commit_to_master?(event) ⇒ Boolean
- .commit_to_realm?(event) ⇒ Boolean
- .commit_to_roles?(event) ⇒ Boolean
- .config!(config) ⇒ Object
- .cookbook_name(event) ⇒ Object
- .cookbook_repo?(event) ⇒ Boolean
- .data_bag_from_file(data_bag, items, knives) ⇒ Object
- .data_bag_repo?(event) ⇒ Boolean
- .db!(path = @@db_path) ⇒ Object
- .environment_from_file(environments, knives) ⇒ Object
- .environment_repo?(event) ⇒ Boolean
- .files_pushed(event) ⇒ Object
- .generic_details(event) ⇒ Object
- .get_environment(environment, knife) ⇒ Object
- .git_daemon_style_url(event) ⇒ Object
- .gitlab_tag_url(event) ⇒ Object
- .gitlab_url(event) ⇒ Object
- .humanize_seconds(secs) ⇒ Object
- .kitchen_upload(knives) ⇒ Object
- .latest_commit(event) ⇒ Object
- .lockfile_constraints(lockfile_path) ⇒ Object
- .not_deleted?(event) ⇒ Boolean
- .notification(entry) ⇒ Object
- .perform_constraint_application(event, knives) ⇒ Object
- .perform_cookbook_upload(event, knives) ⇒ Object
- .perform_kitchen_upload(event, knives) ⇒ Object
- .perform_knife_cookbook_upload(event, knives) ⇒ Object
- .perform_upload_from_file(event, knives) ⇒ Object
- .pluralize(n, singular, plural = nil) ⇒ Object
- .push_details(event) ⇒ Object
- .repo_name(event) ⇒ Object
- .repo_namespace(event) ⇒ Object
- .repo_url(event) ⇒ Object
- .report_error(e, msg = nil) ⇒ Object
- .role_from_file(roles, knives) ⇒ Object
- .role_repo?(event) ⇒ Boolean
- .slack_notification(entry) ⇒ Object
- .sync! ⇒ Object
- .tag_name(event) ⇒ Object
- .tagged_commit(event) ⇒ Object
- .tagged_commit_to_cookbook?(event) ⇒ Boolean
- .tagged_commit_to_realm?(event) ⇒ Boolean
- .tmp!(dir) ⇒ Object
- .tmp_clone(event, commit_method) {|dir| ... } ⇒ Object
- .upload_environment(environment, knife) ⇒ Object
- .verify_constraints(constraints, environment, knife) ⇒ Object
- .version_link(event) ⇒ Object
- .version_url(event) ⇒ Object
- .with_each_knife(command, knives) ⇒ Object
- .with_each_knife_do(command, knives) ⇒ Object
Instance Method Summary collapse
- #generic_details(e) ⇒ Object
- #notification(e) ⇒ Object
- #push_details(e) ⇒ Object
- #slack_notification(e) ⇒ Object
Class Method Details
.apply_constraints(constraints, environment, knife) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/kitchen_hooks/helpers.rb', line 440 def self.apply_constraints constraints, environment, knife # Ripped from Berkshelf::Cli::apply and Berkshelf::Lockfile::apply # https://github.com/berkshelf/berkshelf/blob/master/lib/berkshelf/cli.rb # https://github.com/berkshelf/berkshelf/blob/master/lib/berkshelf/lockfile.rb $stdout.puts 'started apply_constraints environment=%s, knife=%s' % [ environment.inspect, knife.inspect ] chef_environment = get_environment environment, knife raise 'Could not find environment "%s"' % environment if chef_environment.nil? chef_environment.cookbook_versions = constraints chef_environment.save $stdout.puts 'finished apply_constraints: %s' % environment end |
.author(event) ⇒ Object
642 643 644 |
# File 'lib/kitchen_hooks/helpers.rb', line 642 def self. event event['user_name'] end |
.backlog! ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/kitchen_hooks/app.rb', line 37 def self.backlog! @@backlog = Queue.new @@backlog_worker = Thread.new do loop do event = @@backlog.shift App.process event end end end |
.berks_install(berksfile, knife = nil) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/kitchen_hooks/helpers.rb', line 320 def self.berks_install berksfile, knife=nil $stdout.puts 'started berks_install berksfile=%s' % berksfile.inspect env_git_dir = ENV.delete 'GIT_DIR' env_git_work_tree = ENV.delete 'GIT_WORK_TREE' knife_args = if knife '--config %s' % Shellwords::escape(berkshelf_config knife) end cmd = 'berks install --debug --berksfile %s %s 2>&1' % [ Shellwords::escape(berksfile), knife_args ] begin $stdout.puts "berks_install: %s" % cmd out = `#{cmd}` raise out unless $?.exitstatus.zero? rescue raise 'Could not perform berks_install with config %s' % [ berksfile.inspect ] end ENV['GIT_DIR'] = env_git_dir ENV['GIT_WORK_TREE'] = env_git_work_tree $stdout.puts 'finished berks_install: %s' % berksfile end |
.berks_upload(berksfile, knife, options = {}) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/kitchen_hooks/helpers.rb', line 348 def self.berks_upload berksfile, knife, ={} $stdout.puts 'started berks_upload berksfile=%s, knife=%s' % [ berksfile.inspect, knife.inspect ] config_path = berkshelf_config(knife) cmd = 'berks upload --no-ssl-verify --debug --berksfile %s --config %s 2>&1' % [ Shellwords::escape(berksfile), Shellwords::escape(config_path) ] begin $stdout.puts "berks_upload: %s" % cmd out = `#{cmd}` raise out unless $?.exitstatus.zero? rescue raise 'Could not perform berks_upload with config %s, knife %s' % [ berksfile.inspect, knife.inspect ] end FileUtils.rm_rf config_path $stdout.puts 'finished berks_upload: %s' % berksfile end |
.berkshelf_config(knife) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/kitchen_hooks/helpers.rb', line 299 def self.berkshelf_config knife ridley = Ridley::from_chef_config knife, ssl: { verify: false } config = { chef: { node_name: ridley.client_name, client_key: ridley.client_key, chef_server_url: ridley.server_url }, ssl: { verify: false } } $stdout.puts 'Berkshelf config: %s' % JSON::pretty_generate(config) config_path = File.join tmp, "#{SecureRandom.hex}-berkshelf.json" File.open(config_path, 'w') do |f| f.puts JSON::generate(config) end return config_path end |
.close! ⇒ Object
30 31 32 33 34 35 |
# File 'lib/kitchen_hooks/app.rb', line 30 def self.close! @@sync_worker.kill if defined? @@sync_worker @@backlog_worker.kill @@db.flush @@db.close end |
.commit_to_data_bags?(event) ⇒ Boolean
749 750 751 |
# File 'lib/kitchen_hooks/helpers.rb', line 749 def self.commit_to_data_bags? event data_bag_repo?(event) && not_deleted?(event) end |
.commit_to_environments?(event) ⇒ Boolean
744 745 746 |
# File 'lib/kitchen_hooks/helpers.rb', line 744 def self.commit_to_environments? event environment_repo?(event) && not_deleted?(event) end |
.commit_to_kitchen?(event) ⇒ Boolean
734 735 736 |
# File 'lib/kitchen_hooks/helpers.rb', line 734 def self.commit_to_kitchen? event repo_name(event) == 'kitchen' && not_deleted?(event) end |
.commit_to_master?(event) ⇒ Boolean
724 725 726 |
# File 'lib/kitchen_hooks/helpers.rb', line 724 def self.commit_to_master? event event['ref'] == 'refs/heads/master' end |
.commit_to_realm?(event) ⇒ Boolean
754 755 756 |
# File 'lib/kitchen_hooks/helpers.rb', line 754 def self.commit_to_realm? event repo_name(event) =~ /^realm_/ end |
.commit_to_roles?(event) ⇒ Boolean
739 740 741 |
# File 'lib/kitchen_hooks/helpers.rb', line 739 def self.commit_to_roles? event role_repo?(event) && not_deleted?(event) end |
.config!(config) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kitchen_hooks/app.rb', line 58 def self.config! config $stderr.puts 'ENV: %s' % JSON::pretty_generate(ENV.to_hash) @@config = config @@hipchat = nil if config['hipchat'] @@hipchat = HipChat::Client.new config['hipchat']['token'] @@hipchat_nick = config['hipchat']['nick'] || raise('No HipChat "nick" provided') @@hipchat_room = config['hipchat']['room'] || raise('No HipChat "room" provided') end @@slack = nil if config['slack'] @@slack_token = config['slack']['token'] || raise('No Slack "token" provided') @@slack_channel = config['slack']['channel'] || raise('No Slack "channel" provided') require_relative 'slack_api' @@slack = SlackAPI.new token: @@slack_token end @@git_protocol = config.fetch('git_protocol', 'daemon') @@cookbook_upload_method = config.fetch('cookbook_upload_method', 'berkshelf') @@knives = config['knives'].map do |_, knife| Pathname.new(knife)..realpath.to_s end @@sync_interval = config['sync_interval'] end |
.cookbook_name(event) ⇒ Object
656 657 658 |
# File 'lib/kitchen_hooks/helpers.rb', line 656 def self.cookbook_name event repo_name(event).sub(/^(app|base|realm|fork)_/, 'bjn_') end |
.cookbook_repo?(event) ⇒ Boolean
661 662 663 664 665 |
# File 'lib/kitchen_hooks/helpers.rb', line 661 def self.cookbook_repo? event repo_name(event) =~ /^(app|base|realm|fork)_/ || repo_name(event) =~ /cookbook/ || repo_namespace(event) =~ /cookbook/ end |
.data_bag_from_file(data_bag, items, knives) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/kitchen_hooks/helpers.rb', line 258 def self.data_bag_from_file data_bag, items, knives $stdout.puts 'Uploading data_bags' begin items.each do |item| # Try to guess if there is one repo per data bag or # all data bags are in the sub-folders in the same repo. if item.split('/').length > 1 data_bag = item.split('/')[-2] end with_each_knife_do 'data bag from file ' + data_bag + ' ' + item, knives end rescue raise 'Could not upload data bags' end end |
.data_bag_repo?(event) ⇒ Boolean
668 669 670 671 |
# File 'lib/kitchen_hooks/helpers.rb', line 668 def self.data_bag_repo? event repo_name(event) =~ /data.*bag/ || repo_namespace(event) =~ /data.*bag/ end |
.db!(path = @@db_path) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/kitchen_hooks/app.rb', line 19 def self.db! path=@@db_path if defined? @@db @@db.flush @@db.close end @@db_path = path @@db = Daybreak::DB.new path end |
.environment_from_file(environments, knives) ⇒ Object
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/kitchen_hooks/helpers.rb', line 287 def self.environment_from_file environments, knives $stdout.puts 'Uploading environments' begin environments.each do |environment| with_each_knife_do 'environment from file ' + environment, knives end rescue raise 'Could not upload environments' end end |
.environment_repo?(event) ⇒ Boolean
674 675 676 677 |
# File 'lib/kitchen_hooks/helpers.rb', line 674 def self.environment_repo? event repo_name(event) =~ /environment/ || repo_namespace(event) =~ /environment/ end |
.files_pushed(event) ⇒ Object
633 634 635 636 637 638 639 |
# File 'lib/kitchen_hooks/helpers.rb', line 633 def self.files_pushed event files = [] event.fetch('commits').each do |commit| files << commit.select { |k,v| k =~ /(added|modified)/ }.values.flatten end files.flatten.uniq end |
.generic_details(event) ⇒ Object
614 615 616 617 618 619 |
# File 'lib/kitchen_hooks/helpers.rb', line 614 def self.generic_details event return if event.nil? %Q| <i>#{author(event)}</i> pushed #{push_details(event)} |.strip end |
.get_environment(environment, knife) ⇒ Object
422 423 424 425 |
# File 'lib/kitchen_hooks/helpers.rb', line 422 def self.get_environment environment, knife ridley = Ridley::from_chef_config knife, ssl: { verify: false } ridley.environment.find environment end |
.git_daemon_style_url(event) ⇒ Object
691 692 693 |
# File 'lib/kitchen_hooks/helpers.rb', line 691 def self.git_daemon_style_url event event['repository']['url'].sub(':', '/').sub('@', '://') end |
.gitlab_tag_url(event) ⇒ Object
702 703 704 705 |
# File 'lib/kitchen_hooks/helpers.rb', line 702 def self.gitlab_tag_url event url = git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') "#{url}/commits/#{tag_name(event)}" end |
.gitlab_url(event) ⇒ Object
696 697 698 699 |
# File 'lib/kitchen_hooks/helpers.rb', line 696 def self.gitlab_url event url = git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') "#{url}/commit/#{event['after']}" end |
.humanize_seconds(secs) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kitchen_hooks/helpers.rb', line 32 def self.humanize_seconds secs [ [ 60, :seconds ], [ 60, :minutes ], [ 24, :hours ], [ 1000, :days ] ].map { |count, name| if secs > 0 secs, n = secs.divmod(count) "#{n.to_i} #{name}" end }.compact.reverse.join(' ') end |
.kitchen_upload(knives) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/kitchen_hooks/helpers.rb', line 223 def self.kitchen_upload knives if Dir.exist? 'data_bags' $stdout.puts 'Uploading data_bags' begin with_each_knife_do 'upload data_bags --chef-repo-path .', knives rescue raise 'Could not upload data bags' end end if Dir.exist? 'roles' $stdout.puts 'Uploading roles' begin with_each_knife_do 'upload roles --chef-repo-path .', knives rescue raise 'Could not upload roles' end end if Dir.exist? 'environments' $stdout.puts 'Uploading environments' knives.peach do |k| begin Dir['environments/*'].each do |e| # Can't use the default logic, as we maintain our own pins upload_environment e, k end rescue raise 'Could not upload environments' end end end end |
.latest_commit(event) ⇒ Object
708 709 710 |
# File 'lib/kitchen_hooks/helpers.rb', line 708 def self.latest_commit event event['commits'].last['id'] end |
.lockfile_constraints(lockfile_path) ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/kitchen_hooks/helpers.rb', line 455 def self.lockfile_constraints lockfile_path # Ripped from Berkshelf::Cli::apply and Berkshelf::Lockfile::apply # https://github.com/berkshelf/berkshelf/blob/master/lib/berkshelf/cli.rb # https://github.com/berkshelf/berkshelf/blob/master/lib/berkshelf/lockfile.rb lockfile = Berkshelf::Lockfile.from_file lockfile_path constraints = lockfile.graph.locks.inject({}) do |hash, (name, dependency)| hash[name] = "= #{dependency.locked_version.to_s}" hash end $stdout.puts 'constraints: %s -> %s' % [ lockfile_path, constraints ] return constraints end |
.not_deleted?(event) ⇒ Boolean
729 730 731 |
# File 'lib/kitchen_hooks/helpers.rb', line 729 def self.not_deleted? event event['after'] != '0000000000000000000000000000000000000000' end |
.notification(entry) ⇒ Object
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/kitchen_hooks/helpers.rb', line 504 def self.notification entry return entry[:error] if entry[:error] event = entry[:event] case entry[:type] when 'synced', 'unsynced' if event.is_a? String event else num_deletions = event[:num_deletions].to_i deletions = '%s, ' % pluralize(num_deletions, 'deletion') deletions = '' unless num_deletions > 0 'Synced <b>%d</b> of <b>%d</b> nodes (%s%s elapsed)' % [ event[:num_successes], event[:num_nodes], deletions, humanize_seconds(event[:elapsed]) ] end when 'kitchen upload' %Q| <i>#{author(event)}</i> updated <a href="#{gitlab_url(event)}">the Kitchen</a> | when 'cookbook upload' %Q| <i>#{author(event)}</i> released <a href="#{gitlab_tag_url(event)}">#{tag_name(event)}</a> of <a href="#{gitlab_url(event)}">#{cookbook_name(event)}</a> | when 'constraint application' %Q| <i>#{author(event)}</i> constrained <a href="#{gitlab_tag_url(event)}">#{tag_name(event)}</a> with <a href="#{gitlab_url(event)}">#{cookbook_name(event)}</a> #{version_link event} | when 'release' %Q| Kitchen Hooks <b>v#{event}</b> released! | when 'upload from files' %Q| <i>#{author(event)}</i> uploaded <a href="#{gitlab_url(event)}">files</a> to chef | else raise entry.inspect end.strip end |
.perform_constraint_application(event, knives) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kitchen_hooks/helpers.rb', line 56 def self.perform_constraint_application event, knives $stdout.puts 'started perform_constraint_application event=%s, knives=%s' % [ event['after'], knives.inspect ] tmp_clone event, :tagged_commit do |clone| Dir.chdir clone do $stdout.puts 'Applying constraints' constraints = lockfile_constraints 'Berksfile.lock' environment = tag_name event environments = Dir['environments/*.json'].map { |f| File.basename f, '.json' } unless environments.include?(environment) $stderr.puts 'WARNING: No local environment: %s' % environment next end knives.peach do |k| apply_constraints constraints, environment, k verify_constraints constraints, environment, k end rev = `git rev-list -1 #{environment}`.strip refs = `git show-ref --tags -d | grep '^#{rev}'`.lines.map(&:strip) = refs.map do |r| $1 if r =~ %r|refs/tags/(?<tag>.*?)\^|s end version_tag = .select { |t| t =~ /^v\d+/ }.shift event['version'] = version_tag end end $stdout.puts "finished perform_constraint_application: #{event['after']}" return # no error rescue Git::GitExecuteError $stderr.puts 'WARNING: Could not check out tagged commit' return false end |
.perform_cookbook_upload(event, knives) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/kitchen_hooks/helpers.rb', line 149 def self.perform_cookbook_upload event, knives $stdout.puts 'started perform_cookbook_upload event=%s, knives=%s' % [ event['after'], knives.inspect ] tmp_clone event, :tagged_commit do |clone| tagged_version = tag_name(event).delete('v') if File.exist? File.join(clone, 'VERSION') cookbook_version = File.read(File.join(clone, 'VERSION')).strip else cookbook_version = File.foreach(File.join(clone, 'metadata.rb')).grep(/version/)[0][/\"(.*)\"/,1] end unless tagged_version == cookbook_version raise 'Tagged version does not match cookbook version' end berksfile = File.join clone, 'Berksfile' if File.exist? berksfile $stdout.puts 'Uploading dependencies' FileUtils.rm_rf File.join(ENV['HOME'], '.berkshelf') berks_install berksfile knives.map do |knife| tmp_root = Dir.mktmpdir tmp_path = File.join tmp_root, File.basename(knife, '.rb') FileUtils.copy_entry clone, tmp_path [ knife, tmp_path, tmp_root ] end.peach do |(knife, tmp_path, tmp_root)| tmp_berksfile = File.join tmp_path, 'Berksfile' berks_upload tmp_berksfile, knife FileUtils.rm_rf tmp_root end end Dir.chdir clone do if commit_to_realm? event $stdout.puts 'Uploading bundled roles, environments, and data bags' kitchen_upload knives end end end $stdout.puts "finished cookbook_upload: #{event['after']}" return # no error end |
.perform_kitchen_upload(event, knives) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kitchen_hooks/helpers.rb', line 99 def self.perform_kitchen_upload event, knives return false unless commit_to_master?(event) $stdout.puts 'started perform_kitchen_upload event=%s, knives=%s' % [ event['after'], knives.inspect ] tmp_clone event, :latest_commit do |clone| Dir.chdir clone do kitchen_upload knives end end $stdout.puts "finished perform_kitchen_upload: #{event['after']}" return # no error end |
.perform_knife_cookbook_upload(event, knives) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/kitchen_hooks/helpers.rb', line 116 def self.perform_knife_cookbook_upload event, knives $stdout.puts 'started perform_cookbook_upload event=%s, knives=%s' % [ event['after'], knives.inspect ] tmp_clone event, :tagged_commit do |clone| tagged_version = tag_name(event).delete('v') if File.exist? File.join(clone, 'VERSION') cookbook_version = File.read(File.join(clone, 'VERSION')).strip else cookbook_version = File.foreach(File.join(clone, 'metadata.rb')).grep(/version/)[0][/\"(.*)\"/,1] end unless tagged_version == cookbook_version raise 'Tagged version does not match cookbook version' end Dir.chdir clone do $stdout.puts 'Uploading cookbook' begin with_each_knife_do "cookbook upload #{cookbook_name event} -o .. --freeze", knives rescue => e unless e.to_s =~ /frozen/i # Ignore frozen cookbooks already uploaded raise "Knife exited unsuccessfully: #{e}" end end end end $stdout.puts "finished cookbook_upload: #{event['after']}" return # no error end |
.perform_upload_from_file(event, knives) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/kitchen_hooks/helpers.rb', line 197 def self.perform_upload_from_file event, knives return false unless commit_to_master?(event) $stdout.puts 'started perform_upload_from_file event=%s, knives=%s' % [ event['after'], knives.inspect ] tmp_clone event, :latest_commit do |clone| Dir.chdir clone do if commit_to_data_bags?(event) data_bag_from_file repo_name(event), files_pushed(event), knives end if commit_to_environments?(event) environment_from_file files_pushed(event), knives end if commit_to_roles?(event) role_from_file files_pushed(event), knives end end end $stdout.puts "finished perform_upload_from_file: #{event['after']}" return # no error end |
.pluralize(n, singular, plural = nil) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/kitchen_hooks/helpers.rb', line 23 def self.pluralize n, singular, plural=nil plural = "#{singular}s" if plural.nil? return "no #{plural}" if n.zero? return "1 #{singular}" if n == 1 "#{n} #{plural}" end |
.push_details(event) ⇒ Object
625 626 627 628 629 630 |
# File 'lib/kitchen_hooks/helpers.rb', line 625 def self.push_details event return if event.nil? %Q| <a href="#{gitlab_url(event)}">#{event['after']}</a> to <a href="#{repo_url(event)}">#{repo_name(event)}</a> |.strip end |
.repo_name(event) ⇒ Object
647 648 649 |
# File 'lib/kitchen_hooks/helpers.rb', line 647 def self.repo_name event File::basename event['repository']['url'], '.git' end |
.repo_namespace(event) ⇒ Object
652 653 654 |
# File 'lib/kitchen_hooks/helpers.rb', line 652 def self.repo_namespace event event['project']['path_with_namespace'].split('/')[0] rescue nil end |
.repo_url(event) ⇒ Object
686 687 688 |
# File 'lib/kitchen_hooks/helpers.rb', line 686 def self.repo_url event git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') end |
.report_error(e, msg = nil) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/kitchen_hooks/helpers.rb', line 47 def self.report_error e, msg=nil msg = e. if msg.nil? $stdout.puts msg $stdout.puts e. $stdout.puts e.backtrace.inspect msg end |
.role_from_file(roles, knives) ⇒ Object
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/kitchen_hooks/helpers.rb', line 275 def self.role_from_file roles, knives $stdout.puts 'Uploading roles' begin roles.each do |role| with_each_knife_do 'role from file ' + role, knives end rescue raise 'Could not upload roles' end end |
.role_repo?(event) ⇒ Boolean
680 681 682 683 |
# File 'lib/kitchen_hooks/helpers.rb', line 680 def self.role_repo? event repo_name(event) =~ /role/ || repo_namespace(event) =~ /role/ end |
.slack_notification(entry) ⇒ Object
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/kitchen_hooks/helpers.rb', line 540 def self.slack_notification entry if entry[:error] return { color: 'danger', title: 'Error!', text: entry[:error] } end event = entry[:event] case entry[:type] when 'synced', 'unsynced' title = if event.is_a? String event else num_deletions = event[:num_deletions].to_i deletions = '%s, ' % pluralize(num_deletions, 'deletion') deletions = '' unless num_deletions > 0 'Synced *%d* of *%d* nodes (%s%s elapsed)' % [ event[:num_successes], event[:num_nodes], deletions, humanize_seconds(event[:elapsed]) ] end { color: 'warning', title: 'Sync', text: title } when 'kitchen upload' { color: 'good', text: "_#{author(event)}_ updated the Kitchen", title: 'Kitchen Upload', title_link: gitlab_url(event) } when 'cookbook upload' { color: 'good', text: "_#{author(event)}_ released *#{tag_name(event)}* of #{cookbook_name(event)}", title: 'Cookbook Upload', title_link: gitlab_url(event) } when 'constraint application' v = event['version'] ? " at *#{event['version']}*" : '' { color: 'good', text: "_#{author(event)}_ constrained *#{tag_name(event)}* with #{cookbook_name(event)}#{v}", title: 'Constraint', title_link: gitlab_url(event) } when 'release' { color: '#439FE0', text: "Kitchen Hooks *v#{event}* released!", title: 'Release' } when 'upload from files' { color: 'good', text: "_#{author(event)}_ uploaded files to Chef", title: 'File Upload', title_link: gitlab_url(event) } else raise entry.inspect end end |
.sync! ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/kitchen_hooks/app.rb', line 47 def self.sync! return if @@sync_interval.nil? @@sync_worker = Thread.new do loop do sleep @@sync_interval process_sync end end end |
.tag_name(event) ⇒ Object
719 720 721 |
# File 'lib/kitchen_hooks/helpers.rb', line 719 def self.tag_name event tagged_commit event end |
.tagged_commit(event) ⇒ Object
713 714 715 716 |
# File 'lib/kitchen_hooks/helpers.rb', line 713 def self.tagged_commit event event['ref'] =~ %r{/tags/(.*)$} return $1 # First regex capture end |
.tagged_commit_to_cookbook?(event) ⇒ Boolean
759 760 761 762 763 |
# File 'lib/kitchen_hooks/helpers.rb', line 759 def self.tagged_commit_to_cookbook? event cookbook_repo?(event) && event['ref'] =~ %r{/tags/} && not_deleted?(event) end |
.tagged_commit_to_realm?(event) ⇒ Boolean
766 767 768 769 |
# File 'lib/kitchen_hooks/helpers.rb', line 766 def self.tagged_commit_to_realm? event tagged_commit_to_cookbook?(event) && commit_to_realm?(event) end |
.tmp!(dir) ⇒ Object
28 |
# File 'lib/kitchen_hooks/app.rb', line 28 def self.tmp! dir ; @@tmp = dir end |
.tmp_clone(event, commit_method) {|dir| ... } ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/kitchen_hooks/helpers.rb', line 373 def self.tmp_clone event, commit_method, &block $stdout.puts 'starting tmp_clone event=%s, commit_method=%s' % [ event['after'], commit_method.inspect ] root = File::join tmp, SecureRandom.hex dir = File::join root, Time.now.to_f.to_s, cookbook_name(event) FileUtils.mkdir_p dir git_protocol = event['repository']['protocol'] if git_protocol == 'daemon' git_clone_url = git_daemon_style_url(event) else git_clone_url = event['repository']["git_#{git_protocol}_url"] end repo = Git.clone git_clone_url, dir, log: $stdout commit = self.send(commit_method, event) $stdout.puts 'creating tmp_clone dir=%s, commit=%s' % [ dir.inspect, commit.inspect ] repo.checkout commit yield dir FileUtils.rm_rf root FileUtils.rm_rf dir $stdout.puts 'finished tmp_clone' end |
.upload_environment(environment, knife) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/kitchen_hooks/helpers.rb', line 469 def self.upload_environment environment, knife $stdout.puts 'started upload_environment environment=%s, knife=%s' % [ environment.inspect, knife.inspect ] # Load the local environment from a JSON file local_environment = JSON::parse File.read(environment) local_environment.delete 'chef_type' local_environment.delete 'json_class' local_environment.delete 'cookbook_versions' # Load existing environment object on Chef server Celluloid.logger = nil ridley = Ridley::from_chef_config knife, ssl: { verify: false } chef_environment = ridley.environment.find(local_environment['name']) # Create environment object if it doesn't exist if chef_environment.nil? chef_environment = ridley.environment.create(local_environment) end # Merge the local environment into the existing object local_environment.each do |k, v| chef_environment.send "#{k}=".to_sym, v end # Make it so! chef_environment.save $stdout.puts 'finished upload_environment: %s' % environment rescue raise 'Could not upload environment, check your syntax' end |
.verify_constraints(constraints, environment, knife) ⇒ Object
428 429 430 431 432 433 434 435 436 437 |
# File 'lib/kitchen_hooks/helpers.rb', line 428 def self.verify_constraints constraints, environment, knife $stdout.puts 'started verify_constraints environment=%s, knife=%s' % [ environment.inspect, knife.inspect ] chef_environment = get_environment environment, knife unless constraints == chef_environment.cookbook_versions raise 'Environment did not match constraints' end $stdout.puts 'finished verify_constraints: %s' % environment end |
.version_link(event) ⇒ Object
779 780 781 782 783 |
# File 'lib/kitchen_hooks/helpers.rb', line 779 def self.version_link event return unless v = event['version'] url = version_url(event) 'at <a href="%s">%s</a>' % [ url, v ] end |
.version_url(event) ⇒ Object
772 773 774 775 776 |
# File 'lib/kitchen_hooks/helpers.rb', line 772 def self.version_url event return unless v = event['version'] url = git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') "#{url}/commits/#{v}" end |
.with_each_knife(command, knives) ⇒ Object
411 412 413 414 415 416 417 418 419 |
# File 'lib/kitchen_hooks/helpers.rb', line 411 def self.with_each_knife command, knives knives.pmap do |k| cmd = "#{command} 2>&1" % { knife: Shellwords::escape(k) } $stdout.puts 'with_each_knife: %s' % cmd out = `#{cmd}` raise out unless $?.exitstatus.zero? out end end |
.with_each_knife_do(command, knives) ⇒ Object
407 408 409 |
# File 'lib/kitchen_hooks/helpers.rb', line 407 def self.with_each_knife_do command, knives with_each_knife "knife #{command} --config %{knife}", knives end |
Instance Method Details
#generic_details(e) ⇒ Object
612 |
# File 'lib/kitchen_hooks/helpers.rb', line 612 def generic_details e ; App.generic_details e end |
#notification(e) ⇒ Object
502 |
# File 'lib/kitchen_hooks/helpers.rb', line 502 def notification e ; App.notification e end |
#push_details(e) ⇒ Object
622 |
# File 'lib/kitchen_hooks/helpers.rb', line 622 def push_details e ; App.push_details e end |
#slack_notification(e) ⇒ Object
538 |
# File 'lib/kitchen_hooks/helpers.rb', line 538 def slack_notification e ; App.slack_notification e end |