lib/kuzushi.rb in kuzushi-0.0.21 vs lib/kuzushi.rb in kuzushi-0.0.22
- old
+ new
@@ -9,49 +9,56 @@
## firewall until ready
## ruby 1.9 compatibility
class Kuzushi
+ attr_accessor :config, :config_names
+
def initialize(url)
@base_url = File.dirname(url)
@name = File.basename(url)
@config_names = []
@configs = []
@packages = []
@tasks = []
- load_config_stack(@name)
- @config = @configs.reverse.inject({}) { |i,c| i.merge(c) }
end
def init
@init = true
start
end
def start
+ load_config_stack(@name)
process_stack
- puts "----"
+ log "----"
@tasks.each do |t|
- puts "TASK: #{t[:description]}"
+ log "TASK: #{t[:description]}"
t[:blk].call
end
- puts "----"
+ log "----"
end
protected
def system
ohai = Ohai::System.new
ohai.all_plugins
ohai
end
+ def http_get(url)
+ RestClient.get("#{@base_url}/#{name}")
+ end
+
def load_config_stack(name)
@config_names << name
- @configs << JSON.parse(RestClient.get("#{@base_url}/#{name}"))
+ @configs << JSON.parse(http_get("#{@base_url}/#{name}"))
if import = @configs.last["import"]
load_config_stack(import)
+ else
+ @config = @configs.reverse.inject({}) { |i,c| i.merge(c) }
end
end
def process_stack
script get("before")
@@ -173,58 +180,29 @@
def package_arch
`dpkg --print-architecture`.chomp
end
- def process_files(f)
- if f.template
- write_file("/templates/#{f.template}", f.file) do |file|
- @system = system
- t = ERB.new File.read(file), 0, '<>'
- t.result(binding)
- end
- else
- src = f.source || File.basename(f.file)
- write_file("/files/#{src}", f.file) do |file|
- File.read(file)
- end
- end
+ def erb(data)
+ @system = system
+ ERB.new(data, 0, '<>').result(binding)
end
- def write_file(src, dest, &blk)
- fetch(src) do |file|
- task "write #{dest}" do
- FileUtils.mkdir_p(File.dirname(dest))
- File.open(dest,"w") { |f| f.write(blk.call(file)) }
+ def process_files(f)
+ file(f) do |tmp|
+ task "write #{f.file}" do
+ cp_file(tmp, f.file)
end
end
end
- def handle_crontab(src, user = "root", &blk)
- ## FIXME - this is getting a little silly calling tmpfile twice - should be able to pass erb to fetch as an opton...
- ## unify code for process crontabs + process files
- fetch(src) do |file|
- tmpfile(blk.call(file)) do |tmp|
- task "process crontab for #{user}" do
- shell "crontab -u #{user} #{tmp}"
- end
- end
- end
- end
-
def process_crontab(cron)
- if cron.template
- handle_crontab("/templates/#{cron.template}", cron.user) do |file|
- @system = system
- t = ERB.new File.read(file), 0, '<>'
- t.result(binding)
+ user = cron.user || "root"
+ file(cron) do |tmp|
+ task "process crontab for #{user}" do
+ shell "crontab -u #{user} #{tmp}"
end
- else
- src = cron.source || File.basename(cron.file)
- handle_crontab("/files/#{src}", cron.user) do |file|
- File.read(file)
- end
end
end
def process_users(user)
(user.authorized_keys || []).each do |key|
@@ -283,26 +261,27 @@
end
end
end
def tmpfile(content, file = "tmp_#{rand(1_000_000_000)}", &block)
- tmp_dir = "/tmp/kuzushi"
- Dir.mkdir(tmp_dir) unless File.exists?(tmp_dir)
- file = "#{tmp_dir}/#{File.basename(file)}"
- File.open(file,"w") do |f|
- f.write(content)
- f.chmod(0700)
- end if content
- block.call(file) if block
- file
+ path = "/tmp/kuzushi/#{File.basename(file)}"
+ put_file(content, path)
+ block.call(path) if block
+ path
end
- def fetch(file, &block)
- names = @config_names.clone
+ def file(f, &blk)
+ ## no magic here - move along
+ fetch("/templates/#{f.template}", lambda { |data| erb data }, &blk) if f.template
+ fetch("/files/#{f.source || File.basename(f.file)}", &blk) unless f.template
+ end
+
+ def fetch(file, filter = lambda { |d| d }, &block)
+ names = config_names.clone
begin
## its important that we try each name for the script - allows for polymorphic scripts
- tmpfile RestClient.get("#{@base_url}/#{names.first}#{file}"), file do |tmp|
+ tmpfile(filter.call(http_get("#{@base_url}/#{names.first}#{file}")), file) do |tmp|
block.call(tmp)
end
rescue RestClient::ResourceNotFound
names.shift
retry unless names.empty?
@@ -312,31 +291,32 @@
end
end
def error(message, exception = nil)
puts "ERROR :#{message}"
+ puts exception.message
end
def get(key)
- @config[key.to_s]
+ config[key.to_s]
end
def get_array(key)
[ get(key) || [] ].flatten
end
def wait_for_volume(vol)
## Maybe use ohai here instead -- FIXME
until dev_exists? vol do
- puts "waiting for volume #{vol}"
+ log "waiting for volume #{vol}"
sleep 2
end
end
def shell(cmd)
- puts "# #{cmd}"
- puts Kernel.system cmd ## FIXME - need to handle/report exceptions here
+ log "# #{cmd}"
+ Kernel.system cmd ## FIXME - need to handle/report exceptions here
end
def init?
@init ||= false
end
@@ -346,7 +326,24 @@
@tasks << { :description => description, :blk => blk }
end
def dev_exists?(dev)
File.exists?("/sys/block/#{File.basename(dev)}")
+ end
+
+ def cp_file(src, dest)
+ FileUtils.mkdir_p(File.dirname(dest))
+ FileUtils.cp(src, dest)
+ end
+
+ def put_file(data, dest)
+ FileUtils.mkdir_p(File.dirname(dest))
+ File.open(dest,"w") do |f|
+ f.write(data)
+ f.chmod(0700)
+ end
+ end
+
+ def log(message)
+ puts message
end
end