<commit_before>#
# Cookbook Name:: rabbitmq
# Provider:: cluster
#
# Author: <NAME> <<EMAIL>>
# Copyright (C) 2015 <NAME>
#
# 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.
#

include Chef::Mixin::ShellOut

use_inline_resources

# Get ShellOut
def get_shellout(cmd)
  sh_cmd = Mixlib::ShellOut.new(cmd)
  sh_cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  sh_cmd
end

# Execute rabbitmqctl command with args
def run_rabbitmqctl(*args)
  cmd = "rabbitmqctl #{args.join(' ')}"
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  begin
    cmd.error!
    Chef::Log.debug("[rabbitmq_cluster] #{cmd.stdout}")
  rescue
    Chef::Application.fatal!("[rabbitmq_cluster] #{cmd.stderr}")
  end
end

# Get cluster status result
def cluster_status
  # execute > rabbitmqctl cluster_status"
  # To parse the result string, this function normalize the output string
  # - Removing first line : it returns "Cluster status of node 'rabbit@rabbit1' ..."
  # - Removing "... Done" : old version returns this
  cmd = 'rabbitmqctl cluster_status'
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  cmd.error!
  result = cmd.stdout.split(/\n/, 2).last.squeeze(' ').gsub(/\n/, '').gsub('...done.', '')
  Chef::Log.debug("[rabbitmq_cluster] rabbitmqctl cluster_status : #{result}")
  result
end

# Match regex pattern from result of rabbitmqctl cluster_status
def match_pattern_cluster_status(cluster_status, pattern)
  if cluster_status.nil? || cluster_status.to_s.empty?
    Chef::Application.fatal!('[rabbitmq_cluster] cluster_status should not be empty')
  end
  match = cluster_status.match(pattern)
  match && match[2]
end

# Get currently joined cluster name from result string of "rabbitmqctl cluster_status"
def current_cluster_name(cluster_status)
  pattern = '({cluster_name,<<")(.*?)(">>})'
  result = match_pattern_cluster_status(cluster_status, pattern)
  Chef::Log.debug("[rabbitmq_cluster] current_cluster_name : #{result}")
  result
end

# Get running nodes
def running_nodes(cluster_status)
  pattern = '({running_nodes,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] running_nodes : #{result}")
  !result.nil? ? result.split(',') : []
end

# Get disc nodes
def disc_nodes(cluster_status)
  pattern = '({disc,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub!(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] disc_nodes : #{result}")
  !result.nil? ? result.split(',') : []
end

# Get ram nodes
def ram_nodes(cluster_status)
  pattern = '({ram,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub!(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] ram_nodes : #{result}")
  !result.nil? ? result.split(',') : []
end

# Get node name
def node_name
  # execute > rabbitmqctl eval 'node().'
  cmd = 'rabbitmqctl eval "node()." | head -1'
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  cmd.error!
  result = cmd.stdout.chomp.gsub!(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] node name : #{result}")
  result
end

# Get cluster_node_type of current node
def current_cluster_node_type(node_name, cluster_status)
  var_cluster_node_type = ''
  if disc_nodes(cluster_status).include?(node_name)
    var_cluster_node_type = 'disc'
  elsif ram_nodes(cluster_status).include?(node_name)
    var_cluster_node_type = 'ram'<commit_msg>fixed merge conflict<commit_after>#
# Cookbook Name:: rabbitmq
# Provider:: cluster
#
# Author: <NAME> <<EMAIL>>
# Copyright (C) 2015 <NAME>
#
# 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.
#

include Chef::Mixin::ShellOut

use_inline_resources

# Get ShellOut
def get_shellout(cmd)
  sh_cmd = Mixlib::ShellOut.new(cmd)
  sh_cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  sh_cmd
end

# Execute rabbitmqctl command with args
def run_rabbitmqctl(*args)
  cmd = "rabbitmqctl #{args.join(' ')}"
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  begin
    cmd.error!
    Chef::Log.debug("[rabbitmq_cluster] #{cmd.stdout}")
  rescue
    Chef::Application.fatal!("[rabbitmq_cluster] #{cmd.stderr}")
  end
end

# Get cluster status result
def cluster_status
  # execute > rabbitmqctl cluster_status"
  # To parse the result string, this function normalize the output string
  # - Removing first line : it returns "Cluster status of node 'rabbit@rabbit1' ..."
  # - Removing "... Done" : old version returns this
  cmd = 'rabbitmqctl cluster_status'
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  cmd.error!
  result = cmd.stdout.split(/\n/, 2).last.squeeze(' ').gsub(/\n/, '').gsub('...done.', '')
  Chef::Log.debug("[rabbitmq_cluster] rabbitmqctl cluster_status : #{result}")
  result
end

# Match regex pattern from result of rabbitmqctl cluster_status
def match_pattern_cluster_status(cluster_status, pattern)
  if cluster_status.nil? || cluster_status.to_s.empty?
    Chef::Application.fatal!('[rabbitmq_cluster] cluster_status should not be empty')
  end
  match = cluster_status.match(pattern)
  match && match[2]
end

# Get currently joined cluster name from result string of "rabbitmqctl cluster_status"
def current_cluster_name(cluster_status)
  pattern = '({cluster_name,<<")(.*?)(">>})'
  result = match_pattern_cluster_status(cluster_status, pattern)
  Chef::Log.debug("[rabbitmq_cluster] current_cluster_name : #{result}")
  result
end

# Get running nodes
def running_nodes(cluster_status)
  pattern = '({running_nodes,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] running_nodes : #{result}")
  result.nil? ? [] : result
end

# Get disc nodes
def disc_nodes(cluster_status)
  pattern = '({disc,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub(/'/, '').split(',')
  Chef::Log.debug("[rabbitmq_cluster] disc_nodes : #{result}")
  result.nil? ? [] : result
end

# Get ram nodes
def ram_nodes(cluster_status)
  pattern = '({ram,\[\'*)(.*?)(\'*\]})'
  match = match_pattern_cluster_status(cluster_status, pattern)
  result = match && match.gsub(/'/, '').split(',')
  Chef::Log.debug("[rabbitmq_cluster] ram_nodes : #{result}")
  result.nil? ? [] : result
end

# Get node name
def node_name
  # execute > rabbitmqctl eval 'node().'
  cmd = 'rabbitmqctl eval "node()." | head -1'
  Chef::Log.debug("[rabbitmq_cluster] Executing #{cmd}")
  cmd = get_shellout(cmd)
  cmd.run_command
  cmd.error!
  result = cmd.stdout.chomp.gsub(/'/, '')
  Chef::Log.debug("[rabbitmq_cluster] node name : #{result}")
  result
end

# Get cluster_node_type of current node
def current_cluster_node_type(node_name, cluster_status)
  var_cluster_node_type = ''
  if disc_nodes(cluster_status).include?(node_name)
    var_cluster_node_type = 'disc'
  elsif ram_nodes(cluster_status).include?(node_name)
    var_cluster_node_type = 'ram'
{'fraction_non_alphanumeric': 0.14803111327175497, 'fraction_numerical': 0.002916869227029655, 'mean_word_length': 4.928674351585014, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '6233447', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2705, 'n_words': 683}