Class: Heroku::Command::Fork

Inherits:
Base
  • Object
show all
Defined in:
lib/heroku/command/fork.rb

Overview

clone an existing app

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#api, #app, #heroku, #initialize, namespace, #org, #org_api

Methods included from Helpers

#action, #app_owner, #ask, #confirm, #confirm_command, #create_git_remote, #debug, #debugging?, #default_org_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, #error_log, #error_log_path, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #has_git_remote?, #has_http_git_entry_in_netrc, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #org?, #org_host, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #stderr_print, #stderr_puts, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #update_git_remote, #warn_if_using_jruby, #with_tty

Constructor Details

This class inherits a constructor from Heroku::Command::Base

Instance Method Details

#indexObject

fork [NEWNAME]

Fork an existing app – copy config vars and Heroku Postgres data, and re-provision add-ons to a new app. New app name should not be an existing app. The new app will be created as part of the forking process.

-s, –stack STACK # specify a stack for the new app –region REGION # specify a region


18
19
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
# File 'lib/heroku/command/fork.rb', line 18

def index
  options[:ignore_no_org] = true

  from = app
  to = shift_argument || "#{from}-#{(rand*1000).to_i}"
  if from == to
    raise Heroku::Command::CommandFailed.new("Cannot fork to the same app.")
  end

  begin
    api.get_app(to).body
    error "#{to} app exists.\nUSAGE: heroku fork -a COPY_FROM COPY_TO"
  rescue
  end
  from_info = api.get_app(from).body

  to_info = action("Creating fork #{to}", :org => !!org) do
    params = {
      "name"    => to,
      "region"  => options[:region] || from_info["region"],
      "stack"   => options[:stack] || from_info["stack"],
      "tier"    => from_info["tier"] == "legacy" ? "production" : from_info["tier"]
    }

    if org
      org_api.post_app(params, org).body
    else
      api.post_app(params).body
    end
  end

  action("Copying slug") do
    copy_slug(from_info, to_info)
  end

  from_config = api.get_config_vars(from).body
  from_addons = api.get_addons(from).body

  from_addons.each do |addon|
    print "Adding #{addon["name"]}... "
    begin
      to_addon = api.post_addon(to, addon["name"]).body
      puts "done"
    rescue Heroku::API::Errors::RequestFailed => ex
      puts "skipped (%s)" % json_decode(ex.response.body)["error"]
    rescue Heroku::API::Errors::NotFound
      puts "skipped (not found)"
    end
    if addon["name"] =~ /^heroku-postgresql:/
      from_var_name = "#{addon["attachment_name"]}_URL"
      from_attachment = to_addon["message"].match(/Attached as (\w+)_URL\n/)[1]
      if from_config[from_var_name] == from_config["DATABASE_URL"]
        from_config["DATABASE_URL"] = api.get_config_vars(to).body["#{from_attachment}_URL"]
      end
      from_config.delete(from_var_name)

      plan = addon["name"].split(":").last
      unless %w(dev basic hobby-dev hobby-basic).include? plan
        wait_for_db to, to_addon
      end

      migrate_db addon, from, to_addon, to
    end
  end

  to_config = api.get_config_vars(to).body

  action("Copying config vars") do
    diff = from_config.inject({}) do |ax, (key, val)|
      ax[key] = val unless to_config[key]
      ax
    end
    api.put_config_vars to, diff
  end

  puts "Fork complete, view it at #{to_info['web_url']}"
rescue => e
  raise if e.is_a?(Heroku::Command::CommandFailed)

  puts "Failed to fork app #{from} to #{to}."
  message = "WARNING: Potentially Destructive Action\nThis command will destroy #{to} (including all add-ons)."

  if confirm_command(to, message)
    action("Deleting #{to}") do
      begin
        api.delete_app(to)
      rescue Heroku::API::Errors::NotFound
      end
    end
  end
  puts "Original exception below:"
  raise e
end