lib/app42/command/base.rb in app42-0.5.10 vs lib/app42/command/base.rb in app42-0.6.0

- old
+ new

@@ -42,10 +42,21 @@ @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 + + # collect wordpress name from user + # if wordpress name will not available + # User can try 3 more times with different wordpress name + def get_wordpress_name_and_check_wordpress_url_availability + @aa_retry ||= 1 + exit! if (@aa_retry += 1 ) >= 5 + wordpress_name = get_wordpress_name + available = wordpress_url_availability wordpress_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 @@ -77,10 +88,18 @@ app_name = input "Enter App Name", [], 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 wordpress name from user and + # will wait for user response (user will enter wordpress name) + def get_wordpress_name + wordpress_name = input "Enter Wordpress Name", [], true + valid_wordpress_name = validate_app_and_service_name "Wordpress name", wordpress_name.strip + valid_wordpress_name ? (return valid_wordpress_name) : get_wordpress_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 @@ -228,10 +247,29 @@ get_setup_name_and_check_setup_url_availability end end + # check wordpress name availabilities + def wordpress_url_availability wordpress_name + query_params = params + query_params.store('setupName', wordpress_name) + + response = with_progress(Paint["Checking WordPress Name Availability", :yellow]) do |s| + build_get_request query_params, 'wordpress', 'availability' + end + + if response["success"] + print_new_line + return wordpress_name + else + message "#{response['description']}", true, 'red' + get_wordpress_name_and_check_wordpress_url_availability + end + + end + # check service availabilities def service_name_availability service_name query_params = params query_params.store('serviceName', service_name) @@ -303,11 +341,11 @@ }}}.to_json query_params = params query_params.store('body', body) - response = with_progress(Paint["Setting up the infrastructure", :yellow]) do |s| + response = with_progress(Paint["Setting up the BPaaS 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' @@ -329,23 +367,63 @@ puts Paint[" Command cancelled.", :red] exit! end end + # setup wordpress + def create_wordpress_setup wordpress_name, iaas, vm_type, flavour + begin + body = {'app42' => {"request"=> { + "setupName" => wordpress_name, + "iaas" => iaas, + "vmType" => vm_type, + "setupType" => "setupWordPress", + "flavour" => flavour + }}}.to_json + + query_params = params + query_params.store('body', body) + + response = with_progress(Paint["Setting up the wordpress infrastructure", :yellow]) do |s| + build_post_request body, query_params, 'wordpress', 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', wordpress_name) + host_response = build_get_request transaction_params, "setup", "#{wordpress_name}" + end + + if host_response['success'] + puts Paint["Your wordpress setup has been completed successfully, For details, Login to AppHQ console.", :green] + return true + else + puts Paint["#{response['description']}", :red] + end + rescue Interrupt + puts Paint[" Command cancelled.", :red] + exit! + end + end + # upgrade setup cloud API, as a # # ==== Parameters # setup_name = setup_name provided by user # flavour = flavour provided by user # # ==== return # true:: if cloud setup upgraded # OR # ERROR message in case failed - def upgrade_clouldapi setup_name, flavour + def upgrade_or_downgrade_clouldapi action, setup_name, flavour begin - response = with_progress(Paint["Upgrading Setup", :yellow]) do |s| + response = with_progress(Paint[ action.to_s == "upgrade" ? "Upgrading Setup" : "Downgrading Setup", :yellow]) do |s| query_params = params body = {'app42' => {"request"=> { "setupName" => setup_name, "flavour" => flavour }}}.to_json @@ -359,10 +437,42 @@ puts Paint[" Command cancelled.", :red] exit! rescue Exception => e puts e end + end + + # upgrade Wordpress, as a + # + # ==== Parameters + # wordpress_name = wordpress_name provided by user + # flavour = flavour provided by user + # + # ==== return + # true:: if cloud setup upgraded + # OR + # ERROR message in case failed + def upgrade_or_downgrade_wordpress action, wordpress_name, flavour + begin + response = with_progress(Paint[ action.to_s == "upgrade" ? "Upgrading Wordpress" : "Downgrading Wordpress", :yellow]) do |s| + query_params = params + body = {'app42' => {"request"=> { + "setupName" => wordpress_name, + "flavour" => flavour + }}}.to_json + + query_params.store('body', body) + build_put_request body, query_params, 'wordpress/upgrade', nil + 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 # Binary upload call def upload_binary app_name, app_source, source_url @@ -750,10 +860,38 @@ rescue Exception => e puts e end end + # delete wordpress setup, as a + # + # ==== Parameters + # wordpress_name = wordpress_name provided by user + # + # ==== return + # true:: if wordpress setup deleted + # OR + # ERROR message in case failed + def delete_wordpress wordpress_name + begin + response = with_progress(Paint["Deleting WordPress Setup", :yellow]) do |s| + query_params = params + query_params.store('setupName', wordpress_name) + build_delete_request query_params, "setup", "#{wordpress_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 + # All application operation will take placed like app start, stop etc. # expect +what+ as operation and +setup_name+ as BPaaS name. def clouldapi_operation what, setup_name begin if what.to_s == 'stop' @@ -766,9 +904,50 @@ query_params.store('body', body) build_put_request body, query_params, "setup", "#{what}" if what.to_s == 'stop' end else response = with_progress(Paint["#{what.capitalize}ing BPaaS Setup #{setup_name}", :yellow]) do |s| + body = {'app42' => {"request"=> { + "setupName" => setup_name + }}}.to_json + + query_params = params + query_params.store('body', body) + build_put_request body, query_params, "setup", "#{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 + + # All wordpress operation will take placed like app start, stop etc. + # expect +what+ as operation and +wordpress_name+ as wordpress name. + def wordpress_operation what, setup_name + begin + if what.to_s == 'stop' + response = with_progress(Paint[ "Stopping Wordpress Setup #{setup_name}", :yellow]) do |s| + body = {'app42' => {"request"=> { + "setupName" => setup_name + }}}.to_json + + query_params = params + query_params.store('body', body) + build_put_request body, query_params, "setup", "#{what}" if what.to_s == 'stop' + end + else + response = with_progress(Paint["#{what.capitalize}ing Wordpress Setup #{setup_name}", :yellow]) do |s| body = {'app42' => {"request"=> { "setupName" => setup_name }}}.to_json query_params = params \ No newline at end of file