Created
March 2, 2014 20:27
-
-
Save lmars/9313266 to your computer and use it in GitHub Desktop.
config-exec
This file contains 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/local/bin/ruby | |
# | |
# config-exec will render any files in /config and then exec | |
# whatever command was given on the command line. | |
# | |
# For example, if /config/etc/redis.conf.erb exists, then to | |
# render it into /etc/redis.conf and run Redis with the result: | |
# | |
# $ config-exec redis-server /etc/redis.conf | |
require "erb" | |
require "fileutils" | |
# Any ERB files in this directory will be rendered like so: | |
# | |
# /config/etc/foo/bar.conf.erb -> rendered -> /etc/foo/bar.conf | |
dir = "/config" | |
Dir["#{dir}/**/*.erb"].each do |source| | |
# strip the dir prefix and the .erb extension | |
destination = source[%r{#{dir}/(.*).erb}, 1] | |
# render the config file | |
content = ERB.new(File.read(source)).result | |
# write the result to the filesystem | |
FileUtils.mkdir_p File.dirname(destination) | |
File.open(destination, "w") { |f| f.write content } | |
end | |
# exec whatever command we were given on the command line | |
exec *ARGV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment