Class: CivoCLI::Instance
- Inherits:
-
Thor
- Object
- Thor
- CivoCLI::Instance
- Defined in:
- lib/instance.rb
Constant Summary collapse
- DEFAULT_SIZE =
'g2.small'
- DEFAULT_REGION =
'lon1'
- DEFAULT_INITIAL_USER =
'civo'
- DEFAULT_PUBLIC_IP =
'true'
- DEFAULT_TEMPLATE =
'811a8dfb-8202-49ad-b1ef-1e6320b20497'
- DEFAULT_HOSTNAME =
CivoCLI::NameGenerator.create
Instance Method Summary collapse
- #console(id) ⇒ Object
- #create(*args) ⇒ Object
- #firewall(id, firewall_id) ⇒ Object
- #high_cpu ⇒ Object
- #list ⇒ Object
- #move_ip(id, ip_address) ⇒ Object
- #password(id) ⇒ Object
- #public_ip(id) ⇒ Object
- #reboot(id) ⇒ Object
- #remove(id) ⇒ Object
- #show(id) ⇒ Object
- #soft_reboot(id) ⇒ Object
- #start(id) ⇒ Object
- #stop(id) ⇒ Object
- #tags(id, newtags = nil) ⇒ Object
- #update(id) ⇒ Object
- #upgrade(id, new_size) ⇒ Object
Instance Method Details
#console(id) ⇒ Object
263 264 265 266 267 268 269 270 |
# File 'lib/instance.rb', line 263 def console(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Access #{instance.hostname.colorize(:green)} at #{instance.console.url}" rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#create(*args) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/instance.rb', line 129 def create(*args) CivoCLI::Config.set_api_auth if ![:name] && !args hostname = CivoCLI::NameGenerator.create elsif [:name] hostname = [:name] elsif ![:name] && args hostname = args.join('-') end if [:template] && [:snapshot] puts "Please provide either template OR snapshot ID".colorize(:red) exit 1 end if ![:template] && ![:snapshot] [:template] = DEFAULT_TEMPLATE end if [:script] unless File.exist?(File.([:script])) puts "The file #{[:script].colorize(:red)} doesn't exist or isn't readable" exit 2 end script = File.read(File.([:script])) end if [:template] @instance = Civo::Instance.create(hostname: hostname, size: [:size], template: [:template], public_ip: [:public_ip], initial_user: [:initial_user], region: [:region], ssh_key_id: [:ssh_key], tags: [:tags], script: script) elsif [:snapshot] @instance = Civo::Instance.create(hostname: hostname, size: [:size], snapshot_id: [:snapshot], public_ip: [:public_ip], initial_user: [:initial_user], region: [:region], ssh_key_id: [:ssh_key], tags: [:tags], script: script) end if [:wait] print "Building new instance #{hostname}: " unless [:quiet] timer = CivoCLI::Timer.new timer.start_timer spinner = CivoCLI::Spinner.spin(instance: @instance, quiet: [:quiet]) do |s| Civo::Instance.all.items.each do |instance| if instance.id == @instance.id && instance.status == 'ACTIVE' s[:final_instance] = instance end end s[:final_instance] end timer.end_timer if [:quiet] puts spinner[:final_instance].id else puts "\b Done\nCreated instance #{spinner[:final_instance].hostname.colorize(:green)} - #{spinner[:final_instance].initial_user}@#{spinner[:final_instance].public_ip} in #{Time.at(timer.time_elapsed).utc.strftime("%M min %S sec")}" end else puts "Created instance #{hostname.colorize(:green)}" end rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#firewall(id, firewall_id) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/instance.rb', line 322 def firewall(id, firewall_id) CivoCLI::Config.set_api_auth instance = detect_instance(id) Civo::Instance.firewall(id: instance.id, firewall_id: firewall_id) puts "Set #{instance.hostname.colorize(:green)} to use firewall '#{firewall_id.colorize(:yellow)}'" rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#high_cpu ⇒ Object
33 34 35 36 37 38 |
# File 'lib/instance.rb', line 33 def high_cpu CivoCLI::Config.set_api_auth instance = detect_instance(id) Civo::Instance.high_cpu end |
#list ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/instance.rb', line 12 def list CivoCLI::Config.set_api_auth if [:quiet] Civo::Instance.all(per_page: 10_000_000).items.each do |instance| puts instance.id end else rows = [] sizes = Civo::Size.all.items Civo::Instance.all(per_page: 10_000_000).items.each do |instance| size_name = sizes.detect {|s| s.name == instance.size}&.nice_name rows << [instance.id, instance.hostname, size_name, instance.region, instance.public_ip, instance.status] end puts Terminal::Table.new headings: ['ID', 'Hostname', 'Size', 'Region', 'Public IP', 'Status'], rows: rows end end |
#move_ip(id, ip_address) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/instance.rb', line 309 def move_ip(id, ip_address) CivoCLI::Config.set_api_auth instance = detect_instance(id) Civo::Instance.move_ip(id: instance.id, ip: ip_address) puts "Moved public IP #{ip_address} to instance #{instance.hostname}" rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#password(id) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/instance.rb', line 360 def password(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) if [:quiet] puts instance.initial_password else puts "The password for user #{instance.initial_user.colorize(:green)} on #{instance.hostname.colorize(:green)} is #{instance.initial_password.colorize(:green)}" end rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#public_ip(id) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/instance.rb', line 336 def public_ip(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) unless instance.public_ip.nil? if [:quiet] puts instance.public_ip else puts "The public IP for #{instance.hostname.colorize(:green)} is #{instance.public_ip.colorize(:green)}" end else puts "Error: Instance has no public IP" exit 2 end rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#reboot(id) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/instance.rb', line 238 def reboot(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Rebooting #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status." instance.reboot rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#remove(id) ⇒ Object
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/instance.rb', line 224 def remove(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Removing instance #{instance.hostname.colorize(:red)}" instance.remove rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#show(id) ⇒ Object
42 43 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/instance.rb', line 42 def show(id) CivoCLI::Config.set_api_auth rows = [] instance = detect_instance(id) sizes = Civo::Size.all(all: true).items ssh_keys = Civo::SshKey.all.items networks = Civo::Network.all.items firewalls = Civo::Firewall.all.items size = sizes.detect {|s| s.name == instance.size} if size @size_name = size.description end @network = networks.detect {|n| n.id == instance.network_id} @firewall = firewalls.detect {|fw| fw.id == instance.firewall_id} puts " ID : #{instance.id}" puts " Hostname : #{instance.hostname}" if instance.reverse_dns puts " Reverse DNS : #{instance.reverse_dns}" end puts " Tags : #{instance..join(", ")}" puts " Size : #{@size_name}" case instance.status when "ACTIVE" puts " Status : #{instance.status.colorize(:green)}" when /ING$/ puts " Status : #{instance.status.colorize(:orange)}" else puts " Status : #{instance.status.colorize(:red)}" end puts " Private IP : #{instance.private_ip}" puts " Public IP : #{[instance.pseudo_ip, instance.public_ip].join(" => ")}" puts " Network : #{@network.label} (#{@network.cidr})" puts " Firewall : #{@firewall&.name} (rules: #{@firewall&.rules_count})" puts " Region : #{instance.region}" puts " Initial User : #{instance.initial_user}" if instance.ssh_key.present? key = ssh_keys.detect { |k| k.id == instance.ssh_key } puts " SSH Key : #{key.name} (#{key.fingerprint})" end puts " OpenStack ID : #{instance.openstack_server_id}" puts " Template ID : #{instance.template_id}" puts " Snapshot ID : #{instance.snapshot_id}" puts "" if instance.script.present? puts "-" * 26 + " INIT SCRIPT " + "-" * 26 puts instance.script puts "" end puts "-" * 29 + " NOTES " + "-" * 29 puts "" puts instance.notes rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#soft_reboot(id) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/instance.rb', line 251 def soft_reboot(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Soft-rebooting #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status." instance.soft_reboot rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#start(id) ⇒ Object
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/instance.rb', line 284 def start(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Starting #{instance.hostname.colorize(:green)}. Use 'civo instance show #{instance.hostname}' to see the current status." instance.start rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#stop(id) ⇒ Object
273 274 275 276 277 278 279 280 281 |
# File 'lib/instance.rb', line 273 def stop(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) puts "Stopping #{instance.hostname.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status." Civo::Instance.stop(id: instance.id) rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#tags(id, newtags = nil) ⇒ Object
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/instance.rb', line 189 def (id, = nil) CivoCLI::Config.set_api_auth instance = detect_instance(id) Civo::Instance.(id: instance.id, tags: ) puts "Updated tags on #{instance.hostname.colorize(:green)}. Use 'civo instance show #{instance.hostname}' to see the current tags.'" rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#update(id) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/instance.rb', line 206 def update(id) CivoCLI::Config.set_api_auth instance = detect_instance(id) if [:name] Civo::Instance.update(id: instance.id, hostname: [:name]) puts "Instance #{instance.id} now named #{[:name].colorize(:green)}" end if [:notes] Civo::Instance.update(id: instance.id, notes: [:notes]) puts "Instance #{instance.id} notes are now: #{[:notes].colorize(:green)}" end rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |
#upgrade(id, new_size) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/instance.rb', line 296 def upgrade(id, new_size) CivoCLI::Config.set_api_auth instance = detect_instance(id) Civo::Instance.upgrade(id: instance.id, size: new_size) puts "Resizing #{instance.hostname.colorize(:green)} to #{new_size.colorize(:red)}. Use 'civo instance show #{instance.hostname}' to see the current status." rescue Flexirest::HTTPException => e puts e.result.reason.colorize(:red) exit 1 end |