File gitorious-setup-1st-time of Package gitorious

#!/usr/bin/env ruby

# == Synopsis
# hello: greets user, demonstrates command line parsing
#== Usage
# scrpt [OPTION] ... DIR
# -h, --help:
#    show help
# -d, --dry-run
#    do not modify config files
# -g dir, --gitorious dir
#    set gitorious base dir
#    default ( /srv/www/gitorious )
# -r dir, --repositories
#    set git repositories base dir
#    default ( /srv/git )
# -m, --mysql
#    setup mysql, default off
require 'rubygems'
require 'logger'
require 'sha1'
require 'yaml'
require 'socket'
require 'pp'
require 'diff/lcs'
require 'tempfile'

require 'getoptlong'
require 'rdoc/usage'

log =
log.level = Logger::WARN

opts =
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
  [ '--dry-run', '-d', GetoptLong::NO_ARGUMENT ],
  [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
  [ '--gitorious', '-g', GetoptLong::REQUIRED_ARGUMENT ],
  [ '--repositories', '-r', GetoptLong::REQUIRED_ARGUMENT ],
  [ '--mysql', '-m', GetoptLong::NO_ARGUMENT ]

dry_run = false
gitorious_dir = "/srv/www/gitorious"
repositories = "/srv/git"
setup_mysql = false

opts.each do |opt, arg|
  case opt
  when '--help'
  when '--dry-run'
    dry_run = true
  when '--gitorious'
    raise "#{arg} does not exist" if not
    gitorious_dir = arg.to_s
  when '--repositories'
    raise "#{arg} does not exist" if not
    repositories = arg.to_s
  when '--mysql'
    setup_mysql = true
  when '--verbose'
    log.level = Logger::INFO

# basic gitorious setup
Dir.chdir(gitorious_dir) do
  config = YAML.load_file('config/gitorious.yml')

  target ='gitorious')

  config['production'] = {} if not config.has_key?('production')

  hostname = Socket.gethostbyname(Socket.gethostname).first "Setting hostname to #{hostname}"
  if not config['production'].has_key?('gitorious_hostname')
    config['production']['gitorious_hostname'] = hostname

  if not config['production'].has_key?('cookie_secret') or 
    config['production']['cookie_secret'] == "ssssht"
    seed = "--#{rand(10000)}--#{}--"
    secret = Digest::SHA1.hexdigest(seed)
    config['production']['cookie_secret'] = secret "Secret set to #{secret}"

  target.write(config.to_yaml(:SortKeys	=> true))
  target.close "write config/gitorious.yml", 'config/gitorious.yml') if not dry_run "Configuring ultrasphinx"
  system("RAILS_ENV=production rake ultrasphinx:configure") if not dry_run

# database setup
Dir.chdir(gitorious_dir) do
  if setup_mysql
    require 'mysql' "mysql setup..."
      # connect to the MySQL server and see if we can
      # connect as root and no passwd
      my = Mysql::new("localhost", "root")
      # it works, create a database.yml file and change the
      # mysql password
      config = YAML.load_file('config/database.yml')
      target ='gitorious-database')

      seed = "--#{rand(10000)}--#{}--"
      # create a 5 characters password
      secret = Digest::SHA1.hexdigest(seed)[0,6] "create mysql password..."      

      config['production'] = {}
      config['production']['adapter'] = 'mysql'
      config['production']['database'] = 'gitorious_production'
      config['production']['username'] = 'gitorious'
      config['production']['password'] = secret
      config['production']['host'] = 'localhost'
      config['production']['encoding'] = 'utf8'

      target.write(config.to_yaml(:SortKeys	=> true))
      target.close "set mysql password..."
      my.query("create user gitorious@localhost identified by '#{secret}'")
      my.query("grant all privileges on gitorious_production.* to gitorious") "write config/database.yml", 'config/database.yml') if not dry_run
      log.error "Can't connect to mysql as root w/no pwd, no database setup done"
    end "Preparing and migrating database"
    system("RAILS_ENV=production rake db:create") if not dry_run
    system("RAILS_ENV=production rake db:migrate") if not dry_run "Prepare ultrasphinx"
    system("RAILS_ENV=production rake ultrasphinx:prepare") if not dry_run "Index ultrasphinx"
    system("RAILS_ENV=production rake ultrasphinx:index") if not dry_run
  else "mysql setup skipped..."

Dir.chdir(repositories) do
  sshconf = File.join(repositories, '.ssh')
  if not "Create #{sshconf}"
    Dir.mkdir(sshconf) if not dry_run
  keys = File.join(sshconf, 'authorized_keys') "Create #{keys}"
  FileUtils.touch(keys) if not dry_run