netid-tools 0.4.2 → 0.5.1
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.
- data/README.md +6 -0
- data/VERSION +1 -1
- data/bin/ua_check +7 -2
- data/lib/generic-response.rb +4 -0
- data/lib/netid-tools.rb +73 -62
- data/lib/netid-validator.rb +12 -0
- data/lib/system-connect.rb +57 -0
- data/netid-tools.gemspec +5 -2
- metadata +6 -3
    
        data/README.md
    CHANGED
    
    | @@ -143,6 +143,12 @@ The NetID you want to check | |
| 143 143 | 
             
            Version History
         | 
| 144 144 | 
             
            ===============
         | 
| 145 145 |  | 
| 146 | 
            +
            ## 0.5.0
         | 
| 147 | 
            +
             | 
| 148 | 
            +
            * Switch to new SSH connection method; reuse already existing SSH connection for host if it exists
         | 
| 149 | 
            +
            * Tighten up code
         | 
| 150 | 
            +
            * Fix bugs
         | 
| 151 | 
            +
             | 
| 146 152 | 
             
            ## 0.4.2
         | 
| 147 153 |  | 
| 148 154 | 
             
            * Rename check_quota -> quota_check
         | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0. | 
| 1 | 
            +
            0.5.1
         | 
    
        data/bin/ua_check
    CHANGED
    
    | @@ -23,7 +23,10 @@ raise "No NetID(s) specified. Bailing." if ARGV.empty? | |
| 23 23 |  | 
| 24 24 | 
             
            user = ARGV.map{|n| n.downcase}
         | 
| 25 25 | 
             
            user.each do |netid|
         | 
| 26 | 
            -
               | 
| 26 | 
            +
              unless Netid.validate_netid?(netid)
         | 
| 27 | 
            +
                puts "#{netid} is not a valid NetID! Exiting."
         | 
| 28 | 
            +
                exit 1
         | 
| 29 | 
            +
              end
         | 
| 27 30 | 
             
            end
         | 
| 28 31 |  | 
| 29 32 | 
             
            system_user = `whoami`.chomp
         | 
| @@ -75,7 +78,9 @@ user.each do |netid| | |
| 75 78 |  | 
| 76 79 | 
             
              unless options[:concise]
         | 
| 77 80 | 
             
                puts "\n"
         | 
| 78 | 
            -
                checkr.check_quota
         | 
| 81 | 
            +
                checkr.check_quota.each do |l|
         | 
| 82 | 
            +
                  puts l.join(" ")
         | 
| 83 | 
            +
                end
         | 
| 79 84 | 
             
              end
         | 
| 80 85 | 
             
              puts "\n"
         | 
| 81 86 | 
             
            end
         | 
    
        data/lib/netid-tools.rb
    CHANGED
    
    | @@ -1,94 +1,105 @@ | |
| 1 1 | 
             
            require 'net/ssh'
         | 
| 2 2 | 
             
            require 'colored'
         | 
| 3 | 
            +
            require 'netid-validator'
         | 
| 4 | 
            +
            require 'system-connect'
         | 
| 5 | 
            +
            require 'generic-response'
         | 
| 3 6 |  | 
| 4 7 | 
             
            class Netid
         | 
| 5 | 
            -
               | 
| 8 | 
            +
              include SystemConnect
         | 
| 6 9 |  | 
| 7 | 
            -
               | 
| 10 | 
            +
              attr_accessor :netid, :system_user, :systems, :single_host
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def initialize(netid,system_user=nil,systems=nil,single_host=nil)
         | 
| 8 13 | 
             
                @netid = netid
         | 
| 9 | 
            -
                @system_user = system_user
         | 
| 14 | 
            +
                @system_user = system_user || `whoami`.chomp
         | 
| 15 | 
            +
                @systems = systems || ["ovid01.u.washington.edu",
         | 
| 16 | 
            +
                                       "ovid02.u.washington.edu",
         | 
| 17 | 
            +
                                       "ovid03.u.washington.edu",
         | 
| 18 | 
            +
                                       "vergil.u.washington.edu"
         | 
| 19 | 
            +
                                       ]
         | 
| 20 | 
            +
                @single_host = single_host || "ovid02.u.washington.edu"
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              def validate_netid
         | 
| 24 | 
            +
                NetidValidator.do(netid)
         | 
| 10 25 | 
             
              end
         | 
| 11 26 |  | 
| 12 27 | 
             
              def self.validate_netid?(netid)
         | 
| 13 | 
            -
                 | 
| 14 | 
            -
                  false
         | 
| 15 | 
            -
                else
         | 
| 16 | 
            -
                  true
         | 
| 17 | 
            -
                end
         | 
| 28 | 
            +
                NetidValidator.do(netid).response
         | 
| 18 29 | 
             
              end
         | 
| 19 30 |  | 
| 20 | 
            -
              def validate_netid? | 
| 21 | 
            -
                 | 
| 31 | 
            +
              def validate_netid?
         | 
| 32 | 
            +
                NetidValidator.do(netid).response
         | 
| 22 33 | 
             
              end
         | 
| 23 34 |  | 
| 24 35 | 
             
              def check_for_mysql_presence(host)
         | 
| 25 | 
            -
                 | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
                  end
         | 
| 36 | 
            +
                command = "ps -F -U #{netid} -u #{netid}"
         | 
| 37 | 
            +
                result = run_remote_command(command,host)
         | 
| 38 | 
            +
                if result =~ /mysql/
         | 
| 39 | 
            +
                  /port=(?<port>\d+)/ =~ result
         | 
| 40 | 
            +
                  [host,port]
         | 
| 41 | 
            +
                else
         | 
| 42 | 
            +
                  false
         | 
| 33 43 | 
             
                end
         | 
| 34 44 | 
             
              end
         | 
| 35 45 |  | 
| 36 46 | 
             
              def get_processes(host)
         | 
| 37 | 
            -
                 | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                   | 
| 42 | 
            -
                    output = ssh.exec!("ps -f --user=#{netid}").lines
         | 
| 43 | 
            -
                  end
         | 
| 47 | 
            +
                if /no such user/i =~ run_remote_command("id #{netid}",host)
         | 
| 48 | 
            +
                  result = nil
         | 
| 49 | 
            +
                else
         | 
| 50 | 
            +
                  result = run_remote_command("ps -F --user=#{netid}",host).lines
         | 
| 51 | 
            +
                  result = remove_extra_processes(result)
         | 
| 44 52 | 
             
                end
         | 
| 45 | 
            -
                if  | 
| 53 | 
            +
                if result.nil? || result.count == 1
         | 
| 46 54 | 
             
                  false
         | 
| 47 55 | 
             
                else
         | 
| 48 | 
            -
                   | 
| 56 | 
            +
                  result
         | 
| 49 57 | 
             
                end
         | 
| 50 58 | 
             
              end
         | 
| 51 59 |  | 
| 52 60 | 
             
              def check_for_localhome
         | 
| 53 | 
            -
                 | 
| 54 | 
            -
                 | 
| 55 | 
            -
                   | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
                 end
         | 
| 61 | 
            -
               end
         | 
| 62 | 
            -
             end
         | 
| 61 | 
            +
                result = run_remote_command("cpw -poh #{netid}",single_host)
         | 
| 62 | 
            +
                if result =~ /Unknown/
         | 
| 63 | 
            +
                  false
         | 
| 64 | 
            +
                else
         | 
| 65 | 
            +
                  result.chomp
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
              end
         | 
| 63 68 |  | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
                 | 
| 69 | 
            +
              def check_webtype
         | 
| 70 | 
            +
                result = []
         | 
| 71 | 
            +
                command = "webtype -user #{netid}"
         | 
| 72 | 
            +
                result = run_remote_command(command,single_host).chomp.split
         | 
| 73 | 
            +
                if result[0] == "user"
         | 
| 74 | 
            +
                  result = run_remote_command(command,host).chomp.split(" ")
         | 
| 75 | 
            +
                else
         | 
| 76 | 
            +
                  result
         | 
| 77 | 
            +
                end
         | 
| 68 78 | 
             
              end
         | 
| 69 | 
            -
            end
         | 
| 70 79 |  | 
| 71 80 |  | 
| 72 | 
            -
            def check_quota | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
                result  | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
                    elsif line_components[4] =~ /day/i || line_components[4].to_i > line_components[5].to_i
         | 
| 87 | 
            -
                      puts line.bold.red+'\n'
         | 
| 88 | 
            -
                    else
         | 
| 89 | 
            -
                      puts line
         | 
| 90 | 
            -
                    end
         | 
| 91 | 
            -
                  end
         | 
| 81 | 
            +
              def check_quota
         | 
| 82 | 
            +
                result = run_remote_command("quota #{netid}",single_host)
         | 
| 83 | 
            +
                result = result.chomp.split("\n")
         | 
| 84 | 
            +
                result.delete_at(0) if result.first == ''
         | 
| 85 | 
            +
                uid = /uid\s(\d+)/.match(result.first)[1].to_i
         | 
| 86 | 
            +
                result.delete_at(0)
         | 
| 87 | 
            +
                headings = result.first.split
         | 
| 88 | 
            +
                result.delete_at(0)
         | 
| 89 | 
            +
                results = []
         | 
| 90 | 
            +
                results << headings
         | 
| 91 | 
            +
                result.each do |line|
         | 
| 92 | 
            +
                  line = line.split
         | 
| 93 | 
            +
                  line.insert(4, 'n/a') if line.size == 6
         | 
| 94 | 
            +
                  results << line
         | 
| 92 95 | 
             
                end
         | 
| 96 | 
            +
                results
         | 
| 93 97 | 
             
              end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
              private
         | 
| 100 | 
            +
                def remove_extra_processes(processes)
         | 
| 101 | 
            +
                  processes.select do |line|
         | 
| 102 | 
            +
                    line !~ /ps -F --user|ssh(d:|-agent)|bash|zsh/
         | 
| 103 | 
            +
                  end
         | 
| 104 | 
            +
                end
         | 
| 94 105 | 
             
            end
         | 
| @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            class NetidValidator
         | 
| 2 | 
            +
              def self.do(netid)
         | 
| 3 | 
            +
                response = GenericResponse.new
         | 
| 4 | 
            +
                if netid.to_s.length > 8 || netid !~ /^[a-zA-Z][\w-]{0,7}$/
         | 
| 5 | 
            +
                  response.response = false
         | 
| 6 | 
            +
                  response.error = 'Not a valid NetID'
         | 
| 7 | 
            +
                else
         | 
| 8 | 
            +
                  response.response = true
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
                response
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
            end
         | 
| @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            module SystemConnect
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              # def initialize_ssh_connections
         | 
| 4 | 
            +
              #   connections = []
         | 
| 5 | 
            +
              #   systems.each do |host|
         | 
| 6 | 
            +
              #     puts "Attempting connection for #{system_user}@#{host}"
         | 
| 7 | 
            +
              #     connections << SystemSSH.new(host,connect(host, system_user))
         | 
| 8 | 
            +
              #     puts "Connection successful for #{host}"
         | 
| 9 | 
            +
              #   end
         | 
| 10 | 
            +
              #   @connections = connections
         | 
| 11 | 
            +
              # end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              def connect(host,user)
         | 
| 14 | 
            +
                Net::SSH.start(host,user,{auth_methods: %w(publickey)})
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def run_remote_command(command, host)
         | 
| 18 | 
            +
                connection = find_connection_for_host(host)
         | 
| 19 | 
            +
                connection.exec!(command)
         | 
| 20 | 
            +
                # if connection
         | 
| 21 | 
            +
                #   connection.exec(command) do |ch, stream, data|
         | 
| 22 | 
            +
                #     unless stream == :stderr
         | 
| 23 | 
            +
                #       puts data
         | 
| 24 | 
            +
                #     end
         | 
| 25 | 
            +
                #   end
         | 
| 26 | 
            +
                # else
         | 
| 27 | 
            +
                #   puts "No connection found for host: #{host}"
         | 
| 28 | 
            +
                # end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              def loop
         | 
| 32 | 
            +
                connection.loop
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              def find_connection_for_host(host)
         | 
| 36 | 
            +
                @connections ||= []
         | 
| 37 | 
            +
                unless @connections.select{ |conn| conn.hostname == host}.empty?
         | 
| 38 | 
            +
                  @connections.select{ |conn| conn.hostname == host}.first.connection_object
         | 
| 39 | 
            +
                else
         | 
| 40 | 
            +
                  lazy_load_connection_for_host(host)
         | 
| 41 | 
            +
                  find_connection_for_host(host)
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              def lazy_load_connection_for_host(host)
         | 
| 46 | 
            +
                @connections << SystemSSH.new(host,connect(host,system_user))
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
            end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
             | 
| 51 | 
            +
            class SystemSSH
         | 
| 52 | 
            +
              attr_accessor :hostname, :connection_object
         | 
| 53 | 
            +
              def initialize(hostname, connection_object)
         | 
| 54 | 
            +
                @hostname = hostname
         | 
| 55 | 
            +
                @connection_object = connection_object
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
    
        data/netid-tools.gemspec
    CHANGED
    
    | @@ -5,11 +5,11 @@ | |
| 5 5 |  | 
| 6 6 | 
             
            Gem::Specification.new do |s|
         | 
| 7 7 | 
             
              s.name = "netid-tools"
         | 
| 8 | 
            -
              s.version = "0. | 
| 8 | 
            +
              s.version = "0.5.1"
         | 
| 9 9 |  | 
| 10 10 | 
             
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 11 | 
             
              s.authors = ["Nikky Southerland"]
         | 
| 12 | 
            -
              s.date = "2013-06- | 
| 12 | 
            +
              s.date = "2013-06-25"
         | 
| 13 13 | 
             
              s.description = "Gem with various methods to support UW NetIDs"
         | 
| 14 14 | 
             
              s.email = "nikky@uw.edu"
         | 
| 15 15 | 
             
              s.executables = ["ua_check"]
         | 
| @@ -25,7 +25,10 @@ Gem::Specification.new do |s| | |
| 25 25 | 
             
                "Rakefile",
         | 
| 26 26 | 
             
                "VERSION",
         | 
| 27 27 | 
             
                "bin/ua_check",
         | 
| 28 | 
            +
                "lib/generic-response.rb",
         | 
| 28 29 | 
             
                "lib/netid-tools.rb",
         | 
| 30 | 
            +
                "lib/netid-validator.rb",
         | 
| 31 | 
            +
                "lib/system-connect.rb",
         | 
| 29 32 | 
             
                "netid-tools.gemspec"
         | 
| 30 33 | 
             
              ]
         | 
| 31 34 | 
             
              s.homepage = "http://github.com/allynfolksjr/netid-tools"
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: netid-tools
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.5.1
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013-06- | 
| 12 | 
            +
            date: 2013-06-25 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: nokogiri
         | 
| @@ -187,7 +187,10 @@ files: | |
| 187 187 | 
             
            - Rakefile
         | 
| 188 188 | 
             
            - VERSION
         | 
| 189 189 | 
             
            - bin/ua_check
         | 
| 190 | 
            +
            - lib/generic-response.rb
         | 
| 190 191 | 
             
            - lib/netid-tools.rb
         | 
| 192 | 
            +
            - lib/netid-validator.rb
         | 
| 193 | 
            +
            - lib/system-connect.rb
         | 
| 191 194 | 
             
            - netid-tools.gemspec
         | 
| 192 195 | 
             
            homepage: http://github.com/allynfolksjr/netid-tools
         | 
| 193 196 | 
             
            licenses:
         | 
| @@ -204,7 +207,7 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 204 207 | 
             
                  version: '0'
         | 
| 205 208 | 
             
                  segments:
         | 
| 206 209 | 
             
                  - 0
         | 
| 207 | 
            -
                  hash:  | 
| 210 | 
            +
                  hash: 1029826805295521850
         | 
| 208 211 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 209 212 | 
             
              none: false
         | 
| 210 213 | 
             
              requirements:
         |