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

.detect_vrfObject



43
44
45
46
47
48
49
50
51
# File 'lib/cisco_node_utils/yum.rb', line 43

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/cisco_node_utils/yum.rb', line 53

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_installed(pkg)
end

.query(pkg) ⇒ Object

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



65
66
67
68
69
70
71
# File 'lib/cisco_node_utils/yum.rb', line 65

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cisco_node_utils/yum.rb', line 73

def self.remove(pkg)
  config_set('yum', 'deactivate', pkg)
  # May not be able to remove the package immediately after
  # deactivation.
  while (try ||= 1) < 20
    o = config_set('yum', 'remove', pkg)
    break unless o[/operation is in progress, please try again later/]
    sleep 1
    try += 1
  end
end

.validate_installed(pkg) ⇒ Object



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

def self.validate_installed(pkg)
  # Sample data returned from config_get('yum', 'query_all')
  # ["nxos.sample-n8k_EOR.lib32_nxos", "1.0.0-7.0.3.F1.1", "@patching"],
  patch_data = config_get('yum', 'query_all')
  patch_data.each do |name_arch, version, _state|
    # Separate name and architecture
    next if name_arch.rindex('.').nil?
    arch = name_arch.slice!(name_arch.rindex('.')..-1).delete('.')
    # Version/Architecture info not available when only pkg name specified.
    version = arch = '' if name_arch == pkg
    # Check for match
    if pkg.match(name_arch) && pkg.match(version) && pkg.match(arch)
      return true
    end
  end
  fail 'Failed to install the requested rpm'
end