A tutorial for VPS CRUD with Linode Cloud Hosting and Ruby. As a Rails developer have you ever wondered how to launch and delete cloud instance on demand with Ruby? In this post I will take you through how you can do a CRUD(Create, Read, Update, Delete) of a VPS or SSD Cloud server hosted on Linode.
To implement a VPS CRUD you need to have a Linode account and obtain a API key from profile API keys section and use this obtained API key in Linode API client gem to create API client.
Install linode client gem
gem install linode
Once you install this gem, instantiate a API client as
API_KEY = 'xyz'
@client = Linode.new(api_key: API_KEY)
Before you proceed there are some prerequisites you need to know and have them configured
One or More Template/Source Linodes, Which Will Be Used While Spawning/Creating Instances by Cloning
- Source Linode is a instance with all your necessary setup.
- This source Linode has running Rails application or scripts that can be executed remotely or services you need to trigger remotely.
- Once you launch source linodes and deploy tested code on these instances next thing you need to do is ‘Power Off’ these source linodes.
- This is very important to note that if you want to clone a new Linode from source Linode then source Linode instance needs to be in ‘Powered off’ state.
- Also there is a limit of 5 active clone operations per source/template Linode.
- I would recommend to have one source Linode per region/datacenter if you need more that one Linode and keep all in one group something like ‘Source Linodes’.
- Once you have list of source linodes, configure list of source Linode ids with location as following which will be used in clone operation.
#List of powered off instances to be cloned from
SOURCE_LINODES = {
london: 5679,
newark: 1234
}
- You need to give a data center id while cloning a instance .
- Essentially this is the location where your new instance will be spawned/created.
- Linode offers 8 data centers.
#List of available data centers and data center ids
DATACENTERS = {
newark: 6,
london: 7
}
- You need to give a plan id while cloning a instance.
- Linode offers various plans for instances.
- Configure the best plan as per your needs.
#Linode plan id used while launching new instances
PLAN_ID = 1
Once you configure above all then we are set to do a CRUD of instances.
Launching and Booting Linode using source linodeid, datacenterid and planid.
#Launching instance
@linode = @client.linode.clone(
linodeid: SOURCE_LINODES[:london],
datacenterid: DATACENTERS[:london],
planid: PLAN_ID
)
#Booting instance
@linode.boot(linodeid: @linode.linodeid)
Updating Linode label, display group by linodeid
#Updating instance
@client.linode.update(
linodeid: @linode.linodeid,
label: 'Instance' + '-' + @linode.linodeid.to_s,
lpm_displaygroup: 'My Linodes'
)
Listing all instances using instantiated linode client
#Listing instances
@client.linode.list
#[#<OpenStruct datacenterid=6, lpm_displaygroup='My Linodes', totalxfer=600, alert_bwquota_enabled=1,...]
Getting IP address by linodeid of created linode instance
data = @client.linode.ip.list(linodeid: @linode.linodeid)
#[#<OpenStruct ipaddress='209.123.234.161', linodeid=12446, ...]
data.first.ipaddress
#209.123.234.161
Deleting Linode by linodeid
Note: ‘skipChecks’ option which Skips the safety checks and will always delete the Linode, its kind of force delete.
@linode.delete(linodeid: linodeid, skipChecks: true)
- First and more common use of the spot instances is for web scraping by having farm of instances.
- If you need to execute various services on different servers which takes long time to complete.
- If you have limited infrastructures for your services and you want to replicate your service on other server very quickly.
I have created a template gist which is a consolidation of all the steps that I explained above. If you got stuck in some problem while implementing this, Please let me know It will be my pleasure to help you out and suggestions are most welcome.
Have a nice day!