Class: SpecInfra::Command::Linux

Inherits:
Base
  • Object
show all
Defined in:
lib/specinfra/command/linux.rb

Direct Known Subclasses

Arch, Debian, Gentoo, NixOS, Plamo, RedHat, SuSE

Instance Method Summary collapse

Methods inherited from Base

#check_authorized_key, #check_belonging_group, #check_cotainer_running, #check_cron_entry, #check_directory, #check_enabled, #check_file, #check_file_checksum, #check_file_contain, #check_file_contain_lines, #check_file_contain_with_fixed_strings, #check_file_contain_with_regexp, #check_file_contain_within, #check_file_md5checksum, #check_file_sha256checksum, #check_gid, #check_group, #check_grouped, #check_home_directory, #check_installed, #check_installed_by_cpan, #check_installed_by_gem, #check_installed_by_npm, #check_installed_by_pear, #check_installed_by_pecl, #check_installed_by_pip, #check_ipfilter_rule, #check_ipnat_rule, #check_link, #check_listening, #check_listening_with_protocol, #check_login_shell, #check_mail_alias, #check_mode, #check_monitored_by_god, #check_monitored_by_monit, #check_mounted, #check_owner, #check_primary_group, #check_process, #check_process_count, #check_reachable, #check_resolvable, #check_routing_table, #check_running, #check_running_under_supervisor, #check_running_under_upstart, #check_service_installed, #check_service_start_mode, #check_socket, #check_svcprop, #check_svcprops, #check_uid, #check_user, #check_yumrepo, #check_yumrepo_enabled, #escape, #get_file_content, #get_file_mtime, #get_file_size, #get_ipaddress_of_host, #get_mode, #get_package_version, #get_process

Instance Method Details

#check_access_by_user(file, user, access) ⇒ Object



4
5
6
# File 'lib/specinfra/command/linux.rb', line 4

def check_access_by_user(file, user, access)
  "su -s /bin/sh -c \"test -#{access} #{file}\" #{user}"
end

#check_attribute(file, attribute) ⇒ Object



69
70
71
# File 'lib/specinfra/command/linux.rb', line 69

def check_attribute(file, attribute)
  "lsattr -d #{escape(file)} 2>&1 | awk '$1~/^-*#{escape(attribute)}-*$/ {exit 0} {exit 1}'"
end

#check_container(container) ⇒ Object



61
62
63
# File 'lib/specinfra/command/linux.rb', line 61

def check_container(container)
  "lxc-ls -1 | grep -w #{escape(container)}"
end

#check_container_running(container) ⇒ Object



65
66
67
# File 'lib/specinfra/command/linux.rb', line 65

def check_container_running(container)
  "lxc-info -n #{escape(container)} -s | grep -w RUNNING"
end

#check_immutable(file) ⇒ Object



73
74
75
# File 'lib/specinfra/command/linux.rb', line 73

def check_immutable(file)
  check_attribute(file, 'i')
end

#check_iptables_rule(rule, table = nil, chain = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/specinfra/command/linux.rb', line 8

def check_iptables_rule(rule, table=nil, chain=nil)
  cmd = "iptables"
  cmd += " -t #{escape(table)}" if table
  cmd += " -S"
  cmd += " #{escape(chain)}" if chain
  cmd += " | grep -- #{escape(rule)}"
  cmd += " || iptables-save"
  cmd += " -t #{escape(table)}" if table
  cmd += " | grep -- #{escape(rule)}"
  cmd
end

#check_ipv4_address(interface, ip_address) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/specinfra/command/linux.rb', line 37

def check_ipv4_address(interface, ip_address)
  ip_address = ip_address.dup
  if ip_address =~ /\/\d+$/
    ip_address << " "
  else
    ip_address << "/"
  end
  ip_address.gsub!(".", "\\.")
  "ip addr show #{interface} | grep 'inet #{ip_address}'"
end

#check_kernel_module_loaded(name) ⇒ Object



29
30
31
# File 'lib/specinfra/command/linux.rb', line 29

def check_kernel_module_loaded(name)
  "lsmod | grep ^#{name}"
end

#check_selinux(mode) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/specinfra/command/linux.rb', line 20

def check_selinux(mode)
  cmd =  ""
  cmd += "test ! -f /etc/selinux/config || (" if mode == "disabled"
  cmd += "getenforce | grep -i -- #{escape(mode)} "
  cmd += "&& grep -i -- ^SELINUX=#{escape(mode)}$ /etc/selinux/config"
  cmd += ")" if mode == "disabled"
  cmd
end

#check_zfs(zfs, property = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/specinfra/command/linux.rb', line 48

def check_zfs(zfs, property=nil)
  if property.nil?
    "zfs list -H #{escape(zfs)}"
  else
    commands = []
    property.sort.each do |key, value|
      regexp = "^#{value}$"
      commands << "zfs list -H -o #{escape(key)} #{escape(zfs)} | grep -- #{escape(regexp)}"
    end
    commands.join(' && ')
  end
end

#get_interface_speed_of(name) ⇒ Object



33
34
35
# File 'lib/specinfra/command/linux.rb', line 33

def get_interface_speed_of(name)
  "ethtool #{name} | grep Speed | gawk '{print gensub(/Speed: ([0-9]+)Mb\\\/s/,\"\\\\1\",\"\")}'"
end