specinfra 2.21.1 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/multiple_backends.rb +46 -0
- data/lib/specinfra.rb +1 -1
- data/lib/specinfra/backend/base.rb +30 -1
- data/lib/specinfra/backend/cmd.rb +2 -2
- data/lib/specinfra/backend/docker.rb +6 -6
- data/lib/specinfra/backend/dockerfile.rb +2 -2
- data/lib/specinfra/backend/exec.rb +5 -5
- data/lib/specinfra/backend/lxc.rb +1 -1
- data/lib/specinfra/backend/powershell/script_helper.rb +4 -4
- data/lib/specinfra/backend/ssh.rb +18 -18
- data/lib/specinfra/backend/telnet.rb +11 -11
- data/lib/specinfra/backend/winrm.rb +2 -2
- data/lib/specinfra/command_factory.rb +64 -58
- data/lib/specinfra/helper/detect_os.rb +14 -2
- data/lib/specinfra/helper/detect_os/aix.rb +1 -1
- data/lib/specinfra/helper/detect_os/alpine.rb +1 -1
- data/lib/specinfra/helper/detect_os/arch.rb +1 -1
- data/lib/specinfra/helper/detect_os/coreos.rb +1 -1
- data/lib/specinfra/helper/detect_os/darwin.rb +1 -1
- data/lib/specinfra/helper/detect_os/debian.rb +1 -1
- data/lib/specinfra/helper/detect_os/esxi.rb +1 -1
- data/lib/specinfra/helper/detect_os/freebsd.rb +1 -1
- data/lib/specinfra/helper/detect_os/gentoo.rb +1 -1
- data/lib/specinfra/helper/detect_os/nixos.rb +1 -1
- data/lib/specinfra/helper/detect_os/openbsd.rb +1 -1
- data/lib/specinfra/helper/detect_os/plamo.rb +1 -1
- data/lib/specinfra/helper/detect_os/redhat.rb +1 -1
- data/lib/specinfra/helper/detect_os/solaris.rb +1 -1
- data/lib/specinfra/helper/detect_os/suse.rb +1 -1
- data/lib/specinfra/version.rb +1 -1
- metadata +2 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0bfa588a9ff12b8acac2ca45a3d330e9cb458bfa
         | 
| 4 | 
            +
              data.tar.gz: 011fd091e8a33237f34ba487a6c1cc59526219fa
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 13debcb36c02720cad205eafec9d4290e4bd48b36aedf581f4b011e754699640f31a235f33d8e7423916740dfc13f1a2941eec8c04b4a1ec1e3ad86d0d1be4e5
         | 
| 7 | 
            +
              data.tar.gz: 2c35a3d831a4d4993aab5fc305d8a756801c839933a4ae75b3945e55766e27057b4f39dade5d3298443939613970853c6663d4710557407981370e4c2f49654e
         | 
| @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            require 'specinfra'
         | 
| 2 | 
            +
            require 'pp'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            a = Specinfra::Backend::Exec.new
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            ssh_options = Net::SSH::Config.for(ENV['SSH_HOST'], [ENV['SSH_CONFIG']])
         | 
| 7 | 
            +
            b = Specinfra::Backend::Ssh.new(
         | 
| 8 | 
            +
              :host => ssh_options[:host_name],
         | 
| 9 | 
            +
              :ssh_options => ssh_options,
         | 
| 10 | 
            +
            )
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            threads = [a, b].map do |backend|
         | 
| 13 | 
            +
              Thread.start(backend) do |backend|
         | 
| 14 | 
            +
                result = []
         | 
| 15 | 
            +
                result << backend.run_command("uname -a")
         | 
| 16 | 
            +
                result << backend.command
         | 
| 17 | 
            +
                result << backend.command.get(:install_package, 'dstat')
         | 
| 18 | 
            +
                result
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            threads.each(&:join)
         | 
| 23 | 
            +
            threads.each do |t|
         | 
| 24 | 
            +
              pp t.value
         | 
| 25 | 
            +
            end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            # example:
         | 
| 28 | 
            +
            # [#<Specinfra::CommandResult:0x007fb3b1352e30
         | 
| 29 | 
            +
            #   @exit_signal=nil,
         | 
| 30 | 
            +
            #   @exit_status=0,
         | 
| 31 | 
            +
            #   @stderr="",
         | 
| 32 | 
            +
            #   @stdout=
         | 
| 33 | 
            +
            #    "Darwin p411 14.1.0 Darwin Kernel Version 14.1.0: Thu Feb 26 19:26:47 PST 2015; root:xnu-2782.10.73~1/RELEASE_X86_64 x86_64\n">,
         | 
| 34 | 
            +
            #  #<Specinfra::CommandFactory:0x007fb3b1153508
         | 
| 35 | 
            +
            #   @os_info={:family=>"darwin", :release=>nil, :arch=>"x86_64"}>,
         | 
| 36 | 
            +
            #  "/usr/local/bin/brew install  'dstat'"]
         | 
| 37 | 
            +
            # [#<Specinfra::CommandResult:0x007fb3b0b9c2e0
         | 
| 38 | 
            +
            #   @exit_signal=nil,
         | 
| 39 | 
            +
            #   @exit_status=0,
         | 
| 40 | 
            +
            #   @stderr="",
         | 
| 41 | 
            +
            #   @stdout=
         | 
| 42 | 
            +
            #    "Linux itamae-trusty 3.13.0-39-generic #66-Ubuntu SMP Tue Oct 28 13:30:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux\n">,
         | 
| 43 | 
            +
            #  #<Specinfra::CommandFactory:0x007fb3b0d4d4b8
         | 
| 44 | 
            +
            #   @os_info={:family=>"ubuntu", :release=>"14.04", :arch=>"x86_64"}>,
         | 
| 45 | 
            +
            #  "DEBIAN_FRONTEND='noninteractive' apt-get -y -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold'  install dstat"]
         | 
| 46 | 
            +
             | 
    
        data/lib/specinfra.rb
    CHANGED
    
    
| @@ -3,7 +3,36 @@ require 'specinfra/command_result' | |
| 3 3 |  | 
| 4 4 | 
             
            module Specinfra::Backend
         | 
| 5 5 | 
             
              class Base
         | 
| 6 | 
            -
                 | 
| 6 | 
            +
                def self.instance
         | 
| 7 | 
            +
                  @instance ||= self.new
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def initialize(config = {})
         | 
| 11 | 
            +
                  @config = config
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def get_config(key)
         | 
| 15 | 
            +
                  @config[key] || Specinfra.configuration.send(key)
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def set_config(key, value)
         | 
| 19 | 
            +
                  @config[key] = value
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def os_info
         | 
| 23 | 
            +
                  return @os_info if @os_info
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  Specinfra::Helper::DetectOs.subclasses.each do |klass|
         | 
| 26 | 
            +
                    if @os_info = klass.new(self).detect
         | 
| 27 | 
            +
                      @os_info[:arch] ||= self.run_command('uname -m').stdout.strip
         | 
| 28 | 
            +
                      return @os_info
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def command
         | 
| 34 | 
            +
                  CommandFactory.new(os_info)
         | 
| 35 | 
            +
                end
         | 
| 7 36 |  | 
| 8 37 | 
             
                def set_example(e)
         | 
| 9 38 | 
             
                  @example = e
         | 
| @@ -6,7 +6,7 @@ module Specinfra | |
| 6 6 | 
             
                  include PowerShell::ScriptHelper
         | 
| 7 7 |  | 
| 8 8 | 
             
                  def run_command(cmd, opts={})
         | 
| 9 | 
            -
                     | 
| 9 | 
            +
                    set_config(:os, { :family => 'windows' })
         | 
| 10 10 | 
             
                    script = create_script(cmd)
         | 
| 11 11 | 
             
                    result = execute_script %Q{#{powershell} -encodedCommand #{encode_script(script)}}
         | 
| 12 12 |  | 
| @@ -39,7 +39,7 @@ module Specinfra | |
| 39 39 | 
             
                  private
         | 
| 40 40 |  | 
| 41 41 | 
             
                  def powershell
         | 
| 42 | 
            -
                    architecture = @example.metadata[:architecture] ||  | 
| 42 | 
            +
                    architecture = @example.metadata[:architecture] || get_config(:architecture)
         | 
| 43 43 |  | 
| 44 44 | 
             
                    case architecture
         | 
| 45 45 | 
             
                    when :i386 then x86_powershell
         | 
| @@ -7,15 +7,15 @@ module Specinfra::Backend | |
| 7 7 | 
             
                    fail "Docker client library is not available. Try installing `docker-api' gem."
         | 
| 8 8 | 
             
                  end
         | 
| 9 9 |  | 
| 10 | 
            -
                  ::Docker.url =  | 
| 10 | 
            +
                  ::Docker.url = get_config(:docker_url)
         | 
| 11 11 |  | 
| 12 | 
            -
                  if image =  | 
| 12 | 
            +
                  if image = get_config(:docker_image)
         | 
| 13 13 | 
             
                    @images = []
         | 
| 14 14 | 
             
                    @base_image = get_or_pull_image(image)
         | 
| 15 15 |  | 
| 16 16 | 
             
                    create_and_start_container
         | 
| 17 17 | 
             
                    ObjectSpace.define_finalizer(self, proc { cleanup_container })
         | 
| 18 | 
            -
                  elsif container =  | 
| 18 | 
            +
                  elsif container = get_config(:docker_container)
         | 
| 19 19 | 
             
                    @container = ::Docker::Container.get(container)
         | 
| 20 20 | 
             
                  else
         | 
| 21 21 | 
             
                    fail 'Please specify docker_image or docker_container.'
         | 
| @@ -61,14 +61,14 @@ module Specinfra::Backend | |
| 61 61 |  | 
| 62 62 | 
             
                  opts.merge!({'OpenStdin' => true})
         | 
| 63 63 |  | 
| 64 | 
            -
                  if path =  | 
| 64 | 
            +
                  if path = get_config(:path)
         | 
| 65 65 | 
             
                    (opts['Env'] ||= []) << "PATH=#{path}"
         | 
| 66 66 | 
             
                  end
         | 
| 67 67 |  | 
| 68 | 
            -
                  env =  | 
| 68 | 
            +
                  env = get_config(:env).to_a.map { |v| v.join('=') }
         | 
| 69 69 | 
             
                  opts['Env'] = opts['Env'].to_a.concat(env)
         | 
| 70 70 |  | 
| 71 | 
            -
                  opts.merge!( | 
| 71 | 
            +
                  opts.merge!(get_config(:docker_container_create_options) || {})
         | 
| 72 72 |  | 
| 73 73 | 
             
                  @container = ::Docker::Container.create(opts)
         | 
| 74 74 | 
             
                  @container.start
         | 
| @@ -4,10 +4,10 @@ module Specinfra::Backend | |
| 4 4 | 
             
                def initialize
         | 
| 5 5 | 
             
                  @lines = []
         | 
| 6 6 | 
             
                  ObjectSpace.define_finalizer(self) {
         | 
| 7 | 
            -
                    if  | 
| 7 | 
            +
                    if get_config(:dockerfile_finalizer).nil?
         | 
| 8 8 | 
             
                      puts @lines
         | 
| 9 9 | 
             
                    else
         | 
| 10 | 
            -
                       | 
| 10 | 
            +
                      get_config(:dockerfile_finalizer).call(@lines)
         | 
| 11 11 | 
             
                    end
         | 
| 12 12 | 
             
                  }
         | 
| 13 13 | 
             
                end
         | 
| @@ -30,11 +30,11 @@ module Specinfra::Backend | |
| 30 30 | 
             
                end
         | 
| 31 31 |  | 
| 32 32 | 
             
                def build_command(cmd)
         | 
| 33 | 
            -
                  shell =  | 
| 33 | 
            +
                  shell = get_config(:shell) || '/bin/sh'
         | 
| 34 34 | 
             
                  cmd = cmd.shelljoin if cmd.is_a?(Array)
         | 
| 35 35 | 
             
                  cmd = "#{shell.shellescape} -c #{cmd.to_s.shellescape}"
         | 
| 36 36 |  | 
| 37 | 
            -
                  path =  | 
| 37 | 
            +
                  path = get_config(:path)
         | 
| 38 38 | 
             
                  if path
         | 
| 39 39 | 
             
                    cmd = %Q{env PATH="#{path}" #{cmd}}
         | 
| 40 40 | 
             
                  end
         | 
| @@ -49,7 +49,7 @@ module Specinfra::Backend | |
| 49 49 |  | 
| 50 50 | 
             
                  keys.each { |key| ENV["_SPECINFRA_#{key}"] = ENV[key] ; ENV.delete(key) }
         | 
| 51 51 |  | 
| 52 | 
            -
                  env =  | 
| 52 | 
            +
                  env = get_config(:env) || {}
         | 
| 53 53 | 
             
                  env[:LANG] ||= 'C'
         | 
| 54 54 |  | 
| 55 55 | 
             
                  env.each do |key, value|
         | 
| @@ -68,8 +68,8 @@ module Specinfra::Backend | |
| 68 68 | 
             
                end
         | 
| 69 69 |  | 
| 70 70 | 
             
                def add_pre_command(cmd)
         | 
| 71 | 
            -
                  if  | 
| 72 | 
            -
                    pre_cmd = build_command( | 
| 71 | 
            +
                  if get_config(:pre_command)
         | 
| 72 | 
            +
                    pre_cmd = build_command(get_config(:pre_command))
         | 
| 73 73 | 
             
                    "#{pre_cmd} && #{cmd}"
         | 
| 74 74 | 
             
                  else
         | 
| 75 75 | 
             
                    cmd
         | 
| @@ -5,7 +5,7 @@ module Specinfra | |
| 5 5 | 
             
                module PowerShell
         | 
| 6 6 | 
             
                  module ScriptHelper
         | 
| 7 7 | 
             
                    def build_command(cmd)
         | 
| 8 | 
            -
                      path =  | 
| 8 | 
            +
                      path = get_config(:path)
         | 
| 9 9 | 
             
                      if path
         | 
| 10 10 | 
             
                        cmd.strip!
         | 
| 11 11 | 
             
                        cmd = 
         | 
| @@ -18,12 +18,12 @@ EOF | |
| 18 18 | 
             
                    end
         | 
| 19 19 |  | 
| 20 20 | 
             
                    def add_pre_command(cmd)
         | 
| 21 | 
            -
                      path =  | 
| 22 | 
            -
                      if  | 
| 21 | 
            +
                      path = get_config(:path)
         | 
| 22 | 
            +
                      if get_config(:pre_command)
         | 
| 23 23 | 
             
                        cmd.strip!
         | 
| 24 24 | 
             
                        cmd = 
         | 
| 25 25 | 
             
            <<-EOF
         | 
| 26 | 
            -
            if (#{ | 
| 26 | 
            +
            if (#{get_config(:pre_command)})
         | 
| 27 27 | 
             
            {
         | 
| 28 28 | 
             
            #{cmd}
         | 
| 29 29 | 
             
            }
         | 
| @@ -45,10 +45,10 @@ module Specinfra::Backend | |
| 45 45 | 
             
                end
         | 
| 46 46 |  | 
| 47 47 | 
             
                def with_env
         | 
| 48 | 
            -
                  env =  | 
| 48 | 
            +
                  env = get_config(:env) || {}
         | 
| 49 49 | 
             
                  env[:LANG] ||= 'C'
         | 
| 50 50 |  | 
| 51 | 
            -
                  ssh_options =  | 
| 51 | 
            +
                  ssh_options = get_config(:ssh_options) || {}
         | 
| 52 52 | 
             
                  ssh_options[:send_env] ||= []
         | 
| 53 53 |  | 
| 54 54 | 
             
                  env.each do |key, value|
         | 
| @@ -68,14 +68,14 @@ module Specinfra::Backend | |
| 68 68 |  | 
| 69 69 | 
             
                def create_ssh
         | 
| 70 70 | 
             
                  Net::SSH.start(
         | 
| 71 | 
            -
                     | 
| 72 | 
            -
                     | 
| 73 | 
            -
                     | 
| 71 | 
            +
                    get_config(:host),
         | 
| 72 | 
            +
                    get_config(:ssh_options)[:user],
         | 
| 73 | 
            +
                    get_config(:ssh_options)
         | 
| 74 74 | 
             
                  )
         | 
| 75 75 | 
             
                end
         | 
| 76 76 |  | 
| 77 77 | 
             
                def create_scp
         | 
| 78 | 
            -
                  ssh =  | 
| 78 | 
            +
                  ssh = get_config(:ssh)
         | 
| 79 79 | 
             
                  if ssh.nil?
         | 
| 80 80 | 
             
                    ssh = create_ssh
         | 
| 81 81 | 
             
                  end
         | 
| @@ -83,12 +83,12 @@ module Specinfra::Backend | |
| 83 83 | 
             
                end
         | 
| 84 84 |  | 
| 85 85 | 
             
                def scp_upload!(from, to, opt={})
         | 
| 86 | 
            -
                  if  | 
| 87 | 
            -
                     | 
| 86 | 
            +
                  if get_config(:scp).nil?
         | 
| 87 | 
            +
                    set_config(:scp, create_scp)
         | 
| 88 88 | 
             
                  end
         | 
| 89 89 |  | 
| 90 90 | 
             
                  tmp = File.join('/tmp', File.basename(to))
         | 
| 91 | 
            -
                  scp =  | 
| 91 | 
            +
                  scp = get_config(:scp)
         | 
| 92 92 | 
             
                  scp.upload!(from, tmp, opt)
         | 
| 93 93 | 
             
                  run_command(Specinfra.command.get(:move_file, tmp, to))
         | 
| 94 94 | 
             
                end
         | 
| @@ -100,13 +100,13 @@ module Specinfra::Backend | |
| 100 100 | 
             
                  exit_signal = nil
         | 
| 101 101 | 
             
                  retry_prompt = /^Sorry, try again/
         | 
| 102 102 |  | 
| 103 | 
            -
                  if  | 
| 104 | 
            -
                     | 
| 103 | 
            +
                  if get_config(:ssh).nil?
         | 
| 104 | 
            +
                    set_config(:ssh, create_ssh)
         | 
| 105 105 | 
             
                  end
         | 
| 106 106 |  | 
| 107 | 
            -
                  ssh =  | 
| 107 | 
            +
                  ssh = get_config(:ssh)
         | 
| 108 108 | 
             
                  ssh.open_channel do |channel|
         | 
| 109 | 
            -
                    if  | 
| 109 | 
            +
                    if get_config(:sudo_password) or get_config(:request_pty)
         | 
| 110 110 | 
             
                      channel.request_pty do |ch, success|
         | 
| 111 111 | 
             
                        abort "Could not obtain pty " if !success
         | 
| 112 112 | 
             
                      end
         | 
| @@ -117,7 +117,7 @@ module Specinfra::Backend | |
| 117 117 | 
             
                        if data.match retry_prompt
         | 
| 118 118 | 
             
                          abort 'Wrong sudo password! Please confirm your password.'
         | 
| 119 119 | 
             
                        elsif data.match /^#{prompt}/
         | 
| 120 | 
            -
                          channel.send_data "#{ | 
| 120 | 
            +
                          channel.send_data "#{get_config(:sudo_password)}\n"
         | 
| 121 121 | 
             
                        else
         | 
| 122 122 | 
             
                          stdout_data += data
         | 
| 123 123 | 
             
                        end
         | 
| @@ -149,13 +149,13 @@ module Specinfra::Backend | |
| 149 149 | 
             
                end
         | 
| 150 150 |  | 
| 151 151 | 
             
                def sudo
         | 
| 152 | 
            -
                  if sudo_path =  | 
| 152 | 
            +
                  if sudo_path = get_config(:sudo_path)
         | 
| 153 153 | 
             
                    sudo_path += '/sudo'
         | 
| 154 154 | 
             
                  else
         | 
| 155 155 | 
             
                    sudo_path = 'sudo'
         | 
| 156 156 | 
             
                  end
         | 
| 157 157 |  | 
| 158 | 
            -
                  sudo_options =  | 
| 158 | 
            +
                  sudo_options = get_config(:sudo_options)
         | 
| 159 159 | 
             
                  if sudo_options
         | 
| 160 160 | 
             
                    sudo_options = sudo_options.shelljoin if sudo_options.is_a?(Array)
         | 
| 161 161 | 
             
                    sudo_options = ' ' + sudo_options
         | 
| @@ -165,8 +165,8 @@ module Specinfra::Backend | |
| 165 165 | 
             
                end
         | 
| 166 166 |  | 
| 167 167 | 
             
                def sudo?
         | 
| 168 | 
            -
                  user =  | 
| 169 | 
            -
                  disable_sudo =  | 
| 168 | 
            +
                  user = get_config(:ssh_options)[:user]
         | 
| 169 | 
            +
                  disable_sudo = get_config(:disable_sudo)
         | 
| 170 170 | 
             
                  user != 'root' && !disable_sudo
         | 
| 171 171 | 
             
                end
         | 
| 172 172 | 
             
              end
         | 
| @@ -29,7 +29,7 @@ module Specinfra::Backend | |
| 29 29 | 
             
                end
         | 
| 30 30 |  | 
| 31 31 | 
             
                def with_env
         | 
| 32 | 
            -
                  env =  | 
| 32 | 
            +
                  env = get_config(:env) || {}
         | 
| 33 33 | 
             
                  env[:LANG] ||= 'C'
         | 
| 34 34 |  | 
| 35 35 | 
             
                  env.each do |key, value|
         | 
| @@ -47,8 +47,8 @@ module Specinfra::Backend | |
| 47 47 | 
             
                end
         | 
| 48 48 |  | 
| 49 49 | 
             
                def add_pre_command(cmd)
         | 
| 50 | 
            -
                  if  | 
| 51 | 
            -
                    pre_cmd = build_command( | 
| 50 | 
            +
                  if get_config(:pre_command)
         | 
| 51 | 
            +
                    pre_cmd = build_command(get_config(:pre_command))
         | 
| 52 52 | 
             
                    "#{pre_cmd} && #{cmd}"
         | 
| 53 53 | 
             
                  else
         | 
| 54 54 | 
             
                    cmd
         | 
| @@ -61,10 +61,10 @@ module Specinfra::Backend | |
| 61 61 | 
             
                  exit_status = nil
         | 
| 62 62 | 
             
                  exit_signal = nil
         | 
| 63 63 | 
             
                  retry_prompt = /^Login: /
         | 
| 64 | 
            -
                  if  | 
| 65 | 
            -
                     | 
| 64 | 
            +
                  if get_config(:telnet).nil?
         | 
| 65 | 
            +
                    set_config(:telnet, create_telnet)
         | 
| 66 66 | 
             
                  end
         | 
| 67 | 
            -
                  telnet =  | 
| 67 | 
            +
                  telnet = get_config(:telnet)
         | 
| 68 68 | 
             
                  re = [] 
         | 
| 69 69 | 
             
                  unless telnet.nil?
         | 
| 70 70 | 
             
                    re = telnet.cmd( "#{command}; echo $?" ).split("\n")[0..-2]
         | 
| @@ -75,10 +75,10 @@ module Specinfra::Backend | |
| 75 75 | 
             
                end
         | 
| 76 76 |  | 
| 77 77 | 
             
                def create_telnet
         | 
| 78 | 
            -
                  tel = Net::Telnet.new( "Host" =>  | 
| 78 | 
            +
                  tel = Net::Telnet.new( "Host" => get_config(:host) )
         | 
| 79 79 | 
             
                  tel.login( 
         | 
| 80 | 
            -
                    "Name" =>  | 
| 81 | 
            -
                    "Password" =>  | 
| 80 | 
            +
                    "Name" => get_config(:telnet_options)[:user], 
         | 
| 81 | 
            +
                    "Password" => get_config(:telnet_options)[:pass]
         | 
| 82 82 | 
             
                  )
         | 
| 83 83 | 
             
                  tel
         | 
| 84 84 | 
             
                rescue
         | 
| @@ -86,13 +86,13 @@ module Specinfra::Backend | |
| 86 86 | 
             
                end
         | 
| 87 87 |  | 
| 88 88 | 
             
                def sudo
         | 
| 89 | 
            -
                  if sudo_path =  | 
| 89 | 
            +
                  if sudo_path = get_config(:sudo_path)
         | 
| 90 90 | 
             
                    sudo_path += '/sudo'
         | 
| 91 91 | 
             
                  else
         | 
| 92 92 | 
             
                    sudo_path = 'sudo'
         | 
| 93 93 | 
             
                  end
         | 
| 94 94 |  | 
| 95 | 
            -
                  sudo_options =  | 
| 95 | 
            +
                  sudo_options = get_config(:sudo_options)
         | 
| 96 96 | 
             
                  if sudo_options
         | 
| 97 97 | 
             
                    sudo_options = sudo_options.shelljoin if sudo_options.is_a?(Array)
         | 
| 98 98 | 
             
                    sudo_options = ' ' + sudo_options
         | 
| @@ -3,9 +3,9 @@ module Specinfra::Backend | |
| 3 3 | 
             
                include PowerShell::ScriptHelper
         | 
| 4 4 |  | 
| 5 5 | 
             
                def run_command(cmd, opts={})
         | 
| 6 | 
            -
                   | 
| 6 | 
            +
                  set_config(:os, {:family => 'windows'})
         | 
| 7 7 | 
             
                  script = create_script(cmd)
         | 
| 8 | 
            -
                  winrm =  | 
| 8 | 
            +
                  winrm = get_config(:winrm)
         | 
| 9 9 |  | 
| 10 10 | 
             
                  result = winrm.powershell(script)
         | 
| 11 11 | 
             
                  stdout, stderr = [:stdout, :stderr].map do |s|
         | 
| @@ -1,77 +1,83 @@ | |
| 1 1 | 
             
            class Specinfra::CommandFactory
         | 
| 2 | 
            -
               | 
| 3 | 
            -
                @@types = nil
         | 
| 2 | 
            +
              @@types = nil
         | 
| 4 3 |  | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
                  method += "_#{subaction}" if subaction
         | 
| 9 | 
            -
                  command_class = create_command_class(resource_type)
         | 
| 10 | 
            -
                  if command_class.respond_to?(method)
         | 
| 11 | 
            -
                    command_class.send(method, *args)
         | 
| 12 | 
            -
                  else
         | 
| 13 | 
            -
                    raise NotImplementedError.new("#{method} is not implemented in #{command_class}")
         | 
| 14 | 
            -
                  end
         | 
| 15 | 
            -
                end
         | 
| 4 | 
            +
              def self.instance
         | 
| 5 | 
            +
                self.new(os)
         | 
| 6 | 
            +
              end
         | 
| 16 7 |  | 
| 17 | 
            -
             | 
| 18 | 
            -
                 | 
| 19 | 
            -
             | 
| 20 | 
            -
                  version = os[:release] ? "V#{os[:release].to_i}" : nil
         | 
| 8 | 
            +
              def initialize(os_info)
         | 
| 9 | 
            +
                @os_info = os_info
         | 
| 10 | 
            +
              end
         | 
| 21 11 |  | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 12 | 
            +
              def get(meth, *args)
         | 
| 13 | 
            +
                action, resource_type, subaction = breakdown(meth)
         | 
| 14 | 
            +
                method =  action
         | 
| 15 | 
            +
                method += "_#{subaction}" if subaction
         | 
| 16 | 
            +
                command_class = create_command_class(resource_type)
         | 
| 17 | 
            +
                if command_class.respond_to?(method)
         | 
| 18 | 
            +
                  command_class.send(method, *args)
         | 
| 19 | 
            +
                else
         | 
| 20 | 
            +
                  raise NotImplementedError.new("#{method} is not implemented in #{command_class}")
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 25 23 |  | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
                  elsif family != 'base' && version.nil?
         | 
| 35 | 
            -
                    version_class = os_class.const_get('Base')
         | 
| 36 | 
            -
                  end
         | 
| 24 | 
            +
              private
         | 
| 25 | 
            +
              def create_command_class(resource_type)
         | 
| 26 | 
            +
                family  = @os_info[:family]
         | 
| 27 | 
            +
                version = @os_info[:release] ? "V#{@os_info[:release].to_i}" : nil
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                common_class = Specinfra::Command
         | 
| 30 | 
            +
                base_class   = common_class.const_get('Base')
         | 
| 31 | 
            +
                os_class     = family.nil? ? base_class : common_class.const_get(family.capitalize)
         | 
| 37 32 |  | 
| 33 | 
            +
                if family && version
         | 
| 38 34 | 
             
                  begin
         | 
| 39 | 
            -
                     | 
| 35 | 
            +
                    version_class = os_class.const_get(version)
         | 
| 40 36 | 
             
                  rescue
         | 
| 37 | 
            +
                    version_class = os_class.const_get('Base')
         | 
| 41 38 | 
             
                  end
         | 
| 39 | 
            +
                elsif family.nil?
         | 
| 40 | 
            +
                  version_class = os_class
         | 
| 41 | 
            +
                elsif family != 'base' && version.nil?
         | 
| 42 | 
            +
                  version_class = os_class.const_get('Base')
         | 
| 43 | 
            +
                end
         | 
| 42 44 |  | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 45 | 
            +
                begin
         | 
| 46 | 
            +
                  command_class = version_class.const_get(resource_type.to_camel_case)
         | 
| 47 | 
            +
                rescue
         | 
| 48 | 
            +
                end
         | 
| 46 49 |  | 
| 47 | 
            -
             | 
| 50 | 
            +
                if command_class.nil? ||( (command_class < Specinfra::Command::Base).nil? && (command_class < Specinfra::Command::Windows::Base).nil? )
         | 
| 51 | 
            +
                  command_class = base_class.const_get(resource_type.to_camel_case)
         | 
| 48 52 | 
             
                end
         | 
| 49 53 |  | 
| 50 | 
            -
                 | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
                   | 
| 60 | 
            -
                   | 
| 54 | 
            +
                command_class.create
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              def breakdown(meth)
         | 
| 58 | 
            +
                # Somtimes `selinux_module' type matches `selinux' and error occurs.
         | 
| 59 | 
            +
                # Reverse sorting is needed to avoid this problem.
         | 
| 60 | 
            +
                types = resource_types.map {|t| t.to_snake_case }.sort.reverse.join('|')
         | 
| 61 | 
            +
                md = meth.to_s.match(/^([^_]+)_(#{types})_?(.+)?$/)
         | 
| 62 | 
            +
                if md.nil?
         | 
| 63 | 
            +
                  message =  "Could not break down `#{meth}' to appropriate type and method.\n"
         | 
| 64 | 
            +
                  message += "The method name shoud be in the form of `action_type_subaction'."
         | 
| 65 | 
            +
                  raise message
         | 
| 61 66 | 
             
                end
         | 
| 67 | 
            +
                return md[1], md[2], md[3]
         | 
| 68 | 
            +
              end
         | 
| 62 69 |  | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
                    end
         | 
| 72 | 
            -
                    @@types.uniq!
         | 
| 70 | 
            +
              def resource_types
         | 
| 71 | 
            +
                if @@types.nil?
         | 
| 72 | 
            +
                  @@types = []
         | 
| 73 | 
            +
                  Specinfra::Command::Base.subclasses.each do |s|
         | 
| 74 | 
            +
                    @@types << s.to_s.split(':')[-1]
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
                  Specinfra::Command::Windows::Base.subclasses.each do |s|
         | 
| 77 | 
            +
                    @@types << s.to_s.split(':')[-1]
         | 
| 73 78 | 
             
                  end
         | 
| 74 | 
            -
                  @@types
         | 
| 79 | 
            +
                  @@types.uniq!
         | 
| 75 80 | 
             
                end
         | 
| 81 | 
            +
                @@types
         | 
| 76 82 | 
             
              end
         | 
| 77 83 | 
             
            end
         | 
| @@ -1,7 +1,19 @@ | |
| 1 1 | 
             
            module Specinfra::Helper
         | 
| 2 2 | 
             
              class DetectOs
         | 
| 3 | 
            -
                def self. | 
| 4 | 
            -
                  Specinfra.backend. | 
| 3 | 
            +
                def self.detect
         | 
| 4 | 
            +
                  self.new(Specinfra.backend).detect
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize(backend)
         | 
| 8 | 
            +
                  @backend = backend
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def run_command(cmd)
         | 
| 12 | 
            +
                  @backend.run_command(cmd)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def detect
         | 
| 16 | 
            +
                  raise NotImplementedError
         | 
| 5 17 | 
             
                end
         | 
| 6 18 | 
             
              end
         | 
| 7 19 | 
             
            end
         | 
    
        data/lib/specinfra/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: specinfra
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2. | 
| 4 | 
            +
              version: 2.22.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Gosuke Miyashita
         | 
| @@ -109,6 +109,7 @@ files: | |
| 109 109 | 
             
            - LICENSE.txt
         | 
| 110 110 | 
             
            - README.md
         | 
| 111 111 | 
             
            - Rakefile
         | 
| 112 | 
            +
            - examples/multiple_backends.rb
         | 
| 112 113 | 
             
            - lib/specinfra.rb
         | 
| 113 114 | 
             
            - lib/specinfra/backend.rb
         | 
| 114 115 | 
             
            - lib/specinfra/backend/base.rb
         |