Class: VMC::Cli::Command::Services

Inherits:
Base show all
Includes:
ServicesHelper, TunnelHelper
Defined in:
lib/cli/commands/services.rb

Constant Summary

Constants included from TunnelHelper

TunnelHelper::HELPER_APP, TunnelHelper::HELPER_VERSION, TunnelHelper::PORT_RANGE

Constants inherited from Base

Base::MANIFEST

Instance Attribute Summary

Attributes inherited from Base

#no_prompt, #prompt_ok

Instance Method Summary collapse

Methods included from TunnelHelper

#display_tunnel_connection_info, #grab_ephemeral_port, #invalidate_tunnel_app_info, #pick_tunnel_port, #push_caldecott, #resolve_symbols, #start_caldecott, #start_local_prog, #start_tunnel, #stop_caldecott, #tunnel_app_info, #tunnel_appname, #tunnel_auth, #tunnel_bound?, #tunnel_connection_info, #tunnel_healthy?, #tunnel_pushed?, #tunnel_uniquename, #tunnel_url, #wait_for_tunnel_end, #wait_for_tunnel_start

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

Methods inherited from Base

#auth_token, #client, #client_info, #find_in_hash, #find_symbol, #frameworks_info, #initialize, #load_manifest, #load_manifest_structure, #manifest, #manifest_file, #merge_manifest, #merge_parent, #quota_client_info, #resolve_in, #resolve_lexically, #resolve_manifest, #resolve_symbol, #runtimes_info, #target_base, #target_url

Constructor Details

This class inherits a constructor from VMC::Cli::Command::Base

Instance Method Details

#bind_service(service, appname) ⇒ Object



125
126
127
# File 'lib/cli/commands/services.rb', line 125

def bind_service(service, appname)
  bind_service_banner(service, appname)
end

#clone_services(src_app, dest_app) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cli/commands/services.rb', line 133

def clone_services(src_app, dest_app)
  begin
    src  = client.app_info(src_app)
    dest = client.app_info(dest_app)
  rescue
  end

  err "Application '#{src_app}' does not exist" unless src
  err "Application '#{dest_app}' does not exist" unless dest

  services = src[:services]
  err 'No services to clone' unless services && !services.empty?
  services.each { |service| bind_service_banner(service, dest_app, false) }
  check_app_for_restart(dest_app)
end

#create_service(service = nil, name = nil, appname = nil) ⇒ Object



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
# File 'lib/cli/commands/services.rb', line 22

def create_service(service=nil, name=nil, appname=nil)
  unless no_prompt || service
    services = client.services_info
    err 'No services available to provision' if services.empty?
    service = ask(
      "Which service would you like to provision?",
      { :indexed => true,
        :choices =>
          services.values.collect { |type|
            type.keys.collect(&:to_s)
          }.flatten
      }
    )
  end
  name = @options[:name] unless name

  unless name
    name = random_service_name(service)
    picked_name = true
  end
  
  if "mysql" == service 
      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
 
 # if "mysql" == service
 #    create_service_banner(service, name, picked_name)
 # elsif 
   #  create_service_banner(service, name, picked_name)
 # end 
  
  create_service_banner(service, name,  dbflag, dbjndi, dbuname, dbpasswd, dburl, dbsize, picked_name)
  
  appname = @options[:bind] unless appname
  bind_service_banner(name, appname) if appname
end

#delete_service(service = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cli/commands/services.rb', line 79

def delete_service(service=nil)
  unless no_prompt || service
    user_services = client.services
    err 'No services available to delete' if user_services.empty?
    service = ask(
      "Which service would you like to delete?",
      { :indexed => true,
        :choices => user_services.collect { |s| s[:name] }
      }
    )
  end
  err "Service name required." unless service
  display "Deleting service [#{service}]: ", false
  client.delete_service(service)
  display 'OK'.green
end

#get_clients_for(type) ⇒ Object



237
238
239
240
# File 'lib/cli/commands/services.rb', line 237

def get_clients_for(type)
  conf = VMC::Cli::Config.clients
  conf[type] || {}
end

#quota_service(service = nil) ⇒ Object



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
# File 'lib/cli/commands/services.rb', line 96

def quota_service(service=nil)
  unless no_prompt || service
    user_services = client.services
    err 'No services available to delete' if user_services.empty?
    service = ask(
      "Which service  would you like to update?",
      { :indexed => true,
        :choices => user_services.collect { |s| s[:name] }
      }
    )
  end
  err "Service name required." unless service
  display "Update service's quota [#{service}]: ", false
  # flag = service.index("mysql-")
   #if flag
   max_db_size = client.check_quota(service)
   display "\ncurrent max_db_size is [#{max_db_size} MB], Please enter the number of more than #{max_db_size} : " 
   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)
   err "Please enter the number of more than #{max_db_size}" unless Integer(dbsize) > Integer(max_db_size) 
  #  else 
  #     err "Temporary does not support" 
  #    end
  client.quota_service(service, dbsize, "update")
  display 'OK'.green
end

#servicesObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cli/commands/services.rb', line 9

def services
  ss = client.services_info
  ps = client.services
  ps.sort! {|a, b| a[:name] <=> b[:name] }

  if @options[:json]
    services = { :system => ss, :provisioned => ps }
    return display JSON.pretty_generate(services)
  end
  display_system_services(ss)
  display_provisioned_services(ps)
end

#tunnel(service = nil, client_name = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
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
# File 'lib/cli/commands/services.rb', line 149

def tunnel(service=nil, client_name=nil)
  unless defined? Caldecott
    display "To use `vmc tunnel', you must first install Caldecott:"
    display ""
    display "\tgem install caldecott"
    display ""
    display "Note that you'll need a C compiler. If you're on OS X, Xcode"
    display "will provide one. If you're on Windows, try DevKit."
    display ""
    display "This manual step will be removed in the future."
    display ""
    err "Caldecott is not installed."
  end

  ps = client.services
  err "No services available to tunnel to" if ps.empty?

  unless service
    choices = ps.collect { |s| s[:name] }.sort
    service = ask(
      "Which service to tunnel to?",
      :choices => choices,
      :indexed => true
    )
  end

  info = ps.select { |s| s[:name] == service }.first

  err "Unknown service '#{service}'" unless info

  port = pick_tunnel_port(@options[:port] || 10000)

  raise VMC::Client::AuthError unless client.logged_in?

  if not tunnel_pushed?
    display "Deploying tunnel application '#{tunnel_appname}'."
    auth = UUIDTools::UUID.random_create.to_s
    push_caldecott(auth)
    bind_service_banner(service, tunnel_appname, false)
    start_caldecott
  else
    auth = tunnel_auth
  end

  if not tunnel_healthy?(auth)
    display "Redeploying tunnel application '#{tunnel_appname}'."

    # We don't expect caldecott not to be running, so take the
    # most aggressive restart method.. delete/re-push
    client.delete_app(tunnel_appname)
    invalidate_tunnel_app_info

    push_caldecott(auth)
    bind_service_banner(service, tunnel_appname, false)
    start_caldecott
  end

  if not tunnel_bound?(service)
    bind_service_banner(service, tunnel_appname)
  end

  conn_info = tunnel_connection_info info[:vendor], service, auth
  display_tunnel_connection_info(conn_info)
  display "Starting tunnel to #{service.bold} on port #{port.to_s.bold}."
  start_tunnel(port, conn_info, auth)

  clients = get_clients_for(info[:vendor])

  if clients.empty?
    client_name ||= "none"
  else
    client_name ||= ask(
      "Which client would you like to start?",
      :choices => ["none"] + clients.keys,
      :indexed => true
    )
  end

  if client_name == "none"
    wait_for_tunnel_end
  else
    wait_for_tunnel_start(port)
    unless start_local_prog(clients, client_name, conn_info, port)
      err "'#{client_name}' execution failed; is it in your $PATH?"
    end
  end
end

#unbind_service(service, appname) ⇒ Object



129
130
131
# File 'lib/cli/commands/services.rb', line 129

def unbind_service(service, appname)
  unbind_service_banner(service, appname)
end