class ChefCLI::PolicyfileServices::Push
def http_client
def http_client @http_client ||= Chef::ServerAPI.new(config.chef_server_url, signing_key_filename: config.client_key, client_name: config.node_name) end
def initialize(policyfile: nil, ui: nil, policy_group: nil, config: nil, root_dir: nil)
def initialize(policyfile: nil, ui: nil, policy_group: nil, config: nil, root_dir: nil) @root_dir = root_dir @ui = ui @config = config @policy_group = policy_group 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) @http_client = nil @policy_data = nil end
def policy_data
def policy_data @policy_data ||= FFI_Yajl::Parser.parse(IO.read(policyfile_lock_expanded_path)) rescue => error raise PolicyfilePushError.new("Error reading lockfile #{policyfile_lock_expanded_path}", error) end
def policyfile_lock
def policyfile_lock @policyfile_lock || validate_lockfile end
def run
def run unless File.exist?(policyfile_lock_expanded_path) raise LockfileNotFound, "No lockfile at #{policyfile_lock_expanded_path} - you need to run `install` before `push`" end validate_lockfile write_updated_lockfile upload_policy end
def upload_policy
def upload_policy uploader.upload rescue => error raise PolicyfilePushError.new("Failed to upload policy to policy group #{policy_group}", error) end
def uploader
def uploader ChefCLI::Policyfile::Uploader.new(policyfile_lock, policy_group, ui: ui, http_client: http_client, policy_document_native_api: config.policy_document_native_api) end
def validate_lockfile
def validate_lockfile return @policyfile_lock if @policyfile_lock @policyfile_lock = ChefCLI::PolicyfileLock.new(storage_config).build_from_lock_data(policy_data) # TODO: enumerate any cookbook that have been updated @policyfile_lock.validate_cookbooks! @policyfile_lock rescue => error raise PolicyfilePushError.new("Invalid lockfile data", error) end
def write_updated_lockfile
def write_updated_lockfile with_file(policyfile_lock_expanded_path) do |f| f.print(FFI_Yajl::Encoder.encode(policyfile_lock.to_lock, pretty: true )) end end