Class: Cisco::Yum

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/yum.rb

Overview

This Yum class provides cisco package management functions through nxapi.

Class Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Class Method Details

.decompose_name(file_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cisco_node_utils/yum.rb', line 25

def self.decompose_name(file_name)
  # ex: chef-12.0.0alpha.2+20150319.git.1.b6f-1.el5.x86_64.rpm
  name_ver_arch_regex = /^([\w\-\+]+)-(\d+\..*)\.(\w{4,})(?:\.rpm)?$/

  # ex n9000_sample-1.0.0-7.0.3.x86_64.rpm
  name_ver_arch_regex_nx = /^(.*)-([\d\.]+-[\d\.]+)\.(\w{4,})\.rpm$/

  # ex: b+z-ip2.x64_64
  name_arch_regex = /^([\w\-\+]+)\.(\w+)$/

  file_name.match(name_ver_arch_regex) ||
    file_name.match(name_ver_arch_regex_nx) ||
    file_name.match(name_arch_regex)
end

.detect_vrfObject



59
60
61
62
63
64
65
66
# File 'lib/cisco_node_utils/yum.rb', line 59

def self.detect_vrf
  # Detect current namespace from agent environment
  inode = File::Stat.new('/proc/self/ns/net').ino
  # -L reqd for guestshell's find command
  vrfname = File.basename(`find -L /var/run/netns/ -inum #{inode}`.chop)
  vrf = 'vrf ' + vrfname unless vrfname.empty?
  vrf
end

.install(pkg, vrf = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/cisco_node_utils/yum.rb', line 68

def self.install(pkg, vrf=nil)
  vrf = vrf.nil? ? detect_vrf : "vrf #{vrf}"
  config_set('yum', 'install', pkg, vrf)

  # HACK: The current nxos host installer is a multi-part command
  # which may fail at a later stage yet return a false positive;
  # therefore a post-validation check is needed here to verify the
  # actual outcome.
  validate(pkg)
end

.query(pkg) ⇒ Object

returns version of package, or false if package doesn’t exist



80
81
82
83
84
85
86
# File 'lib/cisco_node_utils/yum.rb', line 80

def self.query(pkg)
  fail TypeError unless pkg.is_a? String
  fail ArgumentError if pkg.empty?
  b = config_get('yum', 'query', pkg)
  fail "Multiple matching packages found for #{pkg}" if b && b.size > 1
  b.nil? ? nil : b.first
end

.remove(pkg) ⇒ Object



88
89
90
# File 'lib/cisco_node_utils/yum.rb', line 88

def self.remove(pkg)
  config_set('yum', 'remove', pkg)
end

.validate(pkg) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cisco_node_utils/yum.rb', line 40

def self.validate(pkg)
  file_name = pkg.strip.tr(':', '/').split('/').last
  pkg_info = Yum.decompose_name(file_name)
  if pkg_info.nil?
    query_name = file_name
  else
    if pkg_info[3].nil?
      query_name = pkg_info[1]
    else
      query_name = "#{pkg_info[1]}.#{pkg_info[3]}"
    end
  end
  should_ver = pkg_info[2] if pkg_info && pkg_info[3]
  ver = query(query_name)
  if ver.nil? || (!should_ver.nil? && should_ver != ver)
    fail 'Failed to install the requested rpm'
  end
end