Created
February 2, 2013 21:13
-
-
Save jonasschneider/4699263 to your computer and use it in GitHub Desktop.
foreman-systemd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'foreman/cli' | |
require File.expand_path(File.dirname(__FILE__)) + '/foreman_export_systemd.rb' | |
Foreman::CLI.start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit]<% engine.each_process do |name, process| %> | |
Wants=<%= app %>-<%= name %>.target<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
StopWhenUnneeded=true<% 1.upto(engine.formation[name]) do |num| %> | |
Wants=<%= app %>-<%= name %>-<%= num %>.service<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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