public
Description: Blanket is a flexible backup framework designed to get the drudgery out of the way and to make automated backups easy.
Homepage: http://jimvanfleet.com/projects/blanket
Clone URL: git://github.com/bigfleet/blanket.git
Search Repo:
blanket / bin / blanket-cfg
100755 87 lines (67 sloc) 2.353 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
 
require 'blanket/init'
 
# default options
OPTIONS = {
}
 
 
ARGV.options do |o|
  script_name = File.basename($0)
  
  o.set_summary_indent(' ')
  o.banner = "Usage: #{script_name} [OPTIONS]"
  o.define_head "Configure blanket task with source and sink information"
  o.separator ""
  
  o.on("-t", "--task=val", String,
       "name of the task") { |OPTIONS[:task]| }
  o.on("-o", "--source=val", String,
       "blanket type of the source") { |OPTIONS[:source]| }
  o.on("-i", "--sink=val", String,
       "blanket type of the sink") { |OPTIONS[:sink]| }
  
  o.separator ""
 
  o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
  
  o.parse!
end
 
BAD_ARGS = "You failed to supply all the required arguments. Please use -h and try again.\n"
$stderr << BAD_ARGS unless OPTIONS[:task] && OPTIONS[:source] && OPTIONS[:sink]
 
task = OPTIONS[:task]
source_class = OPTIONS[:source]
sink_class = OPTIONS[:sink]
@task_dir = FileUtils.pwd + "/" +task
puts "Creating #{@task_dir}"
FileUtils.mkdir_p @task_dir
 
def write_config_for(sym)
  file = @task_dir + "/#{sym.to_s}.yml"
  atts = Object.const_get(OPTIONS[sym]).default_attributes
  writer = Blanket::Writer.new(atts)
  puts "Writing #{sym} configuration #{file}"
  writer.write(file)
end
 
write_config_for(:source)
write_config_for(:sink)
 
requirements = ["'capistrano/recipes/blanket'"]
[:source, :sink].each do |phase|
  phase_class = Object.const_get(OPTIONS[phase])
  (requirements << phase_class.additional_requirements) if phase_class.respond_to? :additional_requirements
end
 
capfile_requires = requirements.inject("") {|statement, r| statement += "require #{r}\r\n" }
 
 
def unindent(string)
  indentation = string[/\A\s*/]
  string.strip.gsub(/^#{indentation}/, "")
end
 
files = {
  "Capfile" => unindent(capfile_requires)
}
 
files.each do |file, content|
  file = File.join(@task_dir, file)
  if File.exists?(file)
    warn "[skip] `#{file}' already exists"
  elsif File.exists?(file.downcase)
    warn "[skip] `#{file.downcase}' exists, which could conflict with `#{file}'"
  elsif !File.exists?(File.dirname(file))
    warn "[skip] directory `#{File.dirname(file)}' does not exist"
  else
    puts "[add] writing `#{file}'"
    File.open(file, "w") { |f| f.write(content) }
  end
end
 
puts "[done] blanketed!"