<commit_before>require 'mixlib/config'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/mixin/deep_merge'
require 'securerandom'
require 'uri'
require 'digest/sha2'
require 'etcd'

module Graylog
  extend(Mixlib::Config)

  bootstrap Mash.new
  user Mash.new
  elasticsearch Mash.new
  mongodb Mash.new
  nginx Mash.new
  graylog_server Mash.new
  graylog_web Mash.new
  timezone String.new
  smtp_server String.new
  smtp_port nil
  smtp_user String.new
  smtp_password String.new
  etcd Mash.new
  node nil

  class << self

    # guards against creating secrets on non-bootstrap node
    def generate_hex(chars)
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      existing_secrets ||= Hash.new
      if File.exists?("/etc/graylog/graylog-secrets.json")
        existing_secrets = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-secrets.json"))
      end
      existing_secrets.each do |k, v|
        v.each do |pk, p|
          Graylog[k][pk] = p
        end
      end

      Graylog['graylog_web']['secret_token']      ||= generate_hex(64)
      Graylog['graylog_server']['secret_token']   ||= generate_hex(64)
      Graylog['graylog_server']['admin_password'] ||= Digest::SHA2.new << "<PASSWORD>"
      Graylog['graylog_server']['admin_username'] ||= "admin"

      if File.directory?("/etc/graylog")
        File.open("/etc/graylog/graylog-secrets.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'graylog_server' => {
                'secret_token' => Graylog['graylog_server']['secret_token'],
                'admin_password' => Graylog['graylog_server']['admin_password'],
                'admin_username' => Graylog['graylog_server']['admin_username'],
              },
              'graylog_web' => {
                'secret_token' => Graylog['graylog_web']['secret_token'],
              }
            })
          )
          system("chmod 0600 /etc/graylog/graylog-secrets.json")
        end
      end
    end

    def generate_bootstrap
      if File.exists?("/var/opt/graylog/bootstrapped")
        Graylog['bootstrap']['bootstrapped'] = true
      else
        Graylog['bootstrap']['bootstrapped'] = false
      end
    end

    def generate_services
      existing_services ||= Hash.new
      if File.exists?("/etc/graylog/graylog-services.json")
        existing_services = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-services.json"))
      end
      existing_services.each do |k, v|
        v.each do |pk, p|
          Graylog[k][pk] = p
        end
      end

      Graylog['etcd']['enabled']            = Graylog[:node]['graylog']['etcd']['enable'] if Graylog['etcd']['enabled'].nil?
      Graylog['nginx']['enabled']           = Graylog[:node]['graylog']['nginx']['enable'] if Graylog['nginx']['enabled'].nil?
      Graylog['mongodb']['enabled']         = Graylog[:node]['graylog']['mongodb']['enable'] if Graylog['mongodb']['enabled'].nil?
      Graylog['elasticsearch']['enabled']   = Graylog[:node]['graylog']['elasticsearch']['enable'] if Graylog['elasticsearch']['enabled'].nil?
      Graylog['graylog_server']['enabled']  = Graylog[:node]['graylog']['graylog-server']['enable'] if Graylog['graylog_server']['enabled'].nil?
      Graylog['graylog_web']['enabled']     = Graylog[:node]['graylog']['graylog-web']['enable'] if Graylog['graylog_web']['enabled'].nil?

      if File.directory?("/etc/graylog")
        File.open("/etc/graylog/graylog-services.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'etcd' => {
                'enabled' => Graylog['etcd']['enabled'],
              },
              'nginx' => {
                'enabled' => Graylog['nginx']['enabled'],
              },
              'mongodb' => {
                'enabled' => Graylog['mongodb']['enabled'],
              },
              'elasticsearch' => {
                'enabled' => Graylog['elasticsearch']['enabled'],
              },
              'graylog_server' => {
                'enabled' => Graylog['graylog_server']['enabled'],
              },
              'graylog_web' => {
                'enabled' => Graylog['graylog_web']['enabled'],
              },
            })
          )
          system("chmod 0644 /etc/graylog/graylog-services.json")
        end
      end
    end

    def enabled?(service)
      Graylog[service.gsub('-', '_')]['enabled']
    end

    def generate_settings
      existing_settings ||= Hash.new
      if File.exists?("/etc/graylog/graylog-settings.json")
        existing_settings = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-settings.json"))
      end
      existing_settings.each do |k, v|
        Graylog[k] = v
      end

      Graylog['timezone']      = Graylog[:node]['graylog']['timezone'] if Graylog['timezone'].empty?
      Graylog['smtp_server']   = Graylog[:node]['graylog']['smtp_server'] if Graylog['smtp_server'].nil?
      Graylog['smtp_port']     = Graylog[:node]['graylog']['smtp_port'] if Graylog['smtp_port'].nil?
      Graylog['smtp_user']     = Graylog[:node]['graylog']['smtp_user'] if Graylog['smtp_user'].nil?
      Graylog['smtp_password'] = Graylog[:node]['graylog']['smtp_password'] if Graylog['smtp_password'].nil?
      Graylog['master_node']   ||= '127.0.0.1'
      Graylog['local_connect'] = false if Graylog['local_connect'].nil?
      Graylog['current_address'] = Graylog[:node][:ipaddress]
      Graylog['last_address']  ||= Graylog['current_address']

      if Graylog['current_address'] != Graylog['last_address']<commit_msg>catch eth0 down<commit_after>require 'mixlib/config'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/mixin/deep_merge'
require 'securerandom'
require 'uri'
require 'digest/sha2'
require 'etcd'

module Graylog
  extend(Mixlib::Config)

  bootstrap Mash.new
  user Mash.new
  elasticsearch Mash.new
  mongodb Mash.new
  nginx Mash.new
  graylog_server Mash.new
  graylog_web Mash.new
  timezone String.new
  smtp_server String.new
  smtp_port nil
  smtp_user String.new
  smtp_password String.new
  etcd Mash.new
  node nil

  class << self

    # guards against creating secrets on non-bootstrap node
    def generate_hex(chars)
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      existing_secrets ||= Hash.new
      if File.exists?("/etc/graylog/graylog-secrets.json")
        existing_secrets = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-secrets.json"))
      end
      existing_secrets.each do |k, v|
        v.each do |pk, p|
          Graylog[k][pk] = p
        end
      end

      Graylog['graylog_web']['secret_token']      ||= generate_hex(64)
      Graylog['graylog_server']['secret_token']   ||= generate_hex(64)
      Graylog['graylog_server']['admin_password'] ||= Digest::SHA2.new << "<PASSWORD>"
      Graylog['graylog_server']['admin_username'] ||= "admin"

      if File.directory?("/etc/graylog")
        File.open("/etc/graylog/graylog-secrets.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'graylog_server' => {
                'secret_token' => Graylog['graylog_server']['secret_token'],
                'admin_password' => Graylog['graylog_server']['admin_password'],
                'admin_username' => Graylog['graylog_server']['admin_username'],
              },
              'graylog_web' => {
                'secret_token' => Graylog['graylog_web']['secret_token'],
              }
            })
          )
          system("chmod 0600 /etc/graylog/graylog-secrets.json")
        end
      end
    end

    def generate_bootstrap
      if File.exists?("/var/opt/graylog/bootstrapped")
        Graylog['bootstrap']['bootstrapped'] = true
      else
        Graylog['bootstrap']['bootstrapped'] = false
      end
    end

    def generate_services
      existing_services ||= Hash.new
      if File.exists?("/etc/graylog/graylog-services.json")
        existing_services = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-services.json"))
      end
      existing_services.each do |k, v|
        v.each do |pk, p|
          Graylog[k][pk] = p
        end
      end

      Graylog['etcd']['enabled']            = Graylog[:node]['graylog']['etcd']['enable'] if Graylog['etcd']['enabled'].nil?
      Graylog['nginx']['enabled']           = Graylog[:node]['graylog']['nginx']['enable'] if Graylog['nginx']['enabled'].nil?
      Graylog['mongodb']['enabled']         = Graylog[:node]['graylog']['mongodb']['enable'] if Graylog['mongodb']['enabled'].nil?
      Graylog['elasticsearch']['enabled']   = Graylog[:node]['graylog']['elasticsearch']['enable'] if Graylog['elasticsearch']['enabled'].nil?
      Graylog['graylog_server']['enabled']  = Graylog[:node]['graylog']['graylog-server']['enable'] if Graylog['graylog_server']['enabled'].nil?
      Graylog['graylog_web']['enabled']     = Graylog[:node]['graylog']['graylog-web']['enable'] if Graylog['graylog_web']['enabled'].nil?

      if File.directory?("/etc/graylog")
        File.open("/etc/graylog/graylog-services.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'etcd' => {
                'enabled' => Graylog['etcd']['enabled'],
              },
              'nginx' => {
                'enabled' => Graylog['nginx']['enabled'],
              },
              'mongodb' => {
                'enabled' => Graylog['mongodb']['enabled'],
              },
              'elasticsearch' => {
                'enabled' => Graylog['elasticsearch']['enabled'],
              },
              'graylog_server' => {
                'enabled' => Graylog['graylog_server']['enabled'],
              },
              'graylog_web' => {
                'enabled' => Graylog['graylog_web']['enabled'],
              },
            })
          )
          system("chmod 0644 /etc/graylog/graylog-services.json")
        end
      end
    end

    def enabled?(service)
      Graylog[service.gsub('-', '_')]['enabled']
    end

    def generate_settings
      existing_settings ||= Hash.new
      if File.exists?("/etc/graylog/graylog-settings.json")
        existing_settings = Chef::JSONCompat.from_json(File.read("/etc/graylog/graylog-settings.json"))
      end
      existing_settings.each do |k, v|
        Graylog[k] = v
      end

      Graylog['timezone']      = Graylog[:node]['graylog']['timezone'] if Graylog['timezone'].empty?
      Graylog['smtp_server']   = Graylog[:node]['graylog']['smtp_server'] if Graylog['smtp_server'].nil?
      Graylog['smtp_port']     = Graylog[:node]['graylog']['smtp_port'] if Graylog['smtp_port'].nil?
      Graylog['smtp_user']     = Graylog[:node]['graylog']['smtp_user'] if Graylog['smtp_user'].nil?
      Graylog['smtp_password'] = Graylog[:node]['graylog']['smtp_password'] if Graylog['smtp_password'].nil?
      Graylog['master_node']   ||= '127.0.0.1'
      Graylog['local_connect'] = false if Graylog['local_connect'].nil?
      Graylog['current_address'] = Graylog[:node][:ipaddress]
      Graylog['last_address']  ||= Graylog['current_address']

      if Graylog['current_address'] == '127.0.0.1'
        Chef::Application.fatal!("eth0 is down! Can not reconfigure Graylog.")
      end

      if Graylog['current_address'] != Graylog['last_address']
{'fraction_non_alphanumeric': 0.16474013941586518, 'fraction_numerical': 0.004235418688784964, 'mean_word_length': 2.739359947212141, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '2363414', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3454, 'n_words': 524}