Module: Helpers
- Defined in:
- lib/vpsmatrix/helpers.rb
Instance Method Summary collapse
-
#create_app_user(api_key) ⇒ Object
will create user with app name on VPS; generate ssh key for him and possibly upload your pub key.
-
#prompt(default, *args) ⇒ Object
simple prompt with defined text return user’s input.
-
#resolve_database ⇒ Object
choose which database will be used and where (same VPS, remote VPS, remote service).
-
#resolve_domain ⇒ Object
choose domain which will be added to nginx.
-
#resolve_upload_strategy ⇒ Object
user may choose how to get files to VPS (git or directory upload).
-
#resolve_vps(api_key) ⇒ Object
user can choose which VPS to use or create new one (get one from pool of prepared VPS).
-
#send_get_request(endpoint, params = {}, api_key = nil, ssl = false) ⇒ Object
create get request, ssl and api_key optional.
-
#send_put_request(endpoint, params = {}, api_key = nil, ssl = false) ⇒ Object
create put request, ssl and api_key optional.
Instance Method Details
#create_app_user(api_key) ⇒ Object
will create user with app name on VPS; generate ssh key for him and possibly upload your pub key.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vpsmatrix/helpers.rb', line 95 def create_app_user api_key upload_ssh_key = prompt("Y", "Do you want to upload your public ssh key to app user on VPS? (Y/n)\n") # TODO consider situation when there is no pub key pub_ssh = upload_ssh_key == "Y" ? File.read("#{ENV['HOME']}/.ssh/id_rsa.pub") : "" = @app_config.content.merge({ssh_key: pub_ssh}) result = send_put_request "#{Vpsmatrix::API_TEST_SERVER}/vps/create_new_user", , api_key if result.code == "200" result.body else puts "Check your api_key in ~/.vpsx.yml; call support" && abort end end |
#prompt(default, *args) ⇒ Object
simple prompt with defined text return user’s input
4 5 6 7 8 |
# File 'lib/vpsmatrix/helpers.rb', line 4 def prompt(default, *args) print(*args) result = gets.strip.gsub("\n", '') return result.empty? ? default : result end |
#resolve_database ⇒ Object
choose which database will be used and where (same VPS, remote VPS, remote service)
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vpsmatrix/helpers.rb', line 76 def resolve_database # let user choose where database is ## mysql is installed with root user without pass # TODO add more database types as well 'postgres' database = prompt("1", "Where database should be stored? \n1: Same VPS\n Other options not available now\n") if database == "1" @app_config.write("database", "current_vps") else puts "You chose invalid option!" && abort end end |
#resolve_domain ⇒ Object
choose domain which will be added to nginx
89 90 91 92 |
# File 'lib/vpsmatrix/helpers.rb', line 89 def resolve_domain domain = prompt("example.com", "Add domain where app will run (will be used in nginx configuration)\n") @app_config.write("domain", domain) end |
#resolve_upload_strategy ⇒ Object
user may choose how to get files to VPS (git or directory upload)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vpsmatrix/helpers.rb', line 58 def resolve_upload_strategy # TODO add possibility to choose which remote to use upload_strategy = prompt("1", "How you want to upload files? \n1: Git (origin remote will be used)\n2: Copy all files in folder [NOT IMPLEMENTED]\n") if upload_strategy == "1" puts "You have no git repository." && abort unless Dir.exist?(".git") remote = `git remote get-url origin`.gsub("\n", '') @app_config.write("upload_strategy", "git") @app_config.write("git_url", remote) puts "#{remote} will be used." elsif upload_strategy == "2" @app_config.write("upload_strategy", "stream") puts "All files in this directory will be streamed to server." else puts "You chose invalid option!" && abort end end |
#resolve_vps(api_key) ⇒ Object
user can choose which VPS to use or create new one (get one from pool of prepared VPS)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vpsmatrix/helpers.rb', line 35 def resolve_vps api_key res = send_get_request "#{Vpsmatrix::API_TEST_SERVER}/vps/list_available", {}, api_key if res.code == "200" vps_list = JSON.parse res.body vps_string = vps_list.map {|vps| "#{vps["id"]}: #{vps["hostname"]} at #{vps["ip"]}"} vps_id = prompt nil, vps_string.join("\n") + "\n" chosen_vps = vps_list.select {|vps| vps["id"].to_s == vps_id}.first if chosen_vps.empty? puts "No such vps exists. Use existing id please." # TODO let's user continue somehow (run prompt again?) abort else # TODO is there more efficient way how to write to YML file? This just opens and closes file again and again. Maybe open it at beginning? @app_config.write("host", chosen_vps["ip"]) @app_config.write("host_id", chosen_vps["id"]) end #puts chosen_vps["hostname"] else puts "Check your api_key in ~/.vpsx.yml; call support" end end |
#send_get_request(endpoint, params = {}, api_key = nil, ssl = false) ⇒ Object
create get request, ssl and api_key optional
24 25 26 27 28 29 30 31 32 |
# File 'lib/vpsmatrix/helpers.rb', line 24 def send_get_request endpoint, params={}, api_key=nil, ssl=false #https://api.vpsmatrix.net/uploads/get_file_list uri = URI.parse(endpoint) uri.query = URI.encode_www_form(params) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if ssl req = Net::HTTP::Get.new(uri.path) req['authorization'] = "Token token=#{api_key}" if api_key http.request(req) end |
#send_put_request(endpoint, params = {}, api_key = nil, ssl = false) ⇒ Object
create put request, ssl and api_key optional
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/vpsmatrix/helpers.rb', line 11 def send_put_request endpoint, params={}, api_key=nil, ssl=false uri = URI.parse(endpoint) Net::HTTP.start(uri.host, uri.port) do |http| http.use_ssl = true if ssl request = Net::HTTP::Put.new(uri.request_uri) request['authorization'] = "Token token=#{api_key}" if api_key request.set_form_data(params) http.request request end end |