methods/in_common.rb
# Routine to execute command
# Prints command if verbose switch is on
# Does not execute cerver/client import/create operations in test mode
Routine to execute command
Prints command if verbose switch is on
Does not execute cerver/client import/create operations in test mode
Routine to execute command Prints command if verbose switch is on Does not execute cerver / client import / create operations in test mode
def execute_command(options,message,command)
  if !command
    handle_output(options,"Warning:\tEmpty command")
    return
  end
  if command.match(/prlctl/) and !options['host-os-name'].to_s.match(/Darwin/)
    return
  else
    if command.match(/prlctl/)
      parallels_test = %x[which prlctl].chomp
      if not parallels_test.match(/prlctl/)
        return
      end
    end
  end
  output  = ""
  execute = 0
  if options['verbose'] == true
    if message.match(/[a-z,A-Z,0-9]/)
      handle_output(options,message)
    end
  end
  if options['test'] == true
    if not command.match(/create|id|groups|update|import|delete|svccfg|rsync|cp|touch|svcadm|VBoxManage|vboxmanage|vmrun|docker/)
      execute = true
    end
  else
    execute = true
  end
  if execute == true
    if options['uid'] != 0
      if !command.match(/brew |sw_vers|id |groups|hg|pip|VBoxManage|vboxmanage|netstat|df|vmrun|noVNC|docker|packer|ansible-playbook|virt-install|qemu|^ls|multipass/) && !options['host-os-name'].to_s.match(/NT/)
        if options['sudo'] == true
          command = "sudo sh -c '"+command+"'"
        else
          if command.match(/ufw|chown|chmod/)
            command = "sudo sh -c '"+command+"'"
          else
            if command.match(/ifconfig/) && command.match(/up$/)
              command = "sudo sh -c '"+command+"'"
            end
          end
        end
      else
        if command.match(/ifconfig/) && command.match(/up$/)
          command = "sudo sh -c '"+command+"'"
        end
        if command.match(/virt-install/)
          command = "sudo sh -c '"+command+"'"
        end
        if command.match(/snap/)
          if !File.exist?("/usr/bin/snap")
            check_apt_package(options,"snapd")
          end
          command = "sudo sh -c '"+command+"'"
        end
        if command.match(/qemu/) && command.match(/chmod|chgrp/)
          command = "sudo sh -c '"+command+"'"
        end
        if options['vm'].to_s.match(/kvm/) && command.match(/libvirt/) && command.match(/ls/)
          command = "sudo sh -c '"+command+"'"
        end
      end
      if options['host-os-name'].to_s.match(/NT/) && command.match(/netsh/)
        batch_file = "/tmp/script.bat"
        File.write(batch_file,command)
        handle_output(options,"Information:\tCreating batch file '#{batch_file}' to run command '#{command}"'')
        command = "cygstart --action=runas "+batch_file
      end
    end
    if command.match(/^sudo/)
      if options['host-os-name'].to_s.match(/Darwin/)
        sudo_check = %x[dscacheutil -q group -a name admin |grep users]
      else
        sudo_check = %x[getent group #{options['sudogroup']}].chomp
      end
      if !sudo_check.match(/#{options['user']}/)
        handle_output(options,"Warning:\tUser #{options['user']} is not in sudoers group")
        exit
      end
    end
    if options['verbose'] == true
      handle_output(options,"Executing:\t#{command}")
    end
    if options['executehost'].to_s.match(/localhost/)
      output = %x[#{command}]
    else
    end
  end
  if options['verbose'] == true
    if output.length > 1
      if not output.match(/\n/)
        handle_output(options,"Output:\t\t#{output}")
      else
        multi_line_output = output.split(/\n/)
        multi_line_output.each do |line|
          handle_output(options,"Output:\t\t#{line}")
        end
      end
    end
  end
  return output
end
def execute_command ( options , message , command ) if ! command handle_output ( options , "Warning:\tEmpty command" ) return end if command . match ( / prlctl / ) and ! options [ 'host-os-name' ] . to_s . match ( / Darwin / ) return else if command . match ( / prlctl / ) parallels_test = %x[ which prlctl ] . chomp if not parallels_test . match ( / prlctl / ) return end end end output = "" execute = 0 if options [ 'verbose' ] == true if message . match ( / [a-z,A-Z,0-9] / ) handle_output ( options , message ) end end if options [ 'test' ] == true if not command . match ( / create|id|groups|update|import|delete|svccfg|rsync|cp|touch|svcadm|VBoxManage|vboxmanage|vmrun|docker / ) execute = true end else execute = true end if execute == true if options [ 'uid' ] != 0 if ! command . match ( / brew |sw_vers|id |groups|hg|pip|VBoxManage|vboxmanage|netstat|df|vmrun|noVNC|docker|packer|ansible-playbook|virt-install|qemu|^ls|multipass / ) && ! options [ 'host-os-name' ] . to_s . match ( / NT / ) if options [ 'sudo' ] == true command = "sudo sh -c '" + command + "'" else if command . match ( / ufw|chown|chmod / ) command = "sudo sh -c '" + command + "'" else if command . match ( / ifconfig / ) && command . match ( / up$ / ) command = "sudo sh -c '" + command + "'" end end end else if command . match ( / ifconfig / ) && command . match ( / up$ / ) command = "sudo sh -c '" + command + "'" end if command . match ( / virt-install / ) command = "sudo sh -c '" + command + "'" end if command . match ( / snap / ) if ! File . exist? ( "/usr/bin/snap" ) check_apt_package ( options , "snapd" ) end command = "sudo sh -c '" + command + "'" end if command . match ( / qemu / ) && command . match ( / chmod|chgrp / ) command = "sudo sh -c '" + command + "'" end if options [ 'vm' ] . to_s . match ( / kvm / ) && command . match ( / libvirt / ) && command . match ( / ls / ) command = "sudo sh -c '" + command + "'" end end if options [ 'host-os-name' ] . to_s . match ( / NT / ) && command . match ( / netsh / ) batch_file = "/tmp/script.bat" File . write ( batch_file , command ) handle_output ( options , "Information:\tCreating batch file '#{batch_file}' to run command '#{command}" '' ) command = "cygstart --action=runas " + batch_file end end if command . match ( / ^sudo / ) if options [ 'host-os-name' ] . to_s . match ( / Darwin / ) sudo_check = %x[ dscacheutil -q group -a name admin |grep users ] else sudo_check = %x[ getent group  #{ options [ 'sudogroup' ] } ] . chomp end if ! sudo_check . match ( / #{ options [ 'user' ] } / ) handle_output ( options , "Warning:\tUser #{options['user']} is not in sudoers group" ) exit end end if options [ 'verbose' ] == true handle_output ( options , "Executing:\t#{command}" ) end if options [ 'executehost' ] . to_s . match ( / localhost / ) output = %x[ #{ command } ] else end end if options [ 'verbose' ] == true if output . length > 1 if not output . match ( / \n / ) handle_output ( options , "Output:\t\t#{output}" ) else multi_line_output = output . split ( / \n / ) multi_line_output . each do | line | handle_output ( options , "Output:\t\t#{line}" ) end end end end return output end
Routine to execute command
Prints command if verbose switch is on
Does not execute cerver/client import/create operations in test mode
Routine to execute command Prints command if verbose switch is on Does not execute cerver / client import / create operations in test mode
#      Net::SSH.start(options['server'], options['serveradmin'], :password => options['serverpassword'], :verify_host_key => "never") do |ssh_session| #        output = ssh_session.exec!(command) #      end
options
<not_specific>
message
<not_specific>
command
<not_specific>