Class: Wire::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/wire/commands/base_command.rb

Overview

(Empty) Base command

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject

params object and project to operate upon



12
13
14
# File 'lib/wire/commands/base_command.rb', line 12

def params
  @params
end

#projectObject

params object and project to operate upon



12
13
14
# File 'lib/wire/commands/base_command.rb', line 12

def project
  @project
end

Instance Method Details

#check_userObject

Issues a warning if we do not run as root.



45
46
47
48
# File 'lib/wire/commands/base_command.rb', line 45

def check_user
  (ENV['USER'] != 'root') &&
      $log.warn("Not running as root. Make sure user #{ENV['USER']} has sudo configured.")
end

#default_handle_resource(resource, resource_type, resource_desc_str, action) ⇒ Object

brings given resource up by first asking if its up?. If not, call up. Writes states, outputs state Params: resource: Resource object, i.e. BridgeResource resource_type: Resource type symbol, i.e. :bridge resource_desc_str: Description to dump out action: one of :up, :down



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wire/commands/base_command.rb', line 123

def default_handle_resource(resource, resource_type, resource_desc_str, action)
  question = "#{action}?".to_sym
  output_method = action.to_s.upcase

  b_ok = false

  if resource.send(question)
    outputs output_method, "#{resource_desc_str} is already #{action}.", :ok2
    b_ok = true
  else
    resource.send(action)

    if resource.send(question)
      outputs output_method, "#{resource_desc_str} is #{action}.", :ok
      b_ok = true
    else
      outputs output_method, "#{resource_desc_str} could not be brought #{action}.", :err
    end
  end

  state.update(resource_type, resource.name, action) if b_ok
  b_ok
end

#dump_stateObject

debugs a single line state



20
21
22
# File 'lib/wire/commands/base_command.rb', line 20

def dump_state
  $log.debug "State: [#{state.to_pretty_s}]"
end

#ensure_hostip_netmask(host_ip, network_data) ⇒ Object

if the hostip is not in cidr, take netmask from network entry, add to hostip params: host_ip i.e. 192.168.10.1 network_data network data object, to take netmask from :network element



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/wire/commands/base_command.rb', line 102

def ensure_hostip_netmask(host_ip, network_data)
  return host_ip  if host_ip =~ /[0-9\.]+\/[0-9]+/

  match_data = network_data[:network].match(/[0-9\.]+(\/[0-9]+)/)
  if match_data && match_data.size >= 2
    netmask = match_data[1]
    $log.debug "Adding netmask #{netmask} to host-ip #{host_ip}"
    return "#{host_ip}#{netmask}"
  else
    $log.error "host-ip #{host_ip} is missing netmask, and none given in network."
    return host_ip
  end
end

#objects_in_zone(type_name, zone_name) ⇒ Object

retrieve all objects of given type_name in zone (by zone_name) returns:

Hash

of model subpart with elements of given type



54
55
56
57
58
# File 'lib/wire/commands/base_command.rb', line 54

def objects_in_zone(type_name, zone_name)
  return {} unless @project.element?(type_name)
  objects = @project.get_element type_name || {}
  objects.select { |_, data| data[:zone] == zone_name }
end

#outputs(type, msg, style = :plain) ⇒ Object

outputs writes a message to stdout, in given style

params: type 1st column as type, i.e. “model” or “network” or “OK” msg message to print style coloring etc, supported:

:plain (default), :err (red), :ok (green)

return

  • nil

:reek:ControlParameter



35
36
37
38
39
40
41
42
# File 'lib/wire/commands/base_command.rb', line 35

def outputs(type, msg, style = :plain)
  line = "#{type}> #{msg}"
  line = line.color(:red) if (style == :err) || (style == :error)
  line = line.color(:green) if style == :ok
  line = line.color(:cyan) if style == :ok2

  $stdout.puts line
end

#run(params = {}) ⇒ Object

runs the command, according to parameters loads project into @project, calls run_on_project (to be defined in subclasses) params params command parameter map, example key i.e. “target_dir”



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wire/commands/base_command.rb', line 65

def run(params = {})
  check_user

  @params = params
  target_dir = @params[:target_dir]
  outputs 'model', "Loading model in #{target_dir}"

  # load it first
  begin
    loader = ProjectYamlLoader.new
    @project = loader.load_project(target_dir)

    # try to load state file.
    state.project = @project
    handle_state_load

    run_on_project

    $log.debug? && puts(@project.to_yaml)

    handle_state_save

  rescue => load_execption
    $stderr.puts "Unable to process project model in #{target_dir}"
    $log.debug? && puts(load_execption.inspect)
    $log.debug? && puts(load_execption.backtrace)

    return false
  end
  true
end

#stateObject

returns the state object



15
16
17
# File 'lib/wire/commands/base_command.rb', line 15

def state
  State.instance
end