<commit_before># Examples:
#
#   ufw_rule "Allow SSH connections" do
#     action :allow
#     port 22
#   end
#
#   ufw_rule "Allow Postgres connections from app server" do
#     action :allow
#     port 5432
#     protocol "tcp"
#     from "10.0.0.100"
#   end
#
#   ufw_rule "Set default firewall action" do
#     action :deny
#     default true
#   end

define :ufw_rule, :default => false, :delete => false do
  include_recipe "server::security"
  
  case params[:action]
  when :allow, :deny
  
    if params[:default]
      execute "ufw default #{params[:action]}" do
        user "root"
      end
    else

      # Build the command
      rule_cmd = "ufw "
      rule_cmd += "delete " if params[:delete]
      rule_cmd += "#{params[:action]} "
      if params[:from]
        rule_cmd += "from #{params[:from]} "
        rule_cmd += "to #{params[:to]} port " if params[:protocol]
      end
      rule_cmd += "#{params[:port]}" if params[:port]
      if params[:protocol] and not params[:from]
        rule_cmd += "/#{params[:protocol]}"
      end
    
      # Execute it
      execute rule_cmd  do
        user "root"
      end
      
      # Make sure ufw is enabled
      execute "yes | ufw enable" do
        user "root"
      end
    
    end
  
  when :disable
    
    execute "ufw disable" do
      user "root"
    end
  
  end
end<commit_msg>Set ufw to reload after adding a rule, and enable only if inactive<commit_after># Examples:
#
#   ufw_rule "Allow SSH connections" do
#     action :allow
#     port 22
#   end
#
#   ufw_rule "Allow Postgres connections from app server" do
#     action :allow
#     port 5432
#     protocol "tcp"
#     from "10.0.0.100"
#   end
#
#   ufw_rule "Set default firewall action" do
#     action :deny
#     default true
#   end

define :ufw_rule, :default => false, :delete => false do
  include_recipe "server::security"
  
  case params[:action]
  when :allow, :deny
  
    if params[:default]
      execute "ufw default #{params[:action]}" do
        user "root"
      end
    else

      # Build the command
      rule_cmd = "ufw "
      rule_cmd += "delete " if params[:delete]
      rule_cmd += "#{params[:action]} "
      if params[:from]
        rule_cmd += "from #{params[:from]} "
        rule_cmd += "to #{params[:to]} port " if params[:protocol]
      end
      rule_cmd += "#{params[:port]}" if params[:port]
      if params[:protocol] and not params[:from]
        rule_cmd += "/#{params[:protocol]}"
      end
      
      # Make sure ufw is enabled
      execute "yes | ufw enable" do
        user "root"
        only_if 'test "`ufw status`" = "Status: inactive"'
      end
      
      # Execute it
      execute rule_cmd  do
        user "root"
        notifies :reload, resources(:service => "ufw"), immediately
      end
    
    end
  
  when :disable
    
    execute "ufw disable" do
      user "root"
    end
  
  end
end
{'fraction_non_alphanumeric': 0.11843922651933701, 'fraction_numerical': 0.008977900552486187, 'mean_word_length': 2.148913043478261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100429', 'n_tokens_mistral': 957, 'n_tokens_neox': 931, 'n_words': 288}