Skip to content

Instantly share code, notes, and snippets.

@p01nt
Created March 12, 2012 09:39
Show Gist options
  • Save p01nt/2020936 to your computer and use it in GitHub Desktop.
Save p01nt/2020936 to your computer and use it in GitHub Desktop.
puppet installer
#!/usr/bin/env ruby
require 'singleton'
if RUBY_VERSION.tr('.', '').to_i < 190
class Platform
include Singleton
Platform::IMPL = case RUBY_PLATFORM.split("-")[1]
when /linux/ then :linux
when /freebsd/ then :freebsd
when /solaris/ then :solaris
when /darwin/ then :macosx
end
end
else
require 'platform'
end
def usage
die "Usage: $0 <puppet_master> <this_server_fqdn>"
end
def die(msg)
$stderr.puts msg
exit 1
end
class SupervisorService
def initialize(svc_name, svc_opts)
@svc_name = svc_name
@s = "[program:#{@svc_name}]\n"
@s << "command=puppet agent --server=#{svc_opts[:puppet_master]} --fqdn=#{svc_opts[:puppet_agent]} --no-daemonize"
end
def store
File.mkdir("/etc/supervisor.d") unless File.directory?("/etc/supervisor.d")
File.open("/etc/supervisor.d/puppet-agent.ini", File::CREAT|File::TRUNC|File::WRONLY) do |f|
f.puts "#{@s}"
end
end
def start
system("supervisorctl update")
system("supervisorctl restart #{@svc_name}")
end
end
class Box
include Singleton
@@Packages = {
:linux => [
{
:package_name => "rubygems-1.8",
:provides => "/usr/bin/gem-1.8"
},
{
:package_name => "puppet",
:provides => "/usr/local/bin/puppet",
:custom_pkgman => :gem
},
{
:package_name => "facter",
:provides => "/usr/local/bin/facter",
:custom_pkgman => :gem
},
{
:package_name => "supervisor",
:provides => "/usr/bin/supervisord",
:postinstall => "mkdir -p /etc/supervisor.d"
}
],
:freebsd => [
{
:package_name => "ruby-gems",
:provides => "/usr/local/bin/gem"
},
{
:package_name => "puppet",
:provides => "/usr/local/bin/puppet",
:custom_pkgman => :gem
},
{
:package_name => "facter",
:provides => "/usr/local/bin/facter",
:custom_pkgman => :gem
},
{
:package_name => "py-supervisor",
:provides => "/usr/bin/supervisord",
:postinstall => [
"echo 'supervisor_enable=YES' >> /etc/rc.conf",
"mkdir -p /etc/supervisor.d"
]
}
],
:solaris => [
{
:package_name => "puppet",
:provides => "/var/ruby/1.8/gem_home/bin/puppet",
:custom_pkgman => :gem,
:postinstall => [
"mkdir -p /var/lib/puppet",
"ln -s /var/ruby/1.8/gem_home/bin/puppet /usr/bin/puppet",
]
},
{
:package_name => "facter",
:provides => "/var/ruby/1.8/gem_home/bin/facter",
:custom_pkgman => :gem,
:postinstall => "ln -s /var/ruby/1.8/gem_home/bin/facter /usr/bin/facter"
},
{
:package_name => "setuptools-26",
:provides => "/usr/bin/easy_install"
},
{
:package_name => "supervisor",
:provides => "/usr/bin/supervisord",
:custom_pkgman => :easy_install,
:postinstall => "mkdir -p /etc/supervisor.d"
}
],
:macosx => [
{
:package_name => "puppet",
:custom_pkgman => :gem
},
{
:package_name => "facter",
:custom_pkgman => :gem
},
]
}
@@PackageManagers = {
:gem => "gem install",
:easy_install => "easy_install",
:portinstall => "portinstall -y",
:aptitude => "aptitude install -y",
:pkg => "pkg install",
:brew => "brew install",
}
def initialize
@os = Platform::IMPL
case @os
when :freebsd then @pkgman = :portinstall
when :linux then @pkgman = :aptitude
when :solaris then @pkgman = :pkg
when :macosx then @pkgman = :brew
end
end
def is_valid?
case @os
when :freebsd, :linux, :solaris, :macosx
return true
else
return false
end
end
def each_package
raise "Something went terribly wrong" unless block_given?
@@Packages[@os].each { |p| yield p }
end
def install_package(pkg_hash)
if pkg_hash[:provides] != nil and File.exists?(pkg_hash[:provides])
return true
end
puts "Installing #{pkg_hash[:package_name]}..."
if pkg_hash[:custom_pkgman] != nil
system("#{@@PackageManagers[pkg_hash[:custom_pkgman]]} #{pkg_hash[:package_name]}") or \
raise "Cannot install package #{pkg_hash[:package_name]}"
else
system("#{@@PackageManagers[@pkgman]} #{pkg_hash[:package_name]}") or \
raise "Cannot install package #{pkg_hash[:package_name]}"
end
if pkg_hash[:postinstall] != nil
pkg_hash[:postinstall].each do |cmd|
$stderr.puts "Postinstall command #{cmd} failed to run" unless system(cmd)
end
end
end
attr_reader :os
end
usage unless ARGV[1]
Box.instance.is_valid? or die "Unsupported platform: #{Box.instance.os}"
Box.instance.each_package { |pkg| Box.instance.install_package pkg }
system("supervisord") unless File.exists?("/tmp/supervisor.sock")
if Box.instance.os != :macosx
svc_options = { :puppet_master => ARGV[0], :puppet_agent => ARGV[1] }
puppet_agent_svc = SupervisorService.new("puppet-agent", svc_options)
puppet_agent_svc.store
puppet_agent_svc.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment