Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created December 21, 2010 11:10
Show Gist options
  • Save shouichi/749800 to your computer and use it in GitHub Desktop.
Save shouichi/749800 to your computer and use it in GitHub Desktop.
module Daemons
class Base
include SSH
attr_accessor :server
delegate :logger, :address, :to => :server
class << self
def inherited(klass)
klass.instance_eval do
cattr_accessor :path
end
end
def has_path(path)
self.path = path
end
end
def initialize(server)
@server = server
end
def reload
exec(:reload)
end
def restart
exec(:restart)
end
def exec(*args)
ssh("c4sa", address, "sudo #{self.class.path} #{args}")
end
end
class Nginx < Base
has_path "/etc/init.d/nginx"
end
class Syslog < Base
has_path "/etc/init.d/syslog-ng"
end
end
class Server < ActiveRecord::Base
def nginx
@nginx ||= Daemons::Nginx.new(self)
end
end
module SSH
def ssh(user, host, command)
ssh = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no #{user}@#{host} #{command}"
logger.info(ssh)
`#{ssh}` if Rails.env.production?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment