Module: VMC::Cli::ManifestHelper

Includes:
ServicesHelper
Included in:
Command::Apps, Command::Manifest
Defined in:
lib/cli/manifest_helper.rb

Constant Summary collapse

DEFAULTS =
{
  "url" => "${name}.${target-base}",
  "mem" => "128M",
  "instances" => 1
}
MANIFEST =
"manifest.yml"
YES_SET =
Set.new(["y", "Y", "yes", "YES"])

Instance Method Summary collapse

Methods included from ServicesHelper

#bind_service_banner, #check_app_for_restart, #create_service_banner, #delete_service_banner, #display_provisioned_services, #display_provisioned_services_table, #display_system_services, #random_service_name, #unbind_service_banner

Instance Method Details

#bind_services(user_services, chosen = 0) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cli/manifest_helper.rb', line 235

def bind_services(user_services, chosen = 0)
  svcname = ask(
    "Which one?",
    :indexed => true,
    :choices => user_services.collect { |p| p[:name] })

  svc = user_services.find { |p| p[:name] == svcname }

  set svc[:vendor], "services", svcname, "type"

  if chosen + 1 < user_services.size && ask("Bind another?", :default => false)
    bind_services(user_services, chosen + 1)
  end
end

#configure_app(many = false) ⇒ Object



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
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cli/manifest_helper.rb', line 82

def configure_app(many=false)
  name = manifest("name") ||
    set(ask("Application Name", :default => manifest("name")), "name")
  if manifest "framework"
    framework = VMC::Cli::Framework.lookup_by_framework manifest("framework","name")
  else
    framework = detect_framework
    set framework.name, "framework", "name"
    set(
      { "mem" => framework.mem,
        "description" => framework.description,
        "exec" => framework.exec
      },
      "framework",
      "info"
    )
  end

  default_runtime = manifest "runtime"
  if not default_runtime
    default_runtime = framework.default_runtime(@application)
    set(detect_runtime(default_runtime), "runtime") if framework.prompt_for_runtime?
  end
  default_command = manifest "command"
  set ask("Start Command", :default => default_command), "command" if framework.require_start_command?

  url_template = manifest("url") || DEFAULTS["url"]
  url_resolved = url_template.dup
  resolve_lexically(url_resolved)

  if !framework.require_url?
    url_resolved = "None"
  end
  url = ask("Application Deployed URL", :default => url_resolved)

  if url == url_resolved && url != "None"
    url = url_template
  end

  # common error case is for prompted users to answer y or Y or yes or
  # YES to this ask() resulting in an unintended URL of y. Special
  # case this common error
  url = url_resolved if YES_SET.member? url

  if(url == "None")
    url = nil
  end

  set url, "url"

  default_mem = manifest("mem")
  default_mem = framework.memory(manifest("runtime")) if not default_mem
  set ask(
    "Memory reservation",
    :default =>
        default_mem ||
        DEFAULTS["mem"],
    :choices => ["128M", "256M", "512M", "1G", "2G"]
  ), "mem"

  set ask(
    "How many instances?",
    :default => manifest("instances") || DEFAULTS["instances"]
  ), "instances"

  unless manifest "services"
    user_services = client.services
    user_services.sort! {|a, b| a[:name] <=> b[:name] }

    unless user_services.empty?
      if ask "Bind existing services to '#{name}'?", :default => false
        bind_services(user_services)
      end
    end

    services = client.services_info
    unless services.empty?
      if ask "Create services to bind to '#{name}'?", :default => false
        create_services(services.values.collect(&:keys).flatten)
      end
    end
  end

  if many and ask("Configure for another application?", :default => false)
    @application = ask "Application path?"
    configure_app
  end
end

#configure_service(vendor) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/cli/manifest_helper.rb', line 266

def configure_service(vendor)
     if "mysql" == vendor 
        databaseChoice = %w[internal_database external_database]
        dbflag =  ask("which type would you like to choice?", {
        :indexed => true,
        :choices => databaseChoice
        }
    ) 
     dbsize =  ask "max_db_size"
     regix = /^[1-9]\d*$/
     err "max_db_size required" unless dbsize
     err "max_db_size must be int and max_db_size more than 0" unless regix.match(dbsize)
    end
    if "external_database" == dbflag
     dbjndi = ask "jndi name"
     dburl = ask "dburl"
     dbuname = ask "dbusername "
     dbpasswd = ask("dbpasswd", :echo => "*")
     err "jndi required" unless dbjndi
     err "dburl required" unless dburl
     err "dbuname required" unless dbuname
     err "dbpasswd required" unless dbpasswd
    end
   
  default_name = random_service_name(vendor)
  name = ask "Specify the name of the service", :default => default_name
   db_config =  {
     "dbflag" => dbflag,
     "dbjndi" => dbjndi,
     "dbuname" => dbuname,
     "dbpasswd" => dbpasswd,
     "dburl" => dburl,
     "dbsize" => Integer(dbsize),
   }
  set vendor,  "services", name,  "type"
  set db_config,  "services", name,  "db_config"
end

#create_services(services) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cli/manifest_helper.rb', line 250

def create_services(services)
  svcs = services.collect(&:to_s).sort!

  configure_service(
    ask(
      "What kind of service?",
      :indexed => true,
      :choices => svcs
    )
  )

  if ask "Create another?", :default => false
    create_services(services)
  end
end

#detect_framework(prompt_ok = true) ⇒ Object

Detect the appropriate framework.



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
# File 'lib/cli/manifest_helper.rb', line 187

def detect_framework(prompt_ok = true)
  framework = VMC::Cli::Framework.detect(@application, frameworks_info)
  framework_correct = ask("Detected a #{framework}, is this correct?", :default => true) if prompt_ok && framework
  if prompt_ok && (framework.nil? || !framework_correct)
    display "#{"[WARNING]".yellow} Can't determine the Application Type." unless framework
    framework = nil if !framework_correct
    framework = VMC::Cli::Framework.lookup(
      ask(
        "Select Application Type",
        :indexed => true,
        :default => framework,
        :choices => VMC::Cli::Framework.known_frameworks(frameworks_info)
      )
    )
    display "Selected #{framework}"
  end
	if framework_correct && framework.to_s == "Java Web Application"
    framework = VMC::Cli::Framework.lookup(
      ask(
        "Select Java Web Application Server",
        :indexed => true,
        :default => "JavaWeb_tongweb",
        :choices => VMC::Cli::Framework.javaweb_appservers
      )
    )
    display "Selected #{framework}"
  end
  framework
end

#detect_runtime(default, prompt_ok = true) ⇒ Object

Detect the appropriate runtime.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cli/manifest_helper.rb', line 218

def detect_runtime(default, prompt_ok=true)
  runtime = nil
  runtime_keys=[]
  runtimes_info.keys.each {|runtime_key| runtime_keys << runtime_key.dup }
  runtime_keys.sort!
  if prompt_ok
    runtime =  ask(
        "Select Runtime",
        :indexed => true,
        :default => default,
        :choices => runtime_keys
    )
    display "Selected #{runtime}"
  end
  runtime
end

#each_app(panic = true) ⇒ Object

take a block and call it once for each app to push/update. with @application and @app_info set appropriately



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
# File 'lib/cli/manifest_helper.rb', line 19

def each_app(panic=true)
  if @manifest and all_apps = @manifest["applications"]
    where = File.expand_path(@path)
    single = false

    all_apps.each do |path, info|
      app = File.expand_path("../" + path, manifest_file)
      if where.start_with?(app)
        @application = app
        @app_info = info
        yield info["name"]
        single = true
        break
      end
    end

    unless single
      if where == File.expand_path("../", manifest_file)
        ordered_by_deps(all_apps).each do |path, info|
          app = File.expand_path("../" + path, manifest_file)
          @application = app
          @app_info = info
          yield info["name"]
        end
      else
        err "Path '#{@path}' is not known to manifest '#{manifest_file}'."
      end
    end
  else
    @application = @path
    @app_info = @manifest
    if @app_info
      yield @app_info["name"]
    elsif panic
      err "No applications."
    end
  end

  nil
ensure
  @application = nil
  @app_info = nil
end

#interact(many = false) ⇒ Object



63
64
65
66
# File 'lib/cli/manifest_helper.rb', line 63

def interact(many=false)
  @manifest ||= {}
  configure_app(many)
end

#save_manifest(save_to = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/cli/manifest_helper.rb', line 72

def save_manifest(save_to = nil)
  save_to ||= target_manifest

  File.open(save_to, "w") do |f|
    f.write @manifest.to_yaml
  end

  say "Manifest written to #{save_to}."
end

#set(what, *where) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cli/manifest_helper.rb', line 171

def set(what, *where)
  where.unshift "applications", @application

  which = @manifest
  where.each_with_index do |k, i|
    if i + 1 == where.size
      which[k] = what
    else
      which = (which[k] ||= {})
    end
  end

  what
end

#target_manifestObject



68
69
70
# File 'lib/cli/manifest_helper.rb', line 68

def target_manifest
  @options[:manifest] || MANIFEST
end