summaryrefslogtreecommitdiff
path: root/lib/chef/mixin
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/mixin')
-rw-r--r--lib/chef/mixin/check_helper.rb31
-rw-r--r--lib/chef/mixin/checksum.rb36
-rw-r--r--lib/chef/mixin/command.rb206
-rw-r--r--lib/chef/mixin/create_path.rb56
-rw-r--r--lib/chef/mixin/from_file.rb36
-rw-r--r--lib/chef/mixin/generate_url.rb46
-rw-r--r--lib/chef/mixin/params_validate.rb197
-rw-r--r--lib/chef/mixin/template.rb39
8 files changed, 0 insertions, 647 deletions
diff --git a/lib/chef/mixin/check_helper.rb b/lib/chef/mixin/check_helper.rb
deleted file mode 100644
index 959b57cbb7..0000000000
--- a/lib/chef/mixin/check_helper.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-class Chef
- module Mixin
- module CheckHelper
- def set_if_args(thing, arguments)
- raise ArgumentError, "Must call set_if_args with a block!" unless Kernel.block_given?
- if arguments != nil
- yield(arguments)
- else
- thing
- end
- end
- end
- end
-end
diff --git a/lib/chef/mixin/checksum.rb b/lib/chef/mixin/checksum.rb
deleted file mode 100644
index 5a7eed4165..0000000000
--- a/lib/chef/mixin/checksum.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'digest/md5'
-
-class Chef
- module Mixin
- module Checksum
-
- def checksum(file)
- digest = Digest::MD5.new
- fh = ::File.open(file)
- fh.each do |line|
- digest.update(line)
- end
- digest.hexdigest
- end
-
- end
- end
-end \ No newline at end of file
diff --git a/lib/chef/mixin/command.rb b/lib/chef/mixin/command.rb
deleted file mode 100644
index 08344148a5..0000000000
--- a/lib/chef/mixin/command.rb
+++ /dev/null
@@ -1,206 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-require 'tmpdir'
-require 'fcntl'
-require 'etc'
-
-class Chef
- module Mixin
- module Command
-
- def run_command(args={})
- if args.has_key?(:creates)
- if File.exists?(args[:creates])
- Chef::Log.debug("Skipping #{args[:command_string]} - creates #{args[:creates]} exists.")
- return false
- end
- end
-
- if args.has_key?(:onlyif)
- status = popen4(args[:onlyif]) { |p, i, o, e| }
- if status.exitstatus != 0
- Chef::Log.debug("Skipping #{args[:command_string]} - onlyif #{args[:onlyif]} returned #{status.exitstatus}")
- return false
- end
- end
-
- if args.has_key?(:not_if)
- status = popen4(args[:not_if]) { |p, i, o, e| }
- if status.exitstatus == 0
- Chef::Log.debug("Skipping #{args[:command_string]} - unless #{args[:not_if]} returned #{status.exitstatus}")
- return false
- end
- end
-
- exec_processing_block = lambda do |pid, stdin, stdout, stderr|
- stdin.close
-
- stdout_string = stdout.gets(nil)
- if stdout_string
- Chef::Log.debug("---- Begin #{args[:command_string]} STDOUT ----")
- Chef::Log.debug(stdout_string.strip)
- Chef::Log.debug("---- End #{args[:command_string]} STDOUT ----")
- end
- stderr_string = stderr.gets(nil)
- if stderr_string
- Chef::Log.debug("---- Begin #{args[:command_string]} STDERR ----")
- Chef::Log.debug(stderr_string.strip)
- Chef::Log.debug("---- End #{args[:command_string]} STDERR ----")
- end
- end
-
- args[:cwd] ||= Dir.tmpdir
- unless File.directory?(args[:cwd])
- raise Chef::Exception::Exec, "#{args[:cwd]} does not exist or is not a directory"
- end
-
- status = nil
- Dir.chdir(args[:cwd]) do
- if args[:timeout]
- begin
- Timeout.timeout(args[:timeout]) do
- status = popen4(args[:command], args, &exec_processing_block)
- end
- rescue Exception => e
- Chef::Log.error("#{args[:command_string]} exceeded timeout #{args[:timeout]}")
- raise(e)
- end
- else
- status = popen4(args[:command], args, &exec_processing_block)
- end
-
- args[:returns] ||= 0
- if status.exitstatus != args[:returns]
- raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
- else
- Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
- end
- end
- status
- end
-
- module_function :run_command
-
- # This is taken directly from Ara T Howard's Open4 library, and then
- # modified to suit the needs of Chef. Any bugs here are most likely
- # my own, and not Ara's.
- #
- # The original appears in external/open4.rb in it's unmodified form.
- #
- # Thanks, Ara.
- def popen4(cmd, args={}, &b)
-
- args[:user] ||= nil
- unless args[:user].kind_of?(Integer)
- args[:user] = Etc.getpwnam(args[:user]).uid if args[:user]
- end
- args[:group] ||= nil
- unless args[:group].kind_of?(Integer)
- args[:group] = Etc.getgrnam(args[:group]).gid if args[:group]
- end
- args[:environment] ||= nil
-
- pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe
-
- verbose = $VERBOSE
- begin
- $VERBOSE = nil
- ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
-
- cid = fork {
- pw.last.close
- STDIN.reopen pw.first
- pw.first.close
-
- pr.first.close
- STDOUT.reopen pr.last
- pr.last.close
-
- pe.first.close
- STDERR.reopen pe.last
- pe.last.close
-
- STDOUT.sync = STDERR.sync = true
-
- if args[:user]
- Process.euid = args[:user]
- Process.uid = args[:user]
- end
-
- if args[:group]
- Process.egid = args[:group]
- Process.gid = args[:group]
- end
-
- if args[:environment]
- args[:environment].each do |key,value|
- ENV[key] = value
- end
- end
-
- begin
- if cmd.kind_of?(Array)
- exec(*cmd)
- else
- exec(cmd)
- end
- raise 'forty-two'
- rescue Exception => e
- Marshal.dump(e, ps.last)
- ps.last.flush
- end
- ps.last.close unless (ps.last.closed?)
- exit!
- }
- ensure
- $VERBOSE = verbose
- end
-
- [pw.first, pr.last, pe.last, ps.last].each{|fd| fd.close}
-
- begin
- e = Marshal.load ps.first
- raise(Exception === e ? e : "unknown failure!")
- rescue EOFError # If we get an EOF error, then the exec was successful
- 42
- ensure
- ps.first.close
- end
-
- pw.last.sync = true
-
- pi = [pw.last, pr.first, pe.first]
-
- if b
- begin
- b[cid, *pi]
- Process.waitpid2(cid).last
- ensure
- pi.each{|fd| fd.close unless fd.closed?}
- end
- else
- [cid, pw.last, pr.first, pe.first]
- end
- end
-
- module_function :popen4
- end
- end
-end \ No newline at end of file
diff --git a/lib/chef/mixin/create_path.rb b/lib/chef/mixin/create_path.rb
deleted file mode 100644
index dc9200eaf5..0000000000
--- a/lib/chef/mixin/create_path.rb
+++ /dev/null
@@ -1,56 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-class Chef
- module Mixin
- module CreatePath
-
- # Creates a given path, including all directories that lead up to it.
- # Like mkdir_p, but without the leaking.
- #
- # === Parameters
- # file_path<String, Array>:: A string that represents the path to create,
- # or an Array with the path-parts.
- #
- # === Returns
- # The created file_path.
- def create_path(file_path)
- unless file_path.kind_of?(String) || file_path.kind_of?(Array)
- raise ArgumentError, "file_path must be a string or an array!"
- end
-
- if file_path.kind_of?(String)
- file_path = File.expand_path(file_path).split(File::SEPARATOR)
- file_path.shift if file_path[0] = ''
- unless file_path[0].match("^#{File::SEPARATOR}")
- file_path[0] = "#{File::SEPARATOR}#{file_path[0]}"
- end
- end
-
- file_path.each_index do |i|
- create_path = File.join(file_path[0, i + 1])
- unless File.directory?(create_path)
- Chef::Log.debug("Creating directory #{create_path}")
- Dir.mkdir(create_path)
- end
- end
- File.expand_path(File.join(file_path))
- end
-
- end
- end
-end \ No newline at end of file
diff --git a/lib/chef/mixin/from_file.rb b/lib/chef/mixin/from_file.rb
deleted file mode 100644
index 05f7fe8e8b..0000000000
--- a/lib/chef/mixin/from_file.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-class Chef
- module Mixin
- module FromFile
-
- # Loads a given ruby file, and runs instance_eval against it in the context of the current
- # object.
- #
- # Raises an IOError if the file cannot be found, or is not readable.
- def from_file(filename)
- if File.exists?(filename) && File.readable?(filename)
- self.instance_eval(IO.read(filename), filename, 1)
- else
- raise IOError, "Cannot open or read #{filename}!"
- end
- end
- end
- end
-end
diff --git a/lib/chef/mixin/generate_url.rb b/lib/chef/mixin/generate_url.rb
deleted file mode 100644
index 3eb8c4f70d..0000000000
--- a/lib/chef/mixin/generate_url.rb
+++ /dev/null
@@ -1,46 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-class Chef
- module Mixin
- module GenerateURL
-
- def generate_cookbook_url(url, cookbook, type, node, args=nil)
- new_url = nil
- if url =~ /^http/
- new_url = url
- else
- new_url = "cookbooks/#{cookbook}/#{type}?"
- new_url += "id=#{url}"
- platform, version = Chef::Platform.find_platform_and_version(node)
- if type == "files" || type == "templates"
- new_url += "&platform=#{platform}&version=#{version}&fqdn=#{node[:fqdn]}"
- end
- if args
- args.each do |key, value|
- new_url += "&#{key}=#{value}"
- end
- end
- end
-
- return new_url
- end
-
- end
- end
-end
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb
deleted file mode 100644
index 4d8ddb74c3..0000000000
--- a/lib/chef/mixin/params_validate.rb
+++ /dev/null
@@ -1,197 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-class Chef
- module Mixin
- module ParamsValidate
-
- # Takes a hash of options, along with a map to validate them. Returns the original
- # options hash, plus any changes that might have been made (through things like setting
- # default values in the validation map)
- #
- # For example:
- #
- # validate({ :one => "neat" }, { :one => { :kind_of => String }})
- #
- # Would raise an exception if the value of :one above is not a kind_of? string. Valid
- # map options are:
- #
- # :default:: Sets the default value for this parameter.
- # :callbacks:: Takes a hash of Procs, which should return true if the argument is valid.
- # The key will be inserted into the error message if the Proc does not return true:
- # "Option #{key}'s value #{value} #{message}!"
- # :kind_of:: Ensure that the value is a kind_of?(Whatever). If passed an array, it will ensure
- # that the value is one of those types.
- # :respond_to:: Ensure that the value has a given method. Takes one method name or an array of
- # method names.
- # :required:: Raise an exception if this parameter is missing. Valid values are true or false,
- # by default, options are not required.
- # :regex:: Match the value of the paramater against a regular expression.
- # :equal_to:: Match the value of the paramater with ==. An array means it can be equal to any
- # of the values.
- def validate(opts, map)
- #--
- # validate works by taking the keys in the validation map, assuming it's a hash, and
- # looking for _pv_:symbol as methods. Assuming it find them, it calls the right
- # one.
- #++
- raise ArgumentError, "Options must be a hash" unless opts.kind_of?(Hash)
- raise ArgumentError, "Validation Map must be a hash" unless map.kind_of?(Hash)
-
- map.each do |key, validation|
- unless key.kind_of?(Symbol) || key.kind_of?(String)
- raise ArgumentError, "Validation map keys must be symbols or strings!"
- end
- case validation
- when true
- _pv_required(opts, key)
- when false
- true
- when Hash
- validation.each do |check, carg|
- check_method = "_pv_#{check.to_s}"
- if self.respond_to?(check_method, true)
- self.send(check_method, opts, key, carg)
- else
- raise ArgumentError, "Validation map has unknown check: #{check}"
- end
- end
- end
- end
- opts
- end
-
- def set_or_return(symbol, arg, validation)
- iv_symbol = "@#{symbol.to_s}".to_sym
- map = {
- symbol => validation
- }
- if arg == nil
- self.instance_variable_get(iv_symbol)
- else
- validate({ symbol => arg }, { symbol => validation })
- self.instance_variable_set(iv_symbol, arg)
- end
- end
-
- private
-
- # Return the value of a parameter, or nil if it doesn't exist.
- def _pv_opts_lookup(opts, key)
- if opts.has_key?(key.to_s)
- opts[key.to_s]
- elsif opts.has_key?(key.to_sym)
- opts[key.to_sym]
- else
- nil
- end
- end
-
- # Raise an exception if the parameter is not found.
- def _pv_required(opts, key, is_required=true)
- if is_required
- if opts.has_key?(key.to_s) || opts.has_key?(key.to_sym)
- true
- else
- raise ArgumentError, "Required argument #{key} is missing!"
- end
- end
- end
-
- def _pv_equal_to(opts, key, to_be)
- value = _pv_opts_lookup(opts, key)
- if value != nil
- passes = false
- [ to_be ].flatten.each do |tb|
- if value == tb
- passes = true
- end
- end
- unless passes
- raise ArgumentError, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}."
- end
- end
- end
-
- # Raise an exception if the parameter is not a kind_of?(to_be)
- def _pv_kind_of(opts, key, to_be)
- value = _pv_opts_lookup(opts, key)
- if value != nil
- passes = false
- [ to_be ].flatten.each do |tb|
- if value.kind_of?(tb)
- passes = true
- end
- end
- unless passes
- raise ArgumentError, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}."
- end
- end
- end
-
- # Raise an exception if the parameter does not respond to a given set of methods.
- def _pv_respond_to(opts, key, method_name_list)
- value = _pv_opts_lookup(opts, key)
- if value != nil
- [ method_name_list ].flatten.each do |method_name|
- unless value.respond_to?(method_name)
- raise ArgumentError, "Option #{key} must have a #{method_name} method!"
- end
- end
- end
- end
-
- # Assign a default value to a parameter.
- def _pv_default(opts, key, default_value)
- value = _pv_opts_lookup(opts, key)
- if value == nil
- opts[key] = default_value
- end
- end
-
- # Check a parameter against a regular expression.
- def _pv_regex(opts, key, regex)
- value = _pv_opts_lookup(opts, key)
- passes = false
- [ regex ].flatten.each do |r|
- if value != nil
- if r.match(value.to_s)
- passes = true
- end
- end
- end
- unless passes
- raise ArgumentError, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}"
- end
- end
-
- # Check a parameter against a hash of proc's.
- def _pv_callbacks(opts, key, callbacks)
- raise ArgumentError, "Callback list must be a hash!" unless callbacks.kind_of?(Hash)
- value = _pv_opts_lookup(opts, key)
- if value != nil
- callbacks.each do |message, zeproc|
- if zeproc.call(value) != true
- raise ArgumentError, "Option #{key}'s value #{value} #{message}!"
- end
- end
- end
- end
- end
- end
-end
-
diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb
deleted file mode 100644
index f3980922a1..0000000000
--- a/lib/chef/mixin/template.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'tempfile'
-require 'erubis'
-
-class Chef
- module Mixin
- module Template
-
- # Render a template with Erubis. Takes a template as a string, and a
- # context hash.
- def render_template(template, context)
- eruby = Erubis::Eruby.new(template)
- output = eruby.evaluate(context)
- final_tempfile = Tempfile.new("chef-rendered-template")
- final_tempfile.print(output)
- final_tempfile.close
- final_tempfile
- end
-
- end
- end
-end