class Wire::BaseCommand

(Empty) Base command

Attributes

params[RW]

params object and project to operate upon

project[RW]

params object and project to operate upon

Public Instance Methods

check_user() click to toggle source

Issues a warning if we do not run as root.

# 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) click to toggle source

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

# 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_state() click to toggle source

debugs a single line state

# 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) click to toggle source

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

# 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) click to toggle source

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

Hash

of model subpart with elements of given type

# 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) click to toggle source

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

# 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 = {}) click to toggle source

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”

# 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
state() click to toggle source

returns the state object

# File lib/wire/commands/base_command.rb, line 15
def state
  State.instance
end

Private Instance Methods

handle_state_load() click to toggle source

Load state from state file

# File lib/wire/commands/base_command.rb, line 159
def handle_state_load
  state.load
  # dump state
  $log.debug? && dump_state
rescue => load_exception
  $stderr.puts "Error loading state, #{load_exception}"
end
handle_state_save() click to toggle source

Save state to state file

# File lib/wire/commands/base_command.rb, line 150
def handle_state_save
  # dump state
  $log.debug? && dump_state
  state.save
rescue => save_exception
  $stderr.puts "Error saving state, #{save_exception}"
end