~ubuntu-branches/ubuntu/trusty/facter/trusty

« back to all changes in this revision

Viewing changes to lib/facter.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Palmer
  • Date: 2006-10-31 06:24:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061031062459-swxk2h8r33lj8r1f
Tags: 1.3.5-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: facter.rb 153 2006-06-28 17:38:18Z luke $
 
1
# $Id: facter.rb 179 2006-09-29 16:13:12Z luke $
2
2
#--
3
3
# Copyright 2006 Luke Kanies <luke@madstop.com>
4
4
22
22
    include Comparable
23
23
    include Enumerable
24
24
 
25
 
    FACTERVERSION = '1.3.3'
 
25
    FACTERVERSION = '1.3.5'
26
26
        # = Facter
27
27
    # Functions as a hash of 'facts' you might care about about your
28
28
    # system, such as mac address, IP address, Video card, etc.
608
608
            setcode 'uname -r'
609
609
        end
610
610
 
 
611
        {   "LSBRelease" => "^LSB Version:\t(.*)$",
 
612
            "LSBDistId" => "^Distributor ID:\t(.*)$",
 
613
            "LSBDistRelease" => "^Release:\t(.*)$",
 
614
            "LSBDistDescription" => "^Description:\t(.*)$",
 
615
            "LSBDistCodeName" => "^Codename:\t(.*)$"
 
616
        }.each do |fact, pattern|
 
617
            Facter.add(fact) do
 
618
                setcode do
 
619
                    output = Resolution.exec('lsb_release -a 2>/dev/null')
 
620
                    if output =~ /#{pattern}/
 
621
                        $1
 
622
                    else
 
623
                        nil
 
624
                    end
 
625
                end
 
626
            end
 
627
        end
 
628
 
611
629
        Facter.add("OperatingSystem") do
612
630
            # Default to just returning the kernel as the operating system
613
631
            setcode do Facter["Kernel"].value end
651
669
        end
652
670
 
653
671
        Facter.add("Architecture") do
654
 
            confine :operatingsystem => :debian
 
672
            confine :kernel => :linux
655
673
            setcode do
656
674
                model = Facter.hardwaremodel
657
675
                case model
658
 
                when 'x86_64': "amd64" 
 
676
                # most linuxen use "x86_64"
 
677
                when 'x86_64':
 
678
                    Facter.operatingsystem == "Debian" ? "amd64" : model;
659
679
                when /(i[3456]86|pentium)/: "i386"
660
680
                else
661
681
                    model
691
711
 
692
712
        Facter.add("Domain") do
693
713
            setcode do
 
714
                # First force the hostname to be checked
 
715
                Facter.hostname
 
716
 
 
717
                # Now check to see if it set the domain
694
718
                if defined? $domain and ! $domain.nil?
695
719
                    $domain
696
720
                else
769
793
            end
770
794
        end
771
795
 
 
796
        Facter.add(:fqdn) do
 
797
            setcode do
 
798
                host = Facter.value(:hostname)
 
799
                domain = Facter.value(:domain)
 
800
                if host and domain
 
801
                    [host, domain].join(".")
 
802
                else
 
803
                    nil
 
804
                end
 
805
            end
 
806
        end
 
807
 
772
808
        Facter.add("IPAddress") do
773
809
            setldapname "iphostnumber"
774
810
            setcode do
775
811
                require 'resolv'
776
812
 
777
813
                begin
778
 
                    if hostname = Facter["hostname"].value
 
814
                    if hostname = Facter.hostname
779
815
                        ip = Resolv.getaddress(hostname)
780
816
                        unless ip == "127.0.0.1"
781
817
                            ip
792
828
        end
793
829
        Facter.add("IPAddress") do
794
830
            setcode do
795
 
                if hostname = Facter["hostname"].value
 
831
                if hostname = Facter.hostname
796
832
                    # we need Hostname to exist for this to work
797
 
                    if list = Resolution.exec("host #{hostname}").chomp.split(/\s/)
 
833
                    host = nil
 
834
                    if host = Resolution.exec("host #{hostname}") and host.chomp.split(/\s/)
798
835
 
799
836
                        if defined? list[-1] and
800
837
                                list[-1] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
831
868
 
832
869
        Facter.add("UniqueId") do
833
870
            setcode 'hostid',  '/bin/sh'
834
 
            confine :operatingsystem => :solaris
 
871
            #confine :kernel => %w{Solaris Linux}
 
872
            confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo}
835
873
        end
836
874
 
837
875
        Facter.add("HardwareISA") do
838
876
            setcode 'uname -p', '/bin/sh'
839
 
            confine :operatingsystem => :solaris
 
877
            confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo}
840
878
        end
841
879
 
842
880
        Facter.add("MacAddress") do
843
 
            confine :operatingsystem => :solaris
 
881
            #confine :kernel => %w{Solaris Linux}
 
882
            confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo}
844
883
            setcode do
845
 
                ether = nil
 
884
                ether = []
846
885
                output = %x{/sbin/ifconfig -a}
847
 
 
848
 
                output =~ /ether (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
849
 
                ether = $1
850
 
 
851
 
                ether
 
886
                output.each {|s|
 
887
                             ether.push($1) if s =~ /(?:ether|HWaddr) (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
 
888
                            }
 
889
                ether.join(" ")
852
890
            end
853
891
        end
854
892
 
867
905
 
868
906
                ether
869
907
            end
 
908
 
 
909
         Facter.add("IPAddress") do
 
910
            confine :kernel => :linux
 
911
            setcode do
 
912
                ip = nil
 
913
                output = %x{/sbin/ifconfig}
 
914
 
 
915
                output.split(/^\S/).each { |str|
 
916
                    if str =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
 
917
                        tmp = $1
 
918
                        unless tmp =~ /127\./
 
919
                            ip = tmp
 
920
                            break
 
921
                        end
 
922
                    end
 
923
                }
 
924
 
 
925
                ip
 
926
            end
 
927
        end
870
928
        end
871
929
        Facter.add("IPAddress") do
872
 
            confine :kernel => :darwin
 
930
            confine :kernel => %w{FreeBSD NetBSD OpenBSD solaris darwin}
873
931
            setcode do
874
932
                ip = nil
875
933
                output = %x{/sbin/ifconfig}
922
980
        end
923
981
 
924
982
        Facter.add("id") do
925
 
            confine :operatingsystem => :linux
 
983
            #confine :kernel => %w{Solaris Linux}
 
984
            confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo}
926
985
            setcode "whoami"
927
986
        end
928
987