Class: VMC::App::Push

Inherits:
Base
  • Object
show all
Defined in:
lib/vmc/cli/app/push.rb

Instance Method Summary collapse

Methods inherited from Base

#app_status, #human_mb, #human_size, #megabytes, #memory_choices, #state_color

Methods inherited from CLI

#check_logged_in, #check_target, #client, client, client=, #client_target, #color_enabled?, #default_action, #ensure_config_dir, #err, #execute, #fail, #force?, #invalidate_client, #log_error, #name_list, #no_v2, #one_of, #precondition, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #set_target, #table, #target_file, #target_info, #targets_info, #tokens_file, #user_colors, #v2?, #verbose?

Methods included from Spacing

#indented, #justify, #line, #lines, #quiet?, #spaced, #start_line, #tabular, #text_width, #trim_escapes

Methods included from Interactive

#ask, #handler, #input_state, #list_choices, #prompt, #show_default

Instance Method Details

#create_app(name, path) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/vmc/cli/app/push.rb', line 160

def create_app(name, path)
  app = client.app
  app.name = name
  app.space = client.current_space if client.current_space
  app.total_instances = input[:instances]
  app.production = !!(input[:plan] =~ /^p/i) if v2?

  detector = VMC::Detector.new(client, path)
  all_frameworks = detector.all_frameworks
  all_runtimes = detector.all_runtimes

  if detected_framework = detector.detect_framework
    framework = input[
      :framework,
      all_frameworks,
      [detected_framework],
      detected_framework,
      :other
    ]
  else
    framework = input[:framework, all_frameworks, all_frameworks]
  end


  if framework.name == "standalone"
    detected_runtimes = detector.detect_runtimes
  else
    detected_runtimes = detector.runtimes(framework)
  end

  if detected_runtimes.size == 1
    default_runtime = detected_runtimes.first
  end

  if detected_runtimes.empty?
    runtime = input[:runtime, all_runtimes, all_runtimes]
  else
    runtime = input[
      :runtime,
      all_runtimes,
      detected_runtimes,
      default_runtime,
      :other
    ]
  end


  fail "Invalid framework '#{input[:framework]}'" unless framework
  fail "Invalid runtime '#{input[:runtime]}'" unless runtime

  app.framework = framework
  app.runtime = runtime

  app.command = input[:command] if framework.name == "standalone"

  default_memory = detector.suggested_memory(framework) || 64
  app.memory = megabytes(input[:memory, human_mb(default_memory)])

  app = filter(:create_app, app)

  with_progress("Creating #{c(app.name, :name)}") do
    app.create!
  end

  line unless quiet?

  url = input[:url, name]

  mapped_url = false
  until !url || mapped_url
    begin
      invoke :map, :app => app, :url => url
      mapped_url = true
    rescue CFoundry::RouteHostTaken, CFoundry::UriAlreadyTaken => e
      line c(e.description, :bad)
      line

      input.forget(:url)
      url = input[:url, name]

      # version bumps on v1 even though mapping fails
      app.invalidate! unless v2?
    end
  end

  bindings = []

  if input[:create_services] && !force?
    while true
      invoke :create_service, { :app => app }, :plan => :interact
      break unless ask "Create another service?", :default => false
    end
  end

  if input[:bind_services] && !force?
    instances = client.service_instances

    while true
      invoke :bind_service, :app => app

      break if (instances - app.services).empty?

      break unless ask("Bind another service?", :default => false)
    end
  end

  app = filter(:push_app, app)

  begin
    upload_app(app, path)
  rescue
    err "Upload failed. Try again with 'vmc push'."
    raise
  end

  invoke :start, :app => app if input[:start]
end

#pushObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/vmc/cli/app/push.rb', line 66

def push
  name = input[:name]
  path = File.expand_path(input[:path])

  if app = client.app_by_name(name)
    sync_app(app, path)
  else
    create_app(name, path)
  end
end

#sync_app(app, path) ⇒ Object



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/vmc/cli/app/push.rb', line 77

def sync_app(app, path)
  upload_app(app, path)

  diff = {}

  if input.given?(:memory)
    mem = megabytes(input[:memory])

    if mem != app.memory
      diff[:memory] = [app.memory, mem]
      app.memory = mem
    end
  end

  if input.given?(:instances)
    instances = input[:instances]

    if instances != app.total_instances
      diff[:instances] = [app.total_instances, instances]
      app.total_instances = instances
    end
  end

  if input.given?(:framework)
    all_frameworks = client.frameworks

    framework = input[:framework, all_frameworks, all_frameworks]

    if framework != app.framework
      diff[:framework] = [app.framework.name, framework.name]
      app.framework = framework
    end
  end

  if input.given?(:runtime)
    all_runtimes = client.runtimes

    runtime = input[:runtime, all_runtimes, all_runtimes]

    if runtime != app.runtime
      diff[:runtime] = [app.runtime.name, runtime.name]
      app.runtime = runtime
    end
  end

  if input.given?(:command) && input[:command] != app.command
    command = input[:command]

    if command != app.command
      diff[:command] = [app.command, command]
      app.command = command
    end
  end

  if input.given?(:plan) && v2?
    production = !!(input[:plan] =~ /^p/i)

    if production != app.production
      diff[:production] = [bool(app.production), bool(production)]
      app.production = production
    end
  end

  unless diff.empty?
    line "Changes:"

    indented do
      diff.each do |name, change|
        old, new = change
        line "#{c(name, :name)}: #{old} #{c("->", :dim)} #{new}"
      end
    end

    with_progress("Updating #{c(app.name, :name)}") do
      app.update!
    end
  end

  if input[:restart] && app.started?
    invoke :restart, :app => app
  end
end