class FPM::Fry::Plugin::Service::DSL

Attributes

limits[R]

@return [Hash<String,Tuple<Numeric,Numeric>]

Public Class Methods

new(name) click to toggle source

@api private

# File lib/fpm/fry/plugin/service.rb, line 29
def initialize(name)
  @name = name
  @command = []
  @limits = {}
  @user = nil
  @group = nil
  @chdir = nil
end

Public Instance Methods

add!(builder) click to toggle source

@api private

# File lib/fpm/fry/plugin/service.rb, line 122
def add!(builder)
  init = builder.plugin('init')
  if init.systemd?
    add_systemd!(builder)
  elsif init.upstart?
    add_upstart!(builder)
  elsif init.sysv?
    add_sysv!(builder)
  end
end
chdir( dir = nil ) click to toggle source

@overload chdir

@return [String,nil] working directory of the service

@overload chdir( dir )

@param  [String] dir new working directory of the service
@return [String] working directory of the service
# File lib/fpm/fry/plugin/service.rb, line 107
def chdir( dir = nil )
  if dir
    @chdir = dir
  end
  @chdir
end
command( *args ) click to toggle source
# File lib/fpm/fry/plugin/service.rb, line 114
def command( *args )
  if args.any?
    @command = args
  end
  return @command
end
group( group = nil ) click to toggle source

@overload group

@return [String] the linux user group this service should run as

@overload group( name )

@param  [String] name new linux user group this service should run as
@return [String] the linux user group this service should run as
# File lib/fpm/fry/plugin/service.rb, line 55
def group( group = nil )
  if group
    @group = group
  end
  return @group
end
limit( name, soft, hard = soft ) click to toggle source

Sets a limit for this service. Valid limits are:

- core
- cpu
- data
- fsize
- memlock
- msgqueue
- nice
- nofile
- nproc
- rss
- rtprio
- sigpending
- stack

@see linux.die.net/man/5/limits.conf Limits.conf manpage for limits and their meanings. @param [String] name see above list for valid limits @param [Numeric,“unlimited”] soft soft limit @param [Numeric,“unlimited”] hard hard limit

# File lib/fpm/fry/plugin/service.rb, line 94
def limit( name, soft, hard = soft )
  unless LIMITS.include? name
    raise ArgumentError, "Unknown limit #{name.inspect}. Known limits are: #{LIMITS.inspect}"
  end
  @limits[name] = [soft,hard]
  return nil
end
name( name = nil ) click to toggle source

@overload name

@return [String] this service's name

@overload name( name )

@param  [String] name new name for this service
@return [String] this service's name
# File lib/fpm/fry/plugin/service.rb, line 43
def name( name = nil )
  if name
    @name = name
  end
  return @name
end
user( n = nil ) click to toggle source

@overload user

@return [String] the linux user this service should run as

@overload user( name )

@param  [String] name new linx user this service should run as
@return [String] the linux user this service should run as
# File lib/fpm/fry/plugin/service.rb, line 67
def user( n = nil )
  if n
    @user = n
  end
  return @user
end

Private Instance Methods

add_systemd!(builder) click to toggle source
# File lib/fpm/fry/plugin/service.rb, line 187
    def add_systemd!(builder)
      edit = builder.plugin('edit_staging')
      env = Environment.new(name, command, "", @limits, @user, @group, @chdir)
      edit.add_file "/lib/systemd/system/#{name}.service", StringIO.new( env.render('systemd.erb') ), chmod: '644'
      builder.plugin('script_helper') do |sh|
        sh.after_install_or_upgrade(<<BASH)
systemctl preset #{Shellwords.shellescape name}.service
if systemctl is-enabled --quiet #{Shellwords.shellescape name}.service ; then
systemctl --system daemon-reload
systemctl restart #{Shellwords.shellescape name}.service
fi
BASH
        sh.before_remove_entirely(<<BASH)
systemctl disable --now #{Shellwords.shellescape name}.service
BASH

      end
    end
add_sysv!(builder) click to toggle source
# File lib/fpm/fry/plugin/service.rb, line 168
    def add_sysv!(builder)
      edit = builder.plugin('edit_staging')
      env = Environment.new(name, command, "", @limits, @user, @group, @chdir)
      edit.add_file "/etc/init.d/#{name}",StringIO.new( env.render('sysv.erb') ), chmod: '750'
      builder.plugin('script_helper') do |sh|
        sh.after_install_or_upgrade(<<BASH)
update-rc.d #{Shellwords.shellescape name} defaults
/etc/init.d/#{Shellwords.shellescape name} restart
BASH
        sh.before_remove_entirely(<<BASH)
/etc/init.d/#{Shellwords.shellescape name} stop
update-rc.d -f #{Shellwords.shellescape name} remove
BASH
      end
      builder.plugin('config', FPM::Fry::Plugin::Config::IMPLICIT => true) do |co|
        co.include "etc/init.d/#{name}"
      end
    end
add_upstart!(builder) click to toggle source
# File lib/fpm/fry/plugin/service.rb, line 133
    def add_upstart!(builder)
      init = builder.plugin('init')
      edit = builder.plugin('edit_staging')
      env = Environment.new(name, command, "", @limits, @user, @group, @chdir)
      edit.add_file "/etc/init/#{name}.conf",StringIO.new( env.render('upstart.erb') )
      if init.with? :sysvcompat
        edit.ln_s init.with[:sysvcompat], "/etc/init.d/#{name}"
      end
      builder.plugin('script_helper') do |sh|
        sh.after_install_or_upgrade(<<BASH)
if status #{Shellwords.shellescape name} 2>/dev/null | grep -q ' start/'; then
# It has to be stop+start because upstart doesn't pickup changes with restart.
if which invoke-rc.d >/dev/null 2>&1; then
  invoke-rc.d #{Shellwords.shellescape name} stop
else
  stop #{Shellwords.shellescape name}
fi
fi
if which invoke-rc.d >/dev/null 2>&1; then
invoke-rc.d #{Shellwords.shellescape name} start
else
start #{Shellwords.shellescape name}
fi
BASH
        sh.before_remove_entirely(<<BASH)
if status #{Shellwords.shellescape name} 2>/dev/null | grep -q ' start/'; then
stop #{Shellwords.shellescape name}
fi
BASH
      end
      builder.plugin('config', FPM::Fry::Plugin::Config::IMPLICIT => true) do |co|
        co.include "etc/init/#{name}.conf"
      end
    end