Class: ChefDK::PolicyfileServices::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile_services/install.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Install.



31
32
33
34
35
36
37
38
# File 'lib/chef-dk/policyfile_services/install.rb', line 31

def initialize(policyfile: nil, ui: nil, root_dir: nil)
  @policyfile_relative_path = policyfile
  @ui = ui
  @root_dir = root_dir

  @policyfile_content = nil
  @policyfile_compiler = nil
end

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir.



28
29
30
# File 'lib/chef-dk/policyfile_services/install.rb', line 28

def root_dir
  @root_dir
end

#uiObject (readonly)

Returns the value of attribute ui.



29
30
31
# File 'lib/chef-dk/policyfile_services/install.rb', line 29

def ui
  @ui
end

Instance Method Details

#expanded_run_listObject



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

def expanded_run_list
  policyfile_compiler.expanded_run_list.to_s
end

#generate_lock_and_installObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef-dk/policyfile_services/install.rb', line 98

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

  File.open(lockfile_path, "w+") do |f|
    f.print(FFI_Yajl::Encoder.encode(lock_data, pretty: true ))
  end

  ui.msg ""

  ui.msg "Lockfile written to #{lockfile_path}"
rescue => error
  raise PolicyfileInstallError.new("Failed to generate Policyfile.lock", error)
end

#install_from_lockObject



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

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

#lockfile_pathObject



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

def lockfile_path
  File.expand_path(lockfile_relative_path, root_dir)
end

#lockfile_relative_pathObject



62
63
64
# File 'lib/chef-dk/policyfile_services/install.rb', line 62

def lockfile_relative_path
  policyfile_relative_path.gsub(/\.rb\Z/, '') + ".lock.json"
end

#policyfile_compilerObject



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

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

#policyfile_contentObject



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

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

#policyfile_lockObject



86
87
88
89
90
91
92
# File 'lib/chef-dk/policyfile_services/install.rb', line 86

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



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

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

#policyfile_pathObject



58
59
60
# File 'lib/chef-dk/policyfile_services/install.rb', line 58

def policyfile_path
  File.expand_path(policyfile_relative_path, root_dir)
end

#policyfile_relative_pathObject



54
55
56
# File 'lib/chef-dk/policyfile_services/install.rb', line 54

def policyfile_relative_path
  @policyfile_relative_path || "Policyfile.rb"
end

#runObject



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

def run
  unless File.exist?(policyfile_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_path}"
  end

  if File.exist?(lockfile_path)
    install_from_lock
  else
    generate_lock_and_install
  end
end

#storage_configObject



94
95
96
# File 'lib/chef-dk/policyfile_services/install.rb', line 94

def storage_config
  @storage_config ||= Policyfile::StorageConfig.new(relative_paths_root: root_dir)
end