Class: ChefDK::PolicyfileServices::ExportRepo

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

Constant Summary collapse

POLICY_GROUP =

Policy groups provide namespaces for policies so that a Chef Server can have multiple active iterations of a policy at once, but we don’t need this when serving a single exported policy via Chef Zero, so hardcode it to a “well known” value:

'local'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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, export_dir: nil, root_dir: nil, archive: false, force: false) ⇒ ExportRepo

Returns a new instance of ExportRepo.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 45

def initialize(policyfile: nil, export_dir: nil, root_dir: nil, archive: false, force: false)
  @root_dir = root_dir
  @export_dir = File.expand_path(export_dir)
  @archive = archive
  @force_export = force

  @policy_data = nil
  @policyfile_lock = nil

  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)

  @staging_dir = nil
end

Instance Attribute Details

#export_dirObject (readonly)

Returns the value of attribute export_dir.



43
44
45
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 43

def export_dir
  @export_dir
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



42
43
44
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 42

def root_dir
  @root_dir
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



41
42
43
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 41

def storage_config
  @storage_config
end

Instance Method Details

#archive?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 61

def archive?
  @archive
end

#archive_file_locationObject



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

def archive_file_location
  return nil unless archive?
  filename = "#{policyfile_lock.name}-#{policyfile_lock.revision_id}.tgz"
  File.join(export_dir, filename)
end

#exportObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 94

def export
  with_staging_dir do
    create_repo_structure
    copy_cookbooks
    create_policyfile_data_item
    copy_policyfile_lock
    if archive?
      create_archive
    else
      mv_staged_repo
    end
  end
rescue => error
  msg = "Failed to export policy (in #{policyfile_filename}) to #{export_dir}"
  raise PolicyfileExportRepoError.new(msg, error)
end

#policy_dataObject



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

def policy_data
  @policy_data ||= FFI_Yajl::Parser.parse(IO.read(policyfile_lock_expanded_path))
rescue => error
  raise PolicyfileExportRepoError.new("Error reading lockfile #{policyfile_lock_expanded_path}", error)
end

#policy_nameObject



65
66
67
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 65

def policy_name
  policyfile_lock.name
end

#policyfile_lockObject



84
85
86
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 84

def policyfile_lock
  @policyfile_lock || validate_lockfile
end

#runObject



69
70
71
72
73
74
75
76
# File 'lib/chef-dk/policyfile_services/export_repo.rb', line 69

def run
  assert_lockfile_exists!
  assert_export_dir_clean!

  validate_lockfile
  write_updated_lockfile
  export
end