-
Notifications
You must be signed in to change notification settings - Fork 5
/
steps_chefrepo_env_cookbook_versions_check.yml
56 lines (47 loc) · 2.23 KB
/
steps_chefrepo_env_cookbook_versions_check.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
steps:
- template: steps_chef_workstation_install.yml@pipeline_templates
- template: steps_knife_config.yml@pipeline_templates
- task: CmdLine@2
displayName: 'Write Cookbook Version Check Script'
inputs:
script: |
cat << EOF > $(Agent.TempDirectory)/env_check.rb
#!/bin/env ruby
require 'json'
environments = Dir['environments/*.json']
cookbooks = api.get('cookbooks?num_versions=all')
pass = true
environments.each do |environment|
environment_json = JSON.parse(File.read(environment))
Chef::Log.info "Checking #{environment_json['name']} environment"
environment_json['cookbook_versions'].each do |cookbook, version_pin|
Chef::Log.info "\t#{cookbook} #{version_pin}"
operator, version = version_pin.split
if operator != '='
Chef::Log.error "Environment #{environment_json['name']}: Cookbook #{cookbook} #{version_pin} version operator used. Must use equality = operator"
pass = false
end
if cookbooks[cookbook].nil?
Chef::Log.error "Environment #{environment_json['name']}: Cookbook #{cookbook} does not exist on the Chef Infra Server!"
pass = false
next # Skip to next cookbook as since it doesn't exist, we can't check for it's version
end
if cookbooks[cookbook]['versions'].select { |c| c['version'] == version }.empty?
Chef::Log.error "Environment #{environment_json['name']}: Cookbook #{cookbook} version #{version} does not exist on Chef Infra Server!"
pass = false
end
end
all_cookbook_names = cookbooks.keys.sort
all_pinned_cookbook_names = environment_json['cookbook_versions'].keys.sort
if all_cookbook_names != all_pinned_cookbook_names
Chef::Log.error "Environment #{environment_json['name']} does not pin all cookbooks. Missing #{all_cookbook_names - all_pinned_cookbook_names}"
pass = false
end
end
raise 'Environments not valid' unless pass
EOF
- task: chef-software.chef-azdo.execute.component@3
displayName: 'Cookbook Version Check'
inputs:
component: 'chef'
arguments: 'exec knife exec $(Agent.TempDirectory)/env_check.rb -V'