filesystem.rb 2.83 KB
#
# Author:: Earth U (<sysadmin @ chromedia.com>)
# Cookbook Name:: cfe-server
# Recipes:: filesystem
#
# Copyright 2017, Chromedia Far East, Inc.
#
# 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.
#

swapfile = node[cookbook_name]['filesystem']['swapfile']
bash 'enable_swap' do
  code <<-EOF.gsub(/^\s+/, '')
    set -e
    if [[ ! -f #{swapfile} ]] ; then
      fallocate -l #{node[cookbook_name]['filesystem']['swapsize']} #{swapfile}
      chmod 600 #{swapfile}
      mkswap #{swapfile}
      swapon #{swapfile}
      echo "#{swapfile} none swap sw 0 0" >> /etc/fstab
    fi
  EOF
  only_if { swapfile }
end

if node['backup-file2s3']['backups']
  node['backup-file2s3']['backups'].each do |xback|
    xbucket = xback[:bucket]
    xregion = xback[:region]

    xback[:paths].each do |xpath|

      # Each path spec can have the following additional attributes:
      # {
      #   :extract    => true, # Default: true
      #   :ex_creates => 'filename' # Relative to path.
      # }
      unless xpath.has_key?(:extract) && xpath[:extract] == false
        ptarget_dir = ::File.dirname(xpath[:path])
        pcreates    = "#{xpath[:path]}/#{xpath[:ex_creates]}"
        pencrypted  = xpath[:bak_encrypted]
        pfname      = xpath[:bak_filename] || ::File.basename(xpath[:path])

        aws_tar_extract pfname do
          target_dir ptarget_dir
          creates    pcreates
          encrypted  pencrypted
          bucket     xbucket
          region     xregion
        end
      end

    end
  end
end

include_recipe 'backup-file2s3'

node[cookbook_name]['filesystem']['perms'].each do |perm|
  if ::Dir.exist?(perm[:path])

    if perm[:owner] && perm[:group]
      execute "chown -R #{perm[:owner]}:#{perm[:group]} #{perm[:path]}"
    elsif perm[:owner]
      execute "chown -R #{perm[:owner]} #{perm[:path]}"
    elsif perm[:group]
      execute "chgrp -R #{perm[:group]} #{perm[:path]}"
    end

    if perm[:mode]
      execute "chmod -R #{perm[:mode]} #{perm[:path]}"
    end

  else

    directory perm[:path] do
      if perm[:owner] then owner perm[:owner] end
      if perm[:group] then group perm[:group] end
      if perm[:mode] then mode perm[:mode] end

      only_if { perm[:create] }
      recursive true
    end

  end
end

node[cookbook_name]['filesystem']['symlinks'].each do |xfrom, xto|
  directory(::File.dirname(xfrom)) { recursive true }
  link(xfrom) { to xto }
end