Module: KitchenHooks::Helpers
- Included in:
- App
- Defined in:
- lib/kitchen_hooks/helpers.rb
Instance Method Summary collapse
- #apply_constraints(constraints, environment, knife) ⇒ Object
- #author(event) ⇒ Object
- #berks_upload(knife, options = {}) ⇒ Object
- #commit_to_kitchen?(event) ⇒ Boolean
- #cookbook_name(event) ⇒ Object
- #cookbook_repo?(event) ⇒ Boolean
- #git_daemon_style_url(event) ⇒ Object
- #gitlab_tag_url(event) ⇒ Object
- #gitlab_url(event) ⇒ Object
- #latest_commit(event) ⇒ Object
- #lockfile_constraints(lockfile_path) ⇒ Object
- #not_deleted?(event) ⇒ Boolean
- #notification(event, type) ⇒ Object
- #perform_constraint_application(event, knives) ⇒ Object
- #perform_cookbook_upload(event, knives) ⇒ Object
- #perform_kitchen_upload(event, knives) ⇒ Object
- #repo_name(event) ⇒ Object
- #tagged_commit(event) ⇒ Object (also: #tag_name)
- #tagged_commit_to_cookbook?(event) ⇒ Boolean
- #tagged_commit_to_realm?(event) ⇒ Boolean
- #tmp_clone(event, commit_method, &block) ⇒ Object
- #upload_environment(environment, knife) ⇒ Object
- #with_each_knife(command, knives) ⇒ Object
Instance Method Details
#apply_constraints(constraints, environment, knife) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/kitchen_hooks/helpers.rb', line 88 def 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 Celluloid.logger = nil ridley = Ridley::from_chef_config knife chef_environment = ridley.environment.find(environment) raise if chef_environment.nil? chef_environment.cookbook_versions = constraints chef_environment.save end |
#author(event) ⇒ Object
148 149 150 |
# File 'lib/kitchen_hooks/helpers.rb', line 148 def event event['user_name'] end |
#berks_upload(knife, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kitchen_hooks/helpers.rb', line 57 def berks_upload knife, ={} ridley = Ridley::from_chef_config knife .merge! \ berksfile: 'Berksfile', freeze: true, validate: true, server_url: ridley.server_url, client_name: ridley.client_name, client_key: ridley.client_key berksfile = Berkshelf::Berksfile.() berksfile.install berksfile.upload [], end |
#commit_to_kitchen?(event) ⇒ Boolean
193 194 195 |
# File 'lib/kitchen_hooks/helpers.rb', line 193 def commit_to_kitchen? event repo_name(event) == 'kitchen' && not_deleted?(event) end |
#cookbook_name(event) ⇒ Object
156 157 158 |
# File 'lib/kitchen_hooks/helpers.rb', line 156 def cookbook_name event repo_name(event).sub /^(app|base|realm|fork)_/, 'bjn_' end |
#cookbook_repo?(event) ⇒ Boolean
160 161 162 |
# File 'lib/kitchen_hooks/helpers.rb', line 160 def cookbook_repo? event repo_name(event) =~ /^(app|base|realm|fork)_/ end |
#git_daemon_style_url(event) ⇒ Object
164 165 166 |
# File 'lib/kitchen_hooks/helpers.rb', line 164 def git_daemon_style_url event event['repository']['url'].sub(':', '/').sub('@', '://') end |
#gitlab_tag_url(event) ⇒ Object
173 174 175 176 |
# File 'lib/kitchen_hooks/helpers.rb', line 173 def 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
168 169 170 171 |
# File 'lib/kitchen_hooks/helpers.rb', line 168 def gitlab_url event url = git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') "#{url}/commit/#{event['after']}" end |
#latest_commit(event) ⇒ Object
178 179 180 |
# File 'lib/kitchen_hooks/helpers.rb', line 178 def latest_commit event event['commits'].last['id'] end |
#lockfile_constraints(lockfile_path) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kitchen_hooks/helpers.rb', line 100 def 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 lockfile.graph.locks.inject({}) do |hash, (name, dependency)| hash[name] = "= #{dependency.locked_version.to_s}" hash end end |
#not_deleted?(event) ⇒ Boolean
189 190 191 |
# File 'lib/kitchen_hooks/helpers.rb', line 189 def not_deleted? event event['after'] != '0000000000000000000000000000000000000000' end |
#notification(event, type) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/kitchen_hooks/helpers.rb', line 137 def notification event, type case type when 'kitchen upload' %Q| <i>#{(event)}</i> updated <a href="#{gitlab_url(event)}">the Kitchen</a></p> | when 'cookbook upload' %Q| <i>#{(event)}</i> released <a href="#{gitlab_tag_url(event)}">#{tag_name(event)}</a> of <a href="#{gitlab_url(event)}">#{repo_name(event)}</a> | when 'constraint application' %Q| <i>#{(event)}</i> constrained <a href="#{gitlab_tag_url(event)}">#{tag_name(event)}</a> with <a href="#{gitlab_url(event)}">#{repo_name(event)}</a> | end.strip end |
#perform_constraint_application(event, knives) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kitchen_hooks/helpers.rb', line 11 def perform_constraint_application event, knives tag = tag_name event tmp_clone event, :tagged_commit do puts 'Applying constraints' constraints = lockfile_constraints 'Berksfile.lock' environment = tag_name event knives.each do |k| apply_constraints constraints, environment, k end end end |
#perform_cookbook_upload(event, knives) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/kitchen_hooks/helpers.rb', line 40 def perform_cookbook_upload event, knives tmp_clone event, :tagged_commit do tagged_version = tag_name(event).delete('v') cookbook_version = File.read('VERSION').strip raise unless tagged_version == cookbook_version puts 'Uploading cookbook' with_each_knife "cookbook upload #{cookbook_name event} -o .. --freeze", knives if File::exist?('Berksfile.lock') puts 'Uploading dependencies' knives.each do |knife| berks_upload knife end end end end |
#perform_kitchen_upload(event, knives) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/kitchen_hooks/helpers.rb', line 23 def perform_kitchen_upload event, knives tmp_clone event, :latest_commit do puts 'Uploading data_bags' with_each_knife 'upload data_bags --chef-repo-path .', knives puts 'Uploading roles' with_each_knife 'upload roles --chef-repo-path .', knives puts 'Uploading environments' Dir['environments/*'].each do |e| knives.each do |k| upload_environment e, k end end end end |
#repo_name(event) ⇒ Object
152 153 154 |
# File 'lib/kitchen_hooks/helpers.rb', line 152 def repo_name event File::basename event['repository']['url'], '.git' end |
#tagged_commit(event) ⇒ Object Also known as: tag_name
182 183 184 185 |
# File 'lib/kitchen_hooks/helpers.rb', line 182 def tagged_commit event event['ref'] =~ %r{/tags/(.*)$} return $1 # First regex capture end |
#tagged_commit_to_cookbook?(event) ⇒ Boolean
197 198 199 200 201 |
# File 'lib/kitchen_hooks/helpers.rb', line 197 def tagged_commit_to_cookbook? event cookbook_repo?(event) && event['ref'] =~ %r{/tags/} && not_deleted?(event) end |
#tagged_commit_to_realm?(event) ⇒ Boolean
203 204 205 206 |
# File 'lib/kitchen_hooks/helpers.rb', line 203 def tagged_commit_to_realm? event tagged_commit_to_cookbook?(event) && repo_name(event) =~ /^realm_/ end |
#tmp_clone(event, commit_method, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kitchen_hooks/helpers.rb', line 71 def tmp_clone event, commit_method, &block Dir.mktmpdir do |tmp| dir = File::join tmp, cookbook_name(event) repo = Git.clone git_daemon_style_url(event), dir, log: $stdout repo.checkout self.send(commit_method, event) Dir.chdir dir do yield end end end |
#upload_environment(environment, knife) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/kitchen_hooks/helpers.rb', line 111 def upload_environment environment, knife # 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 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 end |
#with_each_knife(command, knives) ⇒ Object
82 83 84 85 86 |
# File 'lib/kitchen_hooks/helpers.rb', line 82 def with_each_knife command, knives knives.map do |k| `knife #{command} --config #{Shellwords::escape k}` end end |