/lib/fog/hp/models/compute/server.rb

https://github.com/dylanegan/fog · Ruby · 225 lines · 180 code · 40 blank · 5 comment · 8 complexity · c96db673a85abafc19792effe32ecf8c MD5 · raw file

  1. require 'fog/compute/models/server'
  2. module Fog
  3. module Compute
  4. class HP
  5. class Server < Fog::Compute::Server
  6. identity :id
  7. attribute :addresses
  8. attribute :flavor
  9. attribute :host_id, :aliases => 'hostId'
  10. attribute :image
  11. attribute :metadata
  12. attribute :name
  13. attribute :personality
  14. attribute :progress
  15. attribute :accessIPv4
  16. attribute :accessIPv6
  17. attribute :state, :aliases => 'status'
  18. attribute :created_at, :aliases => 'created'
  19. attribute :updated_at, :aliases => 'updated'
  20. attribute :tenant_id
  21. attribute :user_id
  22. attribute :key_name
  23. # these are implemented as methods
  24. attribute :image_id
  25. attribute :flavor_id
  26. attribute :private_ip_address
  27. attribute :public_ip_address
  28. attr_reader :password
  29. attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username, :image_id, :flavor_id
  30. def initialize(attributes = {})
  31. # assign these attributes first to prevent race condition with new_record?
  32. self.security_groups = attributes.delete(:security_groups)
  33. self.min_count = attributes.delete(:min_count)
  34. self.max_count = attributes.delete(:max_count)
  35. super
  36. end
  37. def destroy
  38. requires :id
  39. connection.delete_server(id)
  40. true
  41. end
  42. def images
  43. requires :id
  44. connection.images(:server => self)
  45. end
  46. def key_pair
  47. requires :key_name
  48. connection.key_pairs.get(key_name)
  49. end
  50. def key_pair=(new_keypair)
  51. self.key_name = new_keypair && new_keypair.name
  52. end
  53. def private_ip_address
  54. addr = addresses.nil? ? nil : addresses.fetch('private', []).first
  55. addr["addr"] if addr
  56. end
  57. def private_key_path
  58. @private_key_path ||= Fog.credentials[:private_key_path]
  59. @private_key_path &&= File.expand_path(@private_key_path)
  60. end
  61. def private_key
  62. @private_key ||= private_key_path && File.read(private_key_path)
  63. end
  64. def public_ip_address
  65. # FIX: Both the private and public ips are bundled under "private" network name
  66. # So hack to get to the public ip address
  67. if !addresses.nil?
  68. addr = addresses.fetch('private', [])
  69. # if we have more than 1 address, then the return the second address which is public
  70. if addr.count > 1
  71. addr[1]["addr"]
  72. else
  73. nil
  74. end
  75. else
  76. nil
  77. end
  78. end
  79. def public_key_path
  80. @public_key_path ||= Fog.credentials[:public_key_path]
  81. @public_key_path &&= File.expand_path(@public_key_path)
  82. end
  83. def public_key
  84. @public_key ||= public_key_path && File.read(public_key_path)
  85. end
  86. def image_id
  87. @image_id ||= (image.nil? ? nil : image["id"])
  88. end
  89. def image_id=(new_image_id)
  90. @image_id = new_image_id
  91. end
  92. def flavor_id
  93. @flavor_id ||= (flavor.nil? ? nil : flavor["id"])
  94. end
  95. def flavor_id=(new_flavor_id)
  96. @flavor_id = new_flavor_id
  97. end
  98. def min_count=(new_min_count)
  99. @min_count = new_min_count
  100. end
  101. def max_count=(new_max_count)
  102. @max_count = new_max_count
  103. end
  104. def security_groups=(new_security_groups)
  105. @security_groups = new_security_groups
  106. end
  107. def ready?
  108. self.state == 'ACTIVE'
  109. end
  110. def change_password(admin_password)
  111. requires :id
  112. connection.change_password_server(id, admin_password)
  113. true
  114. end
  115. def reboot(type = 'SOFT')
  116. requires :id
  117. connection.reboot_server(id, type)
  118. true
  119. end
  120. def rebuild(image_id, name, admin_pass=nil, metadata=nil, personality=nil)
  121. requires :id
  122. connection.rebuild_server(id, image_id, name, admin_pass, metadata, personality)
  123. true
  124. end
  125. def resize(flavor_id)
  126. requires :id
  127. connection.resize_server(id, flavor_id)
  128. true
  129. end
  130. def revert_resize
  131. requires :id
  132. connection.revert_resized_server(id)
  133. true
  134. end
  135. def confirm_resize
  136. requires :id
  137. connection.confirm_resized_server(id)
  138. true
  139. end
  140. def create_image(name, metadata={})
  141. requires :id
  142. connection.create_image(id, name, metadata)
  143. end
  144. def save
  145. raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  146. requires :flavor_id, :image_id, :name
  147. options = {
  148. 'metadata' => metadata,
  149. 'personality' => personality,
  150. 'accessIPv4' => accessIPv4,
  151. 'accessIPv6' => accessIPv6,
  152. 'min_count' => @min_count,
  153. 'max_count' => @max_count,
  154. 'key_name' => key_name,
  155. 'security_groups' => @security_groups
  156. }
  157. options = options.reject {|key, value| value.nil?}
  158. data = connection.create_server(name, flavor_id, image_id, options)
  159. merge_attributes(data.body['server'])
  160. true
  161. end
  162. def setup(credentials = {})
  163. requires :public_ip_address, :identity, :public_key, :username
  164. Fog::SSH.new(public_ip_address, username, credentials).run([
  165. %{mkdir .ssh},
  166. %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
  167. %{passwd -l #{username}},
  168. %{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
  169. %{echo "#{Fog::JSON.encode(metadata)}" >> ~/metadata.json}
  170. ])
  171. rescue Errno::ECONNREFUSED
  172. sleep(1)
  173. retry
  174. end
  175. def username
  176. @username ||= 'root'
  177. end
  178. private
  179. def adminPass=(new_admin_pass)
  180. @password = new_admin_pass
  181. end
  182. end
  183. end
  184. end
  185. end