Class: DeployRubygem::Cookbook

Inherits:
Project
  • Object
show all
Defined in:
lib/deploy_rubygem/cookbook.rb

Overview

Using Cookbook to deploy and manage cookbook

Constant Summary

Constants included from DeployRubygem

VERSION

Instance Attribute Summary collapse

Attributes inherited from Project

#compliance_profile, #cookbooks, #project_options

Instance Method Summary collapse

Methods inherited from Project

#binaries, #change_to_chefrepo, #change_to_directory, #change_to_project_folder, #chefrepo_git, #chefrepo_path, #dependencies, #deploy_cookbooks, #git_commit, #path, #project_name, #release_cookbooks, #test_compliance

Methods included from RSpecTesting

#check_current_version, #help_rspec, #install_gem_via_git, #testing_chef, #testing_gem, #testing_inspec, #testing_project, #testing_workstation

Methods included from DeployRubygem

#bootstrap_chef_workstation, #chef_accept_cmd, #chef_env_cmd, deployer, exemple_cookbook, #install_chef_workstation, #no_option_txt, #rubygem_test, #unknown_option_txt

Constructor Details

#initialize(new_cookbook_info) ⇒ Cookbook

Returns a new instance of Cookbook.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deploy_rubygem/cookbook.rb', line 16

def initialize(new_cookbook_info)
  super
  @profiles = cookbook_info[:compliance_profiles].map do |profile_name|
    input_file = File.join(%w[compliance inputs] + ["#{cookbook_name}.yml"])
    waiver_file = File.join(%w[compliance waivers] + ["#{cookbook_name}.yml"])
    InspecTestor.new(File.join(%w[compliance profiles], profile_name.to_s), input_file, waiver_file)
  end
  @kitchens = cookbook_info[:kitchens].map do |kitchen_name|
    Kitchen.new(kitchen_name, self)
  end
  @execute_profiles = cookbook_info[:execute_profiles].map do |profile_name|
    InspecTestor.new(profile_name)
  end
end

Instance Attribute Details

#execute_profilesObject (readonly)

Returns the value of attribute execute_profiles.



14
15
16
# File 'lib/deploy_rubygem/cookbook.rb', line 14

def execute_profiles
  @execute_profiles
end

#kitchensObject (readonly)

Returns the value of attribute kitchens.



14
15
16
# File 'lib/deploy_rubygem/cookbook.rb', line 14

def kitchens
  @kitchens
end

#profilesObject (readonly)

Returns the value of attribute profiles.



14
15
16
# File 'lib/deploy_rubygem/cookbook.rb', line 14

def profiles
  @profiles
end

Instance Method Details

#connect_cookbookObject



59
60
61
62
63
64
# File 'lib/deploy_rubygem/cookbook.rb', line 59

def connect_cookbook
  system("git submodule add #{git}")
  system("git submodule init cookbooks/#{cookbook_name}")
  system("git submodule sync cookbooks/#{cookbook_name}")
  system("git submodule foreach --recursive 'pwd && git pull'")
end

#cookbook_infoObject



43
44
45
# File 'lib/deploy_rubygem/cookbook.rb', line 43

def cookbook_info
  project_options
end

#cookbook_nameObject



39
40
41
# File 'lib/deploy_rubygem/cookbook.rb', line 39

def cookbook_name
  project_name
end

#cookbook_pathObject



47
48
49
# File 'lib/deploy_rubygem/cookbook.rb', line 47

def cookbook_path
  ::File.join(chefrepo_path, 'cookbooks', cookbook_name.to_s)
end

#deployObject



96
97
98
99
100
101
102
103
104
# File 'lib/deploy_rubygem/cookbook.rb', line 96

def deploy
  save_progress
  switch_to_cookbook
  save_progress
  upload_cookbook
  prepare_test_environment
  verify
  save_progress
end

#destroy_test_environmentObject



92
93
94
# File 'lib/deploy_rubygem/cookbook.rb', line 92

def destroy_test_environment
  kitchens.each(&:destroy)
end

#generateObject



31
32
33
34
35
36
37
# File 'lib/deploy_rubygem/cookbook.rb', line 31

def generate
  return if Dir.exist?(cookbook_path)

  Dir.chdir(File.dirname(cookbook_path)) do
    system("chef generate cookbook #{cookbook_name}")
  end
end

#gitObject



51
52
53
# File 'lib/deploy_rubygem/cookbook.rb', line 51

def git
  cookbook_info[__method__]
end

#groupsObject



55
56
57
# File 'lib/deploy_rubygem/cookbook.rb', line 55

def groups
  cookbook_info[__method__]
end

#prepare_test_environmentObject



87
88
89
90
# File 'lib/deploy_rubygem/cookbook.rb', line 87

def prepare_test_environment
  profiles.each(&:update)
  kitchens.each(&:converge)
end

#save_progressObject



118
119
120
121
122
# File 'lib/deploy_rubygem/cookbook.rb', line 118

def save_progress
  system('git add .')
  system("git commit -m \"Saving: hostname=#{ENV.fetch('HOSTNAME')}, cookbook_name=#{cookbook_name}\"")
  system('git push')
end

#switch_to_cookbookObject



66
67
68
69
70
# File 'lib/deploy_rubygem/cookbook.rb', line 66

def switch_to_cookbook
  change_to_chefrepo
  connect_cookbook
  change_to_project_folder
end

#upload_cookbookObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/deploy_rubygem/cookbook.rb', line 72

def upload_cookbook
  system('git pull')

  system('chef clean-policy-cookbooks')
  # system('chef clean-policy-revisions')

  system("knife cookbook upload --cookbook-path '../' #{cookbook_name}")

  system('chef install Policyfile.rb')

  groups.each do |group|
    system("chef push #{group} Policyfile.lock.json")
  end
end

#verifyObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/deploy_rubygem/cookbook.rb', line 106

def verify
  kitchens.each do |kitchen|
    puts "Verify cookbook #{cookbook_name} on target #{kitchen.target}"
    kitchen.verify
  end

  execute_profiles.each do |inspec_profile|
    inspec_profile.update
    inspec_profile.apply
  end
end