PageRenderTime 98ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/restart_service.rb

https://github.com/sewer2/cb-iptables-ng
Ruby | 51 lines | 21 code | 5 blank | 25 comment | 5 complexity | 39e82d1b7ce7a24c85e1d56578b3de91 MD5 | raw file
  1. #
  2. # Cookbook Name:: iptables-ng
  3. # Recipe:: restart_service
  4. #
  5. # Copyright 2013, Chris Aumann
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # This was implemented as a internal-only provider.
  21. # Apparently, calling a LWRP from a LWRP doesnt' really work with
  22. # subscribes / notifies. Therefore, using this workaround.
  23. module Iptables
  24. module Manage
  25. def restart_service(ip_version)
  26. # Restart iptables service if available
  27. if node['iptables-ng']["service_ipv#{ip_version}"]
  28. # Do not restart twice if the command is the same for ipv4 and ipv6
  29. return if node['iptables-ng']['service_ipv4'] == node['iptables-ng']['service_ipv6'] and ip_version == 6
  30. Chef::Resource::Service.new(node['iptables-ng']["service_ipv#{ip_version}"], run_context).tap do |service|
  31. service.supports(status: true, restart: true)
  32. service.run_action(:enable)
  33. service.run_action(:restart)
  34. end
  35. # If no service is available, apply the rules manually
  36. else
  37. Chef::Log.info 'applying rules manually, as no service is specified'
  38. Chef::Resource::Execute.new("iptables-restore for ipv#{ip_version}", run_context).tap do |execute|
  39. execute.command("iptables-restore < #{node['iptables-ng']['script_ipv4']}") if ip_version == 4
  40. execute.command("ip6tables-restore < #{node['iptables-ng']['script_ipv6']}") if ip_version == 6
  41. execute.run_action(:run)
  42. end
  43. end
  44. end
  45. end
  46. end