<commit_before>#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
#
# All Rights Reserved
#

add_command "master-recover", "Set this server to HA master state, ignoring VRRP.", 1 do
  exec "/var/opt/opscode/keepalived/bin/cluster.sh master"
end

add_command "backup-recover", "Set this server to HA backup state, ignoring VRRP", 1 do
  exec "/var/opt/opscode/keepalived/bin/cluster.sh backup"
end

#
# ha-status
#  - check keepalived is actually enabled in the config
#  - check drbd is actually enabled in the config
#  - check that someone created the underlying block device drbd will use
#  - check that the drbd device itself exists (i.e. kernel module configured+loaded)
#  - check that the current state matches requested state of master or backup
#  - check that the VRRP IP address state is correct based on our state
#  - check that the DRBD state is correct based on our state
#  - check that the DRBD drive is correctly (un)mounted based on our state
#  - check that the DRBD primary+secondary replication IP are both pingable
#  - check that the runit service status is correct based on our state
#

add_command "ha-status", "Show the status of high availability services.", 1 do
  if running_config.nil?
    puts "[ERROR] cannot check HA status if you haven't completed a reconfigure"
    exit 1
  end

  error_exit = 0

  if running_config['private_chef']['keepalived']['enable']
    puts "[OK] keepalived HA services enabled."
  else
    puts "[ERROR] keepalived HA services not enabled, please configure keepalived according to documentation."
    exit 1
  end

  if running_config['private_chef']['drbd']['enable']
    puts "[OK] DRBD disk replication enabled."
  else
    puts "[ERROR] DRBD disk replication services not enabled, please configure DRBD according to documentation."
    exit 1
  end

  drbd_disk = running_config['private_chef']['drbd']['disk']
  if File.exists?(drbd_disk)
    puts "[OK] DRBD partition #{drbd_disk} found."
  else
    puts "[ERROR] DRBD partition #{drbd_disk} not found, please FIXME."
    exit 1
  end

  drbd_device = running_config['private_chef']['drbd']['device']
  if File.exists?(drbd_device)
    puts "[OK] DRBD device #{drbd_device} found."
  else
    puts "[ERROR] DRBD device #{drbd_device} not found, please FIXME."
    exit 1
  end

  requested_cluster_status = File.read("/var/opt/opscode/keepalived/requested_cluster_status").chomp
  current_cluster_status = File.read("/var/opt/opscode/keepalived/current_cluster_status").chomp

  if requested_cluster_status != current_cluster_status
    puts "[WARN] inconsistent cluster status, requested: #{requested_cluster_status}, current: #{current_cluster_status}, failover running?"
    error_exit = 2
  else
    puts "[OK] cluster status = #{current_cluster_status}"
  end

  vrrp_instance_ipaddress = running_config['private_chef']['keepalived']['vrrp_instance_ipaddress']
  vrrp_instance_ipaddress_dev = running_config['private_chef']['keepalived']['vrrp_instance_ipaddress_dev']
  has_vrrp_addr = `ip a show dev #{vrrp_instance_ipaddress_dev}` =~ /#{vrrp_instance_ipaddress}/

  if has_vrrp_addr
    if current_cluster_status == "master"
      puts "[OK] found VIP IP address and I am master"
    else
      puts "[ERROR] found VRRP address and I am not master"
      error_exit = 3
    end
  else
    if current_cluster_status == "master"
      puts "[ERROR] did not find VIP IP address and I am master"
      error_exit = 3
    else
      puts "[OK] did not find VIP IP address and I am not master"<commit_msg>[OC-11499] Use stricter regular expression for IP check in ha-status<commit_after>#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
#
# All Rights Reserved
#

add_command "master-recover", "Set this server to HA master state, ignoring VRRP.", 1 do
  exec "/var/opt/opscode/keepalived/bin/cluster.sh master"
end

add_command "backup-recover", "Set this server to HA backup state, ignoring VRRP", 1 do
  exec "/var/opt/opscode/keepalived/bin/cluster.sh backup"
end

#
# ha-status
#  - check keepalived is actually enabled in the config
#  - check drbd is actually enabled in the config
#  - check that someone created the underlying block device drbd will use
#  - check that the drbd device itself exists (i.e. kernel module configured+loaded)
#  - check that the current state matches requested state of master or backup
#  - check that the VRRP IP address state is correct based on our state
#  - check that the DRBD state is correct based on our state
#  - check that the DRBD drive is correctly (un)mounted based on our state
#  - check that the DRBD primary+secondary replication IP are both pingable
#  - check that the runit service status is correct based on our state
#

add_command "ha-status", "Show the status of high availability services.", 1 do
  if running_config.nil?
    puts "[ERROR] cannot check HA status if you haven't completed a reconfigure"
    exit 1
  end

  error_exit = 0

  if running_config['private_chef']['keepalived']['enable']
    puts "[OK] keepalived HA services enabled."
  else
    puts "[ERROR] keepalived HA services not enabled, please configure keepalived according to documentation."
    exit 1
  end

  if running_config['private_chef']['drbd']['enable']
    puts "[OK] DRBD disk replication enabled."
  else
    puts "[ERROR] DRBD disk replication services not enabled, please configure DRBD according to documentation."
    exit 1
  end

  drbd_disk = running_config['private_chef']['drbd']['disk']
  if File.exists?(drbd_disk)
    puts "[OK] DRBD partition #{drbd_disk} found."
  else
    puts "[ERROR] DRBD partition #{drbd_disk} not found, please FIXME."
    exit 1
  end

  drbd_device = running_config['private_chef']['drbd']['device']
  if File.exists?(drbd_device)
    puts "[OK] DRBD device #{drbd_device} found."
  else
    puts "[ERROR] DRBD device #{drbd_device} not found, please FIXME."
    exit 1
  end

  requested_cluster_status = File.read("/var/opt/opscode/keepalived/requested_cluster_status").chomp
  current_cluster_status = File.read("/var/opt/opscode/keepalived/current_cluster_status").chomp

  if requested_cluster_status != current_cluster_status
    puts "[WARN] inconsistent cluster status, requested: #{requested_cluster_status}, current: #{current_cluster_status}, failover running?"
    error_exit = 2
  else
    puts "[OK] cluster status = #{current_cluster_status}"
  end

  vrrp_instance_ipaddress = running_config['private_chef']['keepalived']['vrrp_instance_ipaddress']
  vrrp_instance_ipaddress_dev = running_config['private_chef']['keepalived']['vrrp_instance_ipaddress_dev']
  has_vrrp_addr = `ip a show dev #{vrrp_instance_ipaddress_dev}` =~ /inet6? #{Regexp.escape(vrrp_instance_ipaddress)}/

  if has_vrrp_addr
    if current_cluster_status == "master"
      puts "[OK] found VIP IP address and I am master"
    else
      puts "[ERROR] found VRRP address and I am not master"
      error_exit = 3
    end
  else
    if current_cluster_status == "master"
      puts "[ERROR] did not find VIP IP address and I am master"
      error_exit = 3
    else
      puts "[OK] did not find VIP IP address and I am not master"
{'fraction_non_alphanumeric': 0.09288620738038446, 'fraction_numerical': 0.005331836677423881, 'mean_word_length': 4.142857142857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6020500', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2162, 'n_words': 769}