Class: Wire::InitCommand

Inherits:
BaseCommand show all
Defined in:
lib/wire/commands/init_command.rb

Overview

Init Command opens an interactive console dialog for initializing a model structure params:

  • :target_dir

Instance Attribute Summary

Attributes inherited from BaseCommand

#params, #project

Instance Method Summary collapse

Methods inherited from BaseCommand

#check_user, #default_handle_resource, #dump_state, #ensure_hostip_netmask, #objects_in_zone, #outputs, #state

Instance Method Details

#export_element_file(element_sym, element_data, target_dir) ⇒ Object

exports an element part of the model to a single file in target_dir, as yaml.



82
83
84
85
86
87
# File 'lib/wire/commands/init_command.rb', line 82

def export_element_file(element_sym, element_data, target_dir)
  filename = File.join(target_dir, "#{element_sym}.yaml")
  open(filename, 'w') do |out_file|
    out_file.puts(element_data.to_yaml)
  end
end

#export_model_file(model_data, target_dir) ⇒ Object

Given a model structure and a target dir, this method ensures that targetdir exists and structure contents are written to individual files



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
# File 'lib/wire/commands/init_command.rb', line 53

def export_model_file(model_data, target_dir)
  puts "Target dir is #{target_dir}"

  # ensure target_dir exists
  if File.exist?(target_dir)
    if File.directory?(target_dir)
      $stdout.puts "Writing output to #{target_dir}"
    else
      $stderr.puts "ERROR: Target dir #{target_dir} exists, " \
                   'but is not a directory.'
      exit Wire.cli_exitcode(:init_dir_error)
    end
  else
    begin
      FileUtils.mkdir_p(target_dir)
    rescue => excpt
      $stderr.puts "ERROR: Unable to create #{target_dir}: #{excpt}"
      exit Wire.cli_exitcode(:init_dir_error)
    end
  end

  [:zones, :networks].each do |element_sym|
    element = model_data[element_sym]
    export_element_file element_sym, element, target_dir
  end
end

#run(params = {}) ⇒ Object



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
# File 'lib/wire/commands/init_command.rb', line 14

def run(params = {})
  puts "Initializing model in #{params[:target_dir]}"

  model_data = {}
  zone_data = {}
  network_data = {}
  model_data.store :zones, zone_data
  model_data.store :networks, network_data

  zone_names = InitInteractive.ask_for_zone_names
  if zone_names.size == 0
    $stderr.puts 'ERROR: must at least have one zone'
    exit Wire.cli_exitcode(:init_bad_input)
  end
  zone_names.each do |zone_name|

    zone_detail = {
      :desc => 'Enter a short description here',
      :long_desc => 'Enter a longer description here'
    }

    zone_data.store zone_name, zone_detail

    networks = InitInteractive.ask_for_network_in_zone zone_name
    networks.each do |network_name|

      network_details = InitInteractive.ask_detail_data_for_network network_name
      network_details.merge!({ :zone => zone_name })
      network_data.store network_name, network_details
    end
  end

  # write resulting model to file
  export_model_file(model_data, params[:target_dir])
end