Statistics
| Branch: | Tag: | Revision:

one / src / vmm_mad / remotes / lib / vcenter_driver / file_helper.rb @ 95fc9267

History | View | Annotate | Download (3.8 KB)

1
require 'fileutils'
2
require 'tempfile'
3

    
4
module VCenterDriver
5

    
6
class FileHelper
7

    
8
    def self.get_img_name(disk, vm_id, vm_name, instantiate_as_persistent=false)
9
        if disk["PERSISTENT"] == "YES" || disk["TYPE"] == "CDROM"
10
            return disk["SOURCE"]
11
        else
12
            disk_id  = disk["DISK_ID"]
13
            if disk["SOURCE"]
14
                if instantiate_as_persistent &&
15
                   disk["OPENNEBULA_MANAGED"] &&
16
                   disk["OPENNEBULA_MANAGED"].upcase == "NO"
17
                    return disk["SOURCE"] # Treat this disk as if was persistent
18
                else
19
                    image_name = disk["SOURCE"].split(".").first
20
                    return "#{image_name}-#{vm_id}-#{disk_id}.vmdk"
21
                end
22
            else
23
                ds_volatile_dir  = disk["VCENTER_DS_VOLATILE_DIR"] || "one-volatile"
24
                return "#{ds_volatile_dir}/#{vm_id}/one-#{vm_id}-#{disk_id}.vmdk"
25
            end
26
        end
27
    end
28

    
29
    def self.get_img_name_from_path(path, vm_id, disk_id)
30
        return "#{path.split(".").first}-#{vm_id}-#{disk_id}.vmdk"
31
    end
32

    
33
    def self.is_remote_or_needs_unpack?(file)
34
        return is_remote?(file) || needs_unpack?(file)
35
    end
36

    
37
    def self.is_remote?(file)
38
        file.match(%r{^https?://})
39
    end
40

    
41
    def self.is_vmdk?(file)
42
        type = %x{file #{file}}
43

    
44
        type.include? "VMware"
45
    end
46

    
47
    def self.is_iso?(file)
48
        type = %x{file #{file}}
49

    
50
        type.include? "ISO"
51
    end
52

    
53
    def self.get_type(file)
54
        type = %x{file -b --mime-type #{file}}
55
        if $?.exitstatus != 0
56
            STDERR.puts "Can not read file #{file}"
57
            exit(-1)
58
        end
59
        type.strip
60
    end
61

    
62
    def self.needs_unpack?(file_path)
63
        type = get_type(file_path)
64
        type.gsub!(%r{^application/(x-)?}, '')
65
        return %w{bzip2 gzip tar}.include?(type)
66
    end
67

    
68
    def self.vcenter_file_info(file_path)
69
        if File.directory?(file_path)
70
            files = Dir["#{file_path}/*.vmdk"]
71
            found = false
72
            count = 0
73
            last  = nil
74

    
75
            files.each do |f|
76
                if get_type(f).strip == "text/plain"
77
                    file_path = f
78
                    found = true
79
                    break
80
                else
81
                    count += 1
82
                    last = f
83
                end
84
            end
85

    
86
            if !found
87
                if count == 1
88
                    file_path = last
89
                    found = true
90
                else
91
                    STDERR.puts "Could not find vmdk"
92
                    exit(-1)
93
                end
94
            end
95
        end
96

    
97
        case get_type(file_path).strip
98
        when "application/octet-stream"
99
            return {
100
                :type   => :standalone,
101
                :file   => file_path,
102
                :dir    => File.dirname(file_path)
103
            }
104
        when "application/x-iso9660-image"
105
            return {
106
                :type   => :standalone,
107
                :file   => file_path,
108
                :dir    => File.dirname(file_path),
109
                :extension => '.iso'
110
            }
111
        when "text/plain"
112
            info = {
113
                :type   => :flat,
114
                :file   => file_path,
115
                :dir    => File.dirname(file_path)
116
            }
117

    
118
            files_list = []
119
            descriptor = File.read(file_path).split("\n")
120
            flat_files = descriptor.select {|l| l.start_with?("RW")}
121

    
122
            flat_files.each do |f|
123
                files_list << info[:dir] + "/" +
124
                    f.split(" ")[3].chomp.chomp('"').reverse.chomp('"').reverse
125
            end
126

    
127
            info[:flat_files] = files_list
128

    
129
            return info
130
        else
131
            STDERR.puts "Unrecognized file type"
132
            exit(-1)
133
        end
134
    end
135

    
136
end # class FileHelper
137

    
138
end # module VCenterDriver