<commit_before>require 'json'
require 'kytoon/util'
require 'rexml/document'
require 'rexml/xpath'
require 'timeout'

module Kytoon

module Providers

module Libvirt
# All in one Libvirt server group provider.
#
# Required setup:
# 1) Libvirt domain XML file or running domain to clone.
#
# 2) Generate an ssh keypair to be injected into the image.
#
class ServerGroup

  KIB_PER_GIG = 1048576

  @@data_dir=File.join(KYTOON_PROJECT, "tmp", "libvirt")

  def self.data_dir
    @@data_dir
  end

  def self.data_dir=(dir)
    @@data_dir=dir
  end

  CONFIG_FILE = KYTOON_PROJECT + File::SEPARATOR + "config" + File::SEPARATOR + "server_group.json"

  attr_accessor :id
  attr_accessor :name
  attr_accessor :use_sudo

  def initialize(options={})
    @id = options[:id] || Time.now.to_f
    @name = options[:name]
    @use_sudo = options[:use_sudo]
    @servers=[]
    end

  def server(name)
    @servers.select {|s| s['hostname'] == name}[0] if @servers.size > 0
  end

  def servers
    @servers
  end

  def gateway_ip
    @servers.select {|s| s['gateway'] == 'true' }[0]['ip_address'] if @servers.size > 0
  end

  # generate a Server Group XML from server_group.json
  def self.from_json(json)

    json_hash=JSON.parse(json)

    configs = Util.load_configs
    use_sudo = ENV['LIBVIRT_USE_SUDO'] || configs['libvirt_use_sudo']

    sg=ServerGroup.new(
      :id => json_hash["id"],
      :name => json_hash["name"],
      :use_sudo => use_sudo
    )
    json_hash["servers"].each do |server_hash|

      sg.servers << {
        'hostname' => server_hash['hostname'],
        'memory' => server_hash['memory'],
        'original' => server_hash['original'],
        'original_xml' => server_hash['original_xml'],
        'create_cow' => server_hash['create_cow'],
        'disk_path' => server_hash['disk_path'],
        'ip_address' => server_hash['ip_address'],
        'gateway' => server_hash['gateway'] || "false"
      }
    end
    return sg
  end

  def pretty_print

    puts "Group ID: #{@id}"<commit_msg>Libvirt: fix libvirt_use_sudo option<commit_after>require 'json'
require 'kytoon/util'
require 'rexml/document'
require 'rexml/xpath'
require 'timeout'

module Kytoon

module Providers

module Libvirt
# All in one Libvirt server group provider.
#
# Required setup:
# 1) Libvirt domain XML file or running domain to clone.
#
# 2) Generate an ssh keypair to be injected into the image.
#
class ServerGroup

  KIB_PER_GIG = 1048576

  @@data_dir=File.join(KYTOON_PROJECT, "tmp", "libvirt")

  def self.data_dir
    @@data_dir
  end

  def self.data_dir=(dir)
    @@data_dir=dir
  end

  CONFIG_FILE = KYTOON_PROJECT + File::SEPARATOR + "config" + File::SEPARATOR + "server_group.json"

  attr_accessor :id
  attr_accessor :name
  attr_accessor :use_sudo

  def initialize(options={})
    @id = options[:id] || Time.now.to_f
    @name = options[:name]
    @use_sudo = options[:use_sudo]
    @servers=[]
    end

  def server(name)
    @servers.select {|s| s['hostname'] == name}[0] if @servers.size > 0
  end

  def servers
    @servers
  end

  def gateway_ip
    @servers.select {|s| s['gateway'] == 'true' }[0]['ip_address'] if @servers.size > 0
  end

  # generate a Server Group XML from server_group.json
  def self.from_json(json)

    json_hash=JSON.parse(json)

    configs = Util.load_configs
    use_sudo = ENV['LIBVIRT_USE_SUDO'] || configs['libvirt_use_sudo'].to_s

    sg=ServerGroup.new(
      :id => json_hash["id"],
      :name => json_hash["name"],
      :use_sudo => use_sudo
    )
    json_hash["servers"].each do |server_hash|

      sg.servers << {
        'hostname' => server_hash['hostname'],
        'memory' => server_hash['memory'],
        'original' => server_hash['original'],
        'original_xml' => server_hash['original_xml'],
        'create_cow' => server_hash['create_cow'],
        'disk_path' => server_hash['disk_path'],
        'ip_address' => server_hash['ip_address'],
        'gateway' => server_hash['gateway'] || "false"
      }
    end
    return sg
  end

  def pretty_print

    puts "Group ID: #{@id}"
{'fraction_non_alphanumeric': 0.1375921375921376, 'fraction_numerical': 0.0063882063882063885, 'mean_word_length': 3.429815016322089, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '7649741', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1427, 'n_words': 326}