Class: Autobot::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/autobot/runner.rb

Class Method Summary collapse

Class Method Details

.check_binariesObject

Public: Check if needed binaries are found: if any of these doesn’t exist the program exits.

Returns nothing.



46
47
48
49
50
51
# File 'lib/autobot/runner.rb', line 46

def self.check_binaries
  puts 'Checking binaries...'

  abort('Missing npm, exiting...') unless which('npm')
  abort('Missing heroku toolbelt, exiting...') unless which('heroku')
end

.create_empty_hubot(app_path) ⇒ Object

Public: Clones a virgin hubot from GitHub repository in a local repository.

app_path - The path of your app.

Returns nothing.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/autobot/runner.rb', line 95

def self.create_empty_hubot(app_path)
  temp_dir = Dir.mktmpdir
  system("git clone https://github.com/github/hubot.git #{temp_dir}")

  Dir.chdir(temp_dir)

  # Install needed node.js packages and create an instance of hubot locally
  system('npm install && make package')

  # Copy hubot and clean tmpdir
  FileUtils.cp_r("./hubot", app_path)
  FileUtils.rm_rf(temp_dir)
end

.create_hubot(app_name) ⇒ Object

Public: Creates an hubot instance starting with an application name.

app_name - The name of your app.

Returns nothing.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/autobot/runner.rb', line 16

def self.create_hubot(app_name)
  check_binaries

  print 'Insert your heroku api token: '
  api_token = $stdin.gets
  heroku = (api_token)

  heroku_app, is_new = get_or_create_heroku_app(heroku, app_name)
  git_remote = heroku_app.data[:body]['git_url']
  app_path = "#{Dir.pwd}/#{app_name}"

  if Dir.exists?(app_path)
    push_hubot_app(app_name, app_path, heroku, git_remote)
  else
    if is_new
      create_empty_hubot(app_path)
    else
      system("git clone #{repository_url} #{app_path}")
    end

    puts "I've just cloned the repository: make every change you want and commit. I'll do the rest."
  end

  puts 'Autobot shutting down...'
end

.get_or_create_heroku_app(heroku, app_name) ⇒ Object

Public: Checks if an app exists: if no it creates one.

heroku - An heroku client instance. app_name - The name of your app.

Returns the duplicated String.



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/autobot/runner.rb', line 115

def self.get_or_create_heroku_app(heroku, app_name)
  begin
    [ heroku.get_app(app_name), false ]
  rescue Heroku::API::Errors::Forbidden
    nil
  rescue Heroku::API::Errors::NotFound
    puts "I didn't find #{app_name} on Heroku. I will create it for you!"
    heroku.post_app('name' => app_name)
    [ heroku.get_app(app_name), true ]
  end
end

.heroku_login(api_token) ⇒ Object

Public: Makes login to heroku.

api_token - The API Token of your heroku account.

Returns an heroku client instance.



132
133
134
135
136
137
138
139
140
141
# File 'lib/autobot/runner.rb', line 132

def self.(api_token)
  heroku = Heroku::API.new(api_key: api_token)
  begin
    heroku.get_user
    puts 'Authentication successful'
    heroku
  rescue Heroku::API::Errors::Unauthorized
    abort('Your api token is not valid. Please verify it and retry to launch autobot!')
  end
end

.push_hubot_app(app_name, app_path, heroku, git_remote) ⇒ Object

Public: Pushes local repository to heroku adding a remote if it doesn’t exist.

app_name - The name of your app. app_path - The path of your app. heroku - An heroku client instance. git_remote - The URL of the remote repository.

Returns nothing.



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
# File 'lib/autobot/runner.rb', line 61

def self.push_hubot_app(app_name, app_path, heroku, git_remote)
  Dir.chdir(app_path)

  # Add heroku application as a remote
  system("git remote add herokuapp #{git_remote}")

  # Make sure we are at the latest version
  system("git pull herokuapp master")

  # Push our commits to heroku remote
  system("git push herokuapp master")

  return unless acceptor('Would you like to insert campfire environment variables?')

  # Ask and add as config campfire data
  puts 'Insert hubot campfire token'
  hubot_token  = $stdin.gets

  puts 'Insert hubot campfire rooms'
  hubot_rooms  = $stdin.gets

  puts 'Insert hubot campfire domain (e.g. the third level domain in your campfire account)'
  hubot_domain = $stdin.gets

  heroku.put_config_vars(app_name, 'HUBOT_CAMPFIRE_TOKEN' => hubot_token,
                                  'HUBOT_CAMPFIRE_ROOMS' => hubot_rooms,
                                  'HUBOT_CAMPFIRE_ACCOUNT' => hubot_domain)
end