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

.activate(pkg) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/cisco_node_utils/yum.rb', line 122

def self.activate(pkg)
  try_operation('activate', pkg)
  while (try ||= 1) < 20
    return if query_activated(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to activate pkg: #{pkg}"
end

.add(pkg, vrf = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/cisco_node_utils/yum.rb', line 112

def self.add(pkg, vrf=nil)
  try_operation('add', pkg, vrf)
  while (try ||= 1) < 20
    return if query_added(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to add pkg: #{pkg}, using vrf #{vrf}"
end

.commit(pkg) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/cisco_node_utils/yum.rb', line 132

def self.commit(pkg)
  try_operation('commit', pkg)
  while (try ||= 1) < 20
    return if query_committed(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to commit pkg: #{pkg}"
end

.commit_deactivate(pkg) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/cisco_node_utils/yum.rb', line 142

def self.commit_deactivate(pkg)
  try_operation('commit', pkg)
  while (try ||= 1) < 20
    return unless query_committed(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to commit after deactivate pkg: #{pkg}"
end

.deactivate(pkg) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/cisco_node_utils/yum.rb', line 152

def self.deactivate(pkg)
  try_operation('deactivate', pkg)
  while (try ||= 1) < 20
    return if query_inactive(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to deactivate pkg: #{pkg}"
end

.delete(pkg) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/cisco_node_utils/yum.rb', line 162

def self.delete(pkg)
  try_operation('remove', pkg)
  while (try ||= 1) < 20
    return if query_removed(pkg)
    sleep 1
    try += 1
  end
  fail "Failed to delete pkg: #{pkg}"
end

.detect_vrfObject



46
47
48
49
50
51
52
53
54
# File 'lib/cisco_node_utils/yum.rb', line 46

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cisco_node_utils/yum.rb', line 56

def self.install(pkg, vrf=nil)
  vrf = vrf.nil? ? detect_vrf : "vrf #{vrf}"
  begin
    # get package name first
    name = pkg_name(pkg)
    # call remove if pkg exists
    remove(name) unless name.empty?
    add(pkg, vrf)
    activate(pkg)
    commit(pkg_name(pkg))
    validate_installed(pkg)
  rescue Cisco::CliError, RuntimeError => e
    raise Cisco::CliError, "#{e.class}, #{e.message}"
  end
end

.pkg_name(pkg) ⇒ Object



182
183
184
# File 'lib/cisco_node_utils/yum.rb', line 182

def self.pkg_name(pkg)
  config_get('yum', 'pkg_name', pkg: pkg)
end

.query(pkg) ⇒ Object

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



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

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

.query_activated(pkg) ⇒ Object



186
187
188
# File 'lib/cisco_node_utils/yum.rb', line 186

def self.query_activated(pkg)
  config_get('yum', 'query_activated', pkg: pkg)
end

.query_added(pkg) ⇒ Object



190
191
192
# File 'lib/cisco_node_utils/yum.rb', line 190

def self.query_added(pkg)
  config_get('yum', 'query_added', pkg: pkg)
end

.query_committed(pkg) ⇒ Object



194
195
196
197
198
# File 'lib/cisco_node_utils/yum.rb', line 194

def self.query_committed(pkg)
  o = config_get('yum', 'query_committed', pkg: pkg)
  return false if o.nil?
  o.include? pkg
end

.query_inactive(pkg) ⇒ Object



200
201
202
# File 'lib/cisco_node_utils/yum.rb', line 200

def self.query_inactive(pkg)
  config_get('yum', 'query_inactive', pkg: pkg)
end

.query_removed(pkg) ⇒ Object



204
205
206
207
# File 'lib/cisco_node_utils/yum.rb', line 204

def self.query_removed(pkg)
  val = config_get('yum', 'query_removed', pkg: pkg)
  val ? false : true
end

.query_state(pkg) ⇒ Object



209
210
211
# File 'lib/cisco_node_utils/yum.rb', line 209

def self.query_state(pkg)
  config_get('yum', 'query_state', pkg: pkg).downcase
end

.remove(pkg) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cisco_node_utils/yum.rb', line 81

def self.remove(pkg)
  # get committed state
  cstate = query_committed(pkg)
  # get activated state
  astate = query_activated(pkg)

  if astate && cstate
    # pkg is active and committed
    # so deactivate, commit and remove
    deactivate(pkg)
    commit_deactivate(pkg)
    delete(pkg)
  elsif cstate
    # pkg is inactive and committed
    # so commit and remove
    commit_deactivate(pkg)
    delete(pkg)
  elsif astate
    # pkg is active and not committed
    # so deactivate and remove
    deactivate(pkg)
    delete(pkg)
  else
    # pkg is inactive and not committed
    # so just remove
    delete(pkg)
  end
rescue Cisco::CliError, RuntimeError => e
  raise Cisco::CliError, "#{e.class}, #{e.message}"
end

.try_operation(operation, pkg, vrf = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/cisco_node_utils/yum.rb', line 172

def self.try_operation(operation, pkg, vrf=nil)
  args = vrf.nil? ? { pkg: pkg } : { pkg: pkg, vrf: vrf }
  while (try ||= 1) < 20
    o = config_set('yum', operation, args)
    break unless o[/Another install operation is in progress/]
    sleep 1
    try += 1
  end
end

.validate_installed(pkg) ⇒ Object



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

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