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