Skip to content

Commit

Permalink
Merge branch 'jk/resource_updates'
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeiser committed Oct 29, 2015
2 parents 8fc8257 + d368077 commit c6a10da
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 424 deletions.
16 changes: 2 additions & 14 deletions lib/chef/resource/chef_acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ class Resource
class ChefAcl < Cheffish::BaseResource
resource_name :chef_acl

def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

# Path of the thing being secured, e.g. nodes, nodes/*, nodes/mynode,
# */*, **, roles/base, data/secrets, cookbooks/apache2, /users/*,
# /organizations/foo/nodes/x
property :path, :kind_of => String, :name_attribute => true
property :path, String, name_property: true

# Whether to change things recursively. true means it will descend all children
# and make the same modifications to them. :on_change will only descend if
# the parent has changed. :on_change is the default.
property :recursive, :equal_to => [ true, false, :on_change ], :default => :on_change

# Specifies that this is a complete specification for the acl (i.e. rights
# you don't specify will be reset to their defaults)
property :complete, :kind_of => [TrueClass, FalseClass]

property :raw_json, :kind_of => Hash
property :chef_server, :kind_of => Hash
property :recursive, [ true, false, :on_change ], default: :on_change

# rights :read, :users => 'jkeiser', :groups => [ 'admins', 'users' ]
# rights [ :create, :read ], :users => [ 'jkeiser', 'adam' ]
Expand Down
23 changes: 6 additions & 17 deletions lib/chef/resource/chef_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,19 @@ class Resource
class ChefClient < Cheffish::ChefActorBase
resource_name :chef_client

def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

# Client attributes
property :name, :kind_of => String, :regex => Cheffish::NAME_REGEX, :name_attribute => true
property :admin, :kind_of => [TrueClass, FalseClass]
property :validator, :kind_of => [TrueClass, FalseClass]
property :name, Cheffish::NAME_REGEX, name_property: true
property :admin, Boolean
property :validator, Boolean

# Input key
property :source_key # String or OpenSSL::PKey::*
property :source_key_path, :kind_of => String
property :source_key_path, String
property :source_key_pass_phrase

# Output public key (if so desired)
property :output_key_path, :kind_of => String
property :output_key_format, :kind_of => Symbol, :default => :openssh, :equal_to => [ :pem, :der, :openssh ]

# If this is set, client is not patchy
property :complete, :kind_of => [TrueClass, FalseClass]

property :raw_json, :kind_of => Hash
property :chef_server, :kind_of => Hash
property :output_key_path, String
property :output_key_format, Symbol, default: :openssh, equal_to: [ :pem, :der, :openssh ]

# Proc that runs just before the resource executes. Called with (resource)
def before(&block)
Expand Down
10 changes: 1 addition & 9 deletions lib/chef/resource/chef_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ class Resource
class ChefContainer < Cheffish::BaseResource
resource_name :chef_container

# Grab environment from with_environment
def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

property :name, :kind_of => String, :regex => Cheffish::NAME_REGEX, :name_attribute => true
property :chef_server, :kind_of => Hash

property :name, Cheffish::NAME_REGEX, name_property: true

action :create do
if !@current_exists
Expand Down
10 changes: 1 addition & 9 deletions lib/chef/resource/chef_data_bag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ class Resource
class ChefDataBag < Cheffish::BaseResource
resource_name :chef_data_bag

def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

property :name, :kind_of => String, :regex => Cheffish::NAME_REGEX, :name_attribute => true

property :chef_server, :kind_of => Hash

property :name, Cheffish::NAME_REGEX, name_property: true

action :create do
if !current_resource_exists?
Expand Down
100 changes: 24 additions & 76 deletions lib/chef/resource/chef_data_bag_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,92 +11,40 @@ class ChefDataBagItem < Cheffish::BaseResource

def initialize(*args)
super
name @name
if !data_bag
if !property_is_set?(:data_bag) && run_context.cheffish.current_data_bag
data_bag run_context.cheffish.current_data_bag
end
if run_context.cheffish.current_data_bag_item_encryption
@encrypt = true if run_context.cheffish.current_data_bag_item_encryption[:encrypt_all]
@secret = run_context.cheffish.current_data_bag_item_encryption[:secret]
@secret_path = run_context.cheffish.current_data_bag_item_encryption[:secret_path] || run_context.config[:encrypted_data_bag_secret]
@encryption_cipher = run_context.cheffish.current_data_bag_item_encryption[:encryption_cipher]
@encryption_version = run_context.cheffish.current_data_bag_item_encryption[:encryption_version] || run_context.config[:data_bag_encrypt_version]
@old_secret = run_context.cheffish.current_data_bag_item_encryption[:old_secret]
@old_secret_path = run_context.cheffish.current_data_bag_item_encryption[:old_secret_path]
encryption = run_context.cheffish.current_data_bag_item_encryption
if encryption
encrypt true if encryption[:encrypt_all]
secret encryption[:secret] if encryption[:secret]
secret_path encryption[:secret_path] || run_context.config[:encrypted_data_bag_secret] if encryption[:secret_path] || run_context.config[:encrypted_data_bag_secret]
encryption_cipher encryption[:encryption_cipher] if encryption[:encryption_cipher]
encryption_version encryption[:encryption_version] || run_context.config[:data_bag_encrypt_version] if encryption[:encryption_version] || run_context.config[:data_bag_encrypt_version]
old_secret encryption[:old_secret] if encryption[:old_secret]
old_secret_path encryption[:old_secret_path] if encryption[:old_secret_path]
end
chef_server run_context.cheffish.current_chef_server
end

def name(*args)
result = super(*args)
if args.size == 1
parts = name.split('/')
if parts.size == 1
@id = parts[0]
elsif parts.size == 2
@data_bag = parts[0]
@id = parts[1]
else
raise "Name #{args[0].inspect} must be a string with 1 or 2 parts, either 'id' or 'data_bag/id"
end
end
result
end
# If data_bag and id are not specified, take them from name.
# name can either be id, or data_bag/id
property :id, String, default: lazy { name.split('/', 2)[-1] }
property :data_bag, String, default: lazy {
split = name.split('/', 2)[0]
split.size >= 2 ? split[0] : nil
}

# `NOT_PASSED` is defined in chef-12.5.0, this guard will ensure we
# don't redefine it if it's already there
NOT_PASSED = Object.new unless defined?(NOT_PASSED)

def id(value = NOT_PASSED)
if value == NOT_PASSED
@id
else
@id = value
name data_bag ? "#{data_bag}/#{id}" : id
end
end
def data_bag(value = NOT_PASSED)
if value == NOT_PASSED
@data_bag
else
@data_bag = value
name data_bag ? "#{data_bag}/#{id}" : id
end
end
property :raw_data, :kind_of => Hash
property :raw_data, Hash

# If secret or secret_path are set, encrypt is assumed true. encrypt exists mainly for with_secret and with_secret_path
property :encrypt, :kind_of => [TrueClass, FalseClass]
#property :secret, :kind_of => String
def secret(new_secret = nil)
if !new_secret
@secret
else
@secret = new_secret
@encrypt = true if @encrypt.nil?
end
end
#property :secret_path, :kind_of => String
def secret_path(new_secret_path = nil)
if !new_secret_path
@secret_path
else
@secret_path = new_secret_path
@encrypt = true if @encrypt.nil?
end
end
property :encryption_version, :kind_of => Integer
property :encrypt, Boolean, default: lazy { secret || secret_path }
property :secret, String
property :secret_path, String
property :encryption_version, Integer

# Old secret (or secrets) to read the old data bag when we are changing keys and re-encrypting data
property :old_secret, :kind_of => [String, Array]
property :old_secret_path, :kind_of => [String, Array]

# Specifies that this is a complete specification for the environment (i.e. attributes you don't specify will be
# reset to their defaults)
property :complete, :kind_of => [TrueClass, FalseClass]

property :raw_json, :kind_of => Hash
property :chef_server, :kind_of => Hash
property :old_secret, [String, Array]
property :old_secret_path, [String, Array]

# value 'ip_address', '127.0.0.1'
# value [ 'pushy', 'port' ], '9000'
Expand Down
28 changes: 6 additions & 22 deletions lib/chef/resource/chef_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,13 @@ class Resource
class ChefEnvironment < Cheffish::BaseResource
resource_name :chef_environment

def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

property :name, :kind_of => String, :regex => Cheffish::NAME_REGEX, :name_attribute => true
property :description, :kind_of => String
property :cookbook_versions, :kind_of => Hash, :callbacks => {
property :name, Cheffish::NAME_REGEX, name_property: true
property :description, String
property :cookbook_versions, Hash, callbacks: {
"should have valid cookbook versions" => lambda { |value| Chef::Environment.validate_cookbook_versions(value) }
}
property :default_attributes, :kind_of => Hash
property :override_attributes, :kind_of => Hash

# Specifies that this is a complete specification for the environment (i.e. attributes you don't specify will be
# reset to their defaults)
property :complete, :kind_of => [TrueClass, FalseClass]

property :raw_json, :kind_of => Hash
property :chef_server, :kind_of => Hash

# `NOT_PASSED` is defined in chef-12.5.0, this guard will ensure we
# don't redefine it if it's already there
NOT_PASSED=Object.new unless defined?(NOT_PASSED)
property :default_attributes, Hash
property :override_attributes, Hash

# default 'ip_address', '127.0.0.1'
# default [ 'pushy', 'port' ], '9000'
Expand Down Expand Up @@ -69,7 +53,7 @@ def override(attribute_path, value=NOT_PASSED, &block)
end

alias :attributes :default_attributes
alias :property :default
alias :attribute :default


action :create do
Expand Down
46 changes: 7 additions & 39 deletions lib/chef/resource/chef_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,13 @@ class Resource
class ChefGroup < Cheffish::BaseResource
resource_name :chef_group

# Grab environment from with_environment
def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
@users = []
@clients = []
@groups = []
@remove_users = []
@remove_clients = []
@remove_groups = []
end

property :name, :kind_of => String, :regex => Cheffish::NAME_REGEX, :name_attribute => true
def users(*users)
users.size == 0 ? @users : (@users |= users.flatten)
end
def clients(*clients)
clients.size == 0 ? @clients : (@clients |= clients.flatten)
end
def groups(*groups)
groups.size == 0 ? @groups : (@groups |= groups.flatten)
end
def remove_users(*remove_users)
remove_users.size == 0 ? @remove_users : (@remove_users |= remove_users.flatten)
end
def remove_clients(*remove_clients)
remove_clients.size == 0 ? @remove_clients : (@remove_clients |= remove_clients.flatten)
end
def remove_groups(*remove_groups)
remove_groups.size == 0 ? @remove_groups : (@remove_groups |= remove_groups.flatten)
end

# Specifies that this is a complete specification for the environment (i.e. attributes you don't specify will be
# reset to their defaults)
property :complete, :kind_of => [TrueClass, FalseClass]

property :raw_json, :kind_of => Hash
property :chef_server, :kind_of => Hash

property :name, Cheffish::NAME_REGEX, name_property: true
property :users, ArrayType
property :clients, ArrayType
property :groups, ArrayType
property :remove_users, ArrayType
property :remove_clients, ArrayType
property :remove_groups, ArrayType

action :create do
differences = json_differences(current_json, new_json)
Expand Down
26 changes: 9 additions & 17 deletions lib/chef/resource/chef_mirror.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,36 @@ class Resource
class ChefMirror < Cheffish::BaseResource
resource_name :chef_mirror

def initialize(*args)
super
chef_server run_context.cheffish.current_chef_server
end

# Path of the data to mirror, e.g. nodes, nodes/*, nodes/mynode,
# */*, **, roles/base, data/secrets, cookbooks/apache2, etc.
property :path, :kind_of => String, :name_attribute => true
property :path, String, name_property: true

# Local path. Can be a string (top level of repository) or hash
# (:chef_repo_path, :node_path, etc.)
# If neither chef_repo_path nor versioned_cookbooks are set, they default to their
# Chef::Config values. If chef_repo_path is set but versioned_cookbooks is not,
# versioned_cookbooks defaults to true.
property :chef_repo_path, :kind_of => [ String, Hash ]
property :chef_repo_path, [ String, Hash ]

# Whether the repo path should contain cookbooks with versioned names,
# i.e. cookbooks/mysql-1.0.0, cookbooks/mysql-1.2.0, etc.
# Defaults to true if chef_repo_path is specified, or to Chef::Config.versioned_cookbooks otherwise.
property :versioned_cookbooks, :kind_of => [ TrueClass, FalseClass ]

# Chef server
property :chef_server, :kind_of => Hash
property :versioned_cookbooks, Boolean

# Whether to purge deleted things: if we do not have cookbooks/x locally and we
# *do* have cookbooks/x remotely, then :upload with purge will delete it.
# Defaults to false.
property :purge, :kind_of => [ TrueClass, FalseClass ]
property :purge, Boolean

# Whether to freeze cookbooks on upload
property :freeze, :kind_of => [ TrueClass, FalseClass ]
property :freeze, Boolean

# If this is true, only new files will be copied. File contents will not be
# diffed, so changed files will never be uploaded.
property :no_diff, :kind_of => [ TrueClass, FalseClass ]
property :no_diff, Boolean

# Number of parallel threads to list/upload/download with. Defaults to 10.
property :concurrency, :kind_of => Integer
property :concurrency, Integer, default: 10, desired_state: false


action :upload do
Expand Down Expand Up @@ -87,11 +79,11 @@ def with_modified_config
end

def copy_to(src_root, dest_root)
if new_resource.concurrency && new_resource.concurrency <= 0
if new_resource.concurrency <= 0
raise "chef_mirror.concurrency must be above 0! Was set to #{new_resource.concurrency}"
end
# Honor concurrency
Chef::ChefFS::Parallelizer.threads = (new_resource.concurrency || 10) - 1
Chef::ChefFS::Parallelizer.threads = new_resource.concurrency - 1

# We don't let the user pass absolute paths; we want to reserve those for
# multi-org support (/organizations/foo).
Expand Down
Loading

0 comments on commit c6a10da

Please sign in to comment.