class Router
Public Instance Methods
destroy(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 117 def destroy(*names) 118 routers = names.map {|name| get_router(name)} 119 print_routers(routers) 120 exit unless options[:force] || yes?("\nDestroy router(s) above? [y/N]:", :magenta) 121 jobs = routers.map do |router| 122 {id: client.destroy_router({id: router['id']}, {sync: true})['jobid'], name: "Destroy router #{router['name']}"} 123 end 124 puts 125 watch_jobs(jobs) 126 end
execute_router_commands(command, routers)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 211 def execute_router_commands(command, routers) 212 unless %w(start stop reboot stop_start).include?(command) 213 say "\nCommand #{options[:command]} not supported.", :red 214 exit 1 215 end 216 exit unless yes?("\n#{command.capitalize} the router(s) above? [y/N]:", :magenta) 217 218 command.split("_").each do |cmd| 219 jobs = routers.map do |router| 220 { 221 job_id: nil, 222 args: { id: router["id"] }, 223 name: "#{cmd.capitalize} router #{router['name']}", 224 status: -1 225 } 226 end 227 run_background_jobs(jobs, "#{cmd}_router") 228 end 229 end
get_router(name)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 144 def get_router(name) 145 unless router = client.list_routers(name: name, listall: true).first 146 unless router = client.list_routers(name: name, project_id: -1).first 147 say "Can't find router with name #{name}.", :red 148 exit 1 149 end 150 end 151 router 152 end
list()
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 21 def list 22 resolve_project 23 resolve_zone 24 resolve_account 25 26 routers = client.list_routers(options) 27 # show all routers unless project or account is set 28 if !options[:project] && !options[:account] 29 client.list_projects(listall: true).each do |project| 30 routers = routers + client.list_routers( 31 options.merge(projectid: project['id']) 32 ) 33 end 34 end 35 options[:listall] = true 36 print_routers(routers, options) 37 execute_router_commands(options[:command].downcase, routers) if options[:command] 38 end
list_from_file(file)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 49 def list_from_file(file) 50 routers = parse_file(file)["routers"] 51 print_routers(routers, options) 52 execute_router_commands(options[:command].downcase, routers) if options[:command] 53 end
print_redundant_state(router)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 207 def print_redundant_state(router) 208 router["isredundantrouter"] == "true" ? router["redundantstate"] : "non-redundant" 209 end
print_routers(routers, options = {})
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 154 def print_routers(routers, options = {}) 155 if routers.size < 1 156 say "No routers found." 157 else 158 if options[:redundant_state] 159 routers = filter_by( 160 routers, 161 'redundantstate', 162 options[:redundant_state].downcase 163 ) 164 end 165 routers.reverse! if options[:reverse] 166 167 options[:format] ||= "table" 168 case options[:format].to_sym 169 when :yaml 170 puts({routers: routers}.to_yaml) 171 when :json 172 puts JSON.pretty_generate(routers: routers) 173 else 174 table = [%w( 175 Name Zone Account/Project IP Linklocal-IP Status Version 176 )] 177 table[0].unshift('ID') if options[:showid] 178 if options[:verbose] 179 table[0].push('Redundant-State', 'Requ-Upgrade', 'Offering') 180 end 181 routers.each do |router| 182 table << [ 183 router["name"], 184 router["zonename"], 185 router["project"] || router["account"], 186 router["nic"] && router["nic"].first ? router["nic"].first['ipaddress'] : "-", 187 router["linklocalip"] || "-", 188 router["state"], 189 router["version"] || "-" 190 ] 191 table[-1].unshift(router["id"]) if options[:showid] 192 if options[:verbose] 193 table[-1].push( 194 print_redundant_state(router), 195 router["requiresupgrade"] || "-", 196 router["serviceofferingname"] 197 ) 198 end 199 end 200 print_table table 201 puts 202 say "Total number of routers: #{routers.size}" 203 end 204 end 205 end
reboot(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 83 def reboot(*names) 84 routers = names.map {|name| client.list_routers(name: name).first} 85 print_routers(routers) 86 exit unless options[:force] || yes?("\nReboot router(s) above? [y/N]:", :magenta) 87 jobs = routers.map do |router| 88 {id: client.reboot_router({id: router['id']}, {sync: true})['jobid'], name: "Reboot router #{router['name']}"} 89 end 90 puts 91 watch_jobs(jobs) 92 end
show(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 130 def show(*names) 131 routers = names.map {|name| get_router(name)} 132 table = [] 133 routers.each do |router| 134 router.each do |key, value| 135 table << [ set_color("#{key}:", :yellow), "#{value}" ] 136 end 137 table << [ "-" * 20 ] unless router == routers[-1] 138 end 139 print_table table 140 end
start(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 70 def start(*names) 71 routers = names.map {|name| get_router(name)} 72 print_routers(routers) 73 exit unless options[:force] || yes?("\nStart router(s) above? [y/N]:", :magenta) 74 jobs = routers.map do |router| 75 {id: client.start_router({id: router['id']}, {sync: true})['jobid'], name: "Start router #{router['name']}"} 76 end 77 puts 78 watch_jobs(jobs) 79 end
stop(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 57 def stop(*names) 58 routers = names.map {|name| get_router(name)} 59 print_routers(routers) 60 exit unless options[:force] || yes?("\nStop router(s) above? [y/N]:", :magenta) 61 jobs = routers.map do |router| 62 {id: client.stop_router({id: router['id']}, {sync: true})['jobid'], name: "Stop router #{router['name']}"} 63 end 64 puts 65 watch_jobs(jobs) 66 end
stop_start(*names)
click to toggle source
# File lib/cloudstack-cli/commands/router.rb 96 def stop_start(*names) 97 routers = names.map {|name| get_router(name)} 98 print_routers(routers) 99 exit unless options[:force] || yes?("\nRestart router(s) above? [y/N]:", :magenta) 100 jobs = routers.map do |router| 101 {id: client.stop_router({id: router['id']}, {sync: true})['jobid'], name: "Stop router #{router['name']}"} 102 end 103 puts 104 watch_jobs(jobs) 105 106 jobs = routers.map do |router| 107 {id: client.start_router({id: router['id']}, {sync: true})['jobid'], name: "Start router #{router['name']}"} 108 end 109 puts 110 watch_jobs(jobs) 111 112 say "Finished.", :green 113 end