<commit_before># Xapi Base Module
#
# Description:: Setup the Session and auth for xapi
#   other common methods used for talking with the xapi
#
# Author:: <NAME> <<EMAIL>>
#
# Copyright:: Copyright (c) 2012, <NAME>
#
# License:: Apache License, Version 2.0
#
# 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.


require 'chef/knife'
require 'units/standard'
require 'xenapi'

class Chef::Knife
    module XapiBase

    attr :defaults

    def self.included(includer)
      includer.class_eval do

        deps do
          require 'xenapi'
          require 'highline'
          require 'highline/import'
          require 'readline'
        end

        option :xapi_host,
          :short => "-h SERVER_URL",
          :long => "--host SERVER_URL",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_host] = key },
          :description => "The url to the xenserver, http://somehost.local.lan/"

        option :xapi_password,
          :short => "-K PASSWORD",
          :long => "--xapi-password PASSWORD",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_password] = key },
          :description => "Your xenserver password"

        option :xapi_username,
          :short => "-A USERNAME",
          :long => "--xapi-username USERNAME",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_username] = key },
          :description => "Your xenserver username"

        option :domain,
          :short => "-f Name",
          :long => "--domain Name",
          :description => "the domain name for the guest",
          :proc => Proc.new { |key| Chef::Config[:knife][:domain] = key }
      end
    end



    def self.set_defaults(config)
      @defaults ||= Hash.new
      @defaults.merge!(config)
    end

    # set the default template
    self.set_defaults( { :template_regex =>  /^CentOS 5.*\(64-bit\)/ } )

    def self.defaults
      @defaults ||= Hash.new
    end

    def self.get_default(key)
      @defaults[key] || nil
    end

    def h
      @highline ||= ui.highline
    end

    # setup and return an authed xen api instance
    def xapi
      @xapi ||= begin

        ui.fatal "Must provide a xapi host with --host "unless locate_config_value(:xapi_host)
        session = XenApi::Client.new(  locate_config_value(:xapi_host) )

        # get the password from the user
        password =  locate_config_value(:xapi_password) || nil
        username =  locate_config_value(:xapi_username) || "root"
        if password.nil?  or password.empty?
          password = h.ask("Enter password for user #{username}:  " ) { |input| input.echo = "*" }
        end
        session.login_with_password(username, password)

        session
      end
    end

    def locate_config_value(key)
      key = key.to_sym
      config[key] || Chef::Config[:knife][key]  || Chef::Knife::XapiBase.get_default(key)
    end

    # get template by name_label
    def get_template(template)
      xapi.VM.get_by_name_label(template).first
    end

    #
    # find a template matching what the user provided
    #
    # returns a ref to the vm or nil if nothing found
    #
    def find_template(template)
      template = locate_config_value(:template_regex) if template.nil?
      # if we got a string then try to find that template exact
      #  if no exact template matches, search
      if template.is_a?(String)
        found = get_template(template)
        return found if found
      end

      #  make sure our nil template gets set to default
      if template.nil?
        template = locate_config_value(:template_regex)
      end

      Chef::Log.debug "Name: #{template.class}"
      # quick and dirty string to regex
      unless template.is_a?(Regexp)
        template = /#{template}/
      end

      # loop over all vm's and find the template
      # Wish there was a better API method for this, and there might be
      #  but i couldn't find it
      Chef::Log.debug "Using regex: #{template}"
      xapi.VM.get_all_records().each_value do |vm|
        if vm["is_a_template"] and  vm["name_label"] =~ template
          Chef::Log.debug "Matched: #{h.color(vm["name_label"], :yellow )}"
          found = vm # we're gonna go with the last found
        end
      end

      # ensure return values
      if found
        ui.msg "Using Template: #{h.color(found["name_label"], :cyan)}"
        return get_template(found["name_label"]) # get the ref to this one
      end
      return nil
    end

    # present a list of options for a user to select
    # return the selected item
    def user_select(items)
      choose do |menu|
        menu.index  = :number
        menu.prompt = "Please Choose One:"<commit_msg>fix choose bare method<commit_after># Xapi Base Module
#
# Description:: Setup the Session and auth for xapi
#   other common methods used for talking with the xapi
#
# Author:: <NAME> <<EMAIL>>
#
# Copyright:: Copyright (c) 2012, <NAME>
#
# License:: Apache License, Version 2.0
#
# 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.


require 'chef/knife'
require 'units/standard'
require 'xenapi'

class Chef::Knife
    module XapiBase

    attr :defaults

    def self.included(includer)
      includer.class_eval do

        deps do
          require 'xenapi'
          require 'highline'
          require 'highline/import'
          require 'readline'
        end

        option :xapi_host,
          :short => "-h SERVER_URL",
          :long => "--host SERVER_URL",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_host] = key },
          :description => "The url to the xenserver, http://somehost.local.lan/"

        option :xapi_password,
          :short => "-K PASSWORD",
          :long => "--xapi-password PASSWORD",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_password] = key },
          :description => "Your xenserver password"

        option :xapi_username,
          :short => "-A USERNAME",
          :long => "--xapi-username USERNAME",
          :proc => Proc.new { |key| Chef::Config[:knife][:xapi_username] = key },
          :description => "Your xenserver username"

        option :domain,
          :short => "-f Name",
          :long => "--domain Name",
          :description => "the domain name for the guest",
          :proc => Proc.new { |key| Chef::Config[:knife][:domain] = key }
      end
    end



    def self.set_defaults(config)
      @defaults ||= Hash.new
      @defaults.merge!(config)
    end

    # set the default template
    self.set_defaults( { :template_regex =>  /^CentOS 5.*\(64-bit\)/ } )

    def self.defaults
      @defaults ||= Hash.new
    end

    def self.get_default(key)
      @defaults[key] || nil
    end

    def h
      @highline ||= ui.highline
    end

    # setup and return an authed xen api instance
    def xapi
      @xapi ||= begin

        ui.fatal "Must provide a xapi host with --host "unless locate_config_value(:xapi_host)
        session = XenApi::Client.new(  locate_config_value(:xapi_host) )

        # get the password from the user
        password =  locate_config_value(:xapi_password) || nil
        username =  locate_config_value(:xapi_username) || "root"
        if password.nil?  or password.empty?
          password = h.ask("Enter password for user #{username}:  " ) { |input| input.echo = "*" }
        end
        session.login_with_password(username, password)

        session
      end
    end

    def locate_config_value(key)
      key = key.to_sym
      config[key] || Chef::Config[:knife][key]  || Chef::Knife::XapiBase.get_default(key)
    end

    # get template by name_label
    def get_template(template)
      xapi.VM.get_by_name_label(template).first
    end

    #
    # find a template matching what the user provided
    #
    # returns a ref to the vm or nil if nothing found
    #
    def find_template(template)
      template = locate_config_value(:template_regex) if template.nil?
      # if we got a string then try to find that template exact
      #  if no exact template matches, search
      if template.is_a?(String)
        found = get_template(template)
        return found if found
      end

      #  make sure our nil template gets set to default
      if template.nil?
        template = locate_config_value(:template_regex)
      end

      Chef::Log.debug "Name: #{template.class}"
      # quick and dirty string to regex
      unless template.is_a?(Regexp)
        template = /#{template}/
      end

      # loop over all vm's and find the template
      # Wish there was a better API method for this, and there might be
      #  but i couldn't find it
      Chef::Log.debug "Using regex: #{template}"
      xapi.VM.get_all_records().each_value do |vm|
        if vm["is_a_template"] and  vm["name_label"] =~ template
          Chef::Log.debug "Matched: #{h.color(vm["name_label"], :yellow )}"
          found = vm # we're gonna go with the last found
        end
      end

      # ensure return values
      if found
        ui.msg "Using Template: #{h.color(found["name_label"], :cyan)}"
        return get_template(found["name_label"]) # get the ref to this one
      end
      return nil
    end

    # present a list of options for a user to select
    # return the selected item
    def user_select(items)
      h.choose do |menu|
        menu.index  = :number
        menu.prompt = "Please Choose One:"
{'fraction_non_alphanumeric': 0.10681488641855968, 'fraction_numerical': 0.0025132914451425807, 'mean_word_length': 2.442928452579035, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '5495934', 'n_tokens_mistral': 3115, 'n_tokens_neox': 3007, 'n_words': 994}