Module: ORS::Helpers

Includes:
Config
Included in:
Commands::Base
Defined in:
lib/ors/helpers.rb

Constant Summary

Constants included from Config

Config::CONFIG_FILENAME

Instance Method Summary collapse

Methods included from Config

#all_servers, #deploy_directory, #revision, #ruby_servers

Methods included from Config::ModuleMethods

#git, #parse_config_file, #parse_options, #valid_environments, #valid_options?

Instance Method Details

#build_command(server, *commands_and_maybe_options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ors/helpers.rb', line 104

def build_command server, *commands_and_maybe_options
  return "" if commands_and_maybe_options.empty?

  if commands_and_maybe_options.last.is_a?(Hash)
    options = commands_and_maybe_options.pop
    command_array = commands_and_maybe_options
  else
    command_array = commands_and_maybe_options
    options = {}
  end

  commands = command_array.join " && "
  psuedo_tty = options[:exec] ? '-t ' : ''
  quiet_ssh = options[:quiet_ssh] ? '-q ' : ''

  if options[:local]
    commands
  else
    if use_gateway
      %(ssh #{quiet_ssh}#{psuedo_tty}#{gateway} 'ssh #{quiet_ssh}#{psuedo_tty}#{deploy_user}@#{server} "#{commands}"')
    else
      %(ssh #{quiet_ssh}#{psuedo_tty}#{deploy_user}@#{server} "#{commands}")
    end
  end
end

#bundle_install(server) ⇒ Object



35
36
37
38
39
40
# File 'lib/ors/helpers.rb', line 35

def bundle_install server
  info "[#{server}] installing bundle..."

  execute_command server, prepare_environment,
                          %(bundle install --without development test osx_development > bundler.log)
end

#execute_command(server, *command_array) ⇒ Object

options = => ?, :capture => ?, :quiet_ssh => ?



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ors/helpers.rb', line 79

def execute_command server, *command_array
  options = {:exec => false, :capture => false, :quiet_ssh => false}
  options.merge!(command_array.pop) if command_array.last.is_a?(Hash)
  options[:local] = true if server.to_s == "localhost"

  command = build_command(server, command_array, options)

  if pretending
    info("[#{server}] #{command}")
  else
    if options[:exec]
      exec command
    else
      results = `#{command}`
      if options[:capture]
        return results
      else
        results.split("\n").each do |result|
          info "[#{server}] #{result.chomp}\n"
        end
      end
    end
  end
end

#execute_in_parallel(servers) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ors/helpers.rb', line 70

def execute_in_parallel servers
  servers.map do |server|
    Thread.new(server) do |server|
      yield server
    end
  end.map {|thread| thread.join }
end

#fatal(message) ⇒ Object



139
140
141
142
# File 'lib/ors/helpers.rb', line 139

def fatal message
  info message
  exit 1
end

#info(message) ⇒ Object



135
136
137
# File 'lib/ors/helpers.rb', line 135

def info message
  STDOUT.puts message
end

#prepare_environmentObject



130
131
132
133
# File 'lib/ors/helpers.rb', line 130

def prepare_environment
  [%(source ~/.rvm/scripts/rvm),
   %({ cd #{deploy_directory} > /dev/null; })] # Silence RVM's "Using... gemset..."
end

#restart_server(server) ⇒ Object



56
57
58
59
60
61
# File 'lib/ors/helpers.rb', line 56

def restart_server server
  info "[#{server}] restarting unicorn..."

  execute_command server, prepare_environment,
                          %(kill -USR2 \\`cat tmp/pids/unicorn.pid\\`)
end

#run_migrations(server) ⇒ Object



63
64
65
66
67
68
# File 'lib/ors/helpers.rb', line 63

def run_migrations server
  info "[#{server}] running migrations..."

  execute_command server, prepare_environment,
                          %(RAILS_ENV=#{environment} bundle exec rake db:migrate db:seed)
end

#setup_repo(server) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/ors/helpers.rb', line 6

def setup_repo server
  info "[#{server}] installing codebase..."

  execute_command server, %(cd #{base_path}),
                          %(rm -rf #{deploy_directory}),
                          %(git clone #{repo}:#{name} #{deploy_directory}),
                          %(mkdir -p #{deploy_directory}/tmp/pids),
                          %(mkdir -p #{deploy_directory}/log)
end

#setup_ruby(server) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ors/helpers.rb', line 16

def setup_ruby server
  info "[#{server}] installing ruby and gems..."

  execute_command server, prepare_environment,
                          %(gem install rubygems-update),
                          %(gem update --system),
                          %(gem install bundler),
                          %(bundle install --without development test osx_development > bundler.log)
end

#start_server(server) ⇒ Object



42
43
44
45
46
47
# File 'lib/ors/helpers.rb', line 42

def start_server server
  info "[#{server}] starting unicorn..."

  execute_command server, prepare_environment,
                          %(if [ -f config.ru ]; then RAILS_ENV=#{environment} bundle exec unicorn -c config/unicorn.rb -D -E #{environment}; else RAILS_ENV=#{environment} bundle exec unicorn_rails -c config/unicorn.rb -D -E #{environment}; fi)
end

#stop_server(server) ⇒ Object



49
50
51
52
53
54
# File 'lib/ors/helpers.rb', line 49

def stop_server server
  info "[#{server}] stopping unicorn..."

  execute_command server, prepare_environment,
                          %(kill \\`cat tmp/pids/unicorn.pid\\`)
end

#update_code(server) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ors/helpers.rb', line 26

def update_code server
  info "[#{server}] updating codebase..."

  execute_command server, prepare_environment,
                          %(git fetch),
                          %(git checkout -q -f origin/#{environment}),
                          %(git reset --hard)
end