fanforce-app-factory 2.0.0.rc36 → 2.0.0.rc37

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,7 @@ class Fanforce::AppFactory::CLI::Env
36
36
  File.open("#{app.dir}/.appenv", 'w') {|f| f.write convert_hash_to_shell_env(vars) }
37
37
  else
38
38
  error 'could not find heroku in .fanforce-app-factory' if config[:heroku][environment].blank?
39
- Fanforce::AppFactory::CLI::Heroku.new(app).update_config(environment, vars)
39
+ Fanforce::AppFactory::CLI::Heroku.new(app, environment).update_config(vars)
40
40
  end
41
41
  end
42
42
 
@@ -1,94 +1,112 @@
1
1
  class Fanforce::AppFactory::CLI::Heroku
2
2
  include Fanforce::AppFactory::CLI::Utils
3
3
 
4
- attr_reader :app
4
+ attr_reader :app, :environment
5
5
 
6
6
  require_gem 'heroku-api', 'heroku-api'
7
7
 
8
- def initialize(app)
8
+ def initialize(app, environment)
9
9
  @app = app
10
+ @environment = environment.to_sym
10
11
  end
11
12
 
12
- def heroku(environment)
13
- @heroku ||= {}
14
- @heroku[environment] ||= Heroku::API.new(:username => config[:heroku][environment][:user], :password => config[:heroku][environment][:password])
13
+ def heroku
14
+ @heroku ||= Heroku::API.new(:username => config[:heroku][environment][:user], :password => config[:heroku][environment][:password])
15
15
  rescue Exception => e
16
16
  error "OOPS... #{environment.to_s.upcase}".format(:red,:bold) + ' has not been setup for Heroku' if config[:heroku].blank? or config[:heroku][environment].blank?
17
17
  #raise "Heroku user #{config[:heroku][environment][:user]} does not seem to exist: #{e.message}" if e.response.status == 404
18
18
  raise
19
19
  end
20
20
 
21
- def setup(environment)
22
- environment = environment.to_sym
23
- heroku = heroku(environment)
24
- heroku_app_name = app_name(environment)
21
+ def ensure_git_remote_exists
22
+ remote_name = "#{env}-heroku"
23
+ remote_url = "git@#{config[:heroku][environment][:git_ssh_domain] || 'heroku.com'}:#{app_name}.git"
25
24
 
26
- # Setup standard app domain
27
- domain = "#{app._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:apps_base]}"
28
- domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
29
- if domain_found
30
- puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
31
- else
32
- heroku.post_domain(heroku_app_name, domain)
33
- puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
34
- end
35
-
36
- # Setup default short domain
37
- domain = "#{app._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:default_short_domain]}"
38
- domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
39
- if domain_found
40
- puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
41
- else
42
- heroku.post_domain(heroku_app_name, domain)
43
- puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
25
+ found_remote = false
26
+ found_valid_remote = false
27
+ (`git remote -v`).lines do |line|
28
+ next if line !~ Regexp.new("^#{remote_name}[\s\t]")
29
+ found_remote = true
30
+ found_valid_remote = true if line.include?(remote_url)
31
+ break
44
32
  end
45
33
 
46
- # Setup git remote
47
- remote_name = "#{env(environment)}-heroku"
48
- if (`git remote`).split(/\r?\n/).include?(remote_name)
49
- puts "#{'Updated '.format(:green,:bold)}" + "git remote for #{remote_name}"
34
+ if found_valid_remote
35
+ return log "#{'Found '.format(:green,:bold)}" + "git remote for #{remote_name}"
36
+ elsif found_remote
37
+ log "#{'Updated '.format(:green,:bold)}" + "git remote for #{remote_name}"
50
38
  `git remote rm #{remote_name}`
51
39
  else
52
- puts "#{'Created '.format(:green,:bold)}" + "git remote for #{remote_name}"
40
+ log "#{'Created '.format(:green,:bold)}" + "git remote for #{remote_name}"
53
41
  end
54
- `git remote add #{remote_name} git@#{config[:heroku][environment][:git_ssh_domain] || 'heroku.com'}:#{heroku_app_name}.git`
42
+ `git remote add #{remote_name} #{remote_url}`
43
+ end
44
+
45
+ def git_remote_name
46
+ "#{env}-heroku"
47
+ end
48
+
49
+ def restart
50
+ heroku.post_ps_restart(app_name)
55
51
  end
56
52
 
57
- def app_exists?(heroku, app_name)
53
+ def destroy
54
+ heroku.delete_app(app_name)
55
+ end
56
+
57
+ def app_exists?
58
58
  heroku.get_app(app_name)
59
59
  return true
60
60
  rescue
61
61
  return false
62
62
  end
63
63
 
64
- def ensure_app_exists(environment)
65
- heroku = heroku(environment)
66
- heroku_app_name = app_name(environment)
67
- if app_exists?(heroku, heroku_app_name)
68
- puts "#{'Found '.format(:green,:bold)}" + "#{environment} app on heroku (#{heroku_app_name})"
64
+ def ensure_app_exists
65
+ return puts "#{'Found '.format(:green,:bold)}" + "#{environment} app on heroku (#{app_name})" if app_exists?
66
+ create_app
67
+ end
68
+
69
+ def create_app
70
+ heroku.post_app(name: app_name)
71
+ puts "#{'Created '.format(:green,:bold)}" + "#{environment} on heroku (#{app_name})"
72
+
73
+ # Setup standard app domain
74
+ domain = "#{app._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:apps_base]}"
75
+ domain_found = heroku.get_domains(app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
76
+ if domain_found
77
+ puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
78
+ else
79
+ heroku.post_domain(app_name, domain)
80
+ puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
81
+ end
82
+
83
+ # Setup default short domain
84
+ domain = "#{app._id}.#{Fanforce::Base::DomainEnvironments.method(environment).call[:default_short_domain]}"
85
+ domain_found = heroku.get_domains(app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
86
+ if domain_found
87
+ puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
69
88
  else
70
- heroku.post_app(name: heroku_app_name)
71
- puts "#{'Created '.format(:green,:bold)}" + "#{environment} on heroku (#{heroku_app_name})"
89
+ heroku.post_domain(app_name, domain)
90
+ puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
72
91
  end
73
92
  end
74
93
 
75
- def update_config(environment, vars)
76
- heroku(environment).put_config_vars(app_name(environment), vars) if vars
94
+ def update_config(vars)
95
+ heroku.put_config_vars(app_name, vars) if vars
77
96
  end
78
97
 
79
- def env(environment)
98
+ def env
80
99
  case environment
81
100
  when :staging then env = :stg
82
101
  when :production then env = :prd
83
102
  end
84
103
  end
85
104
 
86
- def app_name(environment)
87
- environment = environment.to_sym
105
+ def app_name
88
106
  raise 'unknown environment' if ![:production, :staging].include?(environment)
89
107
 
90
- heroku_app_name = "#{env(environment)}-#{app.dir_name}"
91
- heroku_app_name.length > 30 ? heroku_app_name.gsub!(/(a|e|i|o|u)/, '') : heroku_app_name
108
+ app_name = app._id.length > 22 ? app._id.gsub(/(a|e|i|o|u)/, '') : app._id
109
+ "#{env}-app-#{app_name}"
92
110
  end
93
111
 
94
112
  end
@@ -18,19 +18,17 @@ class Fanforce::AppFactory::CLI::Scripts
18
18
  log divider '+-----'
19
19
  confirm("You are about to delete both local and remote files for #{app_id}.\nARE YOU SURE?", true)
20
20
 
21
- # puts "\n---------------------------------------------------------------------------------------------------------------"
22
- # puts 'DELETING HEROKU APPS...'
23
- # [:staging,:production].each do |environment|
24
- # next if $Config[:heroku].blank? or $Config[:heroku][environment].blank?
25
- # heroku = auth_heroku(environment)
26
- # heroku_app_name = get_heroku_app_name(app, environment)
27
- # begin
28
- # heroku.delete_app(heroku_app_name)
29
- # puts "#{'Removed '.format(:green,:bold)}" + " #{heroku_app_name}"
30
- # rescue Exception => e
31
- # puts "#{'Already Removed'.format(:green,:bold)}" + " #{heroku_app_name}"
32
- # end
33
- # end
21
+ log divider '+-----------------------------------------------------------------------------------------------------'
22
+ log 'DELETING HEROKU APPS...'
23
+ [:staging,:production].each do |environment|
24
+ heroku = Fanforce::AppFactory::CLI::Heroku.new(app, environment)
25
+ if heroku.app_exists?
26
+ heroku.destroy
27
+ log "#{'Removed '.format(:green,:bold)}" + " #{heroku.app_name}"
28
+ else
29
+ log "#{'Already Removed'.format(:green,:bold)}" + " #{heroku.app_name}"
30
+ end
31
+ end
34
32
 
35
33
  log divider '+-----------------------------------------------------------------------------------------------------'
36
34
  log 'REMOVING BITBUCKET REPOSITORY...'
@@ -35,8 +35,8 @@ class Fanforce::AppFactory::CLI::Scripts
35
35
  end
36
36
 
37
37
  if [:all,:heroku].include?(command) and environment != :development
38
- heroku = Fanforce::AppFactory::CLI::Heroku.new(app)
39
- heroku.ensure_app_exists(environment)
38
+ heroku = Fanforce::AppFactory::CLI::Heroku.new(app, environment)
39
+ heroku.ensure_app_exists
40
40
  end
41
41
 
42
42
  if [:all,:iron].include?(command)
@@ -69,20 +69,14 @@ class Fanforce::AppFactory::CLI::Scripts
69
69
  end
70
70
 
71
71
  def push_heroku(heroku, app, environment)
72
- case environment
73
- when :staging then env = :stg
74
- when :production then env = :prd
75
- end
76
- remote_name = "#{env}-heroku"
77
-
78
- heroku.setup(environment) if !(`git remote`).split(/\r?\n/).include?(remote_name)
72
+ heroku.ensure_git_remote_exists
79
73
 
80
74
  vars = Fanforce::AppFactory::CLI::Env.new(app).vars(environment)
81
- heroku.update_config(environment, vars)
75
+ heroku.update_config(vars)
82
76
  log 'Updated ENV'.format(:green,:bold) + " on Heroku #{environment.to_s.titleize} (#{vars.size} variables)..."
83
77
 
84
- puts "\n#{'Pushing '.format(:green,:bold)}" + "latest commit to Heroku #{environment.to_s.titleize} (#{remote_name})..."
85
- console_command("git push #{remote_name} master", true)
78
+ puts "\n#{'Pushing '.format(:green,:bold)}" + "latest commit to Heroku #{environment.to_s.titleize} (#{heroku.git_remote_name})..."
79
+ console_command("git push #{heroku.git_remote_name} master", true)
86
80
  end
87
81
 
88
82
  end
@@ -25,13 +25,7 @@ class Fanforce::AppFactory::CLI::Scripts
25
25
  FileUtils.mkdir("#{app.dir}/tmp") if !File.directory?("#{app.dir}/tmp")
26
26
  FileUtils.touch("#{app.dir}/tmp/restart.txt")
27
27
  elsif [:production, :staging].include?(environment)
28
- error 'Production and Staging are not working'
29
- if config[:heroku].blank? or config[:heroku][environment].blank?
30
- puts "#{'OOPS...'.format(:red,:bold)} #{environment} has not been setup on heroku"
31
- next
32
- end
33
- heroku = auth_heroku(environment)
34
- heroku.post_ps_restart get_heroku_app_name(app, environment)
28
+ Fanforce::AppFactory::CLI::Heroku.new(app, environment).restart
35
29
  end
36
30
  log "#{"Restarted #{environment}".format(:bold,:green)} #{app.dir_name}"
37
31
  end
@@ -1,5 +1,5 @@
1
1
  class Fanforce
2
2
  class AppFactory
3
- VERSION = '2.0.0.rc36'
3
+ VERSION = '2.0.0.rc37'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanforce-app-factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc36
4
+ version: 2.0.0.rc37
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: