Class: Blender::Cli::Mix

Inherits:
Blender::Cli show all
Defined in:
lib/blender/cli/mix.rb

Instance Method Summary collapse

Methods inherited from Blender::Cli

#initialize, #run

Constructor Details

This class inherits a constructor from Blender::Cli

Instance Method Details

#executeObject



80
81
82
83
84
85
86
87
# File 'lib/blender/cli/mix.rb', line 80

def execute
  options = parse_options(@args)
  recipe = find_recipe(options)
  run_recipe(recipe, options)

rescue => e
  abort(e.to_s)
end

#find_recipe(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/blender/cli/mix.rb', line 56

def find_recipe(options)
  # check for recipe, recipe.rb, directory_name.rb, and blender-recipe.rb
  if rname = options[:recipe]
    recipes = [rname, "#{rname}.rb"]
  else
    recipes = ["#{File.basename(File.expand_path(options[:dir]))}.rb", "blender-recipe.rb"]
  end

  recipe = recipes.detect {|r| File.file?(File.join(options[:dir], r))} ||
    raise("recipe not found (looking for #{recipes * ' '})\n#{options[:usage]}")
end

#parse_options(args) ⇒ Object



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
# File 'lib/blender/cli/mix.rb', line 5

def parse_options(args)
  options = {}
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: blender mix [OPTIONS] [DIR] HOST"
    opts.separator "Options:"

    opts.on("-r", "--recipe RECIPE", "if RECIPE is not specified blender will first look for <directory_name>.rb and then for blender-recipe.rb") do |val|
      options[:recipe] = val
    end

    opts.on("-N", "--node NODE", "force NODE as the current nodename") do |val|
      options[:node] = val
    end

    opts.on("-R", "--roles ROLES", "comma delimited list of roles that should execute") do |val|
      options[:roles] = val
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on("-h", "--help", "Show this message") do
      raise(opts.to_s)
    end

    opts.separator ""
    opts.separator "Notes:"
    opts.separator '    "." used if DIR not specified'

  end
  opts.parse!(args)

  options[:usage] = opts.to_s

  dir = args.shift
  host = args.shift
  raise("unexpected: #{args*" "}\n#{opts}") unless args.empty?

  if host.nil?
    host = dir
    dir = "."
  end

  raise(opts.to_s) unless dir && host

  raise("#{dir} is not a directory\n#{opts}") unless File.directory?(dir)

  options.merge(:dir => dir, :host => host)
end

#run_recipe(recipe, options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blender/cli/mix.rb', line 68

def run_recipe(recipe, options)
  run "cat #{File.expand_path("files/init.sh", Blender::ROOT)} | ssh #{options[:host]} /bin/bash -l" or raise("failed init.sh")

  run("rsync -qazP --delete --exclude '.*' #{options[:dir]}/ #{options[:host]}:/var/lib/blender/recipes") or raise("failed rsync")

  env_config = "RECIPE=#{recipe}"
  env_config << " NODE=#{options[:node]}" if options[:node]
  env_config << " ROLES=#{options[:roles]}" if options[:roles]

  run "cat #{File.expand_path("files/mix.sh", Blender::ROOT)} | ssh #{options[:host]} #{env_config} /bin/bash -l" or raise("failed mix.sh")
end