lib/app42/command/base.rb in app42-0.5.5 vs lib/app42/command/base.rb in app42-0.5.6

- old
+ new

@@ -31,10 +31,21 @@ @aa_retry ||= 1 exit! if (@aa_retry += 1 ) >= 5 app_name = get_app_name available = app_url_availability app_name return available if available + end + + # collect setup name from user + # if setup name will not available + # User can try 3 more times with different setup name + def get_setup_name_and_check_setup_url_availability + @aa_retry ||= 1 + exit! if (@aa_retry += 1 ) >= 5 + setup_name = get_setup_name + available = setup_url_availability setup_name + return available if available end # get supported virtual machine type by app42paas def get_vm_types vm_type = App42::Command::Config.new.get_vm_type @@ -66,10 +77,18 @@ app_name = ask(prompt) {|q| q.each = true} valid_app_name = validate_app_and_service_name "App name", app_name.strip valid_app_name ? (return valid_app_name) : get_app_name end + # Ask setup name from user and + # will wait for user response (user will enter setup name) + def get_setup_name(prompt = Paint['Enter Setup Name', :cyan]) + setup_name = ask(prompt) {|q| q.each = true} + valid_setup_name = validate_setup_name "Setup name", setup_name.strip + valid_setup_name ? (return valid_setup_name) : get_setup_name + end + # get supported runtime by app42paas def get_runtime runtime_hash = {} runtimes = App42::Command::Config.new.get_runtimes @@ -140,20 +159,14 @@ return operating_sys['osDetail'][0]['id'] end end # get supported vm configuration by app42paas - def get_vmconfig resource, vm_type, iaas - vmconfig_hash = {} - vmconfig = App42::Command::Config.new.get_vmconfig(resource, vm_type, iaas) - - vmconfig['vmconfig'].select {|each_vmcf| vmconfig_hash["#{each_vmcf['id']}"] = each_vmcf['memory'] + ' ' + each_vmcf['memoryUnit']} - vm_cf = input "Memory Limit", vmconfig_hash.values, true - - vm_cf_id = nil - vmconfig_hash.each_pair{|vmcf| vm_cf_id = vmcf[0] if vmcf[1] == vm_cf} - return vm_cf_id + def get_vmconfig + kontena = input "Specify Kontena Power", [], true + valid = validate_vm_config kontena + valid ? (return valid) : get_vmconfig end # collect type of application source supported by app42paas def collect_app_source app_name app_source = 'Binary' @@ -196,10 +209,29 @@ get_app_name_and_check_app_url_availability end end + # check setup name availabilities + def setup_url_availability setup_name + query_params = params + query_params.store('setupName', setup_name) + + response = with_progress(Paint["Checking Setup Name Availability", :yellow]) do |s| + build_get_request query_params, 'setup', 'availability' + end + + if response["success"] + print_new_line + return setup_name + else + message "#{response['description']}", true, 'red' + get_setup_name_and_check_setup_url_availability + end + + end + # check service availabilities def service_name_availability service_name query_params = params query_params.store('serviceName', service_name) @@ -215,21 +247,21 @@ return nil end end # infrastructure setup call - def create_infrastructure app_name, iaas, vm_type, runtime, framework, webserver, os, vmconfig + def create_infrastructure app_name, iaas, vm_type, runtime, framework, webserver, os, kontena begin body = {'app42' => {"request"=> { - "appName" => app_name, - "iaas" => iaas, - "vmType" => vm_type, - "runtime" => runtime, - "framework" => framework, - "webServer" => webserver, - "os" => os, - "vmConfig" => vmconfig + "appName" => app_name, + "iaas" => iaas, + "vmType" => vm_type, + "runtime" => runtime, + "framework" => framework, + "webServer" => webserver, + "os" => os, + "kontenaPower" => kontena }}}.to_json query_params = params query_params.store('body', body) @@ -257,34 +289,75 @@ puts Paint[" Command cancelled.", :red] exit! end end + # setup cloud API + def create_cloud_setup setup_name, iaas, vm_type, setup_type, flavour + begin + body = {'app42' => {"request"=> { + "setupName" => setup_name, + "iaas" => iaas, + "vmType" => vm_type, + "setupType" => setup_type, + "flavour" => flavour + }}}.to_json + + query_params = params + query_params.store('body', body) + + response = with_progress(Paint["Setting up the infrastructure", :yellow]) do |s| + build_post_request body, query_params, 'setup', nil + end + + if response["success"] == true && response["transactionId"] + transaction_success = check_transaction_status_of_setup response["transactionId"], previous_completed = 0, 'created' + end + + if transaction_success + transaction_params = params + transaction_params.store('setupName', setup_name) + host_response = build_get_request transaction_params, "setup", "#{setup_name}" + end + + if host_response['success'] + puts Paint["Your setup has been completed successfully, For details, Login you your AppHQ console.", :green] + return true + else + puts Paint["#{response['description']}", :red] + end + rescue Interrupt + puts Paint[" Command cancelled.", :red] + exit! + end + end + + # Binary upload call def upload_binary app_name, app_source, source_url @code_flag ||= 1 begin if source_url.include?('.war') || source_url.include?('.zip') || source_url.include?('.tar.gz') || source_url.include?('.gzip') query_params = params query_params.store('appName', app_name) message "#{Message::WAIT_FOR_WHILE}", true, 'green' response = with_progress(Paint["Deploying Application", :yellow]) do |s| - @connection.multipart(signature(query_params), resource_url("app", "binary"), query_params, query_params, source_url) + @connection.multipart(signature(query_params), resource_url("app/deploy", "binary"), query_params, query_params, source_url) end else body = {'app42' => {"request"=> { "appName" => app_name, - "sourcetype" => app_source, + "sourceType" => app_source, "sourceUrl" => source_url, }}}.to_json - + query_params = params query_params.store('body', body) response = with_progress(Paint["Deploying Application", :yellow]) do |s| - build_put_request body, query_params, 'app', 'source' + build_put_request body, query_params, 'app/deploy', 'source' end end if response["success"] == true && response["transactionId"] check_transaction_status response["transactionId"], previous_completed = 0, 'Uploaded' @@ -304,10 +377,45 @@ puts e end return response end + # Binary update call + def update_binary app_name + @code_flag ||= 1 + begin + body = {'app42' => {"request"=> { + "appName" => app_name + }}}.to_json + + query_params = params + query_params.store('body', body) + + + response = with_progress(Paint["Updating Application", :yellow]) do |s| + build_put_request body, query_params, 'app/update', 'source' + end + if response["success"] == true && response["transactionId"] + check_transaction_status response["transactionId"], previous_completed = 0, 'Uploaded' + end + + if response['success'] + exit! + else + puts Paint["#{response['description']}", :red] + exit 1 if ( @code_flag += 1 ) > 3 + get_binary_url app_name + end + rescue Interrupt + puts Paint[" Command cancelled.", :red] + exit! + rescue Exception => e + puts e + end + return response + end + # It's common methods of app information like app state, info etc. # methods expect +what+ as operation and +app_name+ as Application name def app_information what, app_name query_params = params query_params.store('appName', app_name) @@ -419,18 +527,18 @@ # os = ubuntu OR Window # # ==== return # will display service details in tabular form and # will exist console - def create_service service, service_name ,database, vm_type, iaas, vmconfig, os + def create_service service, service_name ,database, vm_type, iaas, kontena, os body = {'app42' => {"request"=> { - "service" => service, - "serviceName" => service_name, - "vmType" => vm_type, - "database" => database, - "iaas" => iaas, - "vmconfig" => vmconfig, + "service" => service, + "serviceName" => service_name, + "vmType" => vm_type, + "database" => database, + "iaas" => iaas, + "kontenaPower" => kontena, "os" => os }}}.to_json query_params = params query_params.store('body', body) @@ -496,10 +604,38 @@ rescue Exception => e puts e end end + # delete setup cloud API, as a + # + # ==== Parameters + # setup_name = setup_name provided by user + # + # ==== return + # true:: if cloud setup deleted + # OR + # ERROR message in case failed + def delete_clouldapi setup_name + begin + response = with_progress(Paint["Deleting Clould API", :yellow]) do |s| + query_params = params + query_params.store('setupName', setup_name) + build_delete_request query_params, "setup", "#{setup_name}" + end + + transaction_success = check_transaction_status response["transactionId"], previous_completed = 0, 'deleted' if response["success"] == true && response["transactionId"] + + response['success'] ? (return true) : (message "#{response['description']}", true, 'red') + rescue Interrupt + puts Paint[" Command cancelled.", :red] + exit! + rescue Exception => e + puts e + end + end + # reset service password and fetch service latest details # # ==== Parameters # service_name = service name provided by user # service_token = service token @@ -538,9 +674,51 @@ puts Paint[" Command cancelled.", :red] exit! rescue Exception => e puts e end + end + + + # All application operation will take placed like app start, stop etc. + # expect +what+ as operation and +app_name+ as application name. + def service_operation what, service_name + begin + if what.to_s == 'stop' + response = with_progress(Paint[ "Stopping Service #{service_name}", :yellow]) do |s| + body = {'app42' => {"request"=> { + "serviceName" => service_name + }}}.to_json + + query_params = params + query_params.store('body', body) + build_put_request body, query_params, "service", "#{what}" if what.to_s == 'stop' + end + else + response = with_progress(Paint["#{what.capitalize}ing Service #{service_name}", :yellow]) do |s| + body = {'app42' => {"request"=> { + "serviceName" => service_name + }}}.to_json + + query_params = params + query_params.store('body', body) + build_put_request body, query_params, "service", "#{what}" if what.to_s == 'restart' || what.to_s == 'start' + end + end + + if response["success"] == true && response["transactionId"] + if what.to_s == 'stop' + check_transaction_status response["transactionId"], previous_completed = 0, "#{what}ped" + else + check_transaction_status response["transactionId"], previous_completed = 0, "#{what}ed" + end + end + + response['success'] ? (return true) : (message "#{response['description']}", true, 'red') + rescue Interrupt + puts Paint[" Command cancelled.", :red] + exit! + end end # Bind service to requested source IP and fetch service latest details # # ==== Parameters \ No newline at end of file