solotimes
5/18/2011 - 2:28 PM

the deploy task script

the deploy task script

#!/usr/bin/env ruby
require "rubygems" # ruby1.9 doesn't "require" it though
require "thor"
require "yaml"

class Deploy < Thor
  include Thor::Actions
  desc "init","create a local site"
  def init
    #domain = safe_ask("Deploy domain",options[:domain])
    #database = safe_ask("MySQL database",options[:db])
    #local_path = safe_ask("Project path",options[:local_path])
    domain,database,local_path = options[:domain],options[:db],options[:local_path]
    
    create_vh = yes?("Add virtual host to '#{options[:apache_conf]}' ? y/n [n]")
    if(create_vh)
      invoke(:vhost)
    end
    
    create_dummy = yes?("Create a dummy domain to '#{options[:hosts_file]}' ? y/n [n]")
    
    if(create_dummy)
      invoke(:dummy)
    end
    
    create_db = yes?("Add database to mysql? y/n [n]")
    
    if(create_db)
      invoke(:mysql)
    end
    
  end
  
  desc "apache start | restart | stop","control local apache server"
  def apache command="start"
    puts "wrong command" and return unless ['start','restart','stop'].include?(command)
    %x(#{options[:apache]} -k #{command})
  end
  
  desc "dummy [-d] [--domain=DOMAIN]", "create or delete(width -d) local dummy domain"
  method_option :delete,:aliases => "-d", :default => false,:type => :boolean
  def dummy
    append_file(options[:hosts_file],"#{$/}127.0.0.1 #{options[:domain]}")
  end
  
  desc "mysql [-d] [--domain=DOMAIN]", "create or delete(width -d) local mysql database (name based on domain ie, a.com => a_com )"
  method_option :delete,:aliases => "-d", :default => false,:type => :boolean
  def mysql
    unless options[:delete]
      say "creating database..."
      %x(mysql -u root -e "create database #{options[:db]}" )
    else
      say "dropping database..."
      %x(mysql -u root -e "drop database #{options[:db]}" )
    end
  end
  
  desc "vhost [-d] [--domain=DOMAIN]", "create or delete(width -d) local virtual host"
  method_option :delete,:aliases => "-d", :default => false,:type => :boolean
  def vhost
    vh_config = <<CONF
<VirtualHost *:80>
  DocumentRoot "#{options[:local_path]}"
  ServerName #{options[:domain]}
  #ServerAlias *.#{options[:domain]}
</VirtualHost>
CONF
    #puts "adding vhost #{domain} to #{options[:apache_conf]}..."
    append_file(options[:apache_conf],vh_config)
    # puts("restarting apache...")
    invoke(:apache,'restart')
  end
  
  
  method_option :global,:aliases => "-g", :default => false,:type => :boolean
  desc "config [list] [--key=value [--global]]","set or show config value,use list to show"
  def config(cmd='')
    clone=Hash.new.merge(options)
    file = '.deploy'
    file = File.expand_path('~/.deployconfig') if clone.delete('global')
    if(cmd=='list')
      options.each { |key,value| puts "#{key}: #{value}" }
     else
      File.open(file, 'w' ) do |out|
        YAML.dump( clone, out )
      end
    end
  end
  
  protected
  
  def safe_ask q,default
    a = self.ask("#{q},default: \"#{default}\"")
    return a if a && a.strip!=''
    default
  end
  
  @@defaults=nil
  
  def self.defaults
    return @@defaults if @@defaults
    @@domain = File.basename(Dir.getwd)
    @@defaults = {
      "mysql" => 'mysql',
      "apache" => '~/xampp/xamppfiles/bin/httpd',
      "ssh"   => 'ssh',
      "local_path" => Dir.getwd,
      "domain" => @@domain,
      "repo" => "git://github.com/xtunes/#{@@domain}.git",
      "db" => @@domain.gsub(/\./,'_'),
      "db_file" => 'db.sql',
      "hosts_file" => '/etc/hosts',
      "apache_conf" => '~/xampp/etc/extra/httpd-vhosts.conf'
    }
    if File.exists?(File.expand_path('~/.deployconfig'))
      global = YAML::load(IO.read(File.expand_path('~/.deployconfig')))
      @@defaults.merge!(global)
    end
    
    if File.exists?('.deploy')
      local = YAML::load(IO.read('.deploy'))
      @@defaults.merge!(local)
    end
    
    @@defaults
  end
  
  self.defaults.each do |key,value|
    class_option key.to_sym, :default => value
  end
end

Deploy.start()
gem install thor net-ssh --no-ri --no-rdoc