knife-ovh-cloud 1.0.0.pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ # Author:: Brian Dupras (<bdupras@rallydev.com>)
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'chef/knife'
5
+ require 'chef/knife/base_ovh_cloud_command'
6
+ require 'rbvmomi'
7
+ require 'netaddr'
8
+
9
+ class Chef::Knife::OvhCloudVmConfig < Chef::Knife::BaseOvhCloudCommand
10
+ banner "knife ovh cloud vm config VMNAME PROPERTY VALUE. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\" for allowed ATTRIBUTE values (any property of type xs:string is supported)."
11
+
12
+ get_common_options
13
+
14
+ def run
15
+ $stdout.sync = true
16
+ vmname = @name_args[0]
17
+ if vmname.nil?
18
+ show_usage
19
+ fatal_exit("You must specify a virtual machine name")
20
+ end
21
+
22
+ property_name = @name_args[1]
23
+ if property_name.nil?
24
+ show_usage
25
+ fatal_exit("You must specify a PROPERTY name (e.g. annotation)")
26
+ end
27
+ property_name = property_name.to_sym
28
+
29
+ property_value = @name_args[2]
30
+ if property_value.nil?
31
+ show_usage
32
+ fatal_exit("You must specify a PROPERTY value")
33
+ end
34
+
35
+ vim = get_vim_connection
36
+
37
+ dc = get_datacenter
38
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
39
+
40
+ vm = traverse_folders_for_vm(folder, vmname) or abort "VM #{vmname} not found"
41
+
42
+ properties = {}
43
+ properties[property_name] = property_value
44
+ vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(properties)).wait_for_completion
45
+ end
46
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'chef/knife'
7
+ require 'chef/knife/base_ovh_cloud_command'
8
+ require 'rbvmomi'
9
+
10
+ # These two are needed for the '--purge' deletion case
11
+ require 'chef/node'
12
+ require 'chef/api_client'
13
+
14
+ # Delete a virtual machine from vCenter
15
+ class Chef::Knife::OvhCloudVmDelete < Chef::Knife::BaseOvhCloudCommand
16
+
17
+ banner "knife ovh cloud vm delete VMNAME"
18
+
19
+ option :purge,
20
+ :short => "-P",
21
+ :long => "--purge",
22
+ :boolean => true,
23
+ :description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the VM itself."
24
+
25
+ get_common_options
26
+
27
+ # Extracted from Chef::Knife.delete_object, because it has a
28
+ # confirmation step built in... By specifying the '--purge'
29
+ # flag (and also explicitly confirming the server destruction!)
30
+ # the user is already making their intent known. It is not
31
+ # necessary to make them confirm two more times.
32
+ def destroy_item(itemClass, name, type_name)
33
+ object = itemClass.load(name)
34
+ object.destroy
35
+ puts "Deleted #{type_name} #{name}"
36
+ end
37
+
38
+ def run
39
+ $stdout.sync = true
40
+
41
+ vmname = @name_args[0]
42
+
43
+ if vmname.nil?
44
+ show_usage
45
+ fatal_exit("You must specify a virtual machine name")
46
+ end
47
+
48
+ vim = get_vim_connection
49
+
50
+ baseFolder = find_folder(get_config(:folder));
51
+
52
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
53
+ fatal_exit("VM #{vmname} not found")
54
+
55
+ vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
56
+ vm.Destroy_Task
57
+ puts "Deleted virtual machine #{vmname}"
58
+
59
+ if config[:purge]
60
+ destroy_item(Chef::Node, vmname, "node")
61
+ destroy_item(Chef::ApiClient, vmname, "client")
62
+ else
63
+ puts "Corresponding node and client for the #{vmname} server were not deleted and remain registered with the Chef Server"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,66 @@
1
+ # Author:: Ian Delahorne (<ian@delahorne.com>)
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'chef/knife'
5
+ require 'chef/knife/base_ovh_cloud_command'
6
+ require 'rbvmomi'
7
+ require 'netaddr'
8
+
9
+ class Chef::Knife::OvhCloudVmExecute < Chef::Knife::BaseOvhCloudCommand
10
+ banner "knife ovh cloud vm execute VMNAME COMMAND ARGS"
11
+
12
+ option :exec_user,
13
+ :long => "--exec-user USER",
14
+ :description => "User to execute as",
15
+ :required => true
16
+
17
+ option :exec_passwd,
18
+ :long => "--exec-passwd PASSWORD",
19
+ :description => "Password for execute user",
20
+ :required => true
21
+
22
+ option :exec_dir,
23
+ :long => "--exec-dir DIRECTORY",
24
+ :description => "Working directory to execute in"
25
+
26
+ get_common_options
27
+
28
+ def run
29
+ $stdout.sync = true
30
+ vmname = @name_args[0]
31
+ if vmname.nil?
32
+ show_usage
33
+ fatal_exit("You must specify a virtual machine name")
34
+ end
35
+ command = @name_args[1]
36
+ if command.nil?
37
+ show_usage
38
+ fatal_exit("You must specify a command to execute")
39
+ end
40
+
41
+ args = @name_args[2]
42
+ if args.nil?
43
+ args = ""
44
+ end
45
+
46
+ vim = get_vim_connection
47
+
48
+ dc = get_datacenter
49
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
50
+
51
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
52
+ abort "VM #{vmname} not found"
53
+
54
+ gom = vim.serviceContent.guestOperationsManager
55
+
56
+ guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
57
+ :username => config[:exec_user],
58
+ :password => config[:exec_passwd])
59
+ prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
60
+ :arguments => args,
61
+ :workingDirectory => get_config(:exec_dir))
62
+
63
+ gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
64
+
65
+ end
66
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_ovh_cloud_command'
7
+
8
+ # Lists all known virtual machines in the configured datacenter
9
+ class Chef::Knife::OvhCloudVmList < Chef::Knife::BaseOvhCloudCommand
10
+
11
+ banner "knife ovh cloud vm list"
12
+
13
+ get_common_options
14
+
15
+ option :recursive,
16
+ :long => "--recursive",
17
+ :short => "-r",
18
+ :description => "Recurse down through sub-folders"
19
+
20
+ option :only_folders,
21
+ :long => "--only-folders",
22
+ :description => "Print only sub-folders"
23
+
24
+ def traverse_folders(folder)
25
+ puts "#{ui.color("Folder", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
26
+ print_vms_in_folder(folder) unless get_config(:only_folders)
27
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
28
+ folders.each do |child|
29
+ traverse_folders(child)
30
+ end
31
+ end
32
+
33
+ def print_vms_in_folder(folder)
34
+ vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine)
35
+ vms.each do |vm|
36
+ state = case vm.runtime.powerState
37
+ when PsOn
38
+ ui.color("on", :green)
39
+ when PsOff
40
+ ui.color("off", :red)
41
+ when PsSuspended
42
+ ui.color("suspended", :yellow)
43
+ end
44
+ puts "#{ui.color("VM Name:", :cyan)} #{vm.name}\t#{ui.color("IP:", :magenta)} #{vm.guest.ipAddress}\t#{ui.color("RAM:", :magenta)} #{vm.summary.config.memorySizeMB}\t#{ui.color("State:", :cyan)} #{state}"
45
+ end
46
+ end
47
+
48
+ def print_subfolders(folder)
49
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
50
+ folders.each do |subfolder|
51
+ puts "#{ui.color("Folder Name", :cyan)}: #{subfolder.name}"
52
+ end
53
+ end
54
+
55
+ def run
56
+ $stdout.sync = true
57
+ vim = get_vim_connection
58
+ baseFolder = find_folder(get_config(:folder));
59
+ if get_config(:recursive)
60
+ traverse_folders(baseFolder)
61
+ else
62
+ print_subfolders(baseFolder)
63
+ print_vms_in_folder(baseFolder)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,54 @@
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # Contributor:: Jesse Campbell (<hikeit@gmail.com>)
4
+ # Contributor:: Bethany Erskine (<bethany@paperlesspost.com>)
5
+ # Contributor:: Adrian Stanila (https://github.com/sacx)
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+
9
+ require 'chef/knife'
10
+ require 'chef/knife/base_ovh_cloud_command'
11
+ require 'rbvmomi'
12
+
13
+ # Clone an existing template into a new VM, optionally applying a customization specification.
14
+ # usage:
15
+ # knife vsphere vm markastemplate MyVM --folder /templates
16
+ class Chef::Knife::OvhCloudVmMarkastemplate < Chef::Knife::BaseOvhCloudCommand
17
+
18
+ banner "knife ovh cloud vm markastemplate VMNAME"
19
+
20
+ get_common_options
21
+
22
+ option :folder,
23
+ :long => "--folder FOLDER",
24
+ :description => "The folder which contains the VM"
25
+
26
+ def run
27
+ $stdout.sync = true
28
+
29
+ vmname = @name_args[0]
30
+ if vmname.nil?
31
+ show_usage
32
+ fatal_exit("You must specify a virtual machine name")
33
+ end
34
+ config[:chef_node_name] = vmname unless config[:chef_node_name]
35
+ config[:vmname] = vmname
36
+
37
+ if get_config(:bootstrap) && get_config(:distro) && !@@chef_config_dir
38
+ fatal_exit("Can't find .chef for bootstrap files. chdir to a location with a .chef directory and try again")
39
+ end
40
+
41
+ vim = get_vim_connection
42
+
43
+ dc = get_datacenter
44
+
45
+ src_folder = find_folder(get_config(:folder)) || dc.vmFolder
46
+
47
+ vm = find_in_folder(src_folder, RbVmomi::VIM::VirtualMachine, config[:vmname]) or
48
+ abort "VM not found"
49
+
50
+ puts "Marking VM #{vmname} as template"
51
+ vm.MarkAsTemplate()
52
+ puts "Finished marking VM #{vmname} as template"
53
+ end
54
+ end
@@ -0,0 +1,80 @@
1
+ #
2
+ # Author:: Raducu Deaconu (<rhadoo_io@yahoo.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_ovh_cloud_command'
7
+
8
+ # migrate vm to specified resource pool , datastore and host
9
+ class Chef::Knife::OvhCloudVmMigrate < Chef::Knife::BaseOvhCloudCommand
10
+ #migrate --resource-pool --dest-host --dest-datastore
11
+ banner "knife ovh cloud vm migrate (options)"
12
+
13
+ get_common_options
14
+
15
+ option :dest_host,
16
+ :long => "--dest-host HOST",
17
+ :description => "Destination host for the VM or template"
18
+
19
+ option :dest_datastore,
20
+ :long => "--dest-datastore DATASTORE",
21
+ :description => "The destination datastore"
22
+
23
+ option :priority,
24
+ :long => "--priority PRIORITY",
25
+ :description => "migration priority"
26
+
27
+ option :resource_pool,
28
+ :long => "--resource-pool POOL",
29
+ :description => "The resource pool into which to put the VM"
30
+
31
+
32
+
33
+ def traverse_folders_for_pool(folder, poolname)
34
+ children = folder.children.find_all
35
+ children.each do |child|
36
+ if child.class == RbVmomi::VIM::ClusterComputeResource || child.class == RbVmomi::VIM::ComputeResource || child.class == RbVmomi::VIM::ResourcePool
37
+ if child.name == poolname then return child end
38
+ elsif child.class == RbVmomi::VIM::Folder
39
+ pool = traverse_folders_for_pool(child, poolname)
40
+ if pool then return pool end
41
+ end
42
+ end
43
+ return false
44
+ end
45
+
46
+
47
+ def find_host_folder(folder, type, name)
48
+ folder.host.find { |o| o.name == name }
49
+ end
50
+
51
+ def run
52
+ $stdout.sync = true
53
+ vmname = @name_args[0]
54
+ if vmname.nil?
55
+ show_usage
56
+ fatal_exit("You must specify a virtual machine name")
57
+ end
58
+
59
+ vim = get_vim_connection
60
+ dc=get_datacenter
61
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
62
+
63
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
64
+ abort "VM #{vmname} not found"
65
+
66
+
67
+ priority=config[:priority]
68
+ dest_host=config[:dest_host]
69
+ ndc = find_datastore(config[:dest_datastore]) or abort "dest-datastore not found"
70
+ npool=find_pool(config[:resource_pool])
71
+ folderd = dc.hostFolder
72
+ pool = traverse_folders_for_pool(folderd,config[:resource_pool] ) or abort "Pool #{poolname} not found"
73
+ h=find_host_folder(pool, RbVmomi::VIM::HostSystem, dest_host)
74
+ migrate_spec = RbVmomi::VIM.VirtualMachineRelocateSpec(:datastore => ndc,:pool => npool,:host=>h)
75
+ #puts migrate_spec.host.name
76
+ vm.RelocateVM_Task(:spec => migrate_spec,:priority=>priority).wait_for_completion
77
+
78
+
79
+ end
80
+ end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Author:: Brian Dupras (<bdupras@rallydev.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_ovh_cloud_command'
7
+
8
+ # Lists all known virtual machines in the configured datacenter
9
+ class Chef::Knife::OvhCloudVmMove < Chef::Knife::BaseOvhCloudCommand
10
+
11
+ banner "knife ovh cloud vm move"
12
+
13
+ get_common_options
14
+
15
+ option :dest_name,
16
+ :long => "--dest-name NAME",
17
+ :short => "-r",
18
+ :description => "Destination name of the VM or template"
19
+
20
+ option :dest_folder,
21
+ :long => "--dest-folder FOLDER",
22
+ :description => "The destination folder into which the VM or template should be moved"
23
+
24
+
25
+ def run
26
+ $stdout.sync = true
27
+ vmname = @name_args[0]
28
+ if vmname.nil?
29
+ show_usage
30
+ fatal_exit("You must specify a virtual machine name")
31
+ end
32
+
33
+ vim = get_vim_connection
34
+ dcname = get_config(:vsphere_dc)
35
+ dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
36
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
37
+
38
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
39
+ abort "VM #{vmname} not found"
40
+
41
+ dest_name = config[:dest_name] || vmname
42
+ dest_folder = config[:dest_folder].nil? ? (vm.parent) : (find_folder(get_config(:dest_folder)))
43
+
44
+ vm.Rename_Task(:newName => dest_name).wait_for_completion unless vmname == dest_name
45
+ dest_folder.MoveIntoFolder_Task(:list => [vm]).wait_for_completion unless folder == dest_folder
46
+ end
47
+ end
@@ -0,0 +1,57 @@
1
+ #
2
+ # Author:: Raducu Deaconu (<rhadoo_io@yahoo.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_ovh_cloud_command'
7
+ # Switch VM networking state up/down (on all network interfaces)
8
+ class Chef::Knife::OvhCloudVmNet < Chef::Knife::BaseOvhCloudCommand
9
+ banner "knife ovh cloud vm net STATE VMNAME"
10
+ get_common_options
11
+ def run
12
+ $stdout.sync = true
13
+ vmname = @name_args[1]
14
+ if vmname.nil?
15
+ show_usage
16
+ fatal_exit("You must specify a virtual machine name")
17
+ end
18
+
19
+ state = @name_args[0]
20
+ if state.nil?
21
+ show_usage
22
+ fatal_exit("You must specify networking state up/down")
23
+ end
24
+
25
+ if state=="up"
26
+ if_state=true
27
+ elsif state=="down"
28
+ if_state=false
29
+ end
30
+ vim = get_vim_connection
31
+ dc=get_datacenter
32
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
33
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
34
+ abort "VM #{vmname} not found"
35
+ vm.config.hardware.device.each.grep(RbVmomi::VIM::VirtualEthernetCard) do |a|
36
+ backing=a.backing
37
+ key= a.key
38
+
39
+ puts "#{ui.color("Setting network adapter", :cyan)} :#{a.deviceInfo.label} on vlan :#{a.deviceInfo.summary} :#{state}"
40
+
41
+ conninfo=RbVmomi::VIM.VirtualDeviceConnectInfo(:startConnected => true,
42
+ :allowGuestControl => true,
43
+ :connected => if_state)
44
+
45
+ ndevice = RbVmomi::VIM.VirtualE1000(:key => key,
46
+ :backing => backing,
47
+ :connectable => conninfo)
48
+
49
+ device_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:operation => :edit,
50
+ :device => ndevice)
51
+
52
+ vmspec = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange =>[device_spec])
53
+
54
+ vm.ReconfigVM_Task(:spec => vmspec).wait_for_completion
55
+ end
56
+ end
57
+ end