Created
December 27, 2015 04:33
-
-
Save kaspergrubbe/756f0a227db8aeb92818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def request(linode_method, query_args = {}) | |
begin | |
args = { | |
api_key: @api_key, | |
api_action: linode_method, | |
}.merge(query_args).map{|k,v| "#{k}=#{v}"}.compact.join('&') | |
uri = URI("https://api.linode.com/?#{args}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
req = Net::HTTP::Get.new(uri) | |
resource = http.request(req) | |
json_body = JSON.parse(resource.body) | |
return json_body['DATA'] | |
rescue StandardError => e | |
puts "HTTP Request failed (#{e.message})" | |
end | |
end | |
def boot_linode | |
# CALCULATE AND SET HOSTNAME | |
name = Hostname.new.generate | |
domain = 'srvnce.com' | |
hostname = "#{name}.#{domain}" | |
app_name = 'app_name' | |
role = 'app' | |
compiled_role = "#{app_name}-#{role}" | |
root_pass = SecureRandom.hex | |
puts "Booting #{hostname}!" | |
# FETCH SETUP PARAMATERS | |
datacenter_id = request('avail.datacenters'). | |
select{|center| center['ABBR']=='london'}. | |
first['DATACENTERID'] | |
plan_id = request('avail.linodeplans'). | |
select{|plan| plan['LABEL']=='Linode 1024'}. | |
first['PLANID'] | |
dist_id = request('avail.distributions'). | |
select{|dist| dist['LABEL']=='Ubuntu 14.04 LTS'}. | |
first['DISTRIBUTIONID'] | |
kernel_id = request('avail.kernels'). | |
select{|dist| dist['LABEL'].start_with?('Latest 64 bit')}. | |
first['KERNELID'] | |
# CREATE SERVER | |
linode_id = request('linode.create', { | |
'DatacenterID' => datacenter_id, | |
'PlanID' => plan_id} | |
)['LinodeID'] | |
# CREATE MAIN DISK | |
disk = request('linode.disk.create', { | |
'LinodeID' => linode_id, | |
'FromDistributionID' => dist_id, | |
'rootPass' => root_pass, | |
'rootSSHKey' => ssh_key, | |
'Label' => 'Ubuntu 14.04 LTS Disk', | |
'Type' => 'ext4', | |
'Size' => '20000', | |
})['DiskID'] | |
# CREATE SWAP DISK | |
swap = request('linode.disk.create', { | |
'LinodeID' => linode_id, | |
'Label' => '512MB Swap Image', | |
'Type' => 'swap', | |
'Size' => '512', | |
})['DiskID'] | |
sleep(10) | |
# CREATE KERNEL-CONFIG | |
config = request('linode.config.create', { | |
'LinodeID' => linode_id, | |
'KernelID' => kernel_id, | |
'Label' => 'My Ubuntu 14.04 LTS Profile', | |
'DiskList' => "#{disk},#{swap}", | |
}) | |
sleep(10) | |
# UPDATE LABEL AND DISPLAY-GROUP | |
set_label = request('linode.update', { | |
'LinodeID' => linode_id, | |
'Label' => name, | |
'lpm_displayGroup' => compiled_role, | |
}) | |
# SET DNS | |
ip_addr = request('linode.ip.list', { | |
'LinodeID' => linode_id, | |
}).first['IPADDRESS'] | |
# BOOT SERVER | |
boot = request('linode.boot', { | |
'LinodeID' => linode_id, | |
}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment