Class: RightScale::Platform

Inherits:
Object show all
Includes:
RightSupport::Ruby::EasySingleton
Defined in:
lib/right_agent/platform.rb,
lib/right_agent/platform/unix/platform.rb,
lib/right_agent/platform/windows/platform.rb,
lib/right_agent/platform/unix/linux/platform.rb,
lib/right_agent/platform/unix/darwin/platform.rb,
lib/right_agent/platform/windows/mingw/platform.rb,
lib/right_agent/platform/windows/mswin/platform.rb

Overview

Windows specific implementation

Defined Under Namespace

Classes: CommandError, Controller, Filesystem, Installer, PlatformHelperBase, Process, Rng, Shell, VolumeManager, Win32Error, WindowsCommon, WindowsSecurity, WindowsSystemInformation

Constant Summary collapse

FEDORA_REL =
'/etc/fedora-release'
FEDORA_SIG =
/Fedora release ([0-9]+) \((.*)\)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codenameObject (readonly)

Returns the value of attribute codename.



62
63
64
# File 'lib/right_agent/platform.rb', line 62

def codename
  @codename
end

#flavorObject (readonly)

Returns the value of attribute flavor.



62
63
64
# File 'lib/right_agent/platform.rb', line 62

def flavor
  @flavor
end

#releaseObject (readonly)

Returns the value of attribute release.



62
63
64
# File 'lib/right_agent/platform.rb', line 62

def release
  @release
end

Instance Method Details

#centos?TrueClass|FalseClass

Is this machine running CentOS?

Returns:

  • (TrueClass|FalseClass)

    true if Linux flavor is CentOS



43
44
45
# File 'lib/right_agent/platform/unix/linux/platform.rb', line 43

def centos?
  !!(@flavor =~ /centos/)
end

#controllerController

Returns Platform-specific controller object.

Returns:

  • (Controller)

    Platform-specific controller object



177
178
179
# File 'lib/right_agent/platform.rb', line 177

def controller
  platform_service(:controller)
end

#darwin?TrueClass|FalseClass

Is current platform Darwin?

Returns:

  • (TrueClass|FalseClass)

    true if Darwin



104
105
106
# File 'lib/right_agent/platform.rb', line 104

def darwin?
  species == :darwin
end

#dispatch(*args) { ... } ⇒ Object

Call platform specific implementation of method whose symbol is returned by the passed in block. Arguments are passed through. e.g.

Platform.dispatch(2) { :echo }

will result in ‘echo_linux(2)’ being executed in self if running on linux, ‘echo_windows(2)’ if running on Windows and ‘echo_darwin(2)’ if on Mac OS X. Note that the method is run in the instance of the caller.

Parameters:

  • args (Array)

    as pass-through arguments

Yields:

  • given block should not take any argument and return a symbol

    for the method that should be called.

Returns:

  • (Object)

    result of Platform-specific implementation



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/right_agent/platform.rb', line 164

def dispatch(*args, &blk)
  raise 'Platform.dispatch requires a block' unless blk
  binding = blk.binding.eval('self')
  meth = blk.call
  target = dispatch_candidates(meth).detect do |candidate|
    binding.respond_to?(candidate)
  end
  raise "No platform dispatch target found in #{binding.class} for " +
        "'#{meth.inspect}', tried " + dispatch_candidates(meth).join(', ') unless target
  binding.send(target, *args)
end

#ec2?TrueClass|FalseClass

Deprecated.

use right_link cloud libraries instead.

Are we on an EC2 cloud instance?

Returns:

  • (TrueClass|FalseClass)

    true if EC2



120
121
122
123
124
# File 'lib/right_agent/platform.rb', line 120

def ec2?
  warn "#{self.class.name}#ec2? is deprecated, use right_link cloud libraries instead.\n#{caller[0,2].join("\n")}"
  resolve_cloud_type unless @cloud_type
  @cloud_type == 'ec2'
end

#eucalyptus?TrueClass|FalseClass

Deprecated.

use right_link cloud libraries instead.

Are we on an Eucalyptus cloud instance?

Returns:

  • (TrueClass|FalseClass)

    true if Eucalyptus



142
143
144
145
146
# File 'lib/right_agent/platform.rb', line 142

def eucalyptus?
  warn "#{self.class.name}#eucalyptus? is deprecated, use right_link cloud libraries instead.\n#{caller[0,2].join("\n")}"
  resolve_cloud_type unless @cloud_type
  @cloud_type == 'eucalyptus'
end

#execute(command, options = {}) ⇒ String

Blocking call to invoke a command line tool used to perform platform- specific tasks.

Also provides a consistent interface for mocking command output during spec testing. Implementations should use this method instead of embedding popen/backtick calls to assist with testing.

Parameters:

  • command (String)

    to run

  • options (Hash) (defaults to: {})

    for execution

Options Hash (options):

  • :raise_on_failure (TrueClass|FalseClass)

    true to raise on command failure (Default)

Returns:

  • (String)

    output from command or empty

Raises:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/right_agent/platform.rb', line 225

def execute(command, options = {})
  options = { :raise_on_failure => true }.merge(options)
  raise_on_failure = options[:raise_on_failure]
  output_text = ''
  begin
    output_text = `#{command}`
    if !$?.success? && options[:raise_on_failure]
      message = []
      message << "Command failed with exit code = #{$?.exitstatus}:"
      message << "> #{command}"
      error_output_text = output_text.strip
      message << error_output_text unless error_output_text.empty?
      e = CommandError.new(message.join("\n"))
      e.command = command
      e.status = $?
      e.output_text = output_text
      raise e
    end
  rescue Errno::ENOENT => e
    if raise_on_failure
      raise CommandError, "Command failed: #{e.message}"
    end
  end
  output_text
end

#familySymbol

Deprecated.

family is a legacy definition, see genus/species for a more granular definition.

Generic platform family

Returns:

  • (Symbol)

    result as one of :linux, :windows or :darwin



70
71
72
73
# File 'lib/right_agent/platform.rb', line 70

def family
  warn "#{self.class.name}#family is deprecated, use genus or species instead.\n#{caller[0,2].join("\n")}"
  (genus == :unix) ? species : genus
end

#filesystemFilesystem

Returns Platform-specific filesystem config object.

Returns:

  • (Filesystem)

    Platform-specific filesystem config object



182
183
184
# File 'lib/right_agent/platform.rb', line 182

def filesystem
  platform_service(:filesystem)
end

#genusSymbol

Returns platform genus.

Returns:

  • (Symbol)

    platform genus



76
77
78
79
# File 'lib/right_agent/platform.rb', line 76

def genus
  resolve_taxonomy unless @genus
  @genus
end

#installerInstaller

Returns Platform-specific installer information object.

Returns:

  • (Installer)

    Platform-specific installer information object



207
208
209
# File 'lib/right_agent/platform.rb', line 207

def installer
  platform_service(:installer)
end

#linux?TrueClass|FalseClass

Is current platform Linux?

Returns:

  • (TrueClass|FalseClass)

    true if Linux



97
98
99
# File 'lib/right_agent/platform.rb', line 97

def linux?
  species == :linux
end

#processProcess

Returns Platform-specific process facilities object.

Returns:

  • (Process)

    Platform-specific process facilities object



202
203
204
# File 'lib/right_agent/platform.rb', line 202

def process
  platform_service(:process)
end

#rackspace?TrueClass|FalseClass

Deprecated.

use right_link cloud libraries instead.

Are we on an Rackspace cloud instance?

Returns:

  • (TrueClass|FalseClass)

    true if Rackspace



131
132
133
134
135
# File 'lib/right_agent/platform.rb', line 131

def rackspace?
  warn "#{self.class.name}#rackspace? is deprecated, use right_link cloud libraries instead.\n#{caller[0,2].join("\n")}"
  resolve_cloud_type unless @cloud_type
  @cloud_type == 'rackspace'
end

#resolve_cloud_typeString

Deprecated.

leverage the right_link cloud libraries for any cloud- specific behavior because the behavior of all possible clouds is beyond the scope of hard-coded case statements.

Determines which cloud we’re on by the cheap but simple expedient of reading the RightScale cloud file.

Returns:

  • (String)

    cloud type or nil



259
260
261
262
263
# File 'lib/right_agent/platform.rb', line 259

def resolve_cloud_type
  cloud_file_path = ::File.join(self.filesystem.right_scale_static_state_dir, 'cloud')
  @cloud_type = ::File.read(cloud_file_path) rescue nil
  @cloud_type
end

#rhel?TrueClass|FalseClass

Is this machine running RHEL?

Returns:

  • (TrueClass|FalseClass)

    true if Linux flavor is RHEL



57
58
59
# File 'lib/right_agent/platform/unix/linux/platform.rb', line 57

def rhel?
  !!(@flavor =~ /redhatenterpriseserver/)
end

#rngRng

Returns Platform-specific RNG object.

Returns:

  • (Rng)

    Platform-specific RNG object



197
198
199
# File 'lib/right_agent/platform.rb', line 197

def rng
  platform_service(:rng)
end

#shellShell

Returns Platform-specific shell information object.

Returns:

  • (Shell)

    Platform-specific shell information object



192
193
194
# File 'lib/right_agent/platform.rb', line 192

def shell
  platform_service(:shell)
end

#speciesSymbol

Returns platform species.

Returns:

  • (Symbol)

    platform species



82
83
84
85
# File 'lib/right_agent/platform.rb', line 82

def species
  resolve_taxonomy unless @species
  @species
end

#suse?TrueClass|FalseClass

Is this machine running Suse?

Returns:

  • (TrueClass|FalseClass)

    true if Linux flavor is Suse



50
51
52
# File 'lib/right_agent/platform/unix/linux/platform.rb', line 50

def suse?
  !!(@flavor =~ /suse/)
end

#ubuntu?TrueClass|FalseClass

Is this machine running Ubuntu?

Returns:

  • (TrueClass|FalseClass)

    true if Linux flavor is Ubuntu



36
37
38
# File 'lib/right_agent/platform/unix/linux/platform.rb', line 36

def ubuntu?
  !!(@flavor =~ /ubuntu/)
end

#unix?TrueClass|FalseClass

Is current platform in the Unix genus?

Returns:

  • (TrueClass|FalseClass)

    true if Unix



90
91
92
# File 'lib/right_agent/platform.rb', line 90

def unix?
  genus == :unix
end

#volume_managerVolumeManager

Returns Platform-specific volume manager config object.

Returns:

  • (VolumeManager)

    Platform-specific volume manager config object



187
188
189
# File 'lib/right_agent/platform.rb', line 187

def volume_manager
  platform_service(:volume_manager)
end

#windows?TrueClass|FalseClass

Is current platform Windows?

Returns:

  • (TrueClass|FalseClass)

    true if Windows



111
112
113
# File 'lib/right_agent/platform.rb', line 111

def windows?
  genus == :windows
end

#windows_commonWindowsCommon

Returns Platform-specific Windows common.

Returns:



1769
1770
1771
# File 'lib/right_agent/platform/windows/platform.rb', line 1769

def windows_common
  platform_service(:windows_common)
end

#windows_securityWindowsSecurity

Returns Platform-specific Windows security.

Returns:



1774
1775
1776
# File 'lib/right_agent/platform/windows/platform.rb', line 1774

def windows_security
  platform_service(:windows_security)
end

#windows_system_informationWindowsSystemInformation

Returns Platform-specific Windows system information.

Returns:



1779
1780
1781
# File 'lib/right_agent/platform/windows/platform.rb', line 1779

def windows_system_information
  platform_service(:windows_system_information)
end