Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions libraries/chef_artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ module Artifact
module File

# Returns true if the given file is a symlink.
#
#
# @param path [String] the path to the file to test
#
#
# @return [Boolean]
def symlink?(path)
if windows?
require 'chef/win32/file'
return Chef::ReservedNames::Win32::File.symlink?(path)
end
::File.symlink?(path)
::File.symlink?(path)
end

# Returns the value of the readlink method.
#
#
# @param path [String] the path to a symlink
#
#
# @return [String] the path that the symlink points to
def readlink(path)
if windows?
Expand All @@ -34,11 +34,11 @@ def readlink(path)
end

# Generates a command to execute that either uses the Unix cp
# command or the Windows copy command.
# command or the Windows copy command.
#
# @param source [String] the file to copy
# @param destination [String] the path to copy the source to
#
#
# @return [String] a useable command to copy a file
def copy_command_for(source, destination)
if windows?
Expand All @@ -62,7 +62,7 @@ class << self
#
# @param environment [String] the environment
# @param source [String] the deployment source to load configuration for
#
#
# @return [Chef::DataBagItem] the data bag item
def data_bag_config_for(environment, source)
data_bag_item = if Chef::Config[:solo]
Expand Down Expand Up @@ -155,7 +155,7 @@ def get_s3_object(bucket_name, object_name)
# Nexus source.
#
# @param location [String] the artifact_location
#
#
# @return [Boolean] true when the location is a colon-separated value
def from_nexus?(location)
!from_http?(location) && location.split(":").length > 2
Expand All @@ -173,9 +173,9 @@ def from_s3?(location)

# Returns true when the artifact is believed to be from an
# http source.
#
#
# @param location [String] the artifact_location
#
#
# @return [Boolean] true when the location matches http or https.
def from_http?(location)
location_of_type(location, %w(http https))
Expand Down Expand Up @@ -211,17 +211,26 @@ def snapshot?(version)
version.end_with?("-SNAPSHOT")
end

# Convenience method for determining whether a String is "release"
#
# @param version [String] the version of the configured artifact to check
#
# @return [Boolean] true when version matches (case-insensitive) "release"
def release?(version)
version.casecmp("release") == 0
end

# Returns the currently deployed version of an artifact given that artifacts
# installation directory by reading what directory the 'current' symlink
# points to.
# if the 'current' directory is not a symlink, a ".symlinks" file is opened and the value
# indicated by the 'current' key is returned.
#
# @param deploy_to_dir [String] the directory where an artifact is installed
#
#
# @example
# Chef::Artifact.get_current_deployed_version("/opt/my_deploy_dir") => "2.0.65"
#
#
# @return [String] the currently deployed version of the given artifact
def get_current_deployed_version(deploy_to_dir)

Expand All @@ -238,12 +247,12 @@ def get_current_deployed_version(deploy_to_dir)
end

# Looks for the given data bag in the cache and if not found, will load a
# data bag item named for the chef_environment, '_wildcard', or the old
# data bag item named for the chef_environment, '_wildcard', or the old
# 'nexus' value.
#
# @param environment [String] the environment
# @param data_bag [String] the data bag to load
#
#
# @return [Chef::Mash] the data bag item in Mash form
def encrypted_data_bag_for(environment, data_bag)
@encrypted_data_bags = {} unless @encrypted_data_bags
Expand All @@ -268,7 +277,7 @@ def encrypted_data_bags
# Loads an entry from the encrypted_data_bags class variable.
#
# @param data_bag [String] the data bag to find
#
#
# @return [type] [description]
def get_from_data_bags_cache(data_bag)
encrypted_data_bags[data_bag]
Expand All @@ -280,15 +289,17 @@ def get_from_data_bags_cache(data_bag)
#
# @param data_bag [String]
# @param data_bag_item [String]
#
#
# @raise [Chef::Artifact::DataBagEncryptionError] when the data bag cannot be decrypted
# or transformed into a Mash for some reason (Chef 10 vs Chef 11 data bag changes).
#
#
# @return [Chef::Mash]
def encrypted_data_bag_item(data_bag, data_bag_item)
Mash.from_hash(Chef::EncryptedDataBagItem.load(data_bag, data_bag_item).to_hash)
rescue Net::HTTPServerException => e
nil
rescue ArgumentError, Errno::ENOENT
Mash.from_hash(Chef::DataBagItem.load(data_bag, data_bag_item).to_hash)
rescue NoMethodError
raise DataBagEncryptionError.new
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/chef_artifact_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def remote
# @return [String] the version number that latest resolves to or the passed in value
def get_actual_version(coordinates)
artifact = NexusCli::Artifact.new(coordinates)
if Chef::Artifact.latest?(artifact.version) || Chef::Artifact.snapshot?(artifact.version)
if Chef::Artifact.release?(artifact.version) || Chef::Artifact.latest?(artifact.version) || Chef::Artifact.snapshot?(artifact.version)
REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//version"].text
else
artifact.version
Expand Down
3 changes: 2 additions & 1 deletion providers/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def load_current_resource

if Chef::Artifact.from_nexus?(@new_resource.artifact_location)
chef_gem "nexus_cli" do
version "4.0.2"
version "4.1.1"
action :upgrade
end

@nexus_configuration_object = new_resource.nexus_configuration
Expand Down
13 changes: 7 additions & 6 deletions providers/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Provider:: file
#
# Author:: Kyle Allan (<[email protected]>)
#
#
# Copyright 2013, Riot Games
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -32,7 +32,8 @@ def load_current_resource
if Chef::Artifact.from_nexus?(new_resource.location)

chef_gem "nexus_cli" do
version "4.0.2"
version "4.1.1"
action :upgrade
end

@nexus_configuration = new_resource.nexus_configuration
Expand All @@ -58,7 +59,7 @@ def load_current_resource
run_proc :after_download
end
elsif Chef::Artifact.from_nexus?(file_location)
unless ::File.exists?(new_resource.path) && checksum_valid? && (!Chef::Artifact.snapshot?(file_location) || !Chef::Artifact.latest?(file_location))
unless ::File.exists?(new_resource.path) && checksum_valid? && (!Chef::Artifact.snapshot?(file_location) || !Chef::Artifact.latest?(file_location) || !Chef::Artifact.release?(file_location))
begin
if ::File.exists?(new_resource.path)
if Digest::SHA1.file(new_resource.path).hexdigest != nexus_connection.get_artifact_sha(file_location)
Expand Down Expand Up @@ -102,7 +103,7 @@ def checksum_valid?

if cached_checksum_exists?
if Chef::Artifact.from_nexus?(file_location)
if Chef::Artifact.snapshot?(file_location) || Chef::Artifact.latest?(file_location)
if Chef::Artifact.snapshot?(file_location) || Chef::Artifact.latest?(file_location) || Chef::Artifact.release?(file_location)
return Digest::SHA1.file(new_resource.path).hexdigest == nexus_connection.get_artifact_sha(file_location)
end
end
Expand Down Expand Up @@ -147,7 +148,7 @@ def run_proc(name)
execute_run_proc("artifact_file", new_resource, name)
end

# Scrubs the file_location and returns the path to
# Scrubs the file_location and returns the path to
# the resource's checksum file.
#
# @return [String]
Expand Down Expand Up @@ -178,7 +179,7 @@ def cached_checksum_exists?
::File.exists?(cached_checksum)
end

# Writes a file to file_cache_path. This file contains a SHA256 digest of the
# Writes a file to file_cache_path. This file contains a SHA256 digest of the
# artifact file. Returns the result of the file.puts command, which will be nil.
#
# @return [NilClass]
Expand Down
3 changes: 2 additions & 1 deletion providers/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
def load_current_resource
if Chef::Artifact.from_nexus?(new_resource.location)
chef_gem "nexus_cli" do
version "4.0.2"
version "4.1.1"
action :upgrade
end
require 'nexus_cli'
artifact = NexusCli::Artifact.new(new_resource.location)
Expand Down
10 changes: 8 additions & 2 deletions spec/libraries/chef_artifact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

it "loads an encrypted data bag" do
expect(data_bag_config_for.symbolize_keys).to eq(data_bag_item)
end
end
end

context "when loading legacy data bag format" do
Expand Down Expand Up @@ -137,7 +137,7 @@
it "looks for the '_wildcard' data bag item" do
described_class.stub(:encrypted_data_bag_item).and_return(nil, data_bag_item)
described_class.should_receive(:encrypted_data_bag_item).with(data_bag, "_wildcard")
expect(encrypted_data_bag_for).to eq(data_bag_item)
expect(encrypted_data_bag_for).to eq(data_bag_item)
end

it "looks for the 'nexus' data bag item" do
Expand Down Expand Up @@ -205,6 +205,12 @@
specify { described_class.latest?('3.0.1').should eq(false) }
end

describe ":release?" do
specify { described_class.latest?('release').should eq(true) }
specify { described_class.latest?('RELease').should eq(true) }
specify { described_class.latest?('3.0.1').should eq(false) }
end

describe ":snapshot?" do
specify { described_class.snapshot?('1.0.0-SNAPSHOT').should eq(true) }
specify { described_class.snapshot?('3.0.1').should eq(false) }
Expand Down