Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
require 'linux_admin/chrony'

module LinuxAdmin
extend Common

class << self
attr_writer :logger
end
Expand Down
8 changes: 4 additions & 4 deletions lib/linux_admin/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ module Common

BIN_DIRS = %w(/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin)

def cmd(name)
def self.cmd(name)
BIN_DIRS.collect { |dir| "#{dir}/#{name}" }.detect { |cmd| File.exist?(cmd) }
end

def cmd?(name)
def self.cmd?(name)
!cmd(name).nil?
end

def run(cmd, options = {})
def self.run(cmd, options = {})
AwesomeSpawn.logger ||= logger
AwesomeSpawn.run(cmd, options)
end

def run!(cmd, options = {})
def self.run!(cmd, options = {})
AwesomeSpawn.logger ||= logger
AwesomeSpawn.run!(cmd, options)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/linux_admin/deb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.from_string(apt_cache_string)
end

def self.info(pkg)
self.from_string(run!(APT_CACHE_CMD, :params => ["show", pkg]).output)
from_string(Common.run!(APT_CACHE_CMD, :params => ["show", pkg]).output)
end
end
end
14 changes: 6 additions & 8 deletions lib/linux_admin/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module LinuxAdmin
class Disk
include Common

PARTED_FIELDS =
[:id, :start_sector, :end_sector,
:size, :partition_type, :fs_type]
Expand Down Expand Up @@ -62,7 +60,7 @@ def initialize(args = {})
def size
@size ||= begin
size = nil
out = run!(cmd(:fdisk), :params => {"-l" => nil}).output
out = Common.run!(Common.cmd(:fdisk), :params => {"-l" => nil}).output
out.each_line do |l|
/Disk #{path}: .*B, (\d+) bytes/.match(l) do |m|
size = m[1].to_i
Expand All @@ -86,7 +84,7 @@ def parted_output
# TODO: Should this really catch non-zero RC, set output to the default "" and silently return [] ?
# If so, should other calls to parted also do the same?
# requires sudo
out = run(cmd(:parted),
out = Common.run(Common.cmd(:parted),
:params => { nil => parted_options_array('print') }).output
split = []
out.each_line do |l|
Expand Down Expand Up @@ -121,11 +119,11 @@ def partition_from_parted(output_disk)
public

def create_partition_table(type = "msdos")
run!(cmd(:parted), :params => { nil => parted_options_array("mklabel", type)})
Common.run!(Common.cmd(:parted), :params => {nil => parted_options_array("mklabel", type)})
end

def has_partition_table?
result = run(cmd(:parted), :params => { nil => parted_options_array("print")})
result = Common.run(Common.cmd(:parted), :params => {nil => parted_options_array("print")})

result_indicates_partition_table?(result)
end
Expand All @@ -150,7 +148,7 @@ def create_partition(partition_type, *args)

id = partitions.empty? ? 1 : (partitions.last.id + 1)
options = parted_options_array('mkpart', '-a', 'opt', partition_type, start, finish)
run!(cmd(:parted), :params => { nil => options})
Common.run!(Common.cmd(:parted), :params => {nil => options})

partition = Partition.new(:disk => self,
:id => id,
Expand All @@ -174,7 +172,7 @@ def clear!
@partitions = []

# clear partition table
run!(cmd(:dd),
Common.run!(Common.cmd(:dd),
:params => { 'if=' => '/dev/zero', 'of=' => @path,
'bs=' => 512, 'count=' => 1})

Expand Down
10 changes: 4 additions & 6 deletions lib/linux_admin/hosts.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module LinuxAdmin
class Hosts
include Common

attr_accessor :filename
attr_accessor :raw_lines
attr_accessor :parsed_file
Expand Down Expand Up @@ -33,16 +31,16 @@ def set_canonical_hostname(address, hostname, comment = nil)
end

def hostname=(name)
if cmd?("hostnamectl")
run!(cmd('hostnamectl'), :params => ['set-hostname', name])
if Common.cmd?("hostnamectl")
Common.run!(Common.cmd('hostnamectl'), :params => ['set-hostname', name])
else
File.write("/etc/hostname", name)
run!(cmd('hostname'), :params => {:file => "/etc/hostname"})
Common.run!(Common.cmd('hostname'), :params => {:file => "/etc/hostname"})
end
end

def hostname
result = run(cmd("hostname"))
result = Common.run(Common.cmd("hostname"))
result.success? ? result.output.strip : nil
end

Expand Down
10 changes: 4 additions & 6 deletions lib/linux_admin/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module LinuxAdmin
class IpAddress
include Common

def address
address_list.detect { |ip| IPAddr.new(ip).ipv4? }
end
Expand All @@ -13,21 +11,21 @@ def address6
end

def mac_address(interface)
result = run(cmd("ip"), :params => ["addr", "show", interface])
result = Common.run(Common.cmd("ip"), :params => ["addr", "show", interface])
return nil if result.failure?

parse_output(result.output, %r{link/ether}, 1)
end

def netmask(interface)
result = run(cmd("ifconfig"), :params => [interface])
result = Common.run(Common.cmd("ifconfig"), :params => [interface])
return nil if result.failure?

parse_output(result.output, /netmask/, 3)
end

def gateway
result = run(cmd("ip"), :params => ["route"])
result = Common.run(Common.cmd("ip"), :params => ["route"])
return nil if result.failure?

parse_output(result.output, /^default/, 2)
Expand All @@ -45,7 +43,7 @@ def address_list
# Added retry to account for slow DHCP not assigning an IP quickly at boot; specifically:
# https://github.com/ManageIQ/manageiq-appliance/commit/160d8ccbfbfd617bdb5445e56cdab66b9323b15b
5.times do
result = run(cmd("hostname"), :params => ["-I"])
result = Common.run(Common.cmd("hostname"), :params => ["-I"])
break if result.success?
end

Expand Down
6 changes: 3 additions & 3 deletions lib/linux_admin/logical_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(args = {})
end

def extend_with(vg)
run!(cmd(:lvextend),
Common.run!(Common.cmd(:lvextend),
:params => [self.name, vg.name])
self
end
Expand Down Expand Up @@ -81,7 +81,7 @@ def self.create(name, vg, value)
size = value
params.merge!({'-L' => bytes_to_string(size)})
end
run!(cmd(:lvcreate), :params => params)
Common.run!(Common.cmd(:lvcreate), :params => params)

lv = LogicalVolume.new(:name => name,
:volume_group => vg,
Expand All @@ -92,7 +92,7 @@ def self.create(name, vg, value)

def self.scan
@lvs ||= begin
scan_volumes(cmd(:lvdisplay)) do |fields, vg|
scan_volumes(Common.cmd(:lvdisplay)) do |fields, vg|
LogicalVolume.new(:name => fields[0],
:volume_group => vg,
:sectors => fields[6].to_i)
Expand Down
13 changes: 5 additions & 8 deletions lib/linux_admin/mountable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ module LinuxAdmin
module Mountable
attr_accessor :fs_type
attr_accessor :mount_point
include Common

module ClassMethods
include Common

def mount_point_exists?(mount_point)
result = run!(cmd(:mount))
result = Common.run!(Common.cmd(:mount))
result.output.split("\n").any? { |line| line.split[2] == mount_point }
end

Expand All @@ -22,8 +19,8 @@ def self.included(base)
end

def format_to(filesystem)
run!(cmd(:mke2fs),
:params => { '-t' => filesystem, nil => self.path})
Common.run!(Common.cmd(:mke2fs),
:params => {'-t' => filesystem, nil => path})
@fs_type = filesystem
end

Expand All @@ -34,12 +31,12 @@ def mount(mount_point)
raise ArgumentError, "disk already mounted at #{mount_point}"
end

run!(cmd(:mount), :params => { nil => [self.path, mount_point] })
Common.run!(Common.cmd(:mount), :params => {nil => [path, mount_point]})
@mount_point = mount_point
end

def umount
run!(cmd(:umount), :params => { nil => [@mount_point] })
Common.run!(Common.cmd(:umount), :params => {nil => [@mount_point]})
end
end
end
10 changes: 4 additions & 6 deletions lib/linux_admin/network_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module LinuxAdmin
class NetworkInterface
include Common

# Cached class instance variable for what distro we are running on
@dist_class = nil

Expand Down Expand Up @@ -53,7 +51,7 @@ def reload

@network_conf[:mac] = parse_ip_output(ip_output, %r{link/ether}, 1)

ip_route_res = run!(cmd("ip"), :params => ["route"])
ip_route_res = Common.run!(Common.cmd("ip"), :params => ["route"])
@network_conf[:gateway] = parse_ip_output(ip_route_res.output, /^default/, 2) if ip_route_res.success?
true
rescue AwesomeSpawn::CommandResultError => e
Expand Down Expand Up @@ -121,14 +119,14 @@ def gateway
#
# @return [Boolean] whether the command succeeded or not
def start
run(cmd("ifup"), :params => [@interface]).success?
Common.run(Common.cmd("ifup"), :params => [@interface]).success?
end

# Brings down the network interface
#
# @return [Boolean] whether the command succeeded or not
def stop
run(cmd("ifdown"), :params => [@interface]).success?
Common.run(Common.cmd("ifdown"), :params => [@interface]).success?
end

private
Expand All @@ -149,7 +147,7 @@ def parse_ip_output(output, regex, col)
# @return [String] The command output
# @raise [NetworkInterfaceError] if the command fails
def ip_show
run!(cmd("ip"), :params => ["addr", "show", @interface]).output
Common.run!(Common.cmd("ip"), :params => ["addr", "show", @interface]).output
rescue AwesomeSpawn::CommandResultError => e
raise NetworkInterfaceError.new(e.message, e.result)
end
Expand Down
1 change: 0 additions & 1 deletion lib/linux_admin/package.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module LinuxAdmin
class Package
extend Common
end
end
9 changes: 3 additions & 6 deletions lib/linux_admin/physical_volume.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module LinuxAdmin
class PhysicalVolume < Volume
include Common
extend Common

# physical volume device name
attr_accessor :device_name

Expand All @@ -29,7 +26,7 @@ def initialize(args = {})
end

def attach_to(vg)
run!(cmd(:vgextend),
Common.run!(Common.cmd(:vgextend),
:params => [vg.name, @device_name])
self.volume_group = vg
self
Expand All @@ -38,7 +35,7 @@ def attach_to(vg)
# specify disk or partition instance to create physical volume on
def self.create(device)
self.scan # initialize local physical volumes
run!(cmd(:pvcreate),
Common.run!(Common.cmd(:pvcreate),
:params => { nil => device.path})
pv = PhysicalVolume.new(:device_name => device.path,
:volume_group => nil,
Expand All @@ -49,7 +46,7 @@ def self.create(device)

def self.scan
@pvs ||= begin
scan_volumes(cmd(:pvdisplay)) do |fields, vg|
scan_volumes(Common.cmd(:pvdisplay)) do |fields, vg|
PhysicalVolume.new(:device_name => fields[0],
:volume_group => vg,
:size => fields[2].to_i)
Expand Down
3 changes: 1 addition & 2 deletions lib/linux_admin/registration_system.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module LinuxAdmin
class RegistrationSystem
include Common
include Logging

def self.registration_type(reload = false)
Expand Down Expand Up @@ -51,4 +50,4 @@ def install_server_certificate(server, cert_path)
end
end

Dir.glob(File.join(File.dirname(__FILE__), "registration_system", "*.rb")).each { |f| require f }
Dir.glob(File.join(File.dirname(__FILE__), "registration_system", "*.rb")).each { |f| require f }
10 changes: 5 additions & 5 deletions lib/linux_admin/registration_system/rhn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def register(options)
params["--systemorgid="] = options[:org] if options[:server_url] && options[:org]
params["--sslCACert="] = INSTALLED_SERVER_CERT_PATH if certificate_installed

run!(cmd, :params => params)
Common.run!(cmd, :params => params)
end

def enable_channel(repo, options)
cmd = "rhn-channel -a"
params = user_pwd(options).merge("--channel=" => repo)

logger.info("#{self.class.name}##{__method__} Enabling channel: #{repo}")
run!(cmd, :params => params)
Common.run!(cmd, :params => params)
end
alias_method :subscribe, :enable_channel
alias_method :enable_repo, :enable_channel
Expand All @@ -54,14 +54,14 @@ def disable_channel(repo, options)
cmd = "rhn-channel -r"
params = user_pwd(options).merge("--channel=" => repo)

run!(cmd, :params => params)
Common.run!(cmd, :params => params)
end
alias_method :disable_repo, :disable_channel

def enabled_channels
cmd = "rhn-channel -l"

run!(cmd).output.split("\n").compact
Common.run!(cmd).output.split("\n").compact
end
alias_method :enabled_repos, :enabled_channels
alias_method :subscribed_products, :enabled_channels
Expand All @@ -70,7 +70,7 @@ def available_channels(options)
cmd = "rhn-channel -L"
params = user_pwd(options)

run!(cmd, :params => params).output.chomp.split("\n").compact
Common.run!(cmd, :params => params).output.chomp.split("\n").compact
end

def all_repos(options)
Expand Down
Loading