Sha256: 4dfd3d58e5576b1f96b11739b5e9b8293cfc584ee7fee30db9e6af6117506250

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Clusterfuck::TestHelpers::CommandHelpers
  def machine(host_name)
    host_name = host_name.to_sym

    Clusterfuck::Cluster.all.each do |cluster|
      if machine = cluster[host_name]
        return machine
      end
    end
  end

  def run_command(host, cmd)
    cmd = "#{ssh_command(host)} -- #{cmd}"
    `#{cmd}`
  end

  def ssh_command(host)
    port = machine(host).ssh_port

    cmd = <<-CMD
      ssh vagrant@localhost
        -p #{port}
        -i ~/.vagrant.d/insecure_private_key
        -o UserKnownHostsFile=/dev/null
        -o StrictHostKeyChecking=no
        -o PasswordAuthentication=no
        -o IdentitiesOnly=yes
        -o LogLevel=quiet
    CMD

    cmd.gsub("\n", ' ').squeeze(" ").strip
  end

  def iptables_drop_port(host, port, direction, action: "-D", device: "eth1")
    @hosts_with_iptables ||= []
    @hosts_with_iptables << host

    device_flag = direction == 'INPUT' ? '-i' : '-o'

    ['tcp', 'udp'].each do |proto|
      run_command(host,
        "sudo iptables #{action} #{direction} -p #{proto} --dport #{port} #{device_flag} #{device} -j DROP > /dev/null"
      )
    end
  end

  def flush_iptables(host)
    run_command(host, "sudo iptables -F")
  end

  def block_traffic(host, port, direction = "INPUT")
    iptables_drop_port(host, port, direction, action: "-A")
  end

  def unblock_traffic(host, port, direction = "INPUT")
    iptables_drop_port(host, port, direction)
  end

  def sv(host, cmd, service)
    if cmd == 'stop'
      @hosts_with_stopped_services ||= {}
      @hosts_with_stopped_services[host] ||= []
      @hosts_with_stopped_services[host] << service
    end

    run_command(host, "sudo sv #{cmd} #{service}")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-clusterfuck-0.0.7 lib/clusterfuck/test_helpers/command_helpers.rb