Method: OpenNebula::VirtualMachine#save_as_template

Defined in:
lib/opennebula/virtual_machine.rb

#save_as_template(name, description, persistent = nil) ⇒ Object



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/opennebula/virtual_machine.rb', line 791

def save_as_template(name,description, persistent=nil)
    img_ids = []
    new_tid = nil
    begin
        rc = info()
        raise if OpenNebula.is_error?(rc)

        tid = self['TEMPLATE/TEMPLATE_ID']
        if tid.nil? || tid.empty?
            rc = Error.new('VM has no template to be saved')
            raise
        end

        if state_str() != "POWEROFF"
            rc = Error.new("VM state must be POWEROFF, "<<
                "current state is #{state_str()}, #{lcm_state_str()}")
            raise
        end

        # Clone the source template
        rc = OpenNebula::Template.new_with_id(tid, @client).clone(name)
        raise if OpenNebula.is_error?(rc)

        new_tid = rc

        # Replace the original template's capacity with the actual VM values
        replace = ""
        if !description.nil?
            replace << "DESCRIPTION = \"#{description}\"\n"
        end
        cpu = self['TEMPLATE/CPU']
        if !cpu.nil? && !cpu.empty?
            replace << "CPU = #{cpu}\n"
        end

        vcpu = self['TEMPLATE/VCPU']
        if !vcpu.nil? && !vcpu.empty?
            replace << "VCPU = #{vcpu}\n"
        end

        mem = self['TEMPLATE/MEMORY']
        if !mem.nil? && !mem.empty?
            replace << "MEMORY = #{mem}\n"
        end

        self.each('TEMPLATE/DISK') do |disk|
            # While the previous snapshot is still in progress, we wait
            # indefinitely
            rc = info()
            raise if OpenNebula.is_error?(rc)

            steps = 0
            while lcm_state_str() == "HOTPLUG_SAVEAS_POWEROFF"
                if steps < 30
                    sleep 1
                else
                    sleep 15
                end

                rc = info()
                raise if OpenNebula.is_error?(rc)

                steps += 1
            end

            # If the VM is not busy with a previous disk snapshot, we wait
            # but this time with a timeout
            rc = wait_state("POWEROFF")
            raise if OpenNebula.is_error?(rc)

            disk_id = disk["DISK_ID"]
            if disk_id.nil? || disk_id.empty?
                rc = Error.new('The DISK_ID is missing from the VM template')
                raise
            end

            image_id           = disk["IMAGE_ID"]
            opennebula_managed = disk["OPENNEBULA_MANAGED"]
            type               = disk["TYPE"]

            REMOVE_IMAGE_ATTRS.each do |attr|
                disk.delete_element(attr)
            end

            if !image_id.nil? && !image_id.empty?
                if type == 'CDROM'
                    replace << "DISK = [ IMAGE_ID = #{image_id}"
                    if opennebula_managed
                        replace << ", OPENNEBULA_MANAGED=#{opennebula_managed}"
                    end
                    replace << " ]\n"
                else
                    rc = disk_saveas(disk_id.to_i,"#{name}-disk-#{disk_id}","",-1)

                    raise if OpenNebula.is_error?(rc)

                    if persistent == true
                        OpenNebula::Image.new_with_id(rc.to_i, @client).persistent()
                    end

                    img_ids << rc.to_i

                    disk_template = disk.template_like_str(".").tr("\n", ",\n")

                    if disk_template.empty?
                        replace << "DISK = [ IMAGE_ID = #{rc} ] \n"
                    else
                        replace << "DISK = [ IMAGE_ID = #{rc}, " <<
                            disk_template << " ] \n"
                    end
                end
            else
                # Volatile disks cannot be saved, so the definition is copied
                replace << self.template_like_str(
                    "TEMPLATE", true, "DISK[DISK_ID=#{disk_id}]") << "\n"
            end
        end

        self.each('TEMPLATE/NIC') do |nic|
            nic_id = nic["NIC_ID"]

            if nic_id.nil? || nic_id.empty?
                rc = Error.new('The NIC_ID is missing from the VM template')
                raise
            end

            REMOVE_VNET_ATTRS.each do |attr|
                nic.delete_element(attr)
            end

            replace << "NIC = [ " << nic.template_like_str(".").tr("\n", ",\n") << " ] \n"
        end

        # Required by the Sunstone Cloud View
        replace << "SAVED_TEMPLATE_ID = #{tid}\n"

        new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)

        rc = new_tmpl.update(replace, true)
        raise if OpenNebula.is_error?(rc)

        return new_tid

    rescue
        # Rollback. Delete the template and the images created
        if !new_tid.nil?
            new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)
            new_tmpl.delete()
        end

        img_ids.each do |id|
            img = OpenNebula::Image.new_with_id(id, @client)
            img.delete()
        end

        return rc
    end
end