class Fanforce::PluginFactory::CLI::Heroku include Fanforce::PluginFactory::CLI::Utils attr_reader :plugin, :environment require_gem 'heroku-api', 'heroku-api' def initialize(plugin, environment) @plugin = plugin @environment = environment.to_sym end def heroku @heroku ||= Heroku::API.new(:username => config[:heroku][environment][:user], :password => config[:heroku][environment][:password]) rescue Exception => e error "OOPS... #{environment.to_s.upcase}".format(:red,:bold) + ' has not been setup for Heroku' if config[:heroku].blank? or config[:heroku][environment].blank? #raise "Heroku user #{config[:heroku][environment][:user]} does not seem to exist: #{e.message}" if e.response.status == 404 raise end def ensure_git_remote_exists remote_name = "#{env}-heroku" remote_url = "git@#{config[:heroku][environment][:git_ssh_domain] || 'heroku.com'}:#{app_name}.git" found_remote = false found_valid_remote = false (`git remote -v`).lines do |line| next if line !~ Regexp.new("^#{remote_name}[\s\t]") found_remote = true found_valid_remote = true if line.include?(remote_url) break end if found_valid_remote return log "#{'Found '.format(:green,:bold)}" + "git remote for #{remote_name}" elsif found_remote log "#{'Updated '.format(:green,:bold)}" + "git remote for #{remote_name}" `git remote rm #{remote_name}` else log "#{'Created '.format(:green,:bold)}" + "git remote for #{remote_name}" end `git remote add #{remote_name} #{remote_url}` end def git_remote_name "#{env}-heroku" end def restart heroku.post_ps_restart(app_name) end def destroy heroku.delete_app(app_name) end def app_exists? heroku.get_app(app_name) return true rescue return false end def ensure_app_exists if app_exists? puts "#{'Found '.format(:green,:bold)}" + "#{environment} app on heroku (#{app_name})" ensure_domains_exist else create_app ensure_domains_exist end end def create_app heroku.post_app(name: app_name) puts "#{'Created '.format(:green,:bold)}" + "#{environment} on heroku (#{app_name})" end def ensure_domains_exist # Setup standard app domain add_domain("#{plugin._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:plugins_base]}") # Setup default short domain add_domain("#{plugin._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:default_short_domain]}") end def add_domain(domain_name) domain_found = heroku.get_domains(app_name).body.inject(false) {|result, d| d['domain'] == domain_name ? (break true) : false } if domain_found puts "#{'Found '.format(:green,:bold)}" + "#{domain_name} domain on #{environment}" else heroku.post_domain(app_name, domain_name) puts "#{'Added '.format(:green,:bold)}" + "#{domain_name} domain to #{environment}" end rescue puts "#{'Error '.format(:red,:bold)}" + "adding #{domain_name} domain on #{environment}" end def update_config(vars) heroku.put_config_vars(app_name, vars) if vars end def env Fanforce::PluginFactory::CLI::Utils.env(environment) end def app_name raise 'unknown environment' if ![:production, :staging].include?(environment) app_name = plugin._id.length > 21 ? plugin._id.gsub(/(a|e|i|o|u)/, '') : plugin._id "#{env}-plgn-#{app_name}" end end