Class: Bonethug::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bonethug/cli.rb

Constant Summary collapse

@@tasks =
[
  'help',
  'auth',
  'install',
  'init-db',
  'init-local-db',
  'setup-db',
  'setup-env',
  'vhost-local',
  'init',
  'update',
  'run',
  'rake',
  'drush',
  'drush-local',
  'sake',
  'sync-state',
  'deploy',
  'setup',
  'remote-backup',
  'local-backup',
  'sync-backup-to',
  'sync-backup-from',
  'force-unlock',
  'cleanup',
  'clean',
  'watch'
]

Class Method Summary collapse

Class Method Details

.display_help(bin_name = 'thug') ⇒ Object



412
413
414
# File 'lib/bonethug/cli.rb', line 412

def self.display_help(bin_name = 'thug')
  puts 'Usage:  ' + bin_name + '  task [argument]...'
end

.handle(bin_name = 'thug') ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/bonethug/cli.rb', line 41

def self.handle(bin_name = 'thug')

  # what are we doing?
  task = ARGV[0] || 'help'

  case task

  when 'help'

    display_help

  when 'tasks'

    @@tasks.each do |task|
      puts task
    end

  when 'version'

    puts 'bonethug v' + VERSION + ' - build date: ' + BUILD_DATE
    exit

  when 'auth'

    # handle args
    env = ARGV[1]

    # validate
    unless env
      puts 'Usage: ' + bin_name + ' auth [environment]'
      return
    end

    exec "export to=#{env} && bundle exec mina -f .bonethug/deploy.rb auth --verbose"

  when 'install'

    # handle args
    type = ARGV[1]
    location = ARGV[2] || '.'

    # validate
    unless type
      puts 'Usage: ' + bin_name + ' install [type] [location]'
      return
    end

    # run the installer
    Installer.install type, location

  when 'init-db', 'init-local-db', 'setup-db'

    # handle args
    env = ARGV.last
    admin_user = ARGV.length == 3 ? ARGV[1] : 'root'
    admin_pass = ARGV.length == 4 ? ARGV[2] : ''

    # validate
    if !env || env == task
      puts 'Usage: ' + bin_name + ' ' + task + ' [admin_user] [admin_pass] [environment]'
      return
    end

    if task == 'init-local-db'
      Installer.execute_init_mysql_db_script env, admin_user, admin_pass
    elsif task == 'setup-db'
      exec "export to=#{env} && export admin_user=#{admin_user} && export admin_pass=#{admin_pass} && bundle exec mina -f .bonethug/deploy.rb setup_db --verbose"
    else
      exec "export to=#{env} && export admin_user=#{admin_user} && export admin_pass=#{admin_pass} && bundle exec mina -f .bonethug/deploy.rb init_db --verbose"
    end

  when 'setup-env'

    # handle args
    env = ARGV.last

    # validate
    if !env || env == task
      puts 'Usage: ' + bin_name + ' setup-env [environment]'
      return
    end

    # find the file
    gem_dir = File.expand_path File.dirname(__FILE__) + '/../..'
    script = gem_dir + '/scripts/ubuntu_setup.sh'

    if env == 'show'

      puts "---------------"
      puts "Pre"
      puts "---------------\n"
      puts File.read script

      puts "\n---------------"
      puts "Parsed"
      puts "---------------"
      puts Installer.parse_sh File.read(script)

    elsif env == 'local'
      exec 'sudo bash ' + script

    else
      exec "export to=#{env} && bundle exec mina -f .bonethug/deploy.rb setup_env --verbose"

    end

  when 'vhost-local'

    # get env
    env = ARGV.last
    env = 'development' if env == 'vhost-local'

    # exec env
    exec_path   = File.expand_path('.')

    # load config
    conf = Conf.new.add exec_path + '/config/cnf.yml'
    conf.add(exec_path + '/config/database.yml' => { root: 'dbs.default' }) if File.exist? exec_path + '/config/database.yml'
    deploy = conf.node_merge 'deploy.common', 'deploy.environments.' + env

    # vhost name
    vhost = deploy.get('project_slug') + '_' + env

    # build the vhost conf
    vh_cnf = conf.get 'vhost'
    vh_cnf = conf.get 'apache' unless vh_cnf
    vh_cnf = vh_cnf.get env

    # exit if we have no env
    unless vh_cnf
      puts "unable to find vhost conf for " + env
      exit
    end

    # get the conf path
    conf_path = vh_cnf.get('conf_path') || '/etc/apache2/sites-available'

    # load the appropriate config for the web server
    vh = Configurator.vhost vh_cnf, exec_path, deploy.get('project_type'), env

    # check if we have a custom config for the webserver type
    case vh_cnf.get('type')
    when "nginx"

      # to be implemented
      puts 'to be implemented'
      exit

    else # apache

      # install the vhost
      system "echo \"#{vh.gsub('"','\"')}\" | sudo tee #{conf_path}/#{vhost}.conf"

    end

    # handle hosts file update
    path = RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i ? '/c/Windows/System32/drivers/etc/hosts' : '/etc/hosts'
    hosts = "
      ## BONETHUG-#{vhost} ##
        #{Configurator.hosts vh_cnf}
      ## END_BONETHUG-#{vhost} ##
    "

    # write the to the hosts file
    # this needs work
    system "sudo sed -i '/## BONETHUG-#{vhost} ##/,/## END_BONETHUG-#{vhost} ##/ s/.*//g' #{path}"
    escaped = (hosts).gsub(/"/, '\"')
    system "echo \"#{escaped}\" | sudo tee -a #{path}"

    # reload apache - linux - but need a debian specific look up
    system "sudo a2ensite #{vhost} && sudo service apache2 restart" if RbConfig::CONFIG['target_os'] =~ /linux/i

  when 'init', 'update'

    # handle args
    location = ARGV[1] || '.'
    stage = ARGV[2] || 0

    # validate
    unless location
      puts 'Usage: ' + bin_name + ' #{task} [location]'
      return
    end

    # run the initaliser
    Installer.bonethugise location, task.to_sym, stage.to_i

  when  'run',
        'rake',
        'drush',
        'drush-local',
        'sake'

    # get env
    environment = ARGV.last

    # handle args
    if task == 'run'
      cmd_task = ARGV[1]
      args = ARGV[2..(ARGV.length-2)]
    else
      case task
      when 'rake'
        cmd_task = 'bundle exec rake'
      when 'drush', 'drush-local'
        cmd_task = File.expand_path('./vendor/drush/drush/drush') + ' -r ' + File.expand_path('./public')
      when 'sake'
        cmd_task = 'public/framework/sake'
      end
      args = ARGV[1..(ARGV.length-2)]
    end

    if task == 'drush-local'

      env_cmd = RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i ? 'set' : 'export'
      cmd = env_cmd + " APPLICATION_ENV=#{environment} && #{cmd_task} #{args.join(' ')}"
      puts
      exec cmd

    else

      # build command
      run = "run_task task=\"#{cmd_task} #{args.join(' ')}\""

      # do it!
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb #{run} --verbose"

    end

  when  'sync-state'

    # operation whitelist
    operations = ['pull-from-local', 'push-to-local', 'pull-from-remote', 'push-to-remote']

    # args
    operation1 = ARGV[1]
    env1       = ARGV[2]
    operation2 = ARGV[3]
    env2       = ARGV[4]

    puts

    # validate operation whitelist
    valid_operation = operations.include?(operation1) and operations.include?(operation2)

    # ensure there is both a pull and push operation
    if valid_operation
      has_pull = (operation1 == 'pull-from-local' or operation2 == 'pull-from-local' or operation1 == 'pull-from-remote' or operation2 == 'pull-from-remote')
      has_push = (operation1 == 'push-to-local'   or operation2 == 'push-to-local'   or operation1 == 'push-to-remote'   or operation2 == 'push-to-remote')
      valid_operation = has_push and has_push
    end

    # validate
    unless operation1 and operation2 and env1 and env2 and valid_operation
      puts 'Usage: thug sync-state [pull-from-{local|remote}] [environment] [push-to-{local|remote}] [environment]'
      exit
    end

    # process env data
    pull_env = (operation1 == 'pull-from-remote' or operation1 =='pull-from-local') ? env1 : env2
    push_env = (operation1 == 'push-to-remote'   or operation1 =='push-to-local')   ? env1 : env2

    # process operation1
    case operation1
    when 'pull-from-local'
      pull_operation = 'local'
    when 'pull-from-remote'
      pull_operation = 'remote'
    when 'push-to-local'
      push_operation = 'local'
    when 'push-to-remote'
      push_operation = 'remote'
    end

    # process operation2
    case operation2
    when 'pull-from-local'
      pull_operation = 'local'
    when 'pull-from-remote'
      pull_operation = 'remote'
    when 'push-to-local'
      push_operation = 'local'
    when 'push-to-remote'
      push_operation = 'remote'
    end

    # stop here if its a local to local because we wont know the file system location of the non-calling local
    if pull_operation == 'local' and push_operation == 'local'
      puts 'local to local sync is not supported at this time'
      exit
    end

    # Do Sync
    if pull_operation == 'local' or push_operation == 'local'
      Syncer.sync pull_operation, pull_env, push_operation, push_env
    else
      # this will call ruby .bonethug/syncer.rb local #{pull_env} remote #{push_env}
      exec "export to=#{pull_env} && export remote_env=#{push_env} && bundle exec mina -f .bonethug/deploy.rb sync_state --verbose"
    end


  when  'deploy',
        'setup',
        'remote-backup',
        'local-backup',
        'sync-backup-to',
        'sync-backup-from',
        'force-unlock',
        'cleanup'

    # handle args
    environment = ARGV[1]

    # validate
    unless environment
      puts 'Usage: thug #{task} [environment]'
      return
    end

    case task

    # Setup and Deploy
    when 'deploy'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb deploy --verbose"
    when 'setup'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb setup --verbose"

    # remote mina scripts
    when 'force-unlock'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb deploy:force_unlock --verbose"
    when 'cleanup'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb deploy:cleanup --verbose"

    # Snapshot Backup
    when 'remote-backup'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb backup --verbose"
    when 'local-backup'
      exec "export to=#{environment} && bundle exec astrails-safe .bonethug/backup.rb"

    # Synchronised backup
    when 'sync-backup-to'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb sync_backup_to --verbose"
    when 'sync-backup-from'
      exec "export to=#{environment} && bundle exec mina -f .bonethug/deploy.rb sync_backup_from --verbose"

    end

  when 'watch'

    # handle args
    type = ARGV[1] || 'coffee_sass'
    location = ARGV[2] || '.'
    watch_only = ARGV[3] || nil

    # run the installer
    Watcher.watch type, location, watch_only

  when 'clean'

    location = ARGV[1] || '.'
    Installer.clean location

  else

    # We didn't find a task
    puts 'Task not found'

  end

end

.tasksObject



37
38
39
# File 'lib/bonethug/cli.rb', line 37

def self.tasks
  @@tasks
end