Class: PuppetX::Eos::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/eos/modules/extension.rb

Overview

The Extensio class provides management of extensions in EOS. Extensions are simply RPM packages that can loaded onto the switch. This class allows installing, deleting and configuring extensions

Constant Summary collapse

BOOTEXT =
'/mnt/flash/boot-extensions'

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Extension

Returns a new instance of Extension.



47
48
49
# File 'lib/puppet_x/eos/modules/extension.rb', line 47

def initialize(api)
  @api = api
end

Instance Method Details

#autoload?(name) ⇒ Boolean

Returns if file is loaded on boot or not

Parameters:

  • name (String)

    The name of the file

Returns:

  • (Boolean)

    True if it loades on boot otherwise False



80
81
82
83
# File 'lib/puppet_x/eos/modules/extension.rb', line 80

def autoload?(name)
  name = URI(name).to_s.split('/')[-1]
  File.open(BOOTEXT).read.scan(/#{name}[\sforce\n|\n]/).size > 0
end

#delete(name) ⇒ Boolean

Uninstalls and removes and extension from an EOS node. The extension will be unloaded and deleted from /mnt/flash

Returns:

  • (Boolean)

    True if the command succeeds or False if it does not succeed



125
126
127
128
129
130
# File 'lib/puppet_x/eos/modules/extension.rb', line 125

def delete(name)
  name = URI(name).to_s.split('/')[-1]
  set_autoload(:false, name, false)
  @api.enable("no extension #{name}")
  @api.enable("delete extension:#{name}") == [{}]
end

#getHash<Hash<String, String>>

Retrieves all of the extenions loaded in EOS and returns an array of hashes using the ‘show extensions’ command over eAPI.

Example:

[{
  "ruby-1.9.3-1.swix": {
    "status": "installed",   # installed, forceInstalled
    "version": "1.9.3.484",
    "presence": "present",
    "release": "32.eos4",
    "numRpms": 10,
    "error": false
  }
}]

Returns:

  • (Hash<Hash<String, String>>)

    Nested hash describing the extension details. If there are no extensions then an empty Hash is returned



70
71
72
# File 'lib/puppet_x/eos/modules/extension.rb', line 70

def get
  @api.enable('show extensions')
end

#install(url, force) ⇒ Boolean

Copies and installs the extension from a remote server to the node running EOS using the eAPI coopy command.

Parameters:

  • url (String)

    The full url to the RPM

  • force (String)

    Specifies the use of the force keyword

Returns:

  • (Boolean)

    True if the installation succeeds and False if it does not succeed



94
95
96
97
98
# File 'lib/puppet_x/eos/modules/extension.rb', line 94

def install(url, force)
  force = false if force.nil?
  @api.enable("copy #{url} extension:")
  load(url, force)
end

#load(name, force) ⇒ Boolean

Loads an existing extension into EOS. The extension must already be copied over to the node using #create

Parameters:

  • opt (Hash)

    Options for loading the extions

  • opts (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    True if the command succeeds or False if it does not succeed



110
111
112
113
114
115
# File 'lib/puppet_x/eos/modules/extension.rb', line 110

def load(name, force)
  name = URI(name).to_s.split('/')[-1]
  command = "extension #{name}"
  command << ' force' if force
  @api.enable(command) == [{}]
end

#set_autoload(enabled, name, force) ⇒ Boolean

Configures the extension to persistent on system restarts

@param force Specifies if the force keyword should be used

Parameters:

  • enabled (String)

    Whether or not the extension is enabled

  • name (String)

    The name of the extension

Returns:

  • (Boolean)

    True if the extension was set to autoload or False if it was not



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet_x/eos/modules/extension.rb', line 141

def set_autoload(enabled, name, force)
  enabled = :true if enabled.nil?
  force = false if force.nil?

  name = URI(name).to_s.split('/')[-1]
  entry = "#{name}"
  entry << ' force' if force

  case enabled
  when :true
    if File.open(BOOTEXT).read.scan(/#{name}[\sforce\n|\n]/).size == 0
      File.open(BOOTEXT, 'a') { |f| f << "#{entry}\n" }
      return true
    end
  when :false
    contents = File.readlines(BOOTEXT)
    contents.delete("#{name}\n")
    File.open(BOOTEXT, 'w') do |f|
      f.puts(contents)
    end
    return true
  end
  false
end