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_url(event) ⇒ Object
- #latest_commit(event) ⇒ Object
- #lockfile_constraints(lockfile_path) ⇒ Object
- #not_deleted?(event) ⇒ Boolean
- #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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kitchen_hooks/helpers.rb', line 86 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
135 136 137 |
# File 'lib/kitchen_hooks/helpers.rb', line 135 def event event['user_name'] end |
#berks_upload(knife, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kitchen_hooks/helpers.rb', line 57 def berks_upload knife, ={} ridley = Ridley::from_chef_config knife = { berksfile: 'Berksfile', freeze: true, validate: true }.merge().merge \ server_url: ridley.server_url, client_name: ridley.client_name, client_key: ridley.client_key berksfile = Berkshelf::Berksfile.() berksfile.upload([], .symbolize_keys) end |
#commit_to_kitchen?(event) ⇒ Boolean
175 176 177 |
# File 'lib/kitchen_hooks/helpers.rb', line 175 def commit_to_kitchen? event repo_name(event) == 'kitchen' && not_deleted?(event) end |
#cookbook_name(event) ⇒ Object
143 144 145 |
# File 'lib/kitchen_hooks/helpers.rb', line 143 def cookbook_name event repo_name(event).sub /^(app|base|realm|fork)_/, 'bjn_' end |
#cookbook_repo?(event) ⇒ Boolean
147 148 149 |
# File 'lib/kitchen_hooks/helpers.rb', line 147 def cookbook_repo? event repo_name(event) =~ /^(app|base|realm|fork)_/ end |
#git_daemon_style_url(event) ⇒ Object
151 152 153 |
# File 'lib/kitchen_hooks/helpers.rb', line 151 def git_daemon_style_url event event['repository']['url'].sub(':', '/').sub('@', '://') end |
#gitlab_url(event) ⇒ Object
155 156 157 158 |
# File 'lib/kitchen_hooks/helpers.rb', line 155 def gitlab_url event url = git_daemon_style_url(event).sub(/^git/, 'http').sub(/\.git$/, '') "#{url}/commit/#{event['after']}" end |
#latest_commit(event) ⇒ Object
160 161 162 |
# File 'lib/kitchen_hooks/helpers.rb', line 160 def latest_commit event event['commits'].last['id'] end |
#lockfile_constraints(lockfile_path) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/kitchen_hooks/helpers.rb', line 98 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
171 172 173 |
# File 'lib/kitchen_hooks/helpers.rb', line 171 def not_deleted? event event['after'] != '0000000000000000000000000000000000000000' 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
139 140 141 |
# File 'lib/kitchen_hooks/helpers.rb', line 139 def repo_name event File::basename event['repository']['url'], '.git' end |
#tagged_commit(event) ⇒ Object Also known as: tag_name
164 165 166 167 |
# File 'lib/kitchen_hooks/helpers.rb', line 164 def tagged_commit event event['ref'] =~ %r{/tags/(.*)$} return $1 # First regex capture end |
#tagged_commit_to_cookbook?(event) ⇒ Boolean
179 180 181 182 183 |
# File 'lib/kitchen_hooks/helpers.rb', line 179 def tagged_commit_to_cookbook? event cookbook_repo?(event) && event['ref'] =~ %r{/tags/} && not_deleted?(event) end |
#tagged_commit_to_realm?(event) ⇒ Boolean
185 186 187 188 |
# File 'lib/kitchen_hooks/helpers.rb', line 185 def tagged_commit_to_realm? event tagged_commit_to_cookbook?(event) && repo_name(event) =~ /^realm_/ end |
#tmp_clone(event, commit_method, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kitchen_hooks/helpers.rb', line 69 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kitchen_hooks/helpers.rb', line 109 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
80 81 82 83 84 |
# File 'lib/kitchen_hooks/helpers.rb', line 80 def with_each_knife command, knives knives.map do |k| `knife #{command} --config #{Shellwords::escape k}` end end |