<commit_before>#!/usr/bin/env ruby
#
# repxe_host.rb
#
# This script coordinates the re-installation and re-chefing of an
# existing host.
#
# Provisos:
#
#  * It still calls out to c-a-r for the chef bootstrap and re-chefing.
#  * Hosts still have to be manually rebooted.
#
# To use from the command line:
#
#  1. If necessary, configure your local rubygems mirror.
#     Replace 'http://mirror.example.com' with your actual mirror.
#     ```
#     bundle config mirror.https://rubygems.org http://mirror.example.com
#     ```
#
#  2. Run 'bundle install --deployment' on a bootstrap node with
#     access to a rubygems mirror.
#
#  3. If not already using the target bootstrap, sync the updated
#     repository, including 'vendor' directory, to the target
#     bootstrap.
#
#  4. Run 'bundle exec ./repxe_host.rb -m <hostname>' to begin the process.
#     To run on a brand new node that doesn't need to be shut down first, run:
#     'bundle exec ./repxe_host.rb -m <hostname> -s'
#
#  5. When prompted, manually reboot the host, then press enter.
#
# It is also possible to use methods from this script at a ruby REPL
# instead of running the script from a UNIX shell.  To load methods
# into `irb`:
#
#  1. Change to the repo directory.
#
#  2. Verify that dependencies are installed:
#     `bundle list`
#
#  3. Run irb inside the repo directory.
#     `bundle exec irb`
#
#  4. Load this file.
#     `irb(main):001:0> load ./repxe_host.rb`
#

require 'chef/provisioning/transport/ssh'
require 'mixlib/shellout'
require 'pry'
require 'timeout'
require 'optparse'

def cluster_assign_roles(environment,type,entry=nil)
  types = [ 'basic', 'hadoop', 'kafka' ]
  if(!types.include?(type.to_s.downcase))
    raise "#{type} is not one of #{types.join(",")} !"
  end

  # We use system() instead of Mixlib::ShellOut specifically so that the
  # child process re-uses our STDOUT/STDERR.
  if entry.nil?
    system('sudo', './cluster-assign-roles.sh',
           environment, type.to_s.downcase.capitalize)
  else
    system('sudo', './cluster-assign-roles.sh',
           environment, type.to_s.downcase.capitalize, entry['hostname'])
  end
  if !$?.success?
    puts "cluster-assign-roles.sh failed!"
  end
end

def restart_chef_server()
  c = Mixlib::ShellOut.new('sudo', 'chef-server-ctl' ,'restart')
  c.run_command
  if !c.status.success?
    raise "Failed to restart chef-server"
  end

  puts "restarted chef-server"
end
def cobbler_unenroll(entry)
  c = Mixlib::ShellOut.new('sudo', 'cobbler' ,'system', 'remove',
                           '--name', entry['hostname'])
  c.run_command
  if !c.status.success?
    raise "Failed to un-enroll #{entry['hostname']}!"
  end

  puts "Un-enrolled #{entry['hostname']} from cobbler"
end

def cobbler_enroll(entry)
  c = Mixlib::ShellOut.new('sudo', 'cobbler', 'system', 'add',
                           '--name', entry['hostname'],
                           '--hostname', fqdn(entry),
                           '--profile', entry['cobbler_profile'],
                           '--ip-address', entry['ip_address'],
                           '--mac', corrected_mac(entry))
  c.run_command
  if !c.status.success?
    raise "Failed to enroll #{entry['hostname']}!"
  end

  puts "Enrolled #{entry['hostname']} in cobbler"
end

# This is mostly copy/pasted out of BACH-next chef helpers.
def cobbler_root_password      
  require 'json'
  require 'mixlib/shellout'
  
  vault_command =
    Mixlib::ShellOut.new('sudo',
                         'knife', 'vault', 'show',
                         'os', 'cobbler',
                         '-F', 'json',
                         '-p', 'all',
                         '-m', 'client')
  
  vault_command.run_command
  
  if !vault_command.status.success?
    raise 'Could not retrieve cobbler password!\n' +
      vault_command.stdout + '\n' +<commit_msg>update cobbler_enroll in repxe script<commit_after>#!/usr/bin/env ruby
#
# repxe_host.rb
#
# This script coordinates the re-installation and re-chefing of an
# existing host.
#
# Provisos:
#
#  * It still calls out to c-a-r for the chef bootstrap and re-chefing.
#  * Hosts still have to be manually rebooted.
#
# To use from the command line:
#
#  1. If necessary, configure your local rubygems mirror.
#     Replace 'http://mirror.example.com' with your actual mirror.
#     ```
#     bundle config mirror.https://rubygems.org http://mirror.example.com
#     ```
#
#  2. Run 'bundle install --deployment' on a bootstrap node with
#     access to a rubygems mirror.
#
#  3. If not already using the target bootstrap, sync the updated
#     repository, including 'vendor' directory, to the target
#     bootstrap.
#
#  4. Run 'bundle exec ./repxe_host.rb -m <hostname>' to begin the process.
#     To run on a brand new node that doesn't need to be shut down first, run:
#     'bundle exec ./repxe_host.rb -m <hostname> -s'
#
#  5. When prompted, manually reboot the host, then press enter.
#
# It is also possible to use methods from this script at a ruby REPL
# instead of running the script from a UNIX shell.  To load methods
# into `irb`:
#
#  1. Change to the repo directory.
#
#  2. Verify that dependencies are installed:
#     `bundle list`
#
#  3. Run irb inside the repo directory.
#     `bundle exec irb`
#
#  4. Load this file.
#     `irb(main):001:0> load ./repxe_host.rb`
#

require 'chef/provisioning/transport/ssh'
require 'mixlib/shellout'
require 'pry'
require 'timeout'
require 'optparse'

def cluster_assign_roles(environment,type,entry=nil)
  types = [ 'basic', 'hadoop', 'kafka' ]
  if(!types.include?(type.to_s.downcase))
    raise "#{type} is not one of #{types.join(",")} !"
  end

  # We use system() instead of Mixlib::ShellOut specifically so that the
  # child process re-uses our STDOUT/STDERR.
  if entry.nil?
    system('sudo', './cluster-assign-roles.sh',
           environment, type.to_s.downcase.capitalize)
  else
    system('sudo', './cluster-assign-roles.sh',
           environment, type.to_s.downcase.capitalize, entry['hostname'])
  end
  if !$?.success?
    puts "cluster-assign-roles.sh failed!"
  end
end

def restart_chef_server()
  c = Mixlib::ShellOut.new('sudo', 'chef-server-ctl' ,'restart')
  c.run_command
  if !c.status.success?
    raise "Failed to restart chef-server"
  end

  puts "restarted chef-server"
end
def cobbler_unenroll(entry)
  c = Mixlib::ShellOut.new('sudo', 'cobbler' ,'system', 'remove',
                           '--name', entry['hostname'])
  c.run_command
  if !c.status.success?
    raise "Failed to un-enroll #{entry['hostname']}!"
  end

  puts "Un-enrolled #{entry['hostname']} from cobbler"
end

def cobbler_enroll(entry)
  c = Mixlib::ShellOut.new('sudo', 'cobbler', 'system', 'add',
                           '--name', entry['hostname'],
                           '--hostname', fqdn(entry),
                           '--profile', entry['cobbler_profile'],
                           '--ip-address', entry['ip_address'],
                           '--interface=eth0',
                           '--mac', corrected_mac(entry))

  c.run_command
  if !c.status.success?
    raise "Failed to enroll #{entry['hostname']}!"
  end

  puts "Enrolled #{entry['hostname']} in cobbler"
end

# This is mostly copy/pasted out of BACH-next chef helpers.
def cobbler_root_password      
  require 'json'
  require 'mixlib/shellout'
  
  vault_command =
    Mixlib::ShellOut.new('sudo',
                         'knife', 'vault', 'show',
                         'os', 'cobbler',
                         '-F', 'json',
                         '-p', 'all',
                         '-m', 'client')
  
  vault_command.run_command
  
  if !vault_command.status.success?
    raise 'Could not retrieve cobbler password!\n' +
      vault_command.stdout + '\n' +
{'fraction_non_alphanumeric': 0.1339331619537275, 'fraction_numerical': 0.0034704370179948585, 'mean_word_length': 2.6056533827618167, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1491566', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2523, 'n_words': 741}