<commit_before># Cookbook:: bcpc
# Library:: utils
#
# Copyright:: 2020 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

return Chef::Log.warn('requires chef-server') if Chef::Config[:solo]

require 'ipaddr'
require 'ipaddress'

def init_cloud?
  nodes = search(:node, 'roles:headnode')
  nodes = nodes.reject { |n| n['hostname'] == node['hostname'] }
  nodes.empty?
end

def init_rmq?
  nodes = search(:node, 'roles:rmqnode')
  nodes = nodes.reject { |n| n['hostname'] == node['hostname'] }
  nodes.empty?
end

def bootstrap?
  search(:node, "role:bootstrap AND hostname:#{node['hostname']}").any?
end

def headnode?
  search(:node, "role:headnode AND hostname:#{node['hostname']}").any?
end

def rmqnode?
  search(:node, "role:rmqnode AND hostname:#{node['hostname']}").any?
end

def storagenode?
  search(:node, "role:storagenode AND hostname:#{node['hostname']}").any?
end

def stubnode?
  search(:node, "role:stubnode AND hostname:#{node['hostname']}").any?
end

def worknode?
  search(:node, "role:worknode AND hostname:#{node['hostname']}").any?
end

def bootstraps
  nodes = search(:node, 'role:bootstrap')
  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def headnodes(exclude: nil, all: false)
  nodes = []

  if !exclude.nil?
    nodes = search(:node, 'roles:headnode')
    nodes = nodes.reject { |h| h['hostname'] == exclude }
  elsif all == true
    nodes = search(:node, 'role:headnode')
  else
    nodes = search(:node, 'roles:headnode')
  end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def rmqnodes(exclude: nil, all: false)
  nodes = []

  if !exclude.nil?
    nodes = search(:node, 'roles:rmqnode')
    nodes = nodes.reject { |h| h['hostname'] == exclude }
  elsif all == true
    nodes = search(:node, 'role:rmqnode')
  else
    nodes = search(:node, 'roles:rmqnode')
  end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def worknodes(all: false)
  nodes = if all
            search(:node, 'role:worknode')
          else
            search(:node, 'roles:worknode')
          end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def all_nodes
  nodes = search(:node, '*:*')
  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def generate_service_catalog_uri(svcprops, access_level)
  fqdn = node['bcpc']['cloud']['fqdn']
  port = svcprops['ports'][access_level]
  path = svcprops['uris'][access_level]
  "https://#{fqdn}:#{port}/#{path}"
end

def mysqladmin
  region = node['bcpc']['cloud']['region']
  config = data_bag_item(region, 'config')
  {
    'username' => 'root',
    'password' => config['mysql']['users']['root']['password'],
  }<commit_msg>Add a library function to fetch chef roles<commit_after># Cookbook:: bcpc
# Library:: utils
#
# Copyright:: 2020 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

return Chef::Log.warn('requires chef-server') if Chef::Config[:solo]

require 'ipaddr'
require 'ipaddress'

def init_cloud?
  nodes = search(:node, 'roles:headnode')
  nodes = nodes.reject { |n| n['hostname'] == node['hostname'] }
  nodes.empty?
end

def init_rmq?
  nodes = search(:node, 'roles:rmqnode')
  nodes = nodes.reject { |n| n['hostname'] == node['hostname'] }
  nodes.empty?
end

def bootstrap?
  search(:node, "role:bootstrap AND hostname:#{node['hostname']}").any?
end

def headnode?
  search(:node, "role:headnode AND hostname:#{node['hostname']}").any?
end

def rmqnode?
  search(:node, "role:rmqnode AND hostname:#{node['hostname']}").any?
end

def storagenode?
  search(:node, "role:storagenode AND hostname:#{node['hostname']}").any?
end

def stubnode?
  search(:node, "role:stubnode AND hostname:#{node['hostname']}").any?
end

def worknode?
  search(:node, "role:worknode AND hostname:#{node['hostname']}").any?
end

def bootstraps
  nodes = search(:node, 'role:bootstrap')
  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def headnodes(exclude: nil, all: false)
  nodes = []

  if !exclude.nil?
    nodes = search(:node, 'roles:headnode')
    nodes = nodes.reject { |h| h['hostname'] == exclude }
  elsif all == true
    nodes = search(:node, 'role:headnode')
  else
    nodes = search(:node, 'roles:headnode')
  end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def rmqnodes(exclude: nil, all: false)
  nodes = []

  if !exclude.nil?
    nodes = search(:node, 'roles:rmqnode')
    nodes = nodes.reject { |h| h['hostname'] == exclude }
  elsif all == true
    nodes = search(:node, 'role:rmqnode')
  else
    nodes = search(:node, 'roles:rmqnode')
  end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def worknodes(all: false)
  nodes = if all
            search(:node, 'role:worknode')
          else
            search(:node, 'roles:worknode')
          end

  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def all_nodes
  nodes = search(:node, '*:*')
  nodes.sort! { |a, b| a['hostname'] <=> b['hostname'] }
end

def node_roles
  matches = search(:node, "hostname:#{node['hostname']}")
  raise "the node '#{node['hostname']}' does not exist or has "\
    'multiple matches' if matches.length != 1
  matches[0]['roles']
end

def generate_service_catalog_uri(svcprops, access_level)
  fqdn = node['bcpc']['cloud']['fqdn']
  port = svcprops['ports'][access_level]
  path = svcprops['uris'][access_level]
  "https://#{fqdn}:#{port}/#{path}"
end

def mysqladmin
  region = node['bcpc']['cloud']['region']
  config = data_bag_item(region, 'config')
  {
    'username' => 'root',
    'password' => config['mysql']['users']['root']['password'],
  }
{'fraction_non_alphanumeric': 0.17301248857752055, 'fraction_numerical': 0.0027413950654888823, 'mean_word_length': 4.431761786600497, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2749858', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2255, 'n_words': 608}