Method: OpenStack::Compute::Connection#create_server

Defined in:
lib/openstack/compute/connection.rb

#create_server(options) ⇒ Object

Creates a new server instance on OpenStack Compute

The argument is a hash of options. The keys :name, :flavorRef, and :imageRef are required; :metadata, :security_groups, :key_name and :personality are optional.

:flavorRef and :imageRef are href strings identifying a particular server flavor and image to use when building the server. The :imageRef can either be a stock image, or one of your own created with the server.create_image method.

The :metadata argument should be a hash of key/value pairs. This metadata will be applied to the server at the OpenStack Compute API level.

The “Personality” option allows you to include up to five files, # of 10Kb or less in size, that will be placed on the created server. For :personality, pass a hash of the form => ‘server_path’. The file located at local_path will be base64-encoded and placed at the location identified by server_path on the new server.

Returns a OpenStack::Compute::Server object. The root password is available in the adminPass instance method.

>> server = cs.create_server(
     :name        => 'NewServer',
     :imageRef    => '3',
     :flavorRef   => '1',
     :metadata    => {'Racker' => 'Fanatical'},
     :personality => {'/home/bob/wedding.jpg' => '/root/wedding.jpg'},
     :key_name    => "mykey",
     :security_groups => [ "devel", "test"])
=> #<OpenStack::Compute::Server:0x101229eb0 ...>
>> server.name
=> "NewServer"
>> server.status
=> "BUILD"
>> server.adminPass
=> "NewServerSHMGpvI"


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/openstack/compute/connection.rb', line 118

def create_server(options)
  raise OpenStack::Exception::MissingArgument, "Server name, flavorRef, and imageRef, must be supplied" unless (options[:name] && options[:flavorRef] && options[:imageRef])
  if options[:personality]
    options[:personality] = Personalities.get_personality(options[:personality])
  end
  options[:security_groups] = (options[:security_groups] || []).inject([]){|res, c| res << {"name"=>c} ;res}
  data = JSON.generate(:server => options)
  response = @connection.csreq("POST",@connection.service_host,"#{@connection.service_path}/servers",@connection.service_port,@connection.service_scheme,{'content-type' => 'application/json'},data)
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  server_info = JSON.parse(response.body)['server']
  server = OpenStack::Compute::Server.new(self,server_info['id'])
  server.adminPass = server_info['adminPass']
  server
end