Module: Clusterfuck::TestHelpers::CommandHelpers

Defined in:
lib/clusterfuck/test_helpers/command_helpers.rb

Instance Method Summary collapse

Instance Method Details

#block_traffic(host, port, direction = "INPUT") ⇒ Object



51
52
53
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 51

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

#flush_iptables(host) ⇒ Object



47
48
49
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 47

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

#iptables_drop_port(host, port, direction, action: "-D", device: "eth1") ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 34

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

#machine(host_name) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 2

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

#run_command(host, cmd) ⇒ Object



12
13
14
15
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 12

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

#ssh_command(host) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 17

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

#sv(host, cmd, service) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 59

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

#unblock_traffic(host, port, direction = "INPUT") ⇒ Object



55
56
57
# File 'lib/clusterfuck/test_helpers/command_helpers.rb', line 55

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