Skip to content

Instantly share code, notes, and snippets.

@LastZactionHero
Created January 23, 2019 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LastZactionHero/53dd1b4a038c0fea2b784386f936e16d to your computer and use it in GitHub Desktop.
Save LastZactionHero/53dd1b4a038c0fea2b784386f936e16d to your computer and use it in GitHub Desktop.
Vivado VNC Server Teardown
# Saving some money!
# Snapshots the Vivado VNC server and terminates it with the Vultr API
#
# Configuration:
# ENV vultr_api_key
# Server Label: 'vivado'
require 'rest-client'
require 'json'
require 'pry'
api_key = ENV['vultr_api_key']
host = 'https://api.vultr.com'
path_servers_destroy = '/v1/server/destroy'
path_servers_list = '/v1/server/list'
path_snapshots_create = '/v1/snapshot/create '
path_snapshots_list = '/v1/snapshot/list'
server_label = 'vivado'
# Find Server
response = RestClient.get("#{host}#{path_servers_list}", 'API-Key' => api_key)
servers = JSON.parse(response.body)
server = servers.select { |_, v| v['label'] == server_label }.map { |_, v| v }.first
# Start Snapshot
response = RestClient.post("#{host}#{path_snapshots_create}",
{ SUBID: server['SUBID'] },
'API-Key' => api_key)
snapshot_id = JSON.parse(response.body)['SNAPSHOTID']
# Wait for Clone to Complete
status = 'pending'
while status == 'pending'
sleep 60
response = RestClient.get("#{host}#{path_snapshots_list}", 'API-Key' => api_key)
status = JSON.parse(response.body)[snapshot_id]['status']
puts status
end
# Destroy Server
if status == 'complete'
response = RestClient.post("#{host}#{path_servers_destroy}",
{ SUBID: server['SUBID'] },
'API-Key' => api_key)
puts response.code
else
puts "Status: #{status}, not destroying"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment