Class: AFCLIClone::Clone

Inherits:
VMC::CLI
  • Object
show all
Defined in:
lib/clone-af-cli-plugin/plugin.rb

Instance Method Summary collapse

Instance Method Details

#cloneObject



13
14
15
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
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
# File 'lib/clone-af-cli-plugin/plugin.rb', line 13

def clone
  src_app = input[:src_app]
  dest_infra = input[:infra]
  dest_app_name = input[:name, "#{src_app.name}-#{"%04x" % [rand(0x0100000)]}"]
  app = client.app_by_name(dest_app_name)
  fail "Application '#{dest_app_name}' already exists" unless app.nil?
  dest_url = input[:url, "#{dest_app_name}.#{dest_infra.base}"]

  # Start Clone
  Dir.mktmpdir do |dir|

    # Download source
    zip_path = File.join(dir, src_app.name)
    with_progress("Pulling last pushed source code") do
      client.app_pull(src_app.name, zip_path)
    end

    # manifest = {
    #   :name => "#{dest_appname}",
    #   :staging => app[:staging],
    #   :uris => [ url ],
    #   :instances => app[:instances],
    #   :resources => app[:resources]
    # }
    # manifest[:staging][:command] = app[:staging][:command] if app[:staging][:command]
    # manifest[:infra] = { :provider => dest_infra.name } if dest_infra

    dest_app = client.app
    dest_app.name = dest_app_name
    dest_app.infra = dest_infra
    dest_app.url = dest_url
    dest_app.uris = [dest_url]
    dest_app.total_instances = 1
    dest_app.framework = src_app.framework
    dest_app.command = src_app.command unless src_app.command.nil?
    dest_app.runtime = src_app.runtime
    dest_app.memory = src_app.memory
    dest_app.env = src_app.env
    dest_app = filter(:create_app, dest_app)
    with_progress("Creating #{c(dest_app.name, :name)}") do
      dest_app.create!
    end

    # Upload source
    with_progress("Uploading source to #{c(dest_app.name, :name)}") do
      dest_app.upload(zip_path)
    end

    # Clone services
    src_app.services.each do |src_service|

      # Export service data
      export_info =
        with_progress("Exporting service #{c(src_service.name, :name)}") do
          client.export_service(src_service.name)
        end

      export_url = export_info[:uri]

      # Create new service
      cloned_service_name = generate_cloned_service_name(src_app.name, dest_app_name, src_service.name, dest_infra.name)
      dest_service = client.service_instance
      dest_service.infra_name = dest_infra.name
      dest_service.name = cloned_service_name
      dest_service.type = src_service.type
      dest_service.vendor = src_service.vendor
      dest_service.version = src_service.version.to_s
      dest_service.tier = src_service.tier
      with_progress("Creating service #{c(dest_service.name, :name)}") do
        dest_service.create!
      end

      # Bind new service to app
      with_progress("Binding service #{c(dest_service.name, :name)} to #{c(dest_app.name, :name)}") do 
        dest_app.bind(dest_service)
      end

      # Import service data
      import_info =
        with_progress("Importing data to service #{c(dest_service.name, :name)}") do
          client.import_service(dest_service.name, export_url)
        end
    end

    if !input[:nostart]
      with_progress("Starting #{c(dest_app.name, :name)}") do 
        dest_app.start!
      end
    end

  end
end