app42 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +119 -0
  6. data/Rakefile +5 -0
  7. data/TODO.txt +36 -0
  8. data/app42.gemspec +41 -0
  9. data/bin/app42 +15 -0
  10. data/lib/app42.rb +23 -0
  11. data/lib/app42/base/constants.rb +33 -0
  12. data/lib/app42/base/help.rb +717 -0
  13. data/lib/app42/base/http_helper.rb +46 -0
  14. data/lib/app42/base/message.rb +35 -0
  15. data/lib/app42/base/shell.rb +39 -0
  16. data/lib/app42/base/util.rb +304 -0
  17. data/lib/app42/client/app42_rest_client.rb +451 -0
  18. data/lib/app42/client/rest_util.rb +66 -0
  19. data/lib/app42/command/app.rb +194 -0
  20. data/lib/app42/command/authorize.rb +29 -0
  21. data/lib/app42/command/base.rb +660 -0
  22. data/lib/app42/command/client.rb +232 -0
  23. data/lib/app42/command/config.rb +185 -0
  24. data/lib/app42/command/info.rb +101 -0
  25. data/lib/app42/command/service.rb +196 -0
  26. data/lib/app42/command/user.rb +140 -0
  27. data/lib/app42/command/user_key.rb +68 -0
  28. data/lib/app42/version.rb +13 -0
  29. data/spec/app42/base/constants_spec.rb +11 -0
  30. data/spec/app42/command/app_spec.rb +103 -0
  31. data/spec/app42/command/base_spec.rb +7 -0
  32. data/spec/app42/command/config_spec.rb +20 -0
  33. data/spec/app42/command/info_spec.rb +27 -0
  34. data/spec/app42/command/service_spec.rb +98 -0
  35. data/spec/app42/command/user_spec.rb +7 -0
  36. data/spec/app42/command/user_token_spec.rb +40 -0
  37. data/spec/app42/version_spec.rb +7 -0
  38. data/spec/app42_spec.rb +2 -0
  39. data/spec/data/info.yml +16 -0
  40. data/spec/data/services.yml +25 -0
  41. data/spec/data/state.yml +16 -0
  42. data/spec/data/user_services.yml +18 -0
  43. data/spec/spec_helper.rb +18 -0
  44. metadata +257 -0
@@ -0,0 +1,66 @@
1
+ require 'base64'
2
+ require 'cgi'
3
+ require 'hmac-sha1'
4
+
5
+ module App42
6
+ module Client
7
+ class RestUtil
8
+
9
+ #
10
+ # Finds the current time
11
+ #
12
+ # @return time format
13
+ #
14
+ #
15
+ def get_timestamp_utc
16
+ timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
17
+ return timestamp.freeze
18
+ end
19
+
20
+ #
21
+ # It signs the request that has to be sent in the Hmac format.
22
+ #
23
+ # @param secretKey
24
+ # @param params
25
+ #
26
+ # @rescue InvalidKeyException
27
+ #
28
+ def sign secret_key, params
29
+ sorted_params = sort_convert_table(params)
30
+ signature = compute_hmac(secret_key, sorted_params)
31
+ signature
32
+ end
33
+
34
+ #
35
+ # This method sorts all the values that are stored in the table.
36
+ #
37
+ # @param table
38
+ # @return sorted string
39
+ #
40
+ #
41
+ def sort_convert_table(table)
42
+ sorted_params = ""
43
+ table.sort {|a,b| a[0] <=> b[0]}.each{ |key, val|
44
+ sorted_params << key
45
+ sorted_params << val
46
+ }
47
+ sorted_params
48
+ end
49
+
50
+ #
51
+ # Encoding and decoding of the queries are done using the Hmac Algorithm.
52
+ #
53
+ # @param baseString
54
+ # @param key
55
+ #
56
+ # @rescue NoSuchAlgorithmException
57
+ #
58
+ def compute_hmac(secret_key, sorted_params)
59
+ signature = Base64.encode64(HMAC::SHA1::digest(secret_key, sorted_params)).strip
60
+ computed_hmac = CGI.escape(signature)
61
+ computed_hmac
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,194 @@
1
+
2
+ require 'fileutils'
3
+ require 'app42/command/base'
4
+ require 'terminal-table'
5
+
6
+ module App42
7
+ module Command
8
+ class App < Base
9
+
10
+ # will collect all required attributes for new VM spawn
11
+ def setup_infra
12
+ app_name = get_app_name_and_check_app_url_availability
13
+ vm_type = get_vm_types
14
+ iaas = get_iaas_providers
15
+ app_name, source_url = get_app_source app_name, iaas, vm_type
16
+ end
17
+
18
+ # will collect all required attributes for app deploy
19
+ def deploy
20
+ @options[:name] = get_app_name if @options[:name].nil?
21
+ response = interactive_get 'info', 'app/uploadtypes' if is_app_exist? @options[:name]
22
+ app_source_type = response['sourceTypes']
23
+
24
+ if response['sourceTypes'].count > 1
25
+ # TODO, for future release
26
+ # get_git_url
27
+ else
28
+ status = get_binary_url @options[:name]
29
+ end
30
+ exit! if status
31
+ end
32
+
33
+ # @param app_name
34
+ # @return binary
35
+ # @return deployment path
36
+ #
37
+ def get_binary_url app_name
38
+ app_source, source_url = collect_app_source app_name
39
+ status = upload_binary app_name, app_source, source_url
40
+ return status
41
+ end
42
+
43
+ #
44
+ # @param app_name
45
+ # @param iaas
46
+ # @param vm_type
47
+ # @return host name
48
+ #
49
+ def collect_vm_details app_name, iaas, vm_type
50
+ runtime = get_runtime
51
+ framework = get_framework iaas, vm_type, runtime
52
+ webserver = get_webserver iaas, vm_type, runtime, framework
53
+ os = get_os_for_app iaas, vm_type, runtime, framework, webserver
54
+ # FIXME, may be configure out later
55
+ # instance = get_instance 'new_vm'
56
+ vmconfig = get_vmconfig vm_type, iaas
57
+ setup_infra_res = App42::Command::Base.new.create_infrastructure app_name, iaas, vm_type, runtime, framework, webserver, os, vmconfig
58
+ exit! if setup_infra_res
59
+ end
60
+
61
+ #
62
+ # @param deployment path
63
+ # @param app_name
64
+ # @return true/SystemExit
65
+ #
66
+ def is_binary_exist? source_url, app_name
67
+ path = escape_path(source_url)
68
+ app_file_name = nil
69
+ @binary_retry ||= 1
70
+ no_of_file = []
71
+ Dir["#{path}/*"].collect {|f| app_file_name = f and no_of_file << f if f.include?('.zip') || f.include?('.war') || f.include?('.tar.gz') || f.include?('.gzip') }
72
+ if no_of_file.count > 1
73
+ message "#{Message::MORE_THAN_ONE_BINARY}", true, 'red'
74
+ app_file_name = input "Please Select Binary", no_of_file, true
75
+ elsif app_file_name.nil?
76
+ message "#{Message::NO_BINARY}", true, 'red'
77
+ exit! if (@binary_retry +=1 ) >= 4
78
+ get_binary_url app_name
79
+ end
80
+ return app_file_name
81
+ end
82
+
83
+ #
84
+ # ask existing app url for deploy
85
+ #
86
+ def ask_existing_app_url
87
+ input "Enter App URL", [], true
88
+ end
89
+
90
+ # @param app_source_url
91
+ def ask_app_source app_source_type
92
+ input "Choose Source Type", app_source_type, true
93
+ end
94
+
95
+ #
96
+ # @param binary/git
97
+ # @return path
98
+ #
99
+ def get_source_path app_source
100
+ path = ask(Paint["#{app_source.capitalize} Deployment Path", :cyan])
101
+ return path.strip
102
+ end
103
+
104
+ #
105
+ # collect git URL from user
106
+ #
107
+ def get_git_url(prompt = Paint["Deployment Path?", :cyan])
108
+ ask(prompt) {|q| q.each = true}
109
+ end
110
+
111
+ #
112
+ # Will scale app by no of instance
113
+ #
114
+ def scale
115
+ @options[:name] = get_app_name if @options[:name].nil?
116
+ instance = get_instance __method__ if is_app_exist? @options[:name]
117
+ scale_or_descal_res = scale_or_descale_app __method__, instance, @options[:name]
118
+ exit! if scale_or_descal_res
119
+ end
120
+
121
+ #
122
+ # Will descale app by no of instance
123
+ #
124
+ def descale
125
+ @options[:name] = get_app_name if @options[:name].nil?
126
+ instance = get_instance __method__ if is_app_exist? @options[:name]
127
+ scale_or_descal_res = scale_or_descale_app __method__, instance, @options[:name]
128
+ exit! if scale_or_descal_res
129
+ end
130
+
131
+ # app42 start
132
+ #
133
+ # will start the app, return true or error code/message
134
+ #
135
+ def start
136
+ @options[:name] = get_app_name if @options[:name].nil?
137
+ app_operation_req = app_operation __method__, @options[:name] if is_app_exist? @options[:name]
138
+ exit! if app_operation_req
139
+ end
140
+
141
+ # app42 stop
142
+ #
143
+ # will stop the app, return true or error code/message
144
+ #
145
+ def stop
146
+ @options[:name] = get_app_name if @options[:name].nil?
147
+ app_operation_req = app_operation __method__, @options[:name] if is_app_exist? @options[:name]
148
+ exit! if app_operation_req
149
+ end
150
+
151
+ # app42 start
152
+ #
153
+ # will restart the app, return true or error code/message
154
+ #
155
+ def restart
156
+ @options[:name] = get_app_name if @options[:name].nil?
157
+ app_operation_req = app_operation __method__, @options[:name] if is_app_exist? @options[:name]
158
+ exit! if app_operation_req
159
+ end
160
+
161
+ #
162
+ # will delete the app, return true or error code/message
163
+ #
164
+ def delete
165
+ @options[:name] = get_app_name if @options[:name].nil?
166
+ app_operation_req = app_operation __method__, @options[:name] if is_app_exist? @options[:name]
167
+ exit! if app_operation_req
168
+ end
169
+
170
+ #
171
+ # List deployed applications
172
+ #
173
+ def apps
174
+ response = build_get_request params, 'app', nil
175
+ rows, rows_header_final, rows_header = [], [], nil
176
+ unless response['apps'].nil?
177
+ response['apps'].each do |e|
178
+ rows_header = e.keys
179
+ rows << e.values
180
+ end
181
+ else
182
+ message "#{Message::NO_APP}", true, 'red'
183
+ exit!
184
+ end
185
+
186
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
187
+
188
+ table = Terminal::Table.new :title => Paint["=== My Apps ===", :green], :headings => rows_header_final, :rows => rows
189
+ puts table
190
+ end
191
+
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,29 @@
1
+
2
+ module App42::Command
3
+ class Auth
4
+ class << self
5
+
6
+ include App42::Command::UserToken
7
+ include App42::Base::Util
8
+
9
+ # @return [TRUE/FALSE]
10
+ def logged_in?
11
+ check_key_file?
12
+ end
13
+
14
+ def is_authorize?(api_key, secret_key)
15
+ if api_key.nil? || secret_key.nil?
16
+ message "#{Message::ADD_KEY}", true, 'red'
17
+ puts App42::Base::Help.addKeys
18
+ exit!
19
+ elsif api_key.size == 1 || secret_key.size == 1
20
+ message "#{Message::WRONG_KEY}", true, 'red'
21
+ puts App42::Base::Help.add
22
+ exit!
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,660 @@
1
+
2
+ require 'app42/command/user_key'
3
+ require "interact/progress"
4
+ require 'highline'
5
+ require "interact"
6
+
7
+ module App42
8
+ module Command
9
+ class Base
10
+
11
+ include App42::Command::UserToken
12
+ include App42::Base::Util
13
+ include App42::Base::HttpHelper
14
+ include Interactive
15
+ include Interact::Progress
16
+
17
+ # @return
18
+ # dup argument to get a non-frozen string
19
+ def initialize(options={} )
20
+ @options = options.dup
21
+ @connection = App42::Client::App42RestClient.new HOST
22
+ @api_key, @secret_key = get_keys if check_key_file?
23
+ App42::Command::Auth.is_authorize?(@api_key, @secret_key)
24
+ @host = HOST
25
+ end
26
+
27
+ # collect application name from user
28
+ # if application name will not available
29
+ # User can try 3 more times with different application name
30
+ def get_app_name_and_check_app_url_availability
31
+ @aa_retry ||= 1
32
+ exit! if (@aa_retry += 1 ) >= 5
33
+ app_name = get_app_name
34
+ available = app_url_availability app_name
35
+ return available if available
36
+ end
37
+
38
+ # get supported virtual machine type by app42paas
39
+ def get_vm_types
40
+ vm_type = App42::Command::Config.new.get_vm_type
41
+ vm_type_array = vm_type['deploymentType'].map(&:capitalize)
42
+ input "Select Virtual Machine Type", vm_type_array, true
43
+ end
44
+
45
+ # @return iaas providers
46
+ def get_iaas_providers
47
+ iaas_provider_hash = {}
48
+ iaas_providers = App42::Command::Config.new.get_iaas_provider
49
+
50
+ iaas_providers['iaas'].select {|each_iaas| iaas_provider_hash["#{each_iaas['id']}"] = each_iaas['name'] + ' ' + each_iaas['zone']}
51
+ iaas = input "Select IaaS Provider", iaas_provider_hash.values, true
52
+
53
+ iaas_provider_id = nil
54
+ iaas_provider_hash.each_pair{|ip| iaas_provider_id = ip[0] if ip[1] == iaas}
55
+ return iaas_provider_id
56
+ end
57
+
58
+ # collect remaining vm configuration details from user
59
+ def get_app_source app_name, iaas, vm_type
60
+ collect_vm_details app_name, iaas, vm_type
61
+ end
62
+
63
+ # Ask application name from user and
64
+ # will wait for user response (user will enter application name)
65
+ def get_app_name(prompt = Paint['Application Name', :cyan])
66
+ app_name = ask(prompt) {|q| q.each = true}
67
+ valid_app_name = validate_app_and_service_name "App name", app_name.strip
68
+ valid_app_name ? (return valid_app_name) : get_app_name
69
+ end
70
+
71
+ # get supported runtime by app42paas
72
+ def get_runtime
73
+ runtime_hash = {}
74
+ runtimes = App42::Command::Config.new.get_runtimes
75
+
76
+ runtimes['runtimes'].select {|each_rt| runtime_hash["#{each_rt['id']}"] = each_rt['name'] + ' ' + each_rt['version']}
77
+ rt = input "Select Runtime", runtime_hash.values, true
78
+
79
+ rt_id = nil
80
+ runtime_hash.each_pair{|r| rt_id = r[0] if r[1] == rt}
81
+ return rt_id
82
+ end
83
+
84
+ # get supported frameworks
85
+ def get_framework iaas, vm_type, rt
86
+ framework_hash = {}
87
+ frameworks = App42::Command::Config.new.get_frameworks iaas, vm_type, rt
88
+
89
+ frameworks['frameworks'].select {|each_fw| framework_hash["#{each_fw['id']}"] = each_fw['name'] + ' ' + each_fw['version']}
90
+ framework = input "Select Framework", framework_hash.values, true
91
+
92
+ framework_id = nil
93
+ framework_hash.each_pair{|f| framework_id = f[0] if f[1] == framework}
94
+ return framework_id
95
+ end
96
+
97
+ # get supported webserver
98
+ def get_webserver iaas, vm_type, rt, framework
99
+ webserver_hash = {}
100
+ webservers = App42::Command::Config.new.get_webservers( iaas, vm_type, rt, framework)
101
+
102
+ webservers['webServer'].select {|each_ws| webserver_hash["#{each_ws['id']}"] = each_ws['name'] + ' ' + each_ws['version']}
103
+ webserver = input "Select Web Server", webserver_hash.values, true
104
+
105
+ webserver_id = nil
106
+ webserver_hash.each_pair{|ws| webserver_id = ws[0] if ws[1] == webserver}
107
+ return webserver_id
108
+ end
109
+
110
+ # get supported operating system for app
111
+ def get_os_for_app iaas, vm_type, rt, framework, webserver
112
+ os_hash = {}
113
+ operating_sys = App42::Command::Config.new.get_operating_sys_for_app( iaas, vm_type, rt, framework, webserver)
114
+
115
+ if operating_sys['osDetail'].length > 1
116
+ operating_sys['osDetail'].select {|each_os| os_hash["#{each_os['id']}"] = each_os['name'] + ' ' + each_os['version']}
117
+ os = input "Select Operating System", os_hash.values, true
118
+
119
+ os_id = nil
120
+ os_hash.each_pair{|o| os_id = o[0] if o[1] == os}
121
+ return os_id
122
+ else
123
+ return operating_sys['osDetail'][0]['id']
124
+ end
125
+ end
126
+
127
+ # get supported operating system for service
128
+ def get_os_for_service iaas, vm_type, service
129
+ os_hash = {}
130
+ operating_sys = App42::Command::Config.new.get_operating_sys_for_service( iaas, vm_type, service)
131
+
132
+ if operating_sys['osDetail'].length > 1
133
+ operating_sys['osDetail'].select {|each_os| os_hash["#{each_os['id']}"] = each_os['name'] + ' ' + each_os['version']}
134
+ os = input "Select Operating System", os_hash.values, true
135
+
136
+ os_id = nil
137
+ os_hash.each_pair{|o| os_id = o[0] if o[1] == os}
138
+ return os_id
139
+ else
140
+ return operating_sys['osDetail'][0]['id']
141
+ end
142
+ end
143
+
144
+ # get supported vm configuration by app42paas
145
+ def get_vmconfig vm_type, iaas
146
+ vmconfig_hash = {}
147
+ vmconfig = App42::Command::Config.new.get_vmconfig(vm_type, iaas)
148
+
149
+ vmconfig['vmconfig'].select {|each_vmcf| vmconfig_hash["#{each_vmcf['id']}"] = each_vmcf['memory'] + ' ' + each_vmcf['memoryUnit']}
150
+ vm_cf = input "Memory Limit", vmconfig_hash.values, true
151
+
152
+ vm_cf_id = nil
153
+ vmconfig_hash.each_pair{|vmcf| vm_cf_id = vmcf[0] if vmcf[1] == vm_cf}
154
+ return vm_cf_id
155
+ end
156
+
157
+ # collect type of application source supported by app42paas
158
+ def collect_app_source app_name
159
+ app_source = 'Binary'
160
+ proceed = ask(
161
+ Paint["Would you like to deploy from the current directory?", :cyan],
162
+ :default => true
163
+ )
164
+
165
+ if proceed
166
+ source_url = Dir.getwd
167
+ app_file_name = is_binary_exist? source_url, app_name
168
+ end
169
+
170
+ unless proceed
171
+ source_url = get_source_path app_source
172
+ if source_url.include?('.zip') || source_url.include?('.war') || source_url.include?('.tar.gz') || source_url.include?('.gzip')
173
+ return app_source, source_url
174
+ end
175
+ app_file_name = is_binary_exist? source_url, app_name
176
+ end
177
+ return app_source, app_file_name
178
+ end
179
+
180
+ # private
181
+
182
+ # check app availabilities
183
+ def app_url_availability app_name
184
+ query_params = params
185
+ query_params.store('appName', app_name)
186
+
187
+ response = with_progress(Paint["\nChecking App Name Availability", :yellow]) do |s|
188
+ build_get_request query_params, 'app', 'availability'
189
+ end
190
+
191
+ if response["success"]
192
+ print_new_line
193
+ return app_name
194
+ else
195
+ message "#{response['description']}", true, 'red'
196
+ get_app_name_and_check_app_url_availability
197
+ end
198
+
199
+ end
200
+
201
+ # check service availabilities
202
+ def service_name_availability service_name
203
+ query_params = params
204
+ query_params.store('serviceName', service_name)
205
+
206
+ response = with_progress(Paint["Checking Service Name Availability", :yellow]) do |s|
207
+ build_get_request query_params, 'service', 'availability'
208
+ end
209
+
210
+ if response["success"]
211
+ print_new_line
212
+ return service_name
213
+ else
214
+ message "#{response['description']}", true, 'red'
215
+ return nil
216
+ end
217
+ end
218
+
219
+ # infrastructure setup call
220
+ def create_infrastructure app_name, iaas, vm_type, runtime, framework, webserver, os, vmconfig
221
+ begin
222
+ body = {'app42' => {"request"=> {
223
+ "appName" => app_name,
224
+ "iaas" => iaas,
225
+ "vmType" => vm_type,
226
+ "runtime" => runtime,
227
+ "framework" => framework,
228
+ "webServer" => webserver,
229
+ "os" => os,
230
+ "vmConfig" => vmconfig
231
+ }}}.to_json
232
+
233
+ query_params = params
234
+ query_params.store('body', body)
235
+
236
+ response = with_progress(Paint["Creating Infrastructure", :yellow]) do |s|
237
+ build_post_request body, query_params, 'app', nil
238
+ end
239
+
240
+ if response["success"] == true && response["transactionId"]
241
+ transaction_success = check_transaction_status response["transactionId"], previous_completed = 0, 'created'
242
+ end
243
+
244
+ if transaction_success
245
+ transaction_params = params
246
+ transaction_params.store('appName', app_name)
247
+ host_response = build_get_request transaction_params, "app", "#{app_name}"
248
+ end
249
+
250
+ if host_response['success']
251
+ puts Paint["Default Application Deployed. URL is: #{host_response['appInfo']['appUrl']}", :green]
252
+ return true
253
+ else
254
+ puts Paint["#{response['description']}", :red]
255
+ end
256
+ rescue Interrupt
257
+ puts Paint[" Command cancelled.", :red]
258
+ exit!
259
+ end
260
+ end
261
+
262
+ # Binary upload call
263
+ def upload_binary app_name, app_source, source_url
264
+ @code_flag ||= 1
265
+ begin
266
+ if source_url.include?('.war') || source_url.include?('.zip') || source_url.include?('.tar.gz') || source_url.include?('.gzip')
267
+ query_params = params
268
+ query_params.store('appName', app_name)
269
+ message "#{Message::WAIT_FOR_WHILE}", true, 'green'
270
+ response = with_progress(Paint["Deploying Application", :yellow]) do |s|
271
+ @connection.multipart(signature(query_params), resource_url("app", "binary"), query_params, query_params, source_url)
272
+ end
273
+ else
274
+ body = {'app42' => {"request"=> {
275
+ "appName" => app_name,
276
+ "sourcetype" => app_source,
277
+ "sourceUrl" => source_url,
278
+ }}}.to_json
279
+
280
+ query_params = params
281
+ query_params.store('body', body)
282
+
283
+
284
+ response = with_progress(Paint["Deploying Application", :yellow]) do |s|
285
+ build_put_request body, query_params, 'app', 'source'
286
+ end
287
+ end
288
+
289
+ if response["success"] == true && response["transactionId"]
290
+ check_transaction_status response["transactionId"], previous_completed = 0, 'Uploaded'
291
+ end
292
+
293
+ if response['success']
294
+ exit!
295
+ else
296
+ puts Paint["#{response['description']}", :red]
297
+ exit 1 if ( @code_flag += 1 ) > 3
298
+ get_binary_url app_name
299
+ end
300
+ rescue Interrupt
301
+ puts Paint[" Command cancelled.", :red]
302
+ exit!
303
+ rescue Exception => e
304
+ puts e
305
+ end
306
+ return response
307
+ end
308
+
309
+ # It's common methods of app information like app state, info etc.
310
+ # methods expect +what+ as operation and +app_name+ as Application name
311
+ def app_information what, app_name
312
+ query_params = params
313
+ query_params.store('appName', app_name)
314
+ begin
315
+ response = build_get_request query_params, "#{what}", "#{app_name}"
316
+ rescue Exception => e
317
+ puts e
318
+ exit!
319
+ end
320
+ return response
321
+ end
322
+
323
+ # Scale or descale application by no of instance,
324
+ # expect +what+ as operation and instance as no of instance
325
+ def scale_or_descale_app what, instance, app_name
326
+ begin
327
+ body = {'app42' => {"request"=> {
328
+ 'appName' => app_name,
329
+ "instanceCount" => instance
330
+ }}}.to_json
331
+
332
+ query_params = params
333
+ query_params.store('body', body)
334
+
335
+ response = with_progress( Paint[ what.to_s == 'scale' ? "Scaling Application #{app_name} by instance #{instance}" : "Descaling Application #{app_name} by instance #{instance}", :yellow]) do |s|
336
+ build_post_request body, query_params, "app", what
337
+ end
338
+
339
+ check_transaction_status response["transactionId"], previous_completed = 0, "#{what}d" if response["success"] == true && response["transactionId"]
340
+ response['success'] ? (return true) : (message "#{response['description']}", true, 'red')
341
+
342
+ rescue Interrupt
343
+ puts Paint[" Command cancelled.", :red]
344
+ exit!
345
+ rescue Exception => e
346
+ puts e
347
+ exit!
348
+ end
349
+ end
350
+
351
+ # common methods for app42paas config request like runtimes,frameworks etc
352
+ def interactive_get resource, get_obj
353
+ begin
354
+ response = build_get_request params, resource, get_obj
355
+ rescue Exception => e
356
+ puts e
357
+ end
358
+ return response
359
+ end
360
+
361
+ # All application operation will take placed like app start, stop etc.
362
+ # expect +what+ as operation and +app_name+ as application name.
363
+ def app_operation what, app_name
364
+ begin
365
+ if what.to_s == 'stop'
366
+ response = with_progress(Paint[ "Stopping Application #{app_name}", :yellow]) do |s|
367
+ body = {'app42' => {"request"=> {
368
+ "appName" => app_name
369
+ }}}.to_json
370
+
371
+ query_params = params
372
+ query_params.store('body', body)
373
+ build_put_request body, query_params, "app", "#{what}" if what.to_s == 'restart' || what.to_s == 'stop' || what.to_s == 'start'
374
+ end
375
+ else
376
+ response = with_progress(Paint[ what.to_s == 'delete' ? "Deleting Application #{app_name}" : "#{what.capitalize}ing Application #{app_name}", :yellow]) do |s|
377
+ if what.to_s == 'delete'
378
+ query_params = params
379
+ query_params.store('appName', app_name)
380
+ build_delete_request query_params, "app", "#{app_name}"
381
+ else
382
+ body = {'app42' => {"request"=> {
383
+ "appName" => app_name
384
+ }}}.to_json
385
+
386
+ query_params = params
387
+ query_params.store('body', body)
388
+ build_put_request body, query_params, "app", "#{what}" if what.to_s == 'restart' || what.to_s == 'stop' || what.to_s == 'start'
389
+ end
390
+ end
391
+ end
392
+
393
+ if response["success"] == true && response["transactionId"]
394
+ if what.to_s == 'delete'
395
+ check_transaction_status response["transactionId"], previous_completed = 0, "#{what}d"
396
+ elsif what.to_s == 'stop'
397
+ check_transaction_status response["transactionId"], previous_completed = 0, "#{what}ped"
398
+ else
399
+ check_transaction_status response["transactionId"], previous_completed = 0, "#{what}ed"
400
+ end
401
+ end
402
+
403
+ response['success'] ? (return true) : (message "#{response['description']}", true, 'red')
404
+ rescue Interrupt
405
+ puts Paint[" Command cancelled.", :red]
406
+ exit!
407
+ end
408
+ end
409
+
410
+ # create service and get all relevant service information post service creation.
411
+ #
412
+ # ==== Parameters
413
+ # service = type of service mysql,mongodb etc
414
+ # service_name = service name provided by user
415
+ # database = database name provided by user
416
+ # vm_type = shared OR dedicated
417
+ # iaas = zone where user want to create service
418
+ # vmconfig = virtual machine configuration details
419
+ # os = ubuntu OR Window
420
+ #
421
+ # ==== return
422
+ # will display service details in tabular form and
423
+ # will exist console
424
+ def create_service service, service_name ,database, vm_type, iaas, vmconfig, os
425
+ body = {'app42' => {"request"=> {
426
+ "service" => service,
427
+ "serviceName" => service_name,
428
+ "vmType" => vm_type,
429
+ "database" => database,
430
+ "iaas" => iaas,
431
+ "vmconfig" => vmconfig,
432
+ "os" => os
433
+ }}}.to_json
434
+
435
+ query_params = params
436
+ query_params.store('body', body)
437
+
438
+ begin
439
+ response = with_progress(Paint["Creating Service", :yellow]) do |s|
440
+ build_post_request body, query_params, 'service', nil
441
+ end
442
+ rescue Interrupt
443
+ puts Paint[" Command cancelled.", :red]
444
+ exit!
445
+ end
446
+
447
+
448
+ if response["success"] == true && response["transactionId"]
449
+ transaction_response = check_transaction_status response["transactionId"], previous_completed = 0, 'created'
450
+ end
451
+
452
+ if transaction_response
453
+ service_info_params = params
454
+ service_info_params.store('serviceName', service_name)
455
+ service_details = build_get_request service_info_params, "service", "#{service_name}"
456
+ end
457
+
458
+ if service_details['success']
459
+ rows, rows_header_final, rows_header = [], [], nil
460
+
461
+ rows_header = service_details['service'].keys
462
+ rows << service_details['service'].values
463
+
464
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
465
+
466
+ table = Terminal::Table.new :title => Paint["=== #{service_name} Details ===", :green], :headings => rows_header_final, :rows => rows
467
+ puts table
468
+ else
469
+ puts Paint["#{response['description']}", :red]
470
+ end
471
+ end
472
+
473
+ # delete service, as a
474
+ #
475
+ # ==== Parameters
476
+ # service_name = service name provided by user
477
+ #
478
+ # ==== return
479
+ # true:: if service deleted
480
+ # OR
481
+ # ERROR message in case failed
482
+ def delete_service service_name
483
+ begin
484
+ response = with_progress(Paint["Deleting Service", :yellow]) do |s|
485
+ query_params = params
486
+ query_params.store('serviceName', service_name)
487
+ build_delete_request query_params, "service", "#{service_name}"
488
+ end
489
+
490
+ transaction_success = check_transaction_status response["transactionId"], previous_completed = 0, 'deleted' if response["success"] == true && response["transactionId"]
491
+
492
+ response['success'] ? (return true) : (message "#{response['description']}", true, 'red')
493
+ rescue Interrupt
494
+ puts Paint[" Command cancelled.", :red]
495
+ exit!
496
+ rescue Exception => e
497
+ puts e
498
+ end
499
+ end
500
+
501
+ # reset service password and fetch service latest details
502
+ #
503
+ # ==== Parameters
504
+ # service_name = service name provided by user
505
+ # old_password = service old password
506
+ #
507
+ # ==== return
508
+ # true:: if reset password successful
509
+ # OR
510
+ # ERROR message in case failed
511
+ def reset_password service_name, old_password
512
+ begin
513
+ body = {'app42' => {"request"=> {
514
+ "serviceName" => service_name,
515
+ "password" => old_password
516
+ }}}.to_json
517
+
518
+ query_params = params
519
+ query_params.store('body', body)
520
+
521
+ response = with_progress(Paint["Resetting Password", :yellow]) do |s|
522
+ build_put_request body, query_params, 'service', "password"
523
+ end
524
+
525
+ if response["success"] == true && response["transactionId"]
526
+ transaction_response = check_transaction_status response["transactionId"], previous_completed = 0, 'created'
527
+ end
528
+
529
+ if transaction_response
530
+ service_info_params = params
531
+ service_info_params.store('serviceName', service_name)
532
+ service_details = build_get_request service_info_params, "service", "#{service_name}"
533
+ end
534
+
535
+ service_details['success'] ? (return service_details) : (message "#{response['description']}", true , 'red')
536
+
537
+ rescue Interrupt
538
+ puts Paint[" Command cancelled.", :red]
539
+ exit!
540
+ rescue Exception => e
541
+ puts e
542
+ end
543
+ end
544
+
545
+ # Bind service to requested source IP and fetch service latest details
546
+ #
547
+ # ==== Parameters
548
+ # service_name = service name provided by user
549
+ # source_ip = source IP to which service will get bind
550
+ # access_time = time duration user want to bind IP to service
551
+ #
552
+ # ==== return
553
+ # display service bind details in tabular form
554
+ # OR
555
+ # ERROR message in case failed
556
+ def create_service_tunnel service_name, source_ip
557
+ begin
558
+ body = {'app42' => {"request"=> {
559
+ "serviceName" => service_name,
560
+ "sourceIp" => source_ip
561
+ }}}.to_json
562
+
563
+ query_params = params
564
+ query_params.store('body', body)
565
+
566
+ response = with_progress(Paint["Binding IP to service", :yellow]) do |s|
567
+ build_put_request body, query_params, 'service', "bind"
568
+ end
569
+
570
+ if response["success"] == true && response["transactionId"]
571
+ transaction_response = check_transaction_status response["transactionId"], previous_completed = 0, 'created'
572
+ end
573
+
574
+ if transaction_response
575
+ service_info_params = params
576
+ service_info_params.store('serviceName', service_name)
577
+ service_tunnel_details = build_get_request service_info_params, "service", "tunnel/#{service_name}"
578
+ end
579
+
580
+ if service_tunnel_details['success']
581
+ rows, rows_header_final, rows_header = [], [], nil
582
+
583
+ rows_header = service_tunnel_details['service'].keys
584
+ rows << service_tunnel_details['service'].values
585
+
586
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
587
+
588
+ table = Terminal::Table.new :title => Paint["=== #{service_name} Details ===", :green], :headings => rows_header_final, :rows => rows
589
+ puts table
590
+
591
+ else
592
+ puts Paint["#{response['description']}", :red]
593
+ end
594
+ rescue Interrupt
595
+ puts Paint[" Command cancelled.", :red]
596
+ exit!
597
+ rescue Exception => e
598
+ puts e
599
+ end
600
+ end
601
+
602
+ # Unbind service to requested source IP
603
+ #
604
+ # ==== Parameters
605
+ # service_name = service name provided by user
606
+ # source_ip = source IP to which service will get bind
607
+ #
608
+ # ==== return
609
+ # display service bind details in tabular form
610
+ # OR
611
+ # ERROR message in case failed
612
+ def delete_service_tunnel service_name, source_ip
613
+ begin
614
+ body = {'app42' => {"request"=> {
615
+ "serviceName" => service_name,
616
+ "sourceIp" => source_ip
617
+ }}}.to_json
618
+
619
+ query_params = params
620
+ query_params.store('body', body)
621
+
622
+ response = with_progress(Paint["Unbinding IP to service", :yellow]) do |s|
623
+ build_put_request body, query_params, 'service', "unbind"
624
+ end
625
+
626
+ if response["success"] == true && response["transactionId"]
627
+ transaction_response = check_transaction_status response["transactionId"], previous_completed = 0, 'created'
628
+ return true
629
+ end
630
+ rescue Interrupt
631
+ puts Paint[" Command cancelled.", :red]
632
+ exit!
633
+ rescue Exception => e
634
+ puts e
635
+ end
636
+ end
637
+
638
+ #
639
+ # return no of instance
640
+ #
641
+ def get_instance obj
642
+ instance = @options[:instance] if @options[:instance]
643
+ unless instance
644
+ instance = nil
645
+ instance = ask Paint[ obj == 'new_vm' ? "No of instances you want?" : "#{obj.capitalize} by instance(s)", :cyan]
646
+
647
+ end
648
+
649
+ instance_count = number_valid? instance
650
+
651
+ if instance_count
652
+ return instance_count
653
+ else
654
+ message "#{Message::NOT_A_VALID_NUM}", true, 'red'
655
+ get_instance obj
656
+ end
657
+ end
658
+ end
659
+ end
660
+ end