Module: CloudstackCli::Helper
- Included in:
- Base
- Defined in:
- lib/cloudstack-cli/helper.rb
Instance Method Summary collapse
- #ask_number(question) ⇒ Object
- #bootstrap_server(args = {}) ⇒ Object
- #bootstrap_server_interactive ⇒ Object
- #get_async_job_status(ids) ⇒ Object
- #print_options(options, attr = 'name') ⇒ Object
- #watch_jobs(jobs) ⇒ Object
Instance Method Details
#ask_number(question) ⇒ Object
9 10 11 12 |
# File 'lib/cloudstack-cli/helper.rb', line 9 def ask_number(question) number = ask(question).to_i - 1 number < 0 ? 0 : number end |
#bootstrap_server(args = {}) ⇒ Object
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 |
# File 'lib/cloudstack-cli/helper.rb', line 44 def bootstrap_server(args = {}) if args[:project] && project = client(quiet: true).get_project(args[:project]) project_id = project["id"] project_name = project['name'] end server = client(quiet: true).get_server(args[:name], project_id) unless server say "Create server #{args[:name]}...", :yellow server = client.create_server(args) say "Server #{server["name"]} has been created.", :green client.wait_for_server_state(server["id"], "Running") say "Server #{server["name"]} is running.", :green else say "Server #{args[:name]} already exists (#{server['state']}).", :yellow end if args[:port_rules] && args[:port_rules].size > 0 frontendip = nil args[:port_rules].each do |pf_rule| ip = pf_rule.split(":")[0] if ip != '' ip_addr = client.get_public_ip_address(ip) else ip_addr = frontendip ||= client.associate_ip_address( server["nic"].first["networkid"] ) end port = pf_rule.split(":")[1] say "Create port forwarding rule #{ip_addr['ipaddress']}:#{port} for server #{args[:name]}.", :yellow client.create_port_forwarding_rule(ip_addr["id"], port, 'TCP', port, server["id"]) end end end |
#bootstrap_server_interactive ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cloudstack-cli/helper.rb', line 78 def bootstrap_server_interactive zones = client.list_zones if zones.size > 1 say "Select a availability zone:", :yellow (zones) zone = ask_number("Zone Nr.: ") else zone = 0 end projects = client.list_projects if yes?("Do you want to deploy your server within a project?") && projects.size > 0 if projects.size > 0 say "Select a project", :yellow (projects) project = ask_number("Project Nr.: ") end project_id = projects[project]['id'] rescue nil end say "Please provide a name for the new server", :yellow say "(spaces or special characters are NOT allowed)" server_name = ask("Server name: ") server_offerings = client.list_service_offerings say "Select a computing offering:", :yellow (server_offerings) service_offering = ask_number("Offering Nr.: ") templates = client.list_templates(project_id: project_id, zone_id: zones[zone]["id"]) say "Select a template:", :yellow (templates) template = ask_number("Template Nr.: ") networks = client.list_networks(project_id: project_id, zone_id: zones[zone]["id"]) if networks.size > 1 say "Select a network:", :yellow (networks) network = ask_number("Network Nr.: ") else network = 0 end say "You entered the following configuration:", :yellow table = [["Zone", zones[zone]["name"]]] table << ["Server Name", server_name] table << ["Template", templates[template]["name"]] table << ["Offering", server_offerings[service_offering]["name"]] table << ["Network", networks[network]["name"]] table << ["Project", projects[project]["name"]] if project print_table table if yes? "Do you want to deploy this server?" bootstrap_server( server_name, zones[zone]["name"], templates[template]["name"], server_offerings[service_offering]["name"], [networks[network]["name"]], nil, project ? projects[project]["name"] : nil ) end end |
#get_async_job_status(ids) ⇒ Object
14 15 16 17 18 |
# File 'lib/cloudstack-cli/helper.rb', line 14 def get_async_job_status(ids) ids.map do |id| client.query_job(id)['jobstatus'] end end |
#print_options(options, attr = 'name') ⇒ Object
3 4 5 6 7 |
# File 'lib/cloudstack-cli/helper.rb', line 3 def (, attr = 'name') .to_enum.with_index(1).each do |option, i| puts "#{i}: #{option[attr]}" end end |
#watch_jobs(jobs) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cloudstack-cli/helper.rb', line 20 def watch_jobs(jobs) chars = %w(| / - \\) async_state = {0 => "running", 1 => "completed", 2 => "error"} status = get_async_job_status(jobs.map {|job| job[:id]}) call = 0 while status.include?(0) do status = call.modulo(40) == 0 ? get_async_job_status(jobs.map {|job| job[:id]}) : status print ("\r" + "\e[A\e[K" * (status.size)) if call > 0 status.each_with_index do |job_status, i| puts "#{jobs[i][:name]} : job #{async_state[job_status]} #{chars[0]}" end sleep 0.1 chars.push chars.shift call += 1 end print ("\r" + "\e[A\e[K" * (status.size)) status.each_with_index do |job_status, i| puts "#{jobs[i][:name]} : job #{async_state[job_status]}" end end |