Class: Slicehost::Helper
- Inherits:
-
Object
- Object
- Slicehost::Helper
- Defined in:
- lib/slicehost.rb
Constant Summary collapse
- Base =
"https://manage.slicehost.com"
- Arch200708 =
Slice images
9
- CentOS5 =
2
- DebianEtch =
4
- Fedora8 =
5
- UbuntuDapper =
6
- UbuntuGutsy =
8
- Slice256 =
Slice sizes
1
- Slice512 =
2
- Slice1024 =
3
- Slice2048 =
4
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #create_googleapps_mx(url) ⇒ Object
- #create_record(url, options) ⇒ Object
- #create_records(url, option_list) ⇒ Object
- #create_simple_zone(url, ipaddr) ⇒ Object
-
#create_slice(name, options = {}) ⇒ Object
WARNING: Calling this method will add a new slice to your account! YOU WILL BE BILLED BY SLICEHOST.
- #create_zone(url) ⇒ Object
- #delete_zone(url) ⇒ Object
-
#initialize ⇒ Helper
constructor
A new instance of Helper.
-
#login(email, password) ⇒ Object
!> ‘&’ interpreted as argument prefix.
- #record_info(url) ⇒ Object
-
#slices ⇒ Object
Output is an Array of: “name”=>“slicehost01”, “url”=>“/slices/1234”, “bandwidth”=>{“total”=>“2.15GB”, “inout”=>“0.17 in / 1.99 out”, “backups”=>“On”, “ip”=>“123.456.789.012”, “ram”=>“512MB”, “age”=>“6 months”}.
- #zone?(url) ⇒ Boolean
- #zone_info(url) ⇒ Object
Constructor Details
#initialize ⇒ Helper
Returns a new instance of Helper.
29 30 31 |
# File 'lib/slicehost.rb', line 29 def initialize @agent = WWW::Mechanize.new end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
27 28 29 |
# File 'lib/slicehost.rb', line 27 def agent @agent end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
27 28 29 |
# File 'lib/slicehost.rb', line 27 def page @page end |
Instance Method Details
#create_googleapps_mx(url) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/slicehost.rb', line 114 def create_googleapps_mx(url) dotted_url = "#{url}." mx_records = <<-RECORD ASPMX.L.GOOGLE.COM. ALT1.ASPMX.L.GOOGLE.COM. ALT2.ASPMX.L.GOOGLE.COM. ASPMX2.GOOGLEMAIL.COM. ASPMX3.GOOGLEMAIL.COM. RECORD mx_records = mx_records.split("\n").map { |mx| mx.strip } mx_aux = %w[1 3 3 5 5 5 ] aux_count = 0 create_records(url, mx_records.inject([]) do |mem, mx| mem << { "name" => dotted_url, "data" => mx, "record_type" => "MX", "aux" => mx_aux[aux_count] } aux_count =+ 1 mem end) end |
#create_record(url, options) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/slicehost.rb', line 88 def create_record(url, ) return unless info = zone_info(url) dotted_url = "#{url}." ["record_type"] ||= "A" ["name"] ||= dotted_url .each_pair { |name, val| [name] = [val] unless val.is_a?(Array) } @page = agent.get("#{info['records']}/new") form = page.form(nil) form.set_fields() @page = agent.submit(form) end |
#create_records(url, option_list) ⇒ Object
100 101 102 103 104 |
# File 'lib/slicehost.rb', line 100 def create_records(url, option_list) option_list.each do || create_record(url, ) end end |
#create_simple_zone(url, ipaddr) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/slicehost.rb', line 106 def create_simple_zone(url, ipaddr) create_record(url, "data" => ipaddr) create_record(url, "name" => "www", "data" => ipaddr) (1..3).to_a.each do |ns| create_record(url, "data" => "ns#{ns}.slicehost.net.", "record_type" => "NS") end end |
#create_slice(name, options = {}) ⇒ Object
WARNING: Calling this method will add a new slice to your account! YOU WILL BE BILLED BY SLICEHOST. name
can be an empty string, Slicehost will allocated a random name to your new slice
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/slicehost.rb', line 136 def create_slice(name, = {}) ["name"] = name ["image_id"] ||= UbuntuDapper ["image_id"] = ["image_id"].to_i.to_s # raise error if not Fixnum ["flavor"] ||= Slice256 ["flavor"] = ["flavor"].to_i.to_s # raise error if not Fixnum @page = agent.get("#{Base}/slices/new") form = page.form(nil) form.set_fields() @page = agent.submit(form) end |
#create_zone(url) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/slicehost.rb', line 62 def create_zone(url) page = agent.get("#{Base}/zones/new") form = page.form(nil) form.origin = url form.ttl = 3600 agent.submit(form) end |
#delete_zone(url) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/slicehost.rb', line 70 def delete_zone(url) return unless info = zone?(url) agent.post(info["delete_url"], { "_method" => "delete", "authenticity_token" => info["authenticity_token"] }) end |
#login(email, password) ⇒ Object
!> ‘&’ interpreted as argument prefix
33 34 35 36 37 38 39 40 |
# File 'lib/slicehost.rb', line 33 def login(email, password) @page = agent.get("#{Base}/login") form = page.form(nil) form.email = email form.password = password @page = agent.submit(form) page.title != "Slicehost Login" end |
#record_info(url) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/slicehost.rb', line 78 def record_info(url) return [] unless info = zone_info(url) @page = agent.get(info["records"]) (page/"#dns tr")[1..-1].inject([]) do |mem, tr| name, data, record_type = (tr/"td")[0..2].map { |td| td.inner_html } mem << { "name" => name, "data" => data, "record_type" => record_type } mem end.sort { |a, b| a["record_type"] <=> b["record_type"] } end |
#slices ⇒ Object
Output is an Array of: “name”=>“slicehost01”, “url”=>“/slices/1234”,
"bandwidth"=>{"total"=>"2.15GB", "inout"=>"0.17 in / 1.99 out",
"backups"=>"On", "ip"=>"123.456.789.012", "ram"=>"512MB", "age"=>"6 months"}
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/slicehost.rb', line 152 def slices @page = agent.get("#{Base}/slices") (page/"#slice_list tr")[1..-1].map do |tr| td_list = tr/"td" slice_name = (td_list[0]/"a").first slice = { "name" => slice_name.inner_html, "url" => slice_name.get_attribute("href"), "status" => (td_list[1]/"span").first.inner_html.strip, "ram" => td_list[2].inner_html, "ip" => (td_list[3]/"span").first.children.first.to_s.strip, "backups" => td_list[5].inner_html, "age" => td_list[6].inner_html } bandwidth = td_list[4] slice["bandwidth"] = { "total" => bandwidth.children.first, "inout" => bandwidth.children.last.inner_html } slice end end |
#zone?(url) ⇒ Boolean
42 43 44 |
# File 'lib/slicehost.rb', line 42 def zone?(url) zone_info(url) end |
#zone_info(url) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/slicehost.rb', line 46 def zone_info(url) dotted_url = "#{url}." @page = agent.get("#{Base}/zones") if row = (page/"#dns tr").select { |tr| td = (tr/"td").first; td && td.inner_html == dotted_url }.first info = {} details_td = (row/"td").last info["records"] = (details_td/"a").first.get_attribute("href") info["zone_id"] = info["records"].match(/\d+/)[0].to_i info["delete_url"] = (details_td/"a").last.get_attribute("href") delete_click = (details_td/"a").last.get_attribute("onclick") match = delete_click.match(/s\.setAttribute\('name', 'authenticity_token'\); s\.setAttribute\('value', '(\w+)'\);/) info["authenticity_token"] = match[1] info end end |