Ask Your Question
0

Help on ERB teample

asked 2015-08-12 08:47:19 -0500

updated 2015-08-12 11:37:06 -0500

GregLarkin gravatar image

Hi I am new to puppet/ruby . Appreciate if someone help me on this.

<%  if TCPSocket.new("host1", 636) and TCPSocket.new("host2", 1636)  %>
host xxxx
port 636

<% else -%>
host yyyy
port 389

<% end %>
<% end %>

Above is sample code of my ERB template..I wana check if port is open for specific hosts before doing anything. The problem is when the port is not opened it raises Connection refused error and hence it failing puppet apply. Can't I catch exception and exit if exception is raised? Also how to define timeout?

 rescue Errno::ECONNREFUSED => e
exit

I tried above code but it didnt work.

Can someone suggest pls?

Thank you

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-08-12 16:30:34 -0500

fura gravatar image

Hi

I think you want something like this:

<% begin
  Timeout.timeout(10) do
    begin
      if TCPSocket.new("host1", 636) and TCPSocket.new("host2", 1636) %>
host xxx
port 636
      <% else %>
host yyy
port 389
      <% end
    rescue Errno::ECONNREFUSED
      raise Puppet::Error, "host unreach" %>
  <% end
end
  rescue Timeout::Error
    raise Puppet::Error, "Timeout"
end %>

but this code never go to else

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-08-12 08:47:19 -0500

Seen: 133 times

Last updated: Aug 12 '15