class Tinet::Command::Base

Public Class Methods

new(options = {}) click to toggle source
# File lib/tinet/command/base.rb, line 10
def initialize(options = {})
  @options = options
  check_docker_installed unless dry_run
  check_ovs_vsctl_installed unless dry_run
end

Protected Instance Methods

data() click to toggle source

@return [Tinet::Data, nil]

# File lib/tinet/command/base.rb, line 34
def data
  return nil if specfile.nil? || specfile.empty?
  @data ||= Tinet::Data.parse(specfile)
end
dry_run() click to toggle source

@return [boolean]

# File lib/tinet/command/base.rb, line 29
def dry_run
  @options.fetch('dry-run', false)
end
exec_post_conf() click to toggle source
# File lib/tinet/command/base.rb, line 93
def exec_post_conf
  data.options[:post_conf].each { |cmd| sudo command }
end
exec_post_down() click to toggle source
# File lib/tinet/command/base.rb, line 101
def exec_post_down
  data.options[:post_down].each { |cmd| sudo command }
end
exec_post_init() click to toggle source
# File lib/tinet/command/base.rb, line 85
def exec_post_init
  data.options[:post_init].each { |cmd| sudo command }
end
exec_pre_cmd() click to toggle source
# File lib/tinet/command/base.rb, line 77
def exec_pre_cmd
  data.options[:pre_cmd].each { |cmd| sudo command }
end
exec_pre_conf() click to toggle source
# File lib/tinet/command/base.rb, line 89
def exec_pre_conf
  data.options[:pre_conf].each { |cmd| sudo command }
end
exec_pre_down() click to toggle source
# File lib/tinet/command/base.rb, line 97
def exec_pre_down
  data.options[:pre_down].each { |cmd| sudo command }
end
exec_pre_init() click to toggle source
# File lib/tinet/command/base.rb, line 81
def exec_pre_init
  data.options[:pre_init].each { |cmd| sudo command }
end
logger() click to toggle source

@return [Logger]

# File lib/tinet/command/base.rb, line 19
def logger
  Tinet.logger
end
mount_docker_netns(container, netns) click to toggle source
# File lib/tinet/command/base.rb, line 66
def mount_docker_netns(container, netns)
  if dry_run
    logger.info "PID=`sudo docker inspect #{container} -f {{.State.Pid}}`"
    pid = '$PID'
  else
    pid, * = sudo "docker inspect #{container} -f {{.State.Pid}}"
  end
  sudo 'mkdir -p /var/run/netns'
  sudo "ln -s /proc/#{pid}/ns/net /var/run/netns/#{netns}"
end
namespaced(name) click to toggle source

@param name [String] @return [String]

# File lib/tinet/command/base.rb, line 57
def namespaced(name)
  "#{Tinet.namespace}-#{name}"
end
nodes() click to toggle source

@return [Array<Tinet::Node>, nil]

# File lib/tinet/command/base.rb, line 40
def nodes
  data.nodes unless data.nil?
end
sh(command, dry_run: dry_run(), print: true, continue: false) click to toggle source

@note Override {Tinet::Shell#sh}

Calls superclass method Tinet::Shell#sh
# File lib/tinet/command/base.rb, line 62
def sh(command, dry_run: dry_run(), print: true, continue: false)
  super
end
specfile() click to toggle source

@return [String]

# File lib/tinet/command/base.rb, line 24
def specfile
  @options.fetch(:specfile, '')
end
switches() click to toggle source

@return [Array<Tinet::Switch>, nil]

# File lib/tinet/command/base.rb, line 45
def switches
  data.switches unless data.nil?
end

Private Instance Methods

check_docker_installed() click to toggle source
# File lib/tinet/command/base.rb, line 113
def check_docker_installed
  unless command_exist?('docker')
    message = 'ERROR: Docker is not installed. TINET requires Docker.'
    logger.error(message)
    exit(1)
  end
end
check_ovs_vsctl_installed() click to toggle source
# File lib/tinet/command/base.rb, line 121
def check_ovs_vsctl_installed
  unless command_exist?('ovs-vsctl')
    message = 'ERROR: Open vSwitch is not installed. TINET requires Open vSwitch.'
    logger.error(message)
    exit(1)
  end
end
command_exist?(command) click to toggle source

@return [boolean]

# File lib/tinet/command/base.rb, line 108
def command_exist?(command)
  *, status = sh "which #{command}", print: false, continue: true
  status.success?
end