Class: ChefDK::PolicyfileServices::Install

Inherits:
Object
  • Object
show all
Includes:
Helpers, ChefDK::Policyfile::StorageConfigDelegation
Defined in:
lib/chef-dk/policyfile_services/install.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Methods included from ChefDK::Policyfile::StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Constructor Details

#initialize(policyfile: nil, ui: nil, root_dir: nil, overwrite: false, config: nil) ⇒ Install

Returns a new instance of Install.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef-dk/policyfile_services/install.rb', line 39

def initialize(policyfile: nil, ui: nil, root_dir: nil, overwrite: false, config: nil)
  @ui = ui
  @overwrite = overwrite
  @chef_config = config

  policyfile_rel_path = policyfile || "Policyfile.rb"
  policyfile_full_path = File.expand_path(policyfile_rel_path, root_dir)
  @storage_config = Policyfile::StorageConfig.new.use_policyfile(policyfile_full_path)

  @policyfile_content = nil
  @policyfile_compiler = nil
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



37
38
39
# File 'lib/chef-dk/policyfile_services/install.rb', line 37

def chef_config
  @chef_config
end

#overwriteObject (readonly)

Returns the value of attribute overwrite.



36
37
38
# File 'lib/chef-dk/policyfile_services/install.rb', line 36

def overwrite
  @overwrite
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



35
36
37
# File 'lib/chef-dk/policyfile_services/install.rb', line 35

def storage_config
  @storage_config
end

#uiObject (readonly)

Returns the value of attribute ui.



34
35
36
# File 'lib/chef-dk/policyfile_services/install.rb', line 34

def ui
  @ui
end

Instance Method Details

#expanded_run_listObject



74
75
76
# File 'lib/chef-dk/policyfile_services/install.rb', line 74

def expanded_run_list
  policyfile_compiler.expanded_run_list.to_s
end

#generate_lock_and_installObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef-dk/policyfile_services/install.rb', line 90

def generate_lock_and_install
  policyfile_compiler.error!

  ui.msg "Building policy #{policyfile_compiler.name}"
  ui.msg "Expanded run list: " + expanded_run_list + "\n"

  ui.msg "Caching Cookbooks..."

  policyfile_compiler.install

  lock_data = policyfile_compiler.lock.to_lock

  with_file(policyfile_lock_expanded_path) do |f|
    f.print(FFI_Yajl::Encoder.encode(lock_data, pretty: true ))
  end

  ui.msg ""

  ui.msg "Lockfile written to #{policyfile_lock_expanded_path}"
  ui.msg "Policy revision id: #{policyfile_lock.revision_id}"
rescue => error
  raise PolicyfileInstallError.new("Failed to generate Policyfile.lock", error)
end

#install_from_lockObject



114
115
116
117
118
119
120
# File 'lib/chef-dk/policyfile_services/install.rb', line 114

def install_from_lock
  ui.msg "Installing cookbooks from lock"

  policyfile_lock.install_cookbooks
rescue => error
  raise PolicyfileInstallError.new("Failed to install cookbooks from lockfile", error)
end

#installing_from_lock?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/chef-dk/policyfile_services/install.rb', line 122

def installing_from_lock?
  !@overwrite && File.exist?(policyfile_lock_expanded_path)
end

#policyfile_compilerObject



70
71
72
# File 'lib/chef-dk/policyfile_services/install.rb', line 70

def policyfile_compiler
  @policyfile_compiler ||= ChefDK::PolicyfileCompiler.evaluate(policyfile_content, policyfile_expanded_path, ui: ui, chef_config: chef_config)
end

#policyfile_contentObject



66
67
68
# File 'lib/chef-dk/policyfile_services/install.rb', line 66

def policyfile_content
  @policyfile_content ||= IO.read(policyfile_expanded_path)
end

#policyfile_lockObject



82
83
84
85
86
87
88
# File 'lib/chef-dk/policyfile_services/install.rb', line 82

def policyfile_lock
  return nil if policyfile_lock_content.nil?
  @policyfile_lock ||= begin
    lock_data = FFI_Yajl::Parser.new.parse(policyfile_lock_content)
    PolicyfileLock.new(storage_config, ui: ui).build_from_lock_data(lock_data)
  end
end

#policyfile_lock_contentObject



78
79
80
# File 'lib/chef-dk/policyfile_services/install.rb', line 78

def policyfile_lock_content
  @policyfile_lock_content ||= IO.read(policyfile_lock_expanded_path) if File.exist?(policyfile_lock_expanded_path)
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef-dk/policyfile_services/install.rb', line 52

def run
  unless File.exist?(policyfile_expanded_path)
    # TODO: suggest next step. Add a generator/init command? Specify path to Policyfile.rb?
    # See card CC-232
    raise PolicyfileNotFound, "Policyfile not found at path #{policyfile_expanded_path}"
  end

  if installing_from_lock?
    install_from_lock
  else
    generate_lock_and_install
  end
end