class SyncWrap::Debian
Customizations for Debian and possibly other deb packaged derivatives. Specific distros/versions may further specialize.
Constants
- UPDATE_LOCK
Attributes
Debian version, i.e. '7.6' No default value.
Debian version, i.e. '7.6' No default value.
Set true/false to override the default, distro version based determination of whether systemd is PID 1 on the system.
Public Class Methods
# File lib/syncwrap/components/debian.rb, line 46 def initialize( opts = {} ) super end
Public Instance Methods
Enable a service by (short) name either via Debian/System V `update-rc.d` or systemd `systemctl enable`.
# File lib/syncwrap/components/debian.rb, line 151 def dist_enable_init_service( name ) if systemd? systemctl( 'enable', dot_service( name ) ) else sudo "/usr/sbin/update-rc.d #{name} enable" end end
Wrap block in a sudo bash conditional testing if the single specified pkg is installed.
# File lib/syncwrap/components/debian.rb, line 138 def dist_if_installed?( pkg, opts = {}, &block ) qry = "dpkg-query -W -f '${db:Status-Status}\\n' #{pkg}" tst = qry + " | grep -q -E '^installed$'" sudo_if( tst, opts, &block ) end
If chk is true, then wrap block in a sudo bash conditional testing if any specified pkgs are not installed. Otherwise just yield to block.
# File lib/syncwrap/components/debian.rb, line 126 def dist_if_not_installed?( pkgs, chk = true, opts = {}, &block ) if chk qry = "dpkg-query -W -f '${db:Status-Status}\\n' #{pkgs.join ' '}" cnt = qry + " | grep -c -E '^installed$'" sudo_if( %Q{[ "$(#{cnt})" != "#{pkgs.count}" ]}, opts, &block ) else block.call end end
Install the specified package names. Also calls dist_update before any package is actually installed. A trailing hash is interpreted as options, see below.
Options¶ ↑
- :check_install
-
Short-circuit if all packages already installed. Thus no upgrades will be performed. (Default: true)
- :minimal
-
If true, pass –no-install-recommends flag to apt-get.
- :update_required
-
Passed to dist_update
Options are also passed to the sudo calls.
# File lib/syncwrap/components/debian.rb, line 73 def dist_install( *args ) opts = args.last.is_a?( Hash ) && args.pop || {} args.flatten! flags = [] flags << '--no-install-recommends' if opts[ :minimal ] chk = opts[ :check_install ] chk = check_install? if chk.nil? dist_if_not_installed?( args, chk != false, opts ) do dist_update( opts ) sudo( "apt-get -yq install #{(flags + args).join ' '}", opts ) end end
Install a System V style init.d service script
# File lib/syncwrap/components/debian.rb, line 145 def dist_install_init_service( name ) sudo "/usr/sbin/update-rc.d #{name} defaults" end
Run the service command typically supporting 'start', 'stop', 'restart', 'status', etc. actions. Arguments are in order: shortname, action
# File lib/syncwrap/components/debian.rb, line 162 def dist_service( *args ) if systemd? dist_service_via_systemctl( *args ) else sudo( [ '/usr/sbin/service', *args ].join( ' ' ) ) end end
Uninstall the specified package names. A trailing hash is interpreted as options and passed to the sudo calls.
# File lib/syncwrap/components/debian.rb, line 113 def dist_uninstall( *pkgs ) opts = pkgs.last.is_a?( Hash ) && pkgs.pop || {} pkgs.flatten! pkgs.each do |pkg| dist_if_installed?( pkg, opts ) do sudo( "apt-get -yq --purge remove #{pkg}", opts ) end end end
Conditionally run “apt-get update” to resynchronize the package index files from their sources. Normally this is handled automatically by dist_install, but it can also be called directly, for example when a new source is added.
Options¶ ↑
- :update_required
-
If true, always perform update. If specified as false, do nothing. If unspecified, update if it hasn't already been run in the last hour via syncwrap (using a lock file).
Options are also passed to the sudo calls.
# File lib/syncwrap/components/debian.rb, line 99 def dist_update( opts = {} ) req = opts[ :update_required ] if ( req != false ) dist_if_update_old?( req != true, opts ) do sudo( " apt-get -yqq update touch #{UPDATE_LOCK} ", opts ) end end end
# File lib/syncwrap/components/debian.rb, line 50 def systemd? if @systemd.nil? @systemd = version_gte?( debian_version, [8] ) end @systemd end
Protected Instance Methods
# File lib/syncwrap/components/debian.rb, line 172 def dist_if_update_old?( chk = true, opts = {}, &block ) if chk l = UPDATE_LOCK sudo_if( %Q{[[ ! -e #{l} || -n "$(find #{l} -mmin +60)" ]]}, opts, &block ) else block.call end end