Module: Methods

Included in:
Core
Defined in:
lib/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ymlObject

Returns the value of attribute yml.



2
3
4
# File 'lib/core.rb', line 2

def yml
  @yml
end

Instance Method Details

#badger_logsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/core.rb', line 10

def badger_logs
  gem_setup
  Dir.chdir(@badger_root + "badger/")
  if ARGV[1] == "tail"
    trap("INT") do
      puts
      puts "Killing tail signal.."
      puts
    end
    `cap rails-app logs -s brat=tail`
  else
    `cap rails-app logs -s brat=nil`
  end
end

#create(app = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/core.rb', line 62

def create(app = nil)
  gem_setup
  Dir.chdir(@badger_root + "badger/")
  app_claw = @badger_root + "badger/core/claws/"
  ensure_ssh
  if app.nil?
    rails_server(app_claw)
  else
    worker_server(app_claw, app)
  end
end

#database_server(app_claw) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/core.rb', line 25

def database_server(app_claw)
  command = "#{app_claw}db-app.claw > #{app_claw}app.tmp; mv -f #{app_claw}app.tmp #{app_claw}db-app.claw"
  case @yml['db']['redis']
  when true
    `sed 's/.*install_source_redis.*/install_source_redis/g' #{command}`
  when false
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  else
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  end
  `cap db-app db_setup;
  cap db-app db_sync;
  cap db-app db_deploy`
  puts "\nDatabase server deployed.\n\nIf you do not have a web project built.\nRun: badger create < name >\n\nOr if a project already exists.\nRun: git commit -m \"database config\"\nRun: git push badger master\nRun: badger deploy\n\n"
end

#dbObject



53
54
55
56
57
58
59
60
# File 'lib/core.rb', line 53

def db
  gem_setup('db')
  Dir.chdir(@badger_root + "badger/")
  app_claw = @badger_root + "badger/core/claws/"
  ensure_ssh
  database_server(app_claw)
  #TODO write to info file.
end

#ensure_badger_ymlObject



74
75
76
77
78
79
# File 'lib/core.rb', line 74

def ensure_badger_yml
  if not File.exists?("badger.yml")
    @errors.errors('yaml exists')
    exit
  end
end

#ensure_external_dbObject



81
82
83
84
85
86
87
# File 'lib/core.rb', line 81

def ensure_external_db
  @yml = YAML.load_file(@rails_root + "/config/badger.yml")
  if @yml['local_db'].nil? or @yml['local_db'] == true
    @errors.errors('local db')
    exit
  end
end

#ensure_rails_rootObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/core.rb', line 89

def ensure_rails_root
  config_dir = Dir.exists?("config/")
  if config_dir
    @rails_root = Dir.pwd
    Dir.chdir("config/")
  else
    while not Dir.pwd == "/"
      if Dir.exists?("config/")
        break
      else
        Dir.chdir("..")
      end
    end
    if Dir.pwd == "/"
      @errors.errors('rails root')
      exit
    else
      @rails_root = Dir.pwd
      Dir.chdir("config/")
    end
  end
end

#ensure_sshObject



112
113
114
115
116
117
# File 'lib/core.rb', line 112

def ensure_ssh
  if not File.exists?("#{Dir.home}/.ssh/id_rsa.pub")
      @errors.errors('ssh')
    exit
  end
end

#exists_errorObject



119
120
121
122
# File 'lib/core.rb', line 119

def exists_error
  @errors.errors('rails project')
  exit
end

#gem_setup(db = nil) ⇒ Object



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
# File 'lib/core.rb', line 124

def gem_setup(db = nil)
  ensure_badger_yml
  @yml = YAML.load_file(@rails_root + "/config/badger.yml")
  `echo git@#{@yml['domain']}:/opt/git/#{ARGV[1]}.git > #{@badger_root}badger/core/files/git/git`
  `echo #{ARGV[1]} > #{@badger_root}badger/core/files/badger/info`
    FileUtils.cp(Dir.pwd + "/badger.yml", @badger_root + "badger/config/rails-app.yml")
  unless db.nil?
    db_yml = YAML.load_file(Dir.pwd + "/database_example.yml")
    if @yml['db']['connection_internal'] == true
      connection = '10%'
    else
      connection = '%'
    end

    db_settings = "\#{db_yml['staging']['username']}\n\#{db_yml['staging']['password']}\n\#{db_yml['staging']['database']}\n\#{db_yml['production']['username']}\n\#{db_yml['production']['password']}\n\#{db_yml['production']['database']}\n\#{connection}\n    eos\n\n    File.open(@badger_root + \"badger/core/files/mysql/db_stats\", 'w') {|f| f.write(db_settings)}\n  end\nend\n"

#git_msgObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/core.rb', line 152

def git_msg
  puts "\nBadger is now setup.\nA remote has been added for git@\#{@yml['domain']}:/opt/git/\#{ARGV[1]}.git\n\nUsage:\ngit add .\ngit commit -m \"first badger commit\"\ngit push badger master\nbadger deploy\n\n"
end

#infoObject



167
168
169
170
171
172
173
174
175
# File 'lib/core.rb', line 167

def info
  gem_setup
  Dir.chdir(@badger_root + "badger/")
  app_claw = @badger_root + "badger/core/claws/"
  ensure_ssh
  exists = `cap #{ARGV[1]} createExists`
  info_error if exists.chomp == "false"
  `cap rails-app info`
end

#info_errorObject



177
178
179
180
# File 'lib/core.rb', line 177

def info_error
  @errors.errors('info error')
  exit
end

#initializeObject



4
5
6
7
8
# File 'lib/core.rb', line 4

def initialize
  @errors = Errors.new
  spec = Gem::Specification.find_by_name("badger-rails")
  @badger_root = spec.gem_dir + "/lib/"
end

#rails_server(app_claw) ⇒ Object



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
# File 'lib/core.rb', line 182

def rails_server(app_claw)
  command = "#{app_claw}rails-app.claw > #{app_claw}app.tmp; mv -f #{app_claw}app.tmp #{app_claw}rails-app.claw"
  case @yml['redis']
  when true
    `sed 's/.*install_source_redis.*/install_source_redis/g' #{command}`
  when false
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  else
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  end

  if @yml['environment'] == "production"
    `sed 's/.*config_capistrano.*/config_capistrano production/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*config_capistrano.*/config_capistrano staging/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  if @yml['environment'] == "production"
    `sed 's/.*nginx_add_site.*/nginx_add_site production-web/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*nginx_add_site.*/nginx_add_site staging-web/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  exists = `cap #{ARGV[1]} createExists`
  exists_error if exists.chomp == "true"
  `cap #{ARGV[1]} setup`
  `cap #{ARGV[1]} sync`
  `cap #{ARGV[1]} git`
  Dir.chdir(@rails_root)
  `git init`
  `git remote add badger git@#{@yml['domain']}:/opt/git/#{ARGV[1]}.git`
  git_msg
end

#rake_tasksObject



220
221
222
223
224
225
226
227
228
# File 'lib/core.rb', line 220

def rake_tasks
  gem_setup
  arguments = ""
  ARGV[1..-1].each do |a|
    arguments << a << " "
  end
  Dir.chdir(@badger_root + "badger/")
  `cap rails-app rake -s brat=#{arguments.gsub!(" ", "^")}`
end

#scale(workers, app) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/core.rb', line 230

def scale(workers, app)
  gem_setup
  Dir.chdir(@badger_root + "badger/")
  app_claw = @badger_root + "badger/core/claws/"
  ensure_ssh
  `cap #{app} scale -s workers=#{workers}`
end

#updateObject



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
# File 'lib/core.rb', line 238

def update
  gem_setup
  Dir.chdir(@badger_root + "badger/")
  app_claw = @badger_root + "badger/core/claws/"
  ensure_ssh
  command = "#{app_claw}rails-app.claw > #{app_claw}app.tmp; mv -f #{app_claw}app.tmp #{app_claw}rails-app.claw"
  case @yml['redis']
  when true
    `sed 's/.*install_source_redis.*/install_source_redis/g' #{command}`
  when false
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  else
    `sed 's/.*install_source_redis.*/#install_source_redis/g' #{command}`
  end

  if @yml['environment'] == "production"
    `sed 's/.*config_capistrano.*/config_capistrano production/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*config_capistrano.*/config_capistrano staging/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  if @yml['environment'] == "production"
    `sed 's/.*nginx_add_site.*/nginx_add_site production-web/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*nginx_add_site.*/nginx_add_site staging-web/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  exists = `cap #{ARGV[1]} exists`
  update_error if not exists.chomp == "true"
  `cap #{ARGV[1]} sync`
end

#update_errorObject



274
275
276
277
# File 'lib/core.rb', line 274

def update_error
  @errors.errors('update error')
  exit
end

#worker_server(app_claw, app) ⇒ Object



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
# File 'lib/core.rb', line 279

def worker_server(app_claw, app)
  command = "#{app_claw}app.claw > #{app_claw}app.tmp; mv -f #{app_claw}app.tmp #{app_claw}app.claw"
  if @yml['environment'] == "production"
    `sed 's/.*config_capistrano.*/config_capistrano production-app #{app}/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*config_capistrano.*/config_capistrano staging-app #{app}/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  if @yml['environment'] == "production"
    `sed 's/.*nginx_add_site.*/nginx_add_site production-web/g' #{command}`
  elsif @yml['environment'] == "staging"
    `sed 's/.*nginx_add_site.*/nginx_add_site staging-web/g' #{command}`
  else
    @errors.errors('environment')
    exit
  end
  case @yml[app]['media']
  when true
    while true
      puts "\nBadger-Rails does not provide licensing for the following installations. You will need to check your use case and compare to the following GPL licenses.\nFFMPEG, X264, LAME, LIBMAD, LIBID3TAG, MADPLAY, SOX, FAAC, XVID\n\nDo you wish to continue with ffmpeg installation? [ y/n ]\n\n"
      response = STDIN.gets.chomp
      if response == "y"
        `sed 's/.*install_media_group.*/install_media_group/g' #{command}`
        break
      elsif response == "n"
        `sed 's/.*install_media_group.*/#install_media_group/g' #{command}`
        break
      end
    end
  when false
    `sed 's/.*install_media_group.*/#install_media_group/g' #{command}`
  else
    `sed 's/.*install_media_group.*/#install_media_group/g' #{command}`
  end
  `echo '#{@yml['password']}' > #{@badger_root}badger/core/files/ssh/app;
  echo '#{@yml['domain']}' >> #{@badger_root}badger/core/files/ssh/app;
  cap #{app} app_setup;
  cap #{app} app_sync;
  cap #{app} app_deploy`

  puts
  puts "Worker server deployed."
  puts
end