Class: Sahara::Session::Libvirt
- Inherits:
-
Object
- Object
- Sahara::Session::Libvirt
- Defined in:
- lib/sahara/session/libvirt.rb
Instance Method Summary collapse
- #commit ⇒ Object
-
#connect_to_libvirt ⇒ Object
based on VagrantPlugins::ProviderLibvirt::Action::ConnectLibvirt.
- #get_domain ⇒ Object
- #get_snapshot_if_exists ⇒ Object
-
#initialize(machine) ⇒ Libvirt
constructor
A new instance of Libvirt.
- #is_snapshot_mode_on? ⇒ Boolean
- #is_vm_created? ⇒ Boolean
- #off ⇒ Object
- #on ⇒ Object
- #rollback ⇒ Object
Constructor Details
#initialize(machine) ⇒ Libvirt
Returns a new instance of Libvirt.
7 8 9 10 11 12 |
# File 'lib/sahara/session/libvirt.rb', line 7 def initialize(machine) @machine=machine @sandboxname="sahara-sandbox" @connection=connect_to_libvirt @domain=get_domain end |
Instance Method Details
#commit ⇒ Object
113 114 115 116 |
# File 'lib/sahara/session/libvirt.rb', line 113 def commit off on end |
#connect_to_libvirt ⇒ Object
based on VagrantPlugins::ProviderLibvirt::Action::ConnectLibvirt
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sahara/session/libvirt.rb', line 15 def connect_to_libvirt # Get config options for libvirt provider. config = @machine.provider_config # Setup connection uri. uri = config.driver if uri == "kvm" uri = "qemu" end if config.connect_via_ssh uri << '+ssh://' if config.username uri << config.username + '@' end if config.host uri << config.host else uri << 'localhost' end else uri << '://' uri << config.host if config.host end uri << '/system?no_verify=1' # set ssh key for access to libvirt host home_dir = `echo ${HOME}`.chomp uri << "&keyfile=#{home_dir}/.ssh/id_rsa" conn_attr = {} conn_attr[:provider] = 'libvirt' conn_attr[:libvirt_uri] = uri conn_attr[:libvirt_username] = config.username if config.username conn_attr[:libvirt_password] = config.password if config.password begin Fog::Compute.new(conn_attr) rescue Fog::Errors::Error => e raise Sahara::Errors::LibvirtConnectionError, :error_message => e. end end |
#get_domain ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/sahara/session/libvirt.rb', line 60 def get_domain if is_vm_created? return @connection.client.lookup_domain_by_uuid(@machine.id) else return nil end end |
#get_snapshot_if_exists ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sahara/session/libvirt.rb', line 68 def get_snapshot_if_exists # if we can get snapshot description without exception it exists begin snapshot = @domain.lookup_snapshot_by_name(@sandboxname) snapshot_desc = snapshot.xml_desc rescue raise Sahara::Errors::SnapshotMissing end return snapshot end |
#is_snapshot_mode_on? ⇒ Boolean
79 80 81 82 83 84 85 86 |
# File 'lib/sahara/session/libvirt.rb', line 79 def is_snapshot_mode_on? begin snapshot = get_snapshot_if_exists rescue Sahara::Errors::SnapshotMissing return false end return true end |
#is_vm_created? ⇒ Boolean
130 131 132 |
# File 'lib/sahara/session/libvirt.rb', line 130 def is_vm_created? return !@machine.id.nil? end |
#off ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/sahara/session/libvirt.rb', line 88 def off snapshot = get_snapshot_if_exists begin snapshot.delete rescue Fog::Errors::Error => e raise Sahara::Errors::SnapshotDeletionError, :error_message => e. end end |
#on ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sahara/session/libvirt.rb', line 98 def on snapshot_desc = <<-EOF <domainsnapshot> <name>sahara-sandbox</name> <description>Snapshot for vagrant sandbox</description> </domainsnapshot> EOF begin @domain.snapshot_create_xml(snapshot_desc) rescue Fog::Errors::Error => e raise Sahara::Errors::SnapshotCreationError, :error_message => e. end end |
#rollback ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sahara/session/libvirt.rb', line 118 def rollback snapshot = get_snapshot_if_exists begin # 4 is VIR_DOMAIN_SNAPSHOT_REVERT_FORCE # needed due to https://bugzilla.redhat.com/show_bug.cgi?id=1006886 @domain.revert_to_snapshot(snapshot, 4) rescue Fog::Errors::Error => e raise Sahara::Errors::SnapshotReversionError, :error_message => e. end end |