Class: Engineyard::Migrate::CLI

Inherits:
Thor
  • Object
show all
Includes:
EY::UtilityMethods
Defined in:
lib/engineyard-migrate/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verboseObject (readonly)

Returns the value of attribute verbose.



14
15
16
# File 'lib/engineyard-migrate/cli.rb', line 14

def verbose
  @verbose
end

Instance Method Details

#heroku(path) ⇒ Object



20
21
22
23
24
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
52
53
54
55
56
57
58
59
60
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/engineyard-migrate/cli.rb', line 20

def heroku(path)
  @verbose = options[:verbose]
  error "Path '#{path}' does not exist" unless File.exists? path
  FileUtils.chdir(path) do
    begin
      heroku_repo = `git config remote.heroku.url`.strip
      if heroku_repo.empty?
        error "Not a Salesforce Heroku application."
      end
      heroku_repo =~ /git@heroku\.com:(.*)\.git/
      heroku_app_name = $1

      say "Requesting Heroku account information..."; $stdout.flush
      say "Heroku app:     "; say heroku_app_name, :green

      heroku_credentials = File.expand_path("~/.heroku/credentials")
      unless File.exists?(heroku_credentials)
        error "Please setup your Salesforce Heroku credentials first."
      end

      say `heroku info`
      say ""
      
      repo = `git config remote.origin.url`.strip
      if repo.empty?
        error "Please host your Git repo externally and add as remote 'origin'.", <<-SUGGESTION.gsub(/^\s{12}/, '')
        You can create a GitHub repository using 'github' gem:
          $ gem install github
          $ gh create-from-local --private
        SUGGESTION
      end
      unless EY::API.read_token
        error "Please create, boot and deploy an AppCloud application for #{repo}."
      end
      
      say "Requesting AppCloud account information..."; $stdout.flush
      @app, @environment = fetch_app_and_environment(options[:app], options[:environment], options[:account])

      unless @app.repository_uri == repo
        error "Please create, boot and deploy an AppCloud application for #{repo}."
      end
      unless @environment.app_master
        error "Please boot your AppCloud environment and then deploy your application."
      end

      @app.name = @app.name
      app_master_host = @environment.app_master.public_hostname
      app_master_user = @environment.username

      say   "Application:    "; say   "#{@app.name}", :green
      say   "Account:        "; say   "#{@environment..name}", :green
      say   "Environment:    "; say   "#{@environment.name}", :green
      say   "Cluster size:   "; say   "#{@environment.instances_count}"
      say   "Hostname:       "; say   "#{app_master_host}"
      debug "$RACK_ENV:      "; debug "#{@environment.framework_env}"
      say ""
  
      # TODO - what if no application deployed yet?
      # bash: line 0: cd: /data/heroku2eysimpleapp/current: No such file or directory

      # TODO - to test for cron setup:
      # dna_env["cron"] - list of:
      # [0] {
      #      "minute" => "0",
      #        "name" => "rake cron",
      #     "command" => "cd /data/heroku2eysimpleapp/current && RAILS_ENV=production rake cron",
      #       "month" => "*",
      #        "hour" => "1",
      #         "day" => "*/1",
      #        "user" => "deploy",
      #     "weekday" => "*"
      # }
      
      say "Testing AppCloud application status..."
      
      deploy_path_found = ssh_appcloud "test -d #{@app.name}/current && echo 'found'", 
        :path => '/data', :return_output => true
      error "Please deploy your AppCloud application before running migration." unless deploy_path_found =~ /found/
  
      say "Setting up Heroku on AppCloud..."

      ssh_appcloud "sudo gem install heroku taps --no-ri --no-rdoc -q"
      ssh_appcloud "git remote rm heroku 2> /dev/null; git remote add heroku #{heroku_repo} 2> /dev/null"

      say "Uploading Heroku credential file..."
      home_path = ssh_appcloud("pwd", :path => "~", :return_output => true)
      debug "AppCloud $HOME: "; debug home_path, :yellow
      ssh_appcloud "mkdir -p .heroku; chmod 700 .heroku", :path => home_path
      
      Net::SFTP.start(app_master_host, app_master_user) do |sftp|
        sftp.upload!(heroku_credentials, "#{home_path}/.heroku/credentials")
      end
      say ""
      
      say "Migrating data from Heroku '#{heroku_app_name}' to AppCloud '#{@app.name}'..."
      env_vars = %w[RAILS_ENV RACK_ENV MERB_ENV].map {|var| "#{var}=#{@environment.framework_env}" }.join(" ")
      ssh_appcloud "#{env_vars} heroku db:pull --confirm #{heroku_app_name} 2>&1"
      say ""
      
      say "Migration complete!", :green
    rescue SystemExit
    rescue EY::MultipleMatchesError => e
      envs = []
      e.message.split(/\n/).map do |line|
        env = {}
        line.scan(/--([^=]+)='([^']+)'/) do
          env[$1] = $2
        end
        envs << env unless env.empty?
      end
      too_many_environments_discovered 'heroku', envs, path
    rescue Net::SSH::AuthenticationFailed => e
      error "Please setup your SSH credentials for AppCloud."
    rescue Net::SFTP::StatusException => e
      error e.description + ": " + e.text
    rescue Exception => e
      say "Migration failed", :red
      puts e.inspect
      puts e.backtrace
    end
  end
end