Class: Topo::Provision::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/topo/provision/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ CLI

Returns a new instance of CLI.



48
49
50
51
# File 'lib/topo/provision/cli.rb', line 48

def initialize(argv=[])
  super()
  parse_and_validate_args
end

Instance Attribute Details

#topologyObject

Returns the value of attribute topology.



46
47
48
# File 'lib/topo/provision/cli.rb', line 46

def topology
  @topology
end

Instance Method Details

#parse_and_validate_argsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/topo/provision/cli.rb', line 53

def parse_and_validate_args
  begin
    parse_options
    @input_file = cli_arguments()[0]
  rescue OptionParser::InvalidOption => e
    STDERR.puts e.message
    puts opt_parser
    exit(-1)
  end
  
  if !@input_file
    STDERR.puts opt_parser
    exit(-1)
  end
end

#redirect_stdout(file) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/topo/provision/cli.rb', line 69

def redirect_stdout(file)
  begin
    $stdout.reopen(file, "w")    
  rescue => e
    STDERR.puts "ERROR: Cannot open provisioning output file #{file} - #{e.message}"
  end 
end

#runObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/topo/provision/cli.rb', line 77

def run        
  @topology = Topo::Loader.from_file(@input_file,  @config[:format] || "default")

  # output topo file
  @topology.to_file(@config[:output_topo]) if(@config[:output_topo])         
    
  # redirect generated recipe to file
  redirect_stdout(@config[:output]) if(@config[:output])
    
  # run generator
  @generator = Topo::Provision::Generator.new(@topology)
  action = @config[:action].to_sym
  @generator.generate_provisioning_recipe(action)
  
end