Skip to content

Instantly share code, notes, and snippets.

@pramodshinde
Last active October 2, 2016 08:23
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 pramodshinde/def0c0824d46019e033457ea3e50558b to your computer and use it in GitHub Desktop.
Save pramodshinde/def0c0824d46019e033457ea3e50558b to your computer and use it in GitHub Desktop.
Sample template for Linode VPS CRUD
#If you are refering this gist, I would suggest to refer my blog post and Linode gem documentation
# post url: https://pramodbshinde.wordpress.com/2016/10/01/spot-instances-with-linode
# gem url: https://github.com/rick/linode
class LinodeService
#Replace following contacts as per your needs
PLAN_ID = 1
API_KEY = 'xyz'
DATACENTERS = {
newark: 6,
london: 7
}
SOURCE_LINODES = {
london: 5679,
newark: 1234
}
def initialize
linode_client
end
#Clone new linode and boot
def launch_linode
@linode = clone
boot
end
#Launching instance
def clone
@client.linode.clone(
linodeid: SOURCE_LINODES[:london],
datacenterid: DATACENTERS[:london],
planid: PLAN_ID
)
end
#Booting instance
def boot
@linode.boot(linodeid: @linode.linodeid)
end
##Updating instance
def update
@client.linode.update(
linodeid: @linode.linodeid,
label: 'Instance' + '-' + @linode.linodeid.to_s,
lpm_displaygroup: 'My Linodes'
)
end
#Listing instances
def list
@client.linode.list
end
#Getting IP address of linode
def ip_address
data = @client.linode.ip.list(linodeid: @linode.linodeid)
data.first.ipaddress
end
#Deleting instance
def delete
@linode.delete(linodeid: linodeid, skipChecks: true)
end
private
def linode_client
@client ||= Linode.new(api_key: API_KEY)
end
end
service = LinodeService.new
service.launch_linode
service.ip_address
=> 209.123.234.161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment