<commit_before>
$:.push(File.expand_path(File.join(Rails.root, %w{.. lib Scvmm})))

class EmsMicrosoft < EmsInfra
  include_concern "Powershell"

  def self.ems_type
    @ems_type ||= "scvmm".freeze
  end

  def self.description
    @description ||= "Microsoft System Center VMM".freeze
  end

  def self.raw_connect(username, password, auth_url)
    # HACK: WinRM depends on the gssapi gem for encryption purposes.
    # The gssapi code outputs the following warning:
    #   WARNING: Could not load IOV methods. Check your GSSAPI C library for an update
    #   WARNING: Could not load AEAD methods. Check your GSSAPI C library for an update
    # After much googling, this warning is considered benign and can be ignored.
    # Please note - the webmock gem depends on gssapi too and prints out the
    # above warning when rspec tests are run.
    silence_warnings { require 'winrm' }

    WinRM::WinRMWebService.new(auth_url, :ssl, :user => username, :pass => password, :disable_sspi => true)
  end

  # Utilize URI::Generic#hostname to add support for IPv6 literals
  # TODO: simplify this once https://github.com/ruby/ruby/pull/765 lands in our ruby
  def self.auth_url(hostname, port = nil)
    port ||= 5985
    require 'uri'
    uri = URI::HTTP.build(:port => port, :path => "/wsman")
    uri.hostname = hostname
    uri.to_s
  end

  def connect(options = {})
    raise "no credentials defined" if self.missing_credentials?(options[:auth_type])

    username = options[:user] || authentication_userid(options[:auth_type])
    password = options[:pass] || authentication_password(options[:auth_type])
    hostname = options[:hostname] || self.hostname
    auth_url = self.class.auth_url(hostname, port)
    self.class.raw_connect(username, password, auth_url)
  end

  def verify_credentials(_auth_type = nil, options = {})
    raise MiqException::MiqHostError, "No credentials defined" if self.missing_credentials?(options[:auth_type])

    begin
      run_dos_command("hostname")
    rescue WinRM::WinRMHTTPTransportError => e # Error 401
      raise MiqException::MiqHostError, "Check credentials and WinRM configuration settings. " \
      "Remote error message: #{e.message}"
    end

    true
  end

  def vm_start(vm, _options = {})
    case vm.power_state
    when "suspended" then execute_power_operation("Resume", vm.uid_ems)
    when "off"       then execute_power_operation("Start", vm.uid_ems)
    end
  end

  def vm_stop(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Force")
  end

  def vm_shutdown_guest(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Shutdown")
  end

  def vm_reset(vm, _options = {})
    execute_power_operation("Reset", vm.uid_ems)
  end

  def vm_reboot_guest(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Shutdown")
    execute_power_operation("Start", vm.uid_ems)
  end

  def vm_suspend(vm, _options = {})
    execute_power_operation("Suspend", vm.uid_ems)
  end

  def vm_resume(vm, _options = {})
    execute_power_operation("Resume", vm.uid_ems)
  end

  private

  def execute_power_operation(cmdlet, vm_uid_ems, *parameters)
    return unless vm_uid_ems.guid?

    params  = parameters.join(" ")
    command = "powershell #{cmdlet}-SCVirtualMachine -VM (Get-SCVirtualMachine -ID #{vm_uid_ems}) #{params}"
    run_dos_command(command)
  end
end<commit_msg>Microsoft VM power function fix.<commit_after>
$:.push(File.expand_path(File.join(Rails.root, %w{.. lib Scvmm})))

class EmsMicrosoft < EmsInfra
  include_concern "Powershell"

  def self.ems_type
    @ems_type ||= "scvmm".freeze
  end

  def self.description
    @description ||= "Microsoft System Center VMM".freeze
  end

  def self.raw_connect(username, password, auth_url)
    # HACK: WinRM depends on the gssapi gem for encryption purposes.
    # The gssapi code outputs the following warning:
    #   WARNING: Could not load IOV methods. Check your GSSAPI C library for an update
    #   WARNING: Could not load AEAD methods. Check your GSSAPI C library for an update
    # After much googling, this warning is considered benign and can be ignored.
    # Please note - the webmock gem depends on gssapi too and prints out the
    # above warning when rspec tests are run.
    silence_warnings { require 'winrm' }

    WinRM::WinRMWebService.new(auth_url, :ssl, :user => username, :pass => password, :disable_sspi => true)
  end

  # Utilize URI::Generic#hostname to add support for IPv6 literals
  # TODO: simplify this once https://github.com/ruby/ruby/pull/765 lands in our ruby
  def self.auth_url(hostname, port = nil)
    port ||= 5985
    require 'uri'
    uri = URI::HTTP.build(:port => port, :path => "/wsman")
    uri.hostname = hostname
    uri.to_s
  end

  def connect(options = {})
    raise "no credentials defined" if self.missing_credentials?(options[:auth_type])

    username = options[:user] || authentication_userid(options[:auth_type])
    password = options[:pass] || authentication_password(options[:auth_type])
    hostname = options[:hostname] || self.hostname
    auth_url = self.class.auth_url(hostname, port)
    self.class.raw_connect(username, password, auth_url)
  end

  def verify_credentials(_auth_type = nil, options = {})
    raise MiqException::MiqHostError, "No credentials defined" if self.missing_credentials?(options[:auth_type])

    begin
      run_dos_command("hostname")
    rescue WinRM::WinRMHTTPTransportError => e # Error 401
      raise MiqException::MiqHostError, "Check credentials and WinRM configuration settings. " \
      "Remote error message: #{e.message}"
    end

    true
  end

  def vm_start(vm, _options = {})
    case vm.power_state
    when "suspended" then execute_power_operation("Resume", vm.uid_ems)
    when "off"       then execute_power_operation("Start", vm.uid_ems)
    end
  end

  def vm_stop(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Force")
  end

  def vm_shutdown_guest(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Shutdown")
  end

  def vm_reset(vm, _options = {})
    execute_power_operation("Reset", vm.uid_ems)
  end

  def vm_reboot_guest(vm, _options = {})
    execute_power_operation("Stop", vm.uid_ems, "-Shutdown")
    execute_power_operation("Start", vm.uid_ems)
  end

  def vm_suspend(vm, _options = {})
    execute_power_operation("Suspend", vm.uid_ems)
  end

  def vm_resume(vm, _options = {})
    execute_power_operation("Resume", vm.uid_ems)
  end

  private

  def execute_power_operation(cmdlet, vm_uid_ems, *parameters)
    return unless vm_uid_ems.guid?

    params  = parameters.join(" ")
    command = "powershell Import-Module VirtualMachineManager; Get-VMMServer #{ipaddress}; #{cmdlet}-SCVirtualMachine -VM (Get-SCVirtualMachine -ID #{vm_uid_ems}) #{params}"
    run_dos_command(command)
  end
end
{'fraction_non_alphanumeric': 0.10990938322128033, 'fraction_numerical': 0.003215434083601286, 'mean_word_length': 3.9658925979680695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '5810188', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2178, 'n_words': 579}