{"action":"created","comment":{"url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/comments/42286591","id":42286591,"diff_hunk":"@@ -0,0 +1,183 @@\n+require 'ipaddr'\n+\n+module LinuxAdmin\n+  class NetworkInterface\n+    include Common\n+\n+    # Cached class instance variable for what distro we are running on\n+    @dist_class = nil\n+\n+    # Gets the subclass specific to the local Linux distro\n+    #\n+    # @param clear_cache [Boolean] Determines if the cached value will be reevaluated\n+    # @return [Class] The proper class to be used\n+    def self.dist_class(clear_cache = false)\n+      @dist_class = nil if clear_cache\n+      @dist_class ||= begin\n+        if [Distros.rhel, Distros.fedora].include?(Distros.local)\n+          NetworkInterfaceRH\n+        else\n+          NetworkInterfaceGeneric\n+        end\n+      end\n+    end\n+\n+    class << self\n+      private\n+\n+      alias_method :orig_new, :new\n+    end\n+\n+    # Creates an instance of the correct NetworkInterface subclass for the local distro\n+    def self.new(*args)\n+      if self == LinuxAdmin::NetworkInterface\n+        dist_class.new(*args)\n+      else\n+        orig_new(*args)\n+      end\n+    end\n+\n+    # @return [String] the interface for networking operations\n+    attr_reader :interface\n+\n+    # @param interface [String] Name of the network interface to manage\n+    def initialize(interface)\n+      @interface = interface\n+      reload\n+    end\n+\n+    # Gathers current network information for this interface\n+    def reload\n+      @network_conf = {}\n+      return false unless (ip_output = ip_show)\n+\n+      parse_ip4(ip_output)\n+      parse_ip6(ip_output, :global)\n+      parse_ip6(ip_output, :link)\n+\n+      @network_conf[:mac] = parse_ip_output(ip_output, %r{link/ether}, 1)\n+\n+      ip_route_res = run(cmd(\"ip\"), :params => [\"route\"])\n+      @network_conf[:gateway] = parse_ip_output(ip_route_res.output, /^default/, 2) if ip_route_res.success?\n+      true\n+    end\n+\n+    # Retrieve the IPv4 address assigned to the interface\n+    #\n+    # @return [String] IPv4 address for the managed interface\n+    def address\n+      @network_conf[:address]\n+    end\n+\n+    # Retrieve the IPv6 address assigned to the interface\n+    #\n+    # @return [String] IPv6 address for the managed interface\n+    # @raise [ArgumentError] if the given scope is not `:global` or `:link`\n+    def address6(scope = :global)\n+      if scope == :global\n+        @network_conf[:address6_global]\n+      elsif scope == :link\n+        @network_conf[:address6_link]\n+      else\n+        raise ArgumentError, \"Unrecognized address scope #{scope}\"\n+      end\n+    end\n+\n+    # Retrieve the MAC address associated with the interface\n+    #\n+    # @return [String] the MAC address\n+    def mac_address\n+      @network_conf[:mac]\n+    end\n+\n+    # Retrieve the IPv4 sub-net mask assigned to the interface\n+    #\n+    # @return [String] IPv4 netmask\n+    def netmask\n+      @network_conf[:mask]\n+    end\n+\n+    # Retrieve the IPv6 sub-net mask assigned to the interface\n+    #\n+    # @return [String] IPv6 netmask\n+    # @raise [ArgumentError] if the given scope is not `:global` or `:link`\n+    def netmask6(scope = :global)\n+      if scope == :global\n+        @network_conf[:mask6_global]\n+      elsif scope == :link\n+        @network_conf[:mask6_link]\n+      else\n+        raise ArgumentError, \"Unrecognized address scope #{scope}\"\n+      end\n+    end\n+\n+    # Retrieve the IPv4 default gateway associated with the interface\n+    #\n+    # @return [String] IPv4 gateway address\n+    def gateway\n+      @network_conf[:gateway]\n+    end\n+\n+    # Brings up the network interface\n+    #\n+    # @return [Boolean] whether the command succeeded or not\n+    def ifup\n+      run(cmd(\"ifup\"), :params => [@interface]).success?\n+    end\n+\n+    # Brings down the network interface\n+    #\n+    # @return [Boolean] whether the command succeeded or not\n+    def ifdown\n+      run(cmd(\"ifdown\"), :params => [@interface]).success?\n+    end\n+\n+    private\n+\n+    # Parses the output of `ip addr show`\n+    #\n+    # @param output [String] The command output\n+    # @param regex  [Regexp] Regular expression to match the desired output line\n+    # @param col    [FixNum] The whitespace delimited column to be returned\n+    # @return [String] The parsed data\n+    def parse_ip_output(output, regex, col)\n+      the_line = output.split(\"\\n\").detect { |l| l =~ regex }\n+      the_line.nil? ? nil : the_line.strip.split(' ')[col]\n+    end\n+\n+    # Runs the command `ip addr show <interface>`\n+    #\n+    # @return [String] The command output, nil on failure\n+    def ip_show\n+      result = run(cmd(\"ip\"), :params => [\"addr\", \"show\", @interface])\n+      result.success? ? result.output : nil\n+    end\n+\n+    # Parses the IPv4 information from the output of `ip addr show <device>`\n+    #\n+    # @param ip_output [String] The command output\n+    def parse_ip4(ip_output)\n+      cidr_ip = parse_ip_output(ip_output, /inet/, 1)\n+      return unless cidr_ip\n+\n+      @network_conf[:address] = cidr_ip.split('/')[0]\n+      @network_conf[:mask] = IPAddr.new('255.255.255.255').mask(cidr_ip.split('/')[1]).to_s\n+    end\n+\n+    # Parses the IPv6 information from the output of `ip addr show <device>`\n+    #\n+    # @param ip_output [String] The command output\n+    # @param scope     [Symbol] The IPv6 scope (either `:global` or `:local`)\n+    def parse_ip6(ip_output, scope)\n+      mask_addr = IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')\n+      cidr_ip = parse_ip_output(ip_output, /inet6 .* scope #{scope}/, 1)\n+      return unless cidr_ip\n+\n+      parts = cidr_ip.split('/')\n+      @network_conf[\"address6_#{scope}\".to_sym] = parts[0]\n+      @network_conf[\"mask6_#{scope}\".to_sym] = mask_addr.mask(parts[1]).to_s\n+    end\n+  end\n+end\n+\n+Dir.glob(File.join(File.dirname(__FILE__), \"network_interface\", \"*.rb\")).each { |f| require f }","path":"lib/linux_admin/network_interface.rb","position":183,"original_position":183,"commit_id":"3689e823668042fbbf1f37936159f74079b11dbb","original_commit_id":"3689e823668042fbbf1f37936159f74079b11dbb","user":{"login":"carbonin","id":12697904,"avatar_url":"https://avatars.githubusercontent.com/u/12697904?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonin","html_url":"https://github.com/carbonin","followers_url":"https://api.github.com/users/carbonin/followers","following_url":"https://api.github.com/users/carbonin/following{/other_user}","gists_url":"https://api.github.com/users/carbonin/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonin/subscriptions","organizations_url":"https://api.github.com/users/carbonin/orgs","repos_url":"https://api.github.com/users/carbonin/repos","events_url":"https://api.github.com/users/carbonin/events{/privacy}","received_events_url":"https://api.github.com/users/carbonin/received_events","type":"User","site_admin":false},"body":"How old?  I think we have travis running for 1.9.3.","created_at":"2015-10-16T20:30:26Z","updated_at":"2015-10-16T20:30:26Z","html_url":"https://github.com/ManageIQ/linux_admin/pull/132#discussion_r42286591","pull_request_url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132","_links":{"self":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/comments/42286591"},"html":{"href":"https://github.com/ManageIQ/linux_admin/pull/132#discussion_r42286591"},"pull_request":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132"}}},"pull_request":{"url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132","id":47456981,"html_url":"https://github.com/ManageIQ/linux_admin/pull/132","diff_url":"https://github.com/ManageIQ/linux_admin/pull/132.diff","patch_url":"https://github.com/ManageIQ/linux_admin/pull/132.patch","issue_url":"https://api.github.com/repos/ManageIQ/linux_admin/issues/132","number":132,"state":"open","locked":false,"title":"Add distro specific network interface class","user":{"login":"carbonin","id":12697904,"avatar_url":"https://avatars.githubusercontent.com/u/12697904?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonin","html_url":"https://github.com/carbonin","followers_url":"https://api.github.com/users/carbonin/followers","following_url":"https://api.github.com/users/carbonin/following{/other_user}","gists_url":"https://api.github.com/users/carbonin/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonin/subscriptions","organizations_url":"https://api.github.com/users/carbonin/orgs","repos_url":"https://api.github.com/users/carbonin/repos","events_url":"https://api.github.com/users/carbonin/events{/privacy}","received_events_url":"https://api.github.com/users/carbonin/received_events","type":"User","site_admin":false},"body":"Implements the current functions of the LinuxAdmin::IpAddress class as well as distro specific functions for setting IP addresses and DNS servers.\r\n\r\nUses the distro detection in LinuxAdmin::Distros to determine which sub-class to instantiate at run-time.\r\n\r\nCurrently supports Red Hat based distros (CentOS, Fedora, and RHEL).\r\n\r\n@bdunne @Fryguy please review.","created_at":"2015-10-12T19:44:33Z","updated_at":"2015-10-16T20:30:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fabdf44730499928af81d6f4c852c4829fd163aa","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132/commits","review_comments_url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132/comments","review_comment_url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ManageIQ/linux_admin/issues/132/comments","statuses_url":"https://api.github.com/repos/ManageIQ/linux_admin/statuses/3689e823668042fbbf1f37936159f74079b11dbb","head":{"label":"carbonin:add_network_interface_class","ref":"add_network_interface_class","sha":"3689e823668042fbbf1f37936159f74079b11dbb","user":{"login":"carbonin","id":12697904,"avatar_url":"https://avatars.githubusercontent.com/u/12697904?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonin","html_url":"https://github.com/carbonin","followers_url":"https://api.github.com/users/carbonin/followers","following_url":"https://api.github.com/users/carbonin/following{/other_user}","gists_url":"https://api.github.com/users/carbonin/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonin/subscriptions","organizations_url":"https://api.github.com/users/carbonin/orgs","repos_url":"https://api.github.com/users/carbonin/repos","events_url":"https://api.github.com/users/carbonin/events{/privacy}","received_events_url":"https://api.github.com/users/carbonin/received_events","type":"User","site_admin":false},"repo":{"id":39075654,"name":"linux_admin","full_name":"carbonin/linux_admin","owner":{"login":"carbonin","id":12697904,"avatar_url":"https://avatars.githubusercontent.com/u/12697904?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonin","html_url":"https://github.com/carbonin","followers_url":"https://api.github.com/users/carbonin/followers","following_url":"https://api.github.com/users/carbonin/following{/other_user}","gists_url":"https://api.github.com/users/carbonin/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonin/subscriptions","organizations_url":"https://api.github.com/users/carbonin/orgs","repos_url":"https://api.github.com/users/carbonin/repos","events_url":"https://api.github.com/users/carbonin/events{/privacy}","received_events_url":"https://api.github.com/users/carbonin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/carbonin/linux_admin","description":"LinuxAdmin is a module to simplify management of linux systems.","fork":true,"url":"https://api.github.com/repos/carbonin/linux_admin","forks_url":"https://api.github.com/repos/carbonin/linux_admin/forks","keys_url":"https://api.github.com/repos/carbonin/linux_admin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/carbonin/linux_admin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/carbonin/linux_admin/teams","hooks_url":"https://api.github.com/repos/carbonin/linux_admin/hooks","issue_events_url":"https://api.github.com/repos/carbonin/linux_admin/issues/events{/number}","events_url":"https://api.github.com/repos/carbonin/linux_admin/events","assignees_url":"https://api.github.com/repos/carbonin/linux_admin/assignees{/user}","branches_url":"https://api.github.com/repos/carbonin/linux_admin/branches{/branch}","tags_url":"https://api.github.com/repos/carbonin/linux_admin/tags","blobs_url":"https://api.github.com/repos/carbonin/linux_admin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/carbonin/linux_admin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/carbonin/linux_admin/git/refs{/sha}","trees_url":"https://api.github.com/repos/carbonin/linux_admin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/carbonin/linux_admin/statuses/{sha}","languages_url":"https://api.github.com/repos/carbonin/linux_admin/languages","stargazers_url":"https://api.github.com/repos/carbonin/linux_admin/stargazers","contributors_url":"https://api.github.com/repos/carbonin/linux_admin/contributors","subscribers_url":"https://api.github.com/repos/carbonin/linux_admin/subscribers","subscription_url":"https://api.github.com/repos/carbonin/linux_admin/subscription","commits_url":"https://api.github.com/repos/carbonin/linux_admin/commits{/sha}","git_commits_url":"https://api.github.com/repos/carbonin/linux_admin/git/commits{/sha}","comments_url":"https://api.github.com/repos/carbonin/linux_admin/comments{/number}","issue_comment_url":"https://api.github.com/repos/carbonin/linux_admin/issues/comments{/number}","contents_url":"https://api.github.com/repos/carbonin/linux_admin/contents/{+path}","compare_url":"https://api.github.com/repos/carbonin/linux_admin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/carbonin/linux_admin/merges","archive_url":"https://api.github.com/repos/carbonin/linux_admin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/carbonin/linux_admin/downloads","issues_url":"https://api.github.com/repos/carbonin/linux_admin/issues{/number}","pulls_url":"https://api.github.com/repos/carbonin/linux_admin/pulls{/number}","milestones_url":"https://api.github.com/repos/carbonin/linux_admin/milestones{/number}","notifications_url":"https://api.github.com/repos/carbonin/linux_admin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/carbonin/linux_admin/labels{/name}","releases_url":"https://api.github.com/repos/carbonin/linux_admin/releases{/id}","created_at":"2015-07-14T13:13:33Z","updated_at":"2015-07-14T13:13:33Z","pushed_at":"2015-10-16T14:47:33Z","git_url":"git://github.com/carbonin/linux_admin.git","ssh_url":"git@github.com:carbonin/linux_admin.git","clone_url":"https://github.com/carbonin/linux_admin.git","svn_url":"https://github.com/carbonin/linux_admin","homepage":null,"size":552,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ManageIQ:master","ref":"master","sha":"ec8fb5e6663b08b4ed7a567bd4f2cd5d9a6f1a25","user":{"login":"ManageIQ","id":1125575,"avatar_url":"https://avatars.githubusercontent.com/u/1125575?v=3","gravatar_id":"","url":"https://api.github.com/users/ManageIQ","html_url":"https://github.com/ManageIQ","followers_url":"https://api.github.com/users/ManageIQ/followers","following_url":"https://api.github.com/users/ManageIQ/following{/other_user}","gists_url":"https://api.github.com/users/ManageIQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ManageIQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ManageIQ/subscriptions","organizations_url":"https://api.github.com/users/ManageIQ/orgs","repos_url":"https://api.github.com/users/ManageIQ/repos","events_url":"https://api.github.com/users/ManageIQ/events{/privacy}","received_events_url":"https://api.github.com/users/ManageIQ/received_events","type":"Organization","site_admin":false},"repo":{"id":9753961,"name":"linux_admin","full_name":"ManageIQ/linux_admin","owner":{"login":"ManageIQ","id":1125575,"avatar_url":"https://avatars.githubusercontent.com/u/1125575?v=3","gravatar_id":"","url":"https://api.github.com/users/ManageIQ","html_url":"https://github.com/ManageIQ","followers_url":"https://api.github.com/users/ManageIQ/followers","following_url":"https://api.github.com/users/ManageIQ/following{/other_user}","gists_url":"https://api.github.com/users/ManageIQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ManageIQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ManageIQ/subscriptions","organizations_url":"https://api.github.com/users/ManageIQ/orgs","repos_url":"https://api.github.com/users/ManageIQ/repos","events_url":"https://api.github.com/users/ManageIQ/events{/privacy}","received_events_url":"https://api.github.com/users/ManageIQ/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ManageIQ/linux_admin","description":"LinuxAdmin is a module to simplify management of linux systems.","fork":false,"url":"https://api.github.com/repos/ManageIQ/linux_admin","forks_url":"https://api.github.com/repos/ManageIQ/linux_admin/forks","keys_url":"https://api.github.com/repos/ManageIQ/linux_admin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ManageIQ/linux_admin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ManageIQ/linux_admin/teams","hooks_url":"https://api.github.com/repos/ManageIQ/linux_admin/hooks","issue_events_url":"https://api.github.com/repos/ManageIQ/linux_admin/issues/events{/number}","events_url":"https://api.github.com/repos/ManageIQ/linux_admin/events","assignees_url":"https://api.github.com/repos/ManageIQ/linux_admin/assignees{/user}","branches_url":"https://api.github.com/repos/ManageIQ/linux_admin/branches{/branch}","tags_url":"https://api.github.com/repos/ManageIQ/linux_admin/tags","blobs_url":"https://api.github.com/repos/ManageIQ/linux_admin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ManageIQ/linux_admin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ManageIQ/linux_admin/git/refs{/sha}","trees_url":"https://api.github.com/repos/ManageIQ/linux_admin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ManageIQ/linux_admin/statuses/{sha}","languages_url":"https://api.github.com/repos/ManageIQ/linux_admin/languages","stargazers_url":"https://api.github.com/repos/ManageIQ/linux_admin/stargazers","contributors_url":"https://api.github.com/repos/ManageIQ/linux_admin/contributors","subscribers_url":"https://api.github.com/repos/ManageIQ/linux_admin/subscribers","subscription_url":"https://api.github.com/repos/ManageIQ/linux_admin/subscription","commits_url":"https://api.github.com/repos/ManageIQ/linux_admin/commits{/sha}","git_commits_url":"https://api.github.com/repos/ManageIQ/linux_admin/git/commits{/sha}","comments_url":"https://api.github.com/repos/ManageIQ/linux_admin/comments{/number}","issue_comment_url":"https://api.github.com/repos/ManageIQ/linux_admin/issues/comments{/number}","contents_url":"https://api.github.com/repos/ManageIQ/linux_admin/contents/{+path}","compare_url":"https://api.github.com/repos/ManageIQ/linux_admin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ManageIQ/linux_admin/merges","archive_url":"https://api.github.com/repos/ManageIQ/linux_admin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ManageIQ/linux_admin/downloads","issues_url":"https://api.github.com/repos/ManageIQ/linux_admin/issues{/number}","pulls_url":"https://api.github.com/repos/ManageIQ/linux_admin/pulls{/number}","milestones_url":"https://api.github.com/repos/ManageIQ/linux_admin/milestones{/number}","notifications_url":"https://api.github.com/repos/ManageIQ/linux_admin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ManageIQ/linux_admin/labels{/name}","releases_url":"https://api.github.com/repos/ManageIQ/linux_admin/releases{/id}","created_at":"2013-04-29T17:36:58Z","updated_at":"2015-10-13T19:12:16Z","pushed_at":"2015-10-16T14:47:34Z","git_url":"git://github.com/ManageIQ/linux_admin.git","ssh_url":"git@github.com:ManageIQ/linux_admin.git","clone_url":"https://github.com/ManageIQ/linux_admin.git","svn_url":"https://github.com/ManageIQ/linux_admin","homepage":null,"size":1473,"stargazers_count":14,"watchers_count":14,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":8,"forks":11,"open_issues":8,"watchers":14,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132"},"html":{"href":"https://github.com/ManageIQ/linux_admin/pull/132"},"issue":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/issues/132"},"comments":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/issues/132/comments"},"review_comments":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132/comments"},"review_comment":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/pulls/132/commits"},"statuses":{"href":"https://api.github.com/repos/ManageIQ/linux_admin/statuses/3689e823668042fbbf1f37936159f74079b11dbb"}}}}