class Chef::Provider

Public Instance Methods

__libarchive__() click to toggle source

Install libarchive package and Gem.

@return [undefined]

@api private

# File lib/garcon/chef/provider/archive.rb, line 355
def __libarchive__
  recipe_eval do
    run_context.include_recipe 'build-essential::default'
  end

  p = Chef::Resource::Package.new('libarchive', run_context)
  p.only_if { node[:platform_family] == 'rhel' }
  p.run_action :install

  p = Chef::Resource::Package.new('libarchive-dev', run_context)
  p.package_name libarchive
  p.run_action :install

  g = Chef::Resource::ChefGem.new('libarchive-ruby', run_context)
  g.compile_time(false) if respond_to?(:compile_time)
  g.version '0.0.3'
  g.run_action :install
rescue LoadError
  recipe_eval do
    run_context.include_recipe 'build-essential::default'
  end
  g.run_action :install
end
__zip__() click to toggle source

Safely install and require the zip Gem

@return [undefined]

@api private

# File lib/garcon/chef/provider/archive.rb, line 340
def __zip__
  __libarchive__
  require 'zip' unless defined?(Zip)
rescue LoadError
  g = Chef::Resource::ChefGem.new('zip', run_context)
  g.compile_time(false) if respond_to?(:compile_time)
  g.run_action :install
  require 'zip'
end
archive_formats() click to toggle source
# File lib/garcon/chef/provider/archive.rb, line 180
def archive_formats
  %w[.zip .tar .gz .bz2 .tar.gz .tar.bz2]
end
args(args = []) click to toggle source

Gather options to download file.

@return [Array]

@api private

# File lib/garcon/chef/provider/download.rb, line 262
def args(args = [])
  args << "--out=#{::File.basename(r.path)}"
  args << "--dir=#{::File.dirname(r.path)}"
  args << "--checksum=sha-256=#{r.checksum}" if r.checksum
  args << "--header='#{r.header}'" if r.header
  args << "--check-certificate=#{r.check_cert}"
  args << "--file-allocation=falloc"
  args << "--max-connection-per-server=#{r.connections}"
  args << r.source
end
backup(file = nil) click to toggle source

Backup the file before overwriting or replacing it unless ‘new_resource.backup` is `false`

@return [undefined]

@api private

# File lib/garcon/chef/provider/download.rb, line 290
def backup(file = nil)
  Chef::Util::Backup.new(new_resource, file).backup!
end
cached_file() click to toggle source

Cache a file locally in Chef::Config.

@note The file is gargbage collected at the end of a run.

@return [String]

Path to the cached file.
# File lib/garcon/chef/provider/archive.rb, line 313
def cached_file
  if r.source =~ URI::ABS_URI &&
    %w[ftp http https].include?(URI.parse(r.source).scheme)
    file = ::File.basename(URI.unescape(URI.parse(r.source).path))
    cache_file_path = file_cache_path(file)

    d ||= Chef::Resource::Download.new(cache_file_path, run_context)
    d.backup      false
    d.source      r.source
    d.owner       r.owner      if r.owner
    d.group       r.group      if r.group
    d.header      r.header     if r.header
    d.checksum    r.checksum   if r.checksum
    d.check_cert  r.check_cert
    d.run_action  :create
  else
    cache_file_path = r.source
  end

  cache_file_path
end
civilize_docker() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 142
def civilize_docker
  r.docker.each { |pkg| package pkg }
end
civilize_locale() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 115
def civilize_locale
  case node[:platform_family]
  when 'debian'
    e = Chef::Resource::Execute.new('Set locale', run_context)
    e.command "/usr/sbin/update-locale LANG=#{r.local}"
    e.action :run unless docker?
  when 'rhel'
    t ||= Chef::Resource::Template.new('/etc/sysconfig/i18n', run_context)
    t.cookbook   'garcon'
    t.owner      'root'
    t.group      'root'
    t.mode        00644
    t.variables   local: r.local
    t.run_action :create unless docker?
  end
end
civilize_platform() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 132
def civilize_platform
  case node[:platform]
  when 'debian', 'ubuntu'
    run_context.include_recipe 'motd-tail::default'
    m = Chef::Resource::MotdTail.new('/etc/motd.tail', run_context)
    m.template_source 'motd.erb'
    m.action          :create
  end
end
do_download() click to toggle source

Command line executioner for running aria2.

@return [undefined]

@api private

# File lib/garcon/chef/provider/download.rb, line 278
def do_download
  retrier(tries: 10, sleep: ->(n) { 4**n }) { installed?('aria2') }
  cmd = [which('aria2c')] << args.flatten.join(' ')
  Chef::Log.info shell_out!(cmd.flatten.join(' ')).stdout
end
dotfiles() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 169
def dotfiles
  users = if r.dotfiles.is_a?(TrueClass)
            Array('root')
          elsif r.dotfiles.respond_to?(:to_ary)
            r.dotfiles
          elsif r.dotfiles.respond_to?(:to_str)
            Array(r.dotfiles)
          end

  users.each do |user|
    home = user =~ /root/ ? '/root' : "/home/#{user}"
    ['.bashrc', '.inputrc'].each do |dot|
      file = ::File.join(home, dot)
      f ||= Chef::Resource::CookbookFile.new(file, run_context)
      f.source      dot[1..-1]
      f.cookbook   'garcon'
      f.backup      false
      f.owner       user
      f.group       user
      f.mode        00644
      f.run_action :create
    end
  end
end
extract() click to toggle source

Extracts an archive file by determining the extention of the file and sending it unzip method or extract. The source can be a file path or a URL, for the later the file is downloaded in the Chef cache path. By default the archive file will not be deleted. To have Chef remove the file after it has been extracted set ‘remove_after true` on the resource.

@example

archive 'file.tar.gz' do
  source 'http://server.example.com/file.tar.gz'
  remove_after true
end

@return

# File lib/garcon/chef/provider/archive.rb, line 199
def extract
  src = ::File.extname(::File.basename r.source)
  Chef::Log.warn ''
  Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  Chef::Log.warn "      The source file: #{r.source}"
  Chef::Log.warn "The file extention is: #{src}"
  Chef::Log.warn "Reckon it's a archive? #{archive_formats.include? src}"
  Chef::Log.warn "Whatcha going to do with that thing then?"
  Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  Chef::Log.warn ''

  if archive_formats.include? src
    if archive_formats.include?(src) && src =~ /^.zip$/i
      Chef::Log.warn "Unzip the mother fucker!"
    elsif src =~ /^(.tar.gz|.tar|.gz|.bz2|.tar.bz2)$/
      Chef::Log.warn "Liberate the archive!!!"
    else
      Chef::Log.warn "Fuck dude, can't do shit with it."
    end
  else
    Chef::Log.warn 'Aint for format I can fuck with dude, stick it'
  end
  Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  Chef::Log.warn ''
  Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'

  if archive_formats.include? src
    if src =~ /^.zip$/i
      Chef::Log.info "Extracting Zip file #{r.source} to #{r.path}"
    elsif src =~ /^(.tar.gz|.tar|.gz|.bz2|.tar.bz2)$/
      Chef::Log.info "Extracting archive file #{r.source} to #{r.path}"
      updated = extract(r.source, r.path, Array(r.options))
    end
  else
    Chef::Log.info "Copying cached file into #{r.path}"
    FileUtils.cp cached_file, r.path
  end
end
handle_prerequisites() click to toggle source
# File lib/garcon/chef/provider/download.rb, line 304
def handle_prerequisites
  if node.platform_family == 'rhel'
    package 'gnutls', :install
    yumrepo(:create)
  end
  package 'aria2', :install
  wipe_repo if node.platform_family == 'rhel'
end
install_gem(name) click to toggle source
# File lib/garcon/chef/provider/development.rb, line 98
def install_gem(name)
  g ||= Chef::Resource::ChefGem.new(name, run_context)
  g.compile_time(false) if respond_to?(:compile_time)
  g.not_if { gem_installed?(name) }
  g.run_action :install
end
iptables() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 150
def iptables
  shell_out!('iptables -F')
end
libarchive() click to toggle source
# File lib/garcon/chef/provider/archive.rb, line 379
def libarchive
  node[:platform_family] == 'rhel' ? 'libarchive-devel' : 'libarchive-dev'
end
load_resource_attributes_from_file(resource) click to toggle source

Reads Access Control Settings on a file and writes them out to a resource, attempting to match the style used by the new resource, that is, if users are specified with usernames in new_resource, then the uids from stat will be looked up and usernames will be added to current_resource.

@return [undefined]

@api private

# File lib/garcon/chef/provider/download.rb, line 252
def load_resource_attributes_from_file(resource)
  acl_scanner = Chef::ScanAccessControl.new(@new_resource, resource)
  acl_scanner.set_all!
end
make_ready() click to toggle source
# File lib/garcon/chef/provider/download.rb, line 298
def make_ready
  return true if @ready.value == true
  @lock.with_write_lock { handle_prerequisites }
  installed?('aria2c') ? @ready.make_true : @ready.make_false
end
options_map() click to toggle source
# File lib/garcon/chef/provider/archive.rb, line 260
def options_map
  {
    owner: ::Archive::EXTRACT_OWNER,
    permissions: ::Archive::EXTRACT_PERM,
    time: ::Archive::EXTRACT_TIME,
    no_overwrite: ::Archive::EXTRACT_NO_OVERWRITE,
    acl: ::Archive::EXTRACT_ACL,
    fflags: ::Archive::EXTRACT_FFLAGS,
    extended_information: ::Archive::EXTRACT_XATTR,
    xattr: ::Archive::EXTRACT_XATTR,
  }
end
package(name, action = :nothing) click to toggle source
# File lib/garcon/chef/provider/download.rb, line 313
def package(name, action = :nothing)
  p = Chef::Resource::Package.new(name, run_context)
  p.retries     30
  p.retry_delay 10
  p.run_action  action
end
ps1prompt() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 158
def ps1prompt
  ver = run_context.cookbook_collection[cookbook_name].metadata.version
  t ||= Chef::Resource::Template.new('/etc/profile.d/ps1.sh', run_context)
  t.cookbook   'garcon'
  t.owner      'root'
  t.group      'root'
  t.mode        00644
  t.variables   ps1_root_msg: r.ps1_root_msg, ps1_usr_msg: r.ps1_usr_msg
  t.run_action :create
end
ready?() click to toggle source
# File lib/garcon/chef/provider/download.rb, line 294
def ready?
  @ready.value
end
rhel_services() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 146
def rhel_services
  r.rhel_svcs.each { |svc| service(svc) { action [:stop, :disable] }}
end
selinux() click to toggle source
# File lib/garcon/chef/provider/civilize.rb, line 154
def selinux
  shell_out!('setenforce 0')
end
unzip() click to toggle source

Unzip the archive.

@return

# File lib/garcon/chef/provider/archive.rb, line 242
def unzip
  converge_by "Unzip #{r.source} to #{r.path}" do
    Zip::File.open(cached_file) do |zip|
      zip.each do |entry|
        path = ::File.join(r.path, entry.name)
        FileUtils.mkdir_p(::File.dirname(path))
        if r.overwrite && ::File.exist?(path) && !::File.directory?(path)
          FileUtils.rm(path)
        end
        zip.extract(entry, path)
      end
    end
    do_acl_changes
    ::File.unlink(cached_file) if r.remove_after
    r.updated_by_last_action(true)
  end
end
wipe_repo() click to toggle source
# File lib/garcon/chef/provider/download.rb, line 328
def wipe_repo
  # Seems Chef::Resource::YumRepository action :delete is foobared, so
  # nuke the repo another way.
  Future.execute do
  ::File.unlink('/etc/yum.repos.d/garcon.repo')
    shell_out!('yum clean all && yum -q makecache')
    Chef::Provider::Package::Yum::YumCache.instance.reload
  end
end
yumrepo(action = :nothing) click to toggle source
# File lib/garcon/chef/provider/download.rb, line 320
def yumrepo(action = :nothing)
  y = Chef::Resource::YumRepository.new('garcon', run_context)
  y.mirrorlist node[:garcon][:repo][:url]
  y.gpgkey     node[:garcon][:repo][:gpg]
  y.gpgcheck   true
  y.run_action action
end