<commit_before>require 'mixlib/shellout'

class OmnibusHelper

  # Returns 'true' if this machine was designated as the bootstrap
  # server in private-chef.rb
  def self.is_bootstrap_server?(node)
    case PrivateChef['topology']
    when 'standalone', 'manual'
      true
    when 'tier'
      PrivateChef['role'] == 'backend'
    when 'ha'
      node_name = node['fqdn']
      !!(PrivateChef["servers"][node_name]['bootstrap'])
    end
  end

  # Return true if the node is the master for data storage replication
  # purposes.
  #
  # This will return true if the node is any of the following:
  #
  #   * A stand-alone EC install
  #   * A tier-topology backend machine
  #   * An HA topology backend keepalived master machine
  #
  # Any other machine will get 'false'.
  #
  # @param node [Node] needs to be passed in to access kepalive
  #   directory path information
  def self.is_data_master?(node)

    topology = PrivateChef['topology']
    role = PrivateChef['role']

    case topology
    when "standalone"
      true # by definition
    when "tier"
      role == "backend"
    when "ha"
      if (role == "backend")
        dir = node['private_chef']['keepalived']['dir']
        cluster_status_file = "#{dir}/current_cluster_status"

        if File.exists?(cluster_status_file)
          File.open(cluster_status_file).read.chomp == "master"
        else
          # If the file doesn't exist, then we are most likely doing
          # the initial setup, because keepalived must be configured
          # after everything else.  In this case, we'll consider
          # ourself the master if we're defined as the bootstrap
          # server
          is_bootstrap_server?(node)
        end
      else
        false # frontends can't be masters, by definition
      end
    end
  end

  # This file is touched once initial bootstrapping of the system is
  # done.
  def self.bootstrap_sentinel_file
    "var/opt/opscode/bootstrapped"
  end

  # Use the presence of a sentinel file as an indicator for whether
  # the server has already had initial bootstrapping performed.
  #
  # @todo: Is there a more robust way to determine this, i.e., based
  #   on some functional aspect of the system?
  def self.has_been_bootstrapped?
    File.exists?(bootstrap_sentinel_file)
  end

  def self.should_notify?(service_name)
    File.symlink?("/opt/opscode/service/#{service_name}") && check_status(service_name)
  end

  def self.check_status(service_name)
    o = Mixlib::ShellOut.new("/opt/opscode/bin/private-chef-ctl status #{service_name}")
    o.run_command
    o.exitstatus == 0 ? true : false
  end

  # generate a certificate signed by the opscode ca key
  #
  # === Returns
  # [cert, key]
  #
  def self.gen_certificate
    key = OpenSSL::PKey::RSA.generate(2048)
    public_key = key.public_key
    cert_uuid = UUIDTools::UUID.random_create
    common_name = "URI:http://opscode.com/GUIDS/#{cert_uuid}"
    info = [["C", "US"], ["ST", "Washington"], ["L", "Seattle"], ["O", "Opscode, Inc."], ["OU", "Certificate Service"], ["CN", common_name]]
    cert = OpenSSL::X509::Certificate.new
    cert.subject = OpenSSL::X509::Name.new(info)
    cert.issuer = ca_certificate.subject
    cert.not_before = Time.now
    cert.not_after = Time.now + 10 * 365 * 24 * 60 * 60 # 10 years
    cert.public_key = public_key
    cert.serial = 1
    cert.version = 3

    ef = OpenSSL::X509::ExtensionFactory.new
    ef.subject_certificate = cert
    ef.issuer_certificate = ca_certificate
    cert.extensions = [
                       ef.create_extension("basicConstraints","CA:FALSE",true),
                       ef.create_extension("subjectKeyIdentifier", "hash")
                      ]
    cert.sign(ca_keypair, OpenSSL::Digest::SHA1.new)

    return cert, key
  end

  ######################################################################
  #
  # the following is the Opscode CA key and certificate, copied from
  # the cert project(s)
  #
  ######################################################################

  def self.ca_certificate
    @_ca_cert ||=
      begin
        cert_string = <<-EOCERT
-----<KEY>END CERTIFICATE-----
EOCERT
        OpenSSL::X509::Certificate.new(cert_string)
      end
  end

  def self.ca_keypair
    @_ca_key ||=
      begin
        keypair_string = <<-EOKEY
-----BEGIN RSA PRIVATE KEY-----
<KEY>==
-----END RSA PRIVATE KEY-----
EOKEY
        OpenSSL::PKey::RSA.new(keypair_string)
      end
  end
end

class Chef::Resource::Template
  def ldap_authentication_enabled?
    node['private_chef'].attribute?('ldap') &&
      !(node['private_chef']['ldap'].nil? || node['private_chef']['ldap'].empty?)
  end
end
<commit_msg>Don't forget the trailing slash!<commit_after>require 'mixlib/shellout'

class OmnibusHelper

  # Returns 'true' if this machine was designated as the bootstrap
  # server in private-chef.rb
  def self.is_bootstrap_server?(node)
    case PrivateChef['topology']
    when 'standalone', 'manual'
      true
    when 'tier'
      PrivateChef['role'] == 'backend'
    when 'ha'
      node_name = node['fqdn']
      !!(PrivateChef["servers"][node_name]['bootstrap'])
    end
  end

  # Return true if the node is the master for data storage replication
  # purposes.
  #
  # This will return true if the node is any of the following:
  #
  #   * A stand-alone EC install
  #   * A tier-topology backend machine
  #   * An HA topology backend keepalived master machine
  #
  # Any other machine will get 'false'.
  #
  # @param node [Node] needs to be passed in to access kepalive
  #   directory path information
  def self.is_data_master?(node)

    topology = PrivateChef['topology']
    role = PrivateChef['role']

    case topology
    when "standalone"
      true # by definition
    when "tier"
      role == "backend"
    when "ha"
      if (role == "backend")
        dir = node['private_chef']['keepalived']['dir']
        cluster_status_file = "#{dir}/current_cluster_status"

        if File.exists?(cluster_status_file)
          File.open(cluster_status_file).read.chomp == "master"
        else
          # If the file doesn't exist, then we are most likely doing
          # the initial setup, because keepalived must be configured
          # after everything else.  In this case, we'll consider
          # ourself the master if we're defined as the bootstrap
          # server
          is_bootstrap_server?(node)
        end
      else
        false # frontends can't be masters, by definition
      end
    end
  end

  # This file is touched once initial bootstrapping of the system is
  # done.
  def self.bootstrap_sentinel_file
    "/var/opt/opscode/bootstrapped"
  end

  # Use the presence of a sentinel file as an indicator for whether
  # the server has already had initial bootstrapping performed.
  #
  # @todo: Is there a more robust way to determine this, i.e., based
  #   on some functional aspect of the system?
  def self.has_been_bootstrapped?
    File.exists?(bootstrap_sentinel_file)
  end

  def self.should_notify?(service_name)
    File.symlink?("/opt/opscode/service/#{service_name}") && check_status(service_name)
  end

  def self.check_status(service_name)
    o = Mixlib::ShellOut.new("/opt/opscode/bin/private-chef-ctl status #{service_name}")
    o.run_command
    o.exitstatus == 0 ? true : false
  end

  # generate a certificate signed by the opscode ca key
  #
  # === Returns
  # [cert, key]
  #
  def self.gen_certificate
    key = OpenSSL::PKey::RSA.generate(2048)
    public_key = key.public_key
    cert_uuid = UUIDTools::UUID.random_create
    common_name = "URI:http://opscode.com/GUIDS/#{cert_uuid}"
    info = [["C", "US"], ["ST", "Washington"], ["L", "Seattle"], ["O", "Opscode, Inc."], ["OU", "Certificate Service"], ["CN", common_name]]
    cert = OpenSSL::X509::Certificate.new
    cert.subject = OpenSSL::X509::Name.new(info)
    cert.issuer = ca_certificate.subject
    cert.not_before = Time.now
    cert.not_after = Time.now + 10 * 365 * 24 * 60 * 60 # 10 years
    cert.public_key = public_key
    cert.serial = 1
    cert.version = 3

    ef = OpenSSL::X509::ExtensionFactory.new
    ef.subject_certificate = cert
    ef.issuer_certificate = ca_certificate
    cert.extensions = [
                       ef.create_extension("basicConstraints","CA:FALSE",true),
                       ef.create_extension("subjectKeyIdentifier", "hash")
                      ]
    cert.sign(ca_keypair, OpenSSL::Digest::SHA1.new)

    return cert, key
  end

  ######################################################################
  #
  # the following is the Opscode CA key and certificate, copied from
  # the cert project(s)
  #
  ######################################################################

  def self.ca_certificate
    @_ca_cert ||=
      begin
        cert_string = <<-EOCERT
-----BEGIN CERTIFICATE-----
<KEY>
-----END CERTIFICATE-----
EOCERT
        OpenSSL::X509::Certificate.new(cert_string)
      end
  end

  def self.ca_keypair
    @_ca_key ||=
      begin
        keypair_string = <<-EOKEY
-----<KEY>
EOKEY
        OpenSSL::PKey::RSA.new(keypair_string)
      end
  end
end

class Chef::Resource::Template
  def ldap_authentication_enabled?
    node['private_chef'].attribute?('ldap') &&
      !(node['private_chef']['ldap'].nil? || node['private_chef']['ldap'].empty?)
  end
end

{'fraction_non_alphanumeric': 0.141999141999142, 'fraction_numerical': 0.007078507078507079, 'mean_word_length': 2.961342395921835, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '372861', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2843, 'n_words': 767}