Skip to content

Instantly share code, notes, and snippets.

@jonasschneider
Created February 2, 2013 21:13
Show Gist options
  • Save jonasschneider/4699263 to your computer and use it in GitHub Desktop.
Save jonasschneider/4699263 to your computer and use it in GitHub Desktop.
foreman-systemd
#! /usr/bin/env ruby
require 'foreman/cli'
require File.expand_path(File.dirname(__FILE__)) + '/foreman_export_systemd.rb'
Foreman::CLI.start
require "erb"
require "foreman/export"
module Foreman::Export
class << self
alias_method :old_formatter, :formatter
def formatter(name)
return Foreman::Export::Systemd if name == 'systemd'
old_formatter(name)
end
end
end
class Foreman::Export::Systemd < Foreman::Export::Base
def export
FileUtils.mkdir_p(location) rescue error("Could not create: #{location}")
Dir["#{location}/#{app}*.conf"].each do |file|
clean file
end
options[:template] = File.dirname(__FILE__)
write_template "x/master.target.erb", "#{app}.target", binding
engine.each_process do |name, process|
next if engine.formation[name] < 1
write_template "x/process_master.target.erb", "#{app}-#{name}.target", binding
1.upto(engine.formation[name]) do |num|
port = engine.port_for(process, num)
write_template "x/process.service.erb", "#{app}-#{name}-#{num}.service", binding
end
end
end
end
[Unit]<% engine.each_process do |name, process| %>
Wants=<%= app %>-<%= name %>.target<% end %>
[Unit]
StopWhenUnneeded=true
[Service]
User=<%= user %>
WorkingDirectory=<%= engine.root %>
Environment=PORT=<%= port %><% engine.env.each_pair do |var,env| %>
Environment=<%= var.upcase %>=<%= env %><% end %>
ExecStart=/bin/bash -lc '<%= process.command %>'
Restart=always
StandardInput=null
KillMode=process
[Unit]
StopWhenUnneeded=true<% 1.upto(engine.formation[name]) do |num| %>
Wants=<%= app %>-<%= name %>-<%= num %>.service<% end %>
@nstielau
Copy link

Hey @jonasschneider

Have you thought about making this into a gem, a la https://github.com/nature/foreman-export-nature? I also found a abandoned pull-request with something similar (https://github.com/ddollar/foreman/pull/294/files). If you were interested, we could add you to the github.com/systemd group, which contains some systemd integration projects.

Cheers

-Nick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment