standup 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,501 @@
1
+ # -----------------------------
2
+ # PostgreSQL configuration file
3
+ # -----------------------------
4
+ #
5
+ # This file consists of lines of the form:
6
+ #
7
+ # name = value
8
+ #
9
+ # (The "=" is optional.) Whitespace may be used. Comments are introduced with
10
+ # "#" anywhere on a line. The complete list of parameter names and allowed
11
+ # values can be found in the PostgreSQL documentation.
12
+ #
13
+ # The commented-out settings shown in this file represent the default values.
14
+ # Re-commenting a setting is NOT sufficient to revert it to the default value;
15
+ # you need to reload the server.
16
+ #
17
+ # This file is read on server startup and when the server receives a SIGHUP
18
+ # signal. If you edit the file on a running system, you have to SIGHUP the
19
+ # server for the changes to take effect, or use "pg_ctl reload". Some
20
+ # parameters, which are marked below, require a server shutdown and restart to
21
+ # take effect.
22
+ #
23
+ # Any parameter can also be given as a command-line option to the server, e.g.,
24
+ # "postgres -c log_connections=on". Some parameters can be changed at run time
25
+ # with the "SET" SQL command.
26
+ #
27
+ # Memory units: kB = kilobytes Time units: ms = milliseconds
28
+ # MB = megabytes s = seconds
29
+ # GB = gigabytes min = minutes
30
+ # h = hours
31
+ # d = days
32
+
33
+
34
+ #------------------------------------------------------------------------------
35
+ # FILE LOCATIONS
36
+ #------------------------------------------------------------------------------
37
+
38
+ # The default values of these variables are driven from the -D command-line
39
+ # option or PGDATA environment variable, represented here as ConfigDir.
40
+
41
+ data_directory = '/var/lib/postgresql/8.4/main' # use data in another directory
42
+ # (change requires restart)
43
+ hba_file = '/etc/postgresql/8.4/main/pg_hba.conf' # host-based authentication file
44
+ # (change requires restart)
45
+ ident_file = '/etc/postgresql/8.4/main/pg_ident.conf' # ident configuration file
46
+ # (change requires restart)
47
+
48
+ # If external_pid_file is not explicitly set, no extra PID file is written.
49
+ external_pid_file = '/var/run/postgresql/8.4-main.pid' # write an extra PID file
50
+ # (change requires restart)
51
+
52
+
53
+ #------------------------------------------------------------------------------
54
+ # CONNECTIONS AND AUTHENTICATION
55
+ #------------------------------------------------------------------------------
56
+
57
+ # - Connection Settings -
58
+
59
+ #listen_addresses = 'localhost' # what IP address(es) to listen on;
60
+ # comma-separated list of addresses;
61
+ # defaults to 'localhost', '*' = all
62
+ # (change requires restart)
63
+ port = 5432 # (change requires restart)
64
+ max_connections = 100 # (change requires restart)
65
+ # Note: Increasing max_connections costs ~400 bytes of shared memory per
66
+ # connection slot, plus lock space (see max_locks_per_transaction).
67
+ superuser_reserved_connections = 3 # (change requires restart)
68
+ unix_socket_directory = '/var/run/postgresql' # (change requires restart)
69
+ #unix_socket_group = '' # (change requires restart)
70
+ #unix_socket_permissions = 0777 # begin with 0 to use octal notation
71
+ # (change requires restart)
72
+ #bonjour_name = '' # defaults to the computer name
73
+ # (change requires restart)
74
+
75
+ # - Security and Authentication -
76
+
77
+ #authentication_timeout = 1min # 1s-600s
78
+ ssl = true # (change requires restart)
79
+ #ssl_ciphers = 'ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH' # allowed SSL ciphers
80
+ # (change requires restart)
81
+ #ssl_renegotiation_limit = 512MB # amount of data between renegotiations
82
+ #password_encryption = on
83
+ #db_user_namespace = off
84
+
85
+ # Kerberos and GSSAPI
86
+ #krb_server_keyfile = ''
87
+ #krb_srvname = 'postgres' # (Kerberos only)
88
+ #krb_caseins_users = off
89
+
90
+ # - TCP Keepalives -
91
+ # see "man 7 tcp" for details
92
+
93
+ #tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
94
+ # 0 selects the system default
95
+ #tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
96
+ # 0 selects the system default
97
+ #tcp_keepalives_count = 0 # TCP_KEEPCNT;
98
+ # 0 selects the system default
99
+
100
+
101
+ #------------------------------------------------------------------------------
102
+ # RESOURCE USAGE (except WAL)
103
+ #------------------------------------------------------------------------------
104
+
105
+ # - Memory -
106
+
107
+ shared_buffers = 64MB # min 128kB
108
+ # (change requires restart)
109
+ temp_buffers = 8MB # min 800kB
110
+ #max_prepared_transactions = 0 # zero disables the feature
111
+ # (change requires restart)
112
+ # Note: Increasing max_prepared_transactions costs ~600 bytes of shared memory
113
+ # per transaction slot, plus lock space (see max_locks_per_transaction).
114
+ # It is not advisable to set max_prepared_transactions nonzero unless you
115
+ # actively intend to use prepared transactions.
116
+ work_mem = 1MB # min 64kB
117
+ #maintenance_work_mem = 16MB # min 1MB
118
+ #max_stack_depth = 2MB # min 100kB
119
+
120
+ # - Kernel Resource Usage -
121
+
122
+ #max_files_per_process = 1000 # min 25
123
+ # (change requires restart)
124
+ #shared_preload_libraries = '' # (change requires restart)
125
+
126
+ # - Cost-Based Vacuum Delay -
127
+
128
+ #vacuum_cost_delay = 0ms # 0-100 milliseconds
129
+ #vacuum_cost_page_hit = 1 # 0-10000 credits
130
+ #vacuum_cost_page_miss = 10 # 0-10000 credits
131
+ #vacuum_cost_page_dirty = 20 # 0-10000 credits
132
+ #vacuum_cost_limit = 200 # 1-10000 credits
133
+
134
+ # - Background Writer -
135
+
136
+ #bgwriter_delay = 200ms # 10-10000ms between rounds
137
+ #bgwriter_lru_maxpages = 100 # 0-1000 max buffers written/round
138
+ #bgwriter_lru_multiplier = 2.0 # 0-10.0 multipler on buffers scanned/round
139
+
140
+ # - Asynchronous Behavior -
141
+
142
+ #effective_io_concurrency = 1 # 1-1000. 0 disables prefetching
143
+
144
+
145
+ #------------------------------------------------------------------------------
146
+ # WRITE AHEAD LOG
147
+ #------------------------------------------------------------------------------
148
+
149
+ # - Settings -
150
+
151
+ #fsync = on # turns forced synchronization on or off
152
+ #synchronous_commit = on # immediate fsync at commit
153
+ #wal_sync_method = fsync # the default is the first option
154
+ # supported by the operating system:
155
+ # open_datasync
156
+ # fdatasync
157
+ # fsync
158
+ # fsync_writethrough
159
+ # open_sync
160
+ #full_page_writes = on # recover from partial page writes
161
+ #wal_buffers = 64kB # min 32kB
162
+ # (change requires restart)
163
+ #wal_writer_delay = 200ms # 1-10000 milliseconds
164
+
165
+ #commit_delay = 0 # range 0-100000, in microseconds
166
+ #commit_siblings = 5 # range 1-1000
167
+
168
+ # - Checkpoints -
169
+
170
+ #checkpoint_segments = 3 # in logfile segments, min 1, 16MB each
171
+ #checkpoint_timeout = 5min # range 30s-1h
172
+ #checkpoint_completion_target = 0.5 # checkpoint target duration, 0.0 - 1.0
173
+ #checkpoint_warning = 30s # 0 disables
174
+
175
+ # - Archiving -
176
+
177
+ #archive_mode = off # allows archiving to be done
178
+ # (change requires restart)
179
+ #archive_command = '' # command to use to archive a logfile segment
180
+ #archive_timeout = 0 # force a logfile segment switch after this
181
+ # number of seconds; 0 disables
182
+
183
+
184
+ #------------------------------------------------------------------------------
185
+ # QUERY TUNING
186
+ #------------------------------------------------------------------------------
187
+
188
+ # - Planner Method Configuration -
189
+
190
+ #enable_bitmapscan = on
191
+ #enable_hashagg = on
192
+ #enable_hashjoin = on
193
+ #enable_indexscan = on
194
+ #enable_mergejoin = on
195
+ #enable_nestloop = on
196
+ #enable_seqscan = on
197
+ #enable_sort = on
198
+ #enable_tidscan = on
199
+
200
+ # - Planner Cost Constants -
201
+
202
+ #seq_page_cost = 1.0 # measured on an arbitrary scale
203
+ #random_page_cost = 4.0 # same scale as above
204
+ #cpu_tuple_cost = 0.01 # same scale as above
205
+ #cpu_index_tuple_cost = 0.005 # same scale as above
206
+ #cpu_operator_cost = 0.0025 # same scale as above
207
+ effective_cache_size = 128MB
208
+
209
+ # - Genetic Query Optimizer -
210
+
211
+ #geqo = on
212
+ #geqo_threshold = 12
213
+ #geqo_effort = 5 # range 1-10
214
+ #geqo_pool_size = 0 # selects default based on effort
215
+ #geqo_generations = 0 # selects default based on effort
216
+ #geqo_selection_bias = 2.0 # range 1.5-2.0
217
+
218
+ # - Other Planner Options -
219
+
220
+ #default_statistics_target = 100 # range 1-10000
221
+ #constraint_exclusion = partition # on, off, or partition
222
+ #cursor_tuple_fraction = 0.1 # range 0.0-1.0
223
+ #from_collapse_limit = 8
224
+ #join_collapse_limit = 8 # 1 disables collapsing of explicit
225
+ # JOIN clauses
226
+
227
+
228
+ #------------------------------------------------------------------------------
229
+ # ERROR REPORTING AND LOGGING
230
+ #------------------------------------------------------------------------------
231
+
232
+ # - Where to Log -
233
+
234
+ #log_destination = 'stderr' # Valid values are combinations of
235
+ # stderr, csvlog, syslog and eventlog,
236
+ # depending on platform. csvlog
237
+ # requires logging_collector to be on.
238
+
239
+ # This is used when logging to stderr:
240
+ #logging_collector = off # Enable capturing of stderr and csvlog
241
+ # into log files. Required to be on for
242
+ # csvlogs.
243
+ # (change requires restart)
244
+
245
+ # These are only used if logging_collector is on:
246
+ #log_directory = 'pg_log' # directory where log files are written,
247
+ # can be absolute or relative to PGDATA
248
+ #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
249
+ # can include strftime() escapes
250
+ #log_truncate_on_rotation = off # If on, an existing log file of the
251
+ # same name as the new log file will be
252
+ # truncated rather than appended to.
253
+ # But such truncation only occurs on
254
+ # time-driven rotation, not on restarts
255
+ # or size-driven rotation. Default is
256
+ # off, meaning append to existing files
257
+ # in all cases.
258
+ #log_rotation_age = 1d # Automatic rotation of logfiles will
259
+ # happen after that time. 0 disables.
260
+ #log_rotation_size = 10MB # Automatic rotation of logfiles will
261
+ # happen after that much log output.
262
+ # 0 disables.
263
+
264
+ # These are relevant when logging to syslog:
265
+ #syslog_facility = 'LOCAL0'
266
+ #syslog_ident = 'postgres'
267
+
268
+ #silent_mode = off # Run server silently.
269
+ # DO NOT USE without syslog or
270
+ # logging_collector
271
+ # (change requires restart)
272
+
273
+
274
+ # - When to Log -
275
+
276
+ #client_min_messages = notice # values in order of decreasing detail:
277
+ # debug5
278
+ # debug4
279
+ # debug3
280
+ # debug2
281
+ # debug1
282
+ # log
283
+ # notice
284
+ # warning
285
+ # error
286
+
287
+ #log_min_messages = warning # values in order of decreasing detail:
288
+ # debug5
289
+ # debug4
290
+ # debug3
291
+ # debug2
292
+ # debug1
293
+ # info
294
+ # notice
295
+ # warning
296
+ # error
297
+ # log
298
+ # fatal
299
+ # panic
300
+
301
+ #log_error_verbosity = default # terse, default, or verbose messages
302
+
303
+ #log_min_error_statement = error # values in order of decreasing detail:
304
+ # debug5
305
+ # debug4
306
+ # debug3
307
+ # debug2
308
+ # debug1
309
+ # info
310
+ # notice
311
+ # warning
312
+ # error
313
+ # log
314
+ # fatal
315
+ # panic (effectively off)
316
+
317
+ #log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements
318
+ # and their durations, > 0 logs only
319
+ # statements running at least this number
320
+ # of milliseconds
321
+
322
+
323
+ # - What to Log -
324
+
325
+ #debug_print_parse = off
326
+ #debug_print_rewritten = off
327
+ #debug_print_plan = off
328
+ #debug_pretty_print = on
329
+ #log_checkpoints = off
330
+ #log_connections = off
331
+ #log_disconnections = off
332
+ #log_duration = off
333
+ #log_hostname = off
334
+ log_line_prefix = '%t ' # special values:
335
+ # %u = user name
336
+ # %d = database name
337
+ # %r = remote host and port
338
+ # %h = remote host
339
+ # %p = process ID
340
+ # %t = timestamp without milliseconds
341
+ # %m = timestamp with milliseconds
342
+ # %i = command tag
343
+ # %c = session ID
344
+ # %l = session line number
345
+ # %s = session start timestamp
346
+ # %v = virtual transaction ID
347
+ # %x = transaction ID (0 if none)
348
+ # %q = stop here in non-session
349
+ # processes
350
+ # %% = '%'
351
+ # e.g. '<%u%%%d> '
352
+ #log_lock_waits = off # log lock waits >= deadlock_timeout
353
+ #log_statement = 'none' # none, ddl, mod, all
354
+ #log_temp_files = -1 # log temporary files equal or larger
355
+ # than the specified size in kilobytes;
356
+ # -1 disables, 0 logs all temp files
357
+ #log_timezone = unknown # actually, defaults to TZ environment
358
+ # setting
359
+
360
+
361
+ #------------------------------------------------------------------------------
362
+ # RUNTIME STATISTICS
363
+ #------------------------------------------------------------------------------
364
+
365
+ # - Query/Index Statistics Collector -
366
+
367
+ track_activities = on
368
+ track_counts = on
369
+ #track_functions = none # none, pl, all
370
+ #track_activity_query_size = 1024
371
+ #update_process_title = on
372
+ #stats_temp_directory = 'pg_stat_tmp'
373
+
374
+
375
+ # - Statistics Monitoring -
376
+
377
+ #log_parser_stats = off
378
+ #log_planner_stats = off
379
+ #log_executor_stats = off
380
+ #log_statement_stats = off
381
+
382
+
383
+ #------------------------------------------------------------------------------
384
+ # AUTOVACUUM PARAMETERS
385
+ #------------------------------------------------------------------------------
386
+
387
+ autovacuum = on # Enable autovacuum subprocess? 'on'
388
+ # requires track_counts to also be on.
389
+ #log_autovacuum_min_duration = -1 # -1 disables, 0 logs all actions and
390
+ # their durations, > 0 logs only
391
+ # actions running at least this number
392
+ # of milliseconds.
393
+ autovacuum_max_workers = 3 # max number of autovacuum subprocesses
394
+ autovacuum_naptime = 7d # time between autovacuum runs
395
+ autovacuum_vacuum_threshold = 1000 # min number of row updates before
396
+ # vacuum
397
+ autovacuum_analyze_threshold = 1000 # min number of row updates before
398
+ # analyze
399
+ autovacuum_vacuum_scale_factor = 0.2 # fraction of table size before vacuum
400
+ autovacuum_analyze_scale_factor = 0.1 # fraction of table size before analyze
401
+ #autovacuum_freeze_max_age = 200000000 # maximum XID age before forced vacuum
402
+ # (change requires restart)
403
+ #autovacuum_vacuum_cost_delay = 20ms # default vacuum cost delay for
404
+ # autovacuum, in milliseconds;
405
+ # -1 means use vacuum_cost_delay
406
+ #autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for
407
+ # autovacuum, -1 means use
408
+ # vacuum_cost_limit
409
+
410
+
411
+ #------------------------------------------------------------------------------
412
+ # CLIENT CONNECTION DEFAULTS
413
+ #------------------------------------------------------------------------------
414
+
415
+ # - Statement Behavior -
416
+
417
+ #search_path = '"$user",public' # schema names
418
+ #default_tablespace = '' # a tablespace name, '' uses the default
419
+ #temp_tablespaces = '' # a list of tablespace names, '' uses
420
+ # only default tablespace
421
+ #check_function_bodies = on
422
+ #default_transaction_isolation = 'read committed'
423
+ #default_transaction_read_only = off
424
+ #session_replication_role = 'origin'
425
+ #statement_timeout = 0 # in milliseconds, 0 is disabled
426
+ #vacuum_freeze_min_age = 50000000
427
+ #vacuum_freeze_table_age = 150000000
428
+ #xmlbinary = 'base64'
429
+ #xmloption = 'content'
430
+
431
+ # - Locale and Formatting -
432
+
433
+ datestyle = 'iso, mdy'
434
+ #intervalstyle = 'postgres'
435
+ #timezone = unknown # actually, defaults to TZ environment
436
+ # setting
437
+ #timezone_abbreviations = 'Default' # Select the set of available time zone
438
+ # abbreviations. Currently, there are
439
+ # Default
440
+ # Australia
441
+ # India
442
+ # You can create your own file in
443
+ # share/timezonesets/.
444
+ #extra_float_digits = 0 # min -15, max 2
445
+ #client_encoding = sql_ascii # actually, defaults to database
446
+ # encoding
447
+
448
+ # These settings are initialized by initdb, but they can be changed.
449
+ lc_messages = 'en_US.UTF-8' # locale for system error message
450
+ # strings
451
+ lc_monetary = 'en_US.UTF-8' # locale for monetary formatting
452
+ lc_numeric = 'en_US.UTF-8' # locale for number formatting
453
+ lc_time = 'en_US.UTF-8' # locale for time formatting
454
+
455
+ # default configuration for text search
456
+ default_text_search_config = 'pg_catalog.english'
457
+
458
+ # - Other Defaults -
459
+
460
+ #dynamic_library_path = '$libdir'
461
+ #local_preload_libraries = ''
462
+
463
+
464
+ #------------------------------------------------------------------------------
465
+ # LOCK MANAGEMENT
466
+ #------------------------------------------------------------------------------
467
+
468
+ #deadlock_timeout = 1s
469
+ #max_locks_per_transaction = 64 # min 10
470
+ # (change requires restart)
471
+ # Note: Each lock table slot uses ~270 bytes of shared memory, and there are
472
+ # max_locks_per_transaction * (max_connections + max_prepared_transactions)
473
+ # lock table slots.
474
+
475
+
476
+ #------------------------------------------------------------------------------
477
+ # VERSION/PLATFORM COMPATIBILITY
478
+ #------------------------------------------------------------------------------
479
+
480
+ # - Previous PostgreSQL Versions -
481
+
482
+ #add_missing_from = off
483
+ #array_nulls = on
484
+ #backslash_quote = safe_encoding # on, off, or safe_encoding
485
+ #default_with_oids = off
486
+ #escape_string_warning = on
487
+ #regex_flavor = advanced # advanced, extended, or basic
488
+ #sql_inheritance = on
489
+ #standard_conforming_strings = off
490
+ #synchronize_seqscans = on
491
+
492
+ # - Other Platforms and Clients -
493
+
494
+ #transform_null_equals = off
495
+
496
+
497
+ #------------------------------------------------------------------------------
498
+ # CUSTOMIZED OPTIONS
499
+ #------------------------------------------------------------------------------
500
+
501
+ #custom_variable_classes = '' # list of custom variable class names
@@ -0,0 +1,24 @@
1
+ Standup.script do
2
+ def run
3
+ install_package 'postgresql-8.4 libpq-dev'
4
+
5
+ upload script_file('postgresql.conf'),
6
+ :to => '/etc/postgresql/8.4/main/postgresql.conf',
7
+ :sudo => true
8
+
9
+ tune_kernel
10
+
11
+ sudo 'service postgresql-8.4 restart'
12
+ end
13
+
14
+ def tune_kernel
15
+ sysctl_params = ['kernel.shmmax=134217728', 'kernel.shmall=2097152']
16
+
17
+ remote_update '/etc/sysctl.conf',
18
+ sysctl_params.join("\n"),
19
+ :delimiter => '# standup script postgresql',
20
+ :sudo => true
21
+
22
+ sysctl_params.each {|p| sudo "sysctl -w #{p}"}
23
+ end
24
+ end
data/scripts/ruby.rb ADDED
@@ -0,0 +1,12 @@
1
+ Standup.script do
2
+ def run
3
+ return if exec('ruby -v') =~ /Ruby Enterprise Edition 2010.02/
4
+
5
+ in_temp_dir do
6
+ arch64 = instance.architecture =~ /64/
7
+ filename = "ruby-enterprise_1.8.7-2010.02_#{arch64 ? 'amd64' : 'i386'}_ubuntu10.04.deb"
8
+ exec "wget -q http://rubyforge.org/frs/download.php/#{arch64 ? '71098' : '71100'}/#{filename}"
9
+ sudo "dpkg -i #{filename}"
10
+ end
11
+ end
12
+ end
data/scripts/setup.rb ADDED
@@ -0,0 +1,11 @@
1
+ Standup.script do
2
+ self.description = 'Do all setup from scratch and/or incrementally'
3
+
4
+ self.default_params = 'ec2 basics ruby postgresql passenger webapp update'
5
+
6
+ def run
7
+ params.strip.split.each do |name|
8
+ scripts[name].titled_run
9
+ end
10
+ end
11
+ end
data/scripts/status.rb ADDED
@@ -0,0 +1,15 @@
1
+ Standup.script do
2
+ self.description = 'Check nodes status and display useful info'
3
+
4
+ def run
5
+ if instance
6
+ puts "Node: #{node.name}"
7
+ puts "State: #{instance.state}"
8
+ puts "IP: #{instance.external_ip}"
9
+ puts "SSH String: #{node.ssh_string}"
10
+ else
11
+ puts "Node: #{node.name}"
12
+ puts "State: not running"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ Standup.script do
2
+ self.description = 'Terminate nodes'
3
+
4
+ def run
5
+ return unless instance
6
+ if bright_ask("Do you really want to terminate node #{node.name}? [yes/NO]").strip.downcase == 'yes'
7
+ instance.terminate
8
+ else
9
+ bright_p "aborted"
10
+ end
11
+ end
12
+ end
data/scripts/update.rb ADDED
@@ -0,0 +1,15 @@
1
+ Standup.script do
2
+ self.description = 'Update working application'
3
+
4
+ def run
5
+ in_dir '/opt/webapp' do
6
+ sudo 'chown -R ubuntu:ubuntu /opt/webapp'
7
+ exec 'git pull'
8
+ sudo 'bundle install'
9
+ sudo 'RAILS_ENV=production rake db:migrate'
10
+ sudo 'mkdir -p tmp'
11
+ sudo 'chown -R nobody:nogroup /opt/webapp'
12
+ sudo 'touch tmp/restart.txt'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ server {
2
+ server_name "";
3
+ listen 80;
4
+
5
+ root /opt/webapp/public;
6
+
7
+ passenger_enabled on;
8
+ passenger_min_instances 3;
9
+ }
10
+
11
+ passenger_pre_start http://127.0.0.1/;
12
+
data/scripts/webapp.rb ADDED
@@ -0,0 +1,79 @@
1
+ Standup.script do
2
+ def run
3
+ install_package 'git-core'
4
+ install_gem 'bundler'
5
+
6
+ unless file_exists? '/opt/webapp'
7
+ sudo 'mkdir -p /opt/webapp'
8
+ end
9
+
10
+ sudo 'chown -R ubuntu:ubuntu /opt/webapp'
11
+
12
+ ensure_github_access
13
+
14
+ unless file_exists? '/opt/webapp/.git'
15
+ exec 'rm -rf /opt/webapp/*'
16
+ exec "git clone git@github.com:#{params.github_user}/#{params.github_repo}.git /opt/webapp"
17
+ end
18
+
19
+ bootstrap_db
20
+
21
+ remote_update '/opt/nginx/conf/nginx.conf',
22
+ File.read(script_file('nginx-server-fragment.conf')),
23
+ :delimiter => '# standup place server fragments here',
24
+ :sudo => true
25
+
26
+ sudo 'chown -R nobody:nogroup /opt/webapp'
27
+
28
+ scripts.passenger.restart_nginx
29
+ end
30
+
31
+ protected
32
+
33
+ def ensure_github_access
34
+ return unless exec('ssh -o StrictHostKeyChecking=no git@github.com') =~ /Permission denied \(publickey\)/
35
+
36
+ unless file_exists? '~/.ssh/id_rsa'
37
+ exec "ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -C `hostname`"
38
+ end
39
+
40
+ password = bright_ask("Enter GitGub password for user #{params.github_user}:", false)
41
+
42
+ github_add_deploy_key params.github_user,
43
+ password,
44
+ params.github_repo,
45
+ exec('hostname').strip,
46
+ exec('cat ~/.ssh/id_rsa.pub').strip
47
+ end
48
+
49
+ def bootstrap_db
50
+ unless run_pg_command("select * from pg_user where usename = 'webapp'") =~ /1 row/
51
+ run_pg_command "create user webapp with password 'webapp'"
52
+ end
53
+
54
+ unless run_pg_command("select * from pg_database where datname = 'webapp'") =~ /1 row/
55
+ run_pg_command "create database webapp with owner webapp"
56
+
57
+ in_dir '/opt/webapp' do
58
+ sudo 'bundle install'
59
+ exec 'RAILS_ENV=production rake db:schema:load'
60
+ exec 'RAILS_ENV=production rake db:seed'
61
+ end
62
+ end
63
+ end
64
+
65
+ def run_pg_command cmd
66
+ sudo("su -c \"psql -c \\\"#{cmd}\\\"\" postgres")
67
+ end
68
+
69
+ def github_add_deploy_key user, password, repo, title, key
70
+ require 'net/http'
71
+ Net::HTTP.start 'github.com' do |http|
72
+ req = Net::HTTP::Post.new "/api/v2/json/repos/key/#{user}/#{repo}/add"
73
+ req.form_data = {'title' => title, 'key' => key}
74
+ req.basic_auth user, password
75
+ response = http.request req
76
+ response.is_a? Net::HTTPSuccess
77
+ end
78
+ end
79
+ end