Skip to content

Instantly share code, notes, and snippets.

@razor-x
Last active January 2, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razor-x/8275626 to your computer and use it in GitHub Desktop.
Save razor-x/8275626 to your computer and use it in GitHub Desktop.
Configuration file manager.
:defaults:
:perm: 644
:hosts:
- MyClient1
- MyClient2
:files:
- :path: etc/ssh/sshd_config
- :path: etc/ntp.conf
- :path: etc/bacula/bacula-fd.conf
:perm: 640
:packages: [ bacula-client ]
:hosts: [ MyClient1, MyClient2 ]
require 'yaml'
require 'open3'
require 'socket'
task :default => [:install]
def command? name
`which #{name}`
$?.success?
end
def hostname
Socket.gethostname
end
def allowed_host? hosts
return true if hosts.nil?
hosts.include? hostname
end
def pkg_exists? pkg
if command? 'pacman'
Open3.popen3 'pacman', '-qQ', pkg do |_,_,_,wait_thr|
wait_thr.value.to_i # the exit status
end
end
end
def pkgs_exists? pkgs
return true if pkgs.nil?
pkgs.map( &method(:pkg_exists?) ).delete_if{ |e| e == 0 }.empty?
end
task :install do
config = YAML::load File.open('install.yml')
config[:files].each do |f|
next unless allowed_host? f[:hosts]
next unless pkgs_exists? f[:packages]
perm = f[:perm]
perm ||= config[:defaults][:perm]
src = File.expand_path (
if File.exists? f[:path]
f[:path]
else
base = File.basename( f[:path].chomp( File.extname(f[:path]) ) )
dir = File.dirname(f[:path])
"#{dir}/" + Dir.entries(dir).grep(/^#{base}.#{hostname.downcase}/).first
end
)
dest = File.expand_path '/' + f[:path]
system "sudo cp #{src} #{dest}"
system "sudo chmod #{perm.to_s} #{dest}"
p "#{src} -> #{dest}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment