fog / fog-vsphere

Fog for vSphere
MIT License
36 stars 63 forks source link

Issue using create_vm -- perhaps I'm clueless #184

Closed heyimacomputer closed 5 years ago

heyimacomputer commented 5 years ago

Hi folks,

I'm trying to do something quite simple, create a VM, using fog-vsphere. And yet I'm running into a brick wall, surely due to my own ignorance, but I'm looking for some guidance.

What seems to be happening is that I'm unable to create a VM without defining a volume, but I can't create a volume without specifying a server_id (aka an already existing VM). At least that's how I'm reading it.

First thing, I thought I'd create a VM with no volumes, since I really don't care to add a volume at this point:

compute.create_vm(
                      name: 'test',
                      guest_id: 'centos64Guest',
                      hardware_version: 'vmx-09',
                      cpus: 2,
                      corespersocket: 1,
                      memory_mb: 1024,
                      datacenter: 'Lab',
                      cluster: 'Intel',
                      datastore: 'freenas',
                      volumes: [],
                      path: '/Datacenters/Lab/vm'
    )

This results in the following error: InvalidDatastorePath: Invalid datastore path '[datastore1]'.

And I can see that if no volumes are defined, it defaults to datastore1.

Ok, so I'll first create a volume then:

@vol_res = compute.volumes.new(datastore: 'freenas', mode: 'persistent', size_gb: '30', thin: 'true', name: 'test-01')
@res = compute.create_vm(
                      name: 'test',
                      guest_id: 'centos64Guest',
                      hardware_version: 'vmx-09',
                      cpus: 2,
                      corespersocket: 1,
                      memory_mb: 1024,
                      datacenter: 'Lab',
                      cluster: 'Intel',
                      datastore: 'freenas',
                      volumes: [@vol_res],
                      path: '/Datacenters/Lab/vm'
    )

Which then errors out on compute.create_vm, different error: server is required for this operation

So it appears I'm lost at what seems like it should be very easy, creating a VM from scratch. Any guidance you folks can provide would be incredibly helpful.

I've spent some time digging through the code, and it seems that you MUST define volumes: when creating a VM, if volumes is an empty array, it defaults to datastore1 which doesn't exist in my environment, and volumes appear to require a server to exist before they can be created (though I'm possibly wrong about this).

timogoebel commented 5 years ago

Let me give you an example that is supposed to work. I hope you can figure it out from here.

client = ::Fog::Compute.new(
  :provider                     => 'vsphere',
  :vsphere_username             => '',
  :vsphere_password             => ''
  :vsphere_server               => '',
  :vsphere_expected_pubkey_hash => '',
)

interface_attrs = {
  network: 'My super network',
  type: 'VirtualE1000'
}

attrs = {
  :name => 'test.example.com',
  :memory_mb => '768',
  :interfaces => [ interface_attrs ],
  :volumes => [
    {
      :storage_pod => 'name-of-datastore-cluster',
      :name => 'Hard disk',
      :size_gb => '10',
      :thin => 'true',
      :eager_zero => 'false'
    }
  ],
  :scsi_controller=> {
    :type=>"VirtualLsiLogicController"
  },
  :datacenter => "DC",
  :boot_order => ["network", "disk"],
  :cpus => "1",
  :corespersocket => "1",
  :cluster=>"ClusterName",
  :resource_pool=>"default",
  :path => "/path/abc/def",
  :guest_id=>"rhel7_64Guest",
  :memoryHotAddEnabled=>"1",
  :cpuHotAddEnabled=>"1",
  :start=>"1",
  :annotation=>"created with fog-vsphere"
}

vm = client.servers.new(attrs)
vm.save
heyimacomputer commented 5 years ago

Worked like a charm, thanks @timogoebel , I was making a huge error calling create_vm directly. This clarified a LOT.