Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created March 11, 2013 20:09
Show Gist options
  • Save cadwallion/5137286 to your computer and use it in GitHub Desktop.
Save cadwallion/5137286 to your computer and use it in GitHub Desktop.
Maestro
module Maestro
class Bootstrap
attr_reader :project_name, :org_name, :app_type
def initialize project_name, org_name, options = {}
@project_name = project_name
@org_name = org_name
@app_type = options[:type]
end
def nginx_config
template = File.read File.dirname(__FILE__) + '/templates/nginx'
template.sub /SERVER_NAME/, server_name
template.gsub /DIRECTORY/, "#{root_directory}/public"
template.gsub /UPSTREAM_NAME/, "#{project_name}_app"
File.write "/usr/local/etc/nginx/sites-available/#{domain}", template
FileUtils.ln_sf "/usr/local/etc/nginx/sites-available/#{domain}", "/usr/local/etc/nginx/sites-enabled/#{domain}"
end
def add_dns
dns = File.read "/etc/hosts"
dns.sub!(/127.0.0.1\t(.*)\n/, "127.0.0.1\t\1 #{server_name}\n"
File.write "/etc/hosts", dns
end
def add_foreman_line
sheet_music = read_maestro_sheet
sheet_music << "#{org_name}.#{project_name}: cd #{root_directory} && #{startup_line}\n"
File.write File.expand_path("~/.maestro"), sheet_music
end
def server_name
"#{domain}.dev"
end
def root_directory
"/Users/cadwallion/code/#{org_name}/#{project_name}"
end
def domain
"#{project_name}.#{org_name}"
end
def read_maestro_sheet
if File.exists? File.expand_path "~/.maestro"
File.read File.expand_path "~/.maestro"
else
""
end
end
def startup_line
case app_type
when :thin
"thin start -S tmp/sockets/development.sock -P tmp/pids/development.pid"
when :unicorn
"unicorn_rails -c config/unicorn.rb"
end
end
end
end
#!/usr/bin/env ruby
require 'thor'
require 'maestro'
module Maestro
class CLI < Thor
include Thor::Actions
desc "Adds an application to Maestro"
method_option type: "unicorn", aliases: '-app'
def add name
org_name, project_name = name.split("/")
bootstrap = Bootstrap.new project_name, org_name, type: options[:type].to_sym
puts "Adding nginx conf for #{project_name}.#{org_name}.dev..."
bootstrap.nginx_config
puts "Adding /etc/hosts line for #{project_name}.#{org_name}.dev..."
bootstrap.add_dns
puts "Adding #{options[:type]} config to Maestro startup scripts..."
bootstrap.add_foreman_line
puts "Done! You may start this app with `maestro start articulate.codename`."
end
desc "Starts an application using Foreman"
def start name
IO.popen("foreman start #{name} -f ~/.maestro", 'r+') do |process|
process.close_write
puts process.gets until process.eof?
end
end
end
end
Maestro::CLI.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment