Class: LastResort::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/last-resort/commands.rb

Class Method Summary collapse

Class Method Details

.ask(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/last-resort/commands.rb', line 24

def self.ask message
  begin
    print message
    response = $stdin.gets.strip
    if response.empty?
      puts "Sorry you must enter something."
    end
  end while response.empty?

  response
end

.ask_if_herokuObject



99
100
101
102
# File 'lib/last-resort/commands.rb', line 99

def self.ask_if_heroku
  puts ''
  @no_heroku = !(ask_yes_no "Do you want it to be hosted on #{'Heroku'.magenta} (recommended)? [Y/n] ")
end

.ask_yes_no(message) ⇒ Object



17
18
19
20
21
22
# File 'lib/last-resort/commands.rb', line 17

def self.ask_yes_no message
  begin
    answer = ask message
  end while !(answer.casecmp('y') == 0 or answer.casecmp('n') == 0)
  answer.casecmp('y') == 0
end

.copy_filesObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/last-resort/commands.rb', line 79

def self.copy_files
  puts '* creating .gitignore'.green
  FileUtils.cp "#{@last_resort_path}/support/dot_gitignore", "#{@project_path}/.gitignore"

  puts '* creating config.ru'.green
  FileUtils.cp "#{@last_resort_path}/support/config.ru", "#{@project_path}"

  puts '* creating Gemfile'.green
  FileUtils.cp "#{@last_resort_path}/support/Gemfile", "#{@project_path}"
end

.copy_schedule_and_add_utcObject



90
91
92
93
94
95
96
97
# File 'lib/last-resort/commands.rb', line 90

def self.copy_schedule_and_add_utc
  puts '* creating schedule.rb'.green
  schedule_file = open(@project_path + '/schedule.rb', 'w') do |f|
    f.puts open(@last_resort_path + '/support/schedule.rb').read % { 
      :utc_offset => Time.now.utc_offset/60/60 
    }
  end
end

.create_envObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/last-resort/commands.rb', line 128

def self.create_env
  open('.env', 'w') do |f|
    f.puts open(@last_resort_path + '/support/dot_env').read % {
      :host => (@no_heroku) ? '' : @host,
      :twilio_sid => @twillio_sid,
      :twilio_auth_token => @twillio_auth_token,
      :contextio_account => @contextio_account,
      :contextio_key => @contextio_key,
      :contextio_secret => @contextio_secret,
      :no_heroku => @no_heroku
    }
  end

  if @no_heroku
    puts 'Settings for Last Resort are stored in a .env file that can be found in the project directory.'.yellow
    puts 'In order to run your project, you must modify the .env to include the domain name of your server.'.yellow 
  end
end

.create_heroku_projectObject



122
123
124
125
126
# File 'lib/last-resort/commands.rb', line 122

def self.create_heroku_project
  puts 'Creating Heroku project'.green
  heroku_output = `heroku create --stack cedar`
  @host = heroku_output.match(/http(.*).com\//)[0]
end

.create_project(project_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/last-resort/commands.rb', line 54

def self.create_project project_name
  @project_path = project_name.to_s

  create_project_folder
  copy_files
  copy_schedule_and_add_utc
  
  old_dir = Dir.pwd
  Dir.chdir("#{@project_path}")

  `bundle install`
  set_up_heroku unless ask_if_heroku
  set_up_git unless @no_heroku
  create_heroku_project unless @no_heroku
  create_env
  `git push heroku master` unless @no_heroku

  Dir.chdir("#{old_dir}")
end

.create_project_folderObject



74
75
76
77
# File 'lib/last-resort/commands.rb', line 74

def self.create_project_folder
  puts "\nCreating project folder"
  FileUtils.mkdir @project_path
end

.get_contextio_infoObject



44
45
46
47
48
49
50
51
52
# File 'lib/last-resort/commands.rb', line 44

def self.get_contextio_info
  answer = ask_yes_no "Do you already have a #{'ContextIO'.yellow} account? [Y/n]"
  Launchy.open("http://www.context.io") unless answer
  puts ''
  puts "Please find the ContextIO key and secret tokens, as well as the ContextIO Account ID of the email account you wish to monitor."
  @contextio_key = ask "#{'ContextIO'.yellow} Key: "
  @contextio_secret = ask "#{'ContextIO'.yellow} Secret: "
  @contextio_account = ask "#{'ContextIO'.yellow} Email Account ID: "
end

.get_twillio_infoObject



36
37
38
39
40
41
42
# File 'lib/last-resort/commands.rb', line 36

def self.get_twillio_info
  answer = ask_yes_no "Do you already have a #{'Twillio'.red} account? [Y/n]"
  Launchy.open("http://www.twilio.com/try-twilio") unless answer
  @twillio_sid = ask "#{'Twillio'.red} SID: "
  @twillio_auth_token = ask "#{'Twillio'.red} Auth Token: "
  puts ''
end

.q_and_a(project_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/last-resort/commands.rb', line 6

def self.q_and_a project_name
  @last_resort_path = File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../../')

  puts "#{"Last Resort".green} is a Ruby gem for monitoring critical emails sent by automated services (monit, logging packages, external \nping services, etc.) and calling your phone to tell you about it.\n\n"

  get_twillio_info
  get_contextio_info

  create_project project_name
end

.run_heroku_or_rackupObject

RUN


149
150
151
152
153
154
155
156
157
# File 'lib/last-resort/commands.rb', line 149

def self.run_heroku_or_rackup
  begin
    LastResort::Config::populate_env_if_required
  rescue
    puts 'Make sure to run "last-resort run" from a last-resort project'.yellow
    return
  end

end

.set_up_gitObject



115
116
117
118
119
120
# File 'lib/last-resort/commands.rb', line 115

def self.set_up_git
  puts 'Initiating git repo'.green
  `git init`
  `git add .`
  `git commit -m "Initializing git"`
end

.set_up_herokuObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/last-resort/commands.rb', line 104

def self.set_up_heroku
  puts 'Installing heroku'.green
  `gem install heroku --no-rdoc --no-ri`
  `heroku plugins:install git://github.com/ddollar/heroku-config.git`

  answer = ask "Do you already have a #{'Heroku'.magenta} account? [Y/n]"
  Launchy.open("http://heroku.com") if answer.casecmp('n') == 0
  puts 'Please login to Heroku'
  system 'heroku login'
end