Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial spike for pleaserun support #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions fpm-cookery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "puppet"
s.add_runtime_dependency "addressable"
s.add_runtime_dependency "systemu"
s.add_runtime_dependency "pleaserun"
end
5 changes: 5 additions & 0 deletions lib/fpm/cookery/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def dispense
recipe.installing = true
Log.info "Installing into #{recipe.destdir}"
recipe.install

# Write startup scripts.
recipe.startup_scripts.each do |script|
script.write_to(recipe.destdir)
end
ensure
recipe.installing = false
end
Expand Down
27 changes: 27 additions & 0 deletions lib/fpm/cookery/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require 'fpm/cookery/package/dir'
require 'fpm/cookery/package/gem'
require 'fpm/cookery/package/npm'
require 'fpm/cookery/startup_script'
require 'fpm/cookery/log'

module FPM
module Cookery
Expand Down Expand Up @@ -38,6 +40,31 @@ def self.architectures(archs)
Array(archs).member?(FPM::Cookery::Facts.arch) and block_given? ? yield : false
end

def self.startup_script(program, options = {})
script = FPM::Cookery::StartupScript.new

options.merge(:program => program).each do |key, value|
next if value.to_s.empty?

begin
script.__send__("#{key}=", value)
rescue NoMethodError
Log.warn(%(Invalid startup script option "#{key}"!))
end
end

@startup_scripts ||= []
@startup_scripts << script
end

def self.startup_scripts
@startup_scripts
end

def startup_scripts
self.class.startup_scripts
end

def self.attr_rw_list(*attrs)
attrs.each do |attr|
class_eval %Q{
Expand Down
50 changes: 50 additions & 0 deletions lib/fpm/cookery/startup_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'pleaserun/namespace'
require 'pleaserun/detector'

module FPM
module Cookery
class StartupScript
attr_accessor :program, :name, :args, :user, :group, :description,
:chdir, :umask, :prestart

def initialize
@platform, @target_version = PleaseRun::Detector.detect

require "pleaserun/platform/#{@platform}"
const = PleaseRun::Platform.constants.find { |c| c.to_s.downcase == @platform.downcase }

@platform_class = PleaseRun::Platform.const_get(const)
@runner = @platform_class.new(@target_version)
end

def write_to(root)
ensure_attributes!

@runner.files.each do |path, content, perms|
fullpath = root + path.gsub(%r(^/+), '')

FileUtils.mkdir_p(File.dirname(fullpath))
File.open(fullpath, 'w') {|f| f.write(content) }
File.chmod(perms, fullpath) if perms
end

# TODO Check how to install action scripts. pre/post script?
#PleaseRun::Installer.write_actions(@runner, ...)
end

private

def ensure_attributes!
@runner.program = program
@runner.name = name || File.basename(program)
@runner.args = args.split(/\s+/) if args
@runner.user = user if user
@runner.group = group if group
@runner.description = description if description
@runner.chdir = chdir if chdir
@runner.umask = umask if umask
@runner.prestart = prestart if prestart
end
end
end
end