Class: RightImageTools::S3HtmlIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_html_indexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket, id = nil, key = nil) ⇒ S3HtmlIndexer

Returns a new instance of S3HtmlIndexer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/s3_html_indexer.rb', line 10

def initialize(bucket, id=nil, key=nil)
  @s3 = RightAws::S3.new(id, key)
  @dev_bucket = bucket.downcase.include?("dev")
  
  # If the bucket holds base images.
  @base_bucket = bucket.downcase.include?("rightimage-base")

  # If the bucket holds reports for the public clouds, ec2, Google, or Azure.
  @public_bucket = bucket.downcase =~ /ec2|google|azure/

  @cloudstack_bucket = bucket.downcase.include?("cloudstack")
  @bucket = @s3.bucket(bucket)
  @keys = @bucket.keys
  @auto_hash = Hash.new{ |h,k| h[k] = Hash.new &h.default_proc }
end

Instance Method Details

#to_html(filename) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/s3_html_indexer.rb', line 26

def to_html(filename)
  # Create hash hierarchy from bucket list
  #@keys.sort! {|x,y| ver(x.full_name) <=> ver(y.full_name) }  #TODO: sort by rightlink version
  @keys.each do |path| 
    
    next unless is_public?(path)

    path_key = path.full_name
    # Ignore index.html and report_viewer.html .
    next if path_key =~ /index.html/
    next if path_key =~ /report_viewer.html/
    path_key.gsub!('xen/','xenserver/')
    path_key.gsub!(/(vmware|esxi)\/(vmware|esxi)\//,'esxi/')
    path_key.gsub!('vmware/','esxi/')

    #if path_key.split("/").size != 5
    #  puts "WARNING: Skipping malformed path: #{path}"
    #  next
    #end

    # Skip files if they are too small to be an image.
    # Except for mega cloud buckets that collect reports.
    if path.size.to_i < 1024*1024*100 and not @public_bucket
      puts "WARNING: Skipping #{path}: it appears to be too small (<100 MB) to be an image"
      next
    end
    
    sub = @auto_hash
    path_key.split( "/" ).each do |dir| 
      sub = sub[dir] 
    end
    
    # Retrieve image and report links duple.
    links = fix_public_endpoint(path.public_link)

    sub[:link] = links[0]
    sub[:report] = links[1]
    sub[:size] = path.size
    sub[:date] = path.last_modified.strftime("%m/%d/%Y")
    sub[:md5sum] = path.e_tag.gsub(/\"/, "")
    
  end

  # make a non-auto hash copy
  dirs = Hash.new
  dirs = dirs.merge(@auto_hash)

  # visualize hierarchy
  dirs.each do |k,v|
    @file = File.open(filename, "w")
    output("<html>")
    add_style
    display_dirs(k,v)
    output("</html>")
    puts "Output written to #{@file.path}"
    @file.close
  end
end

#upload_index(filename) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/s3_html_indexer.rb', line 85

def upload_index(filename)
  # 
  if File.exists?("index.html")
    @bucket.put("index.html", File.open(filename), {}, 'public-read')
    File.delete("index.html")

    # Upload amalgamated report_viewer.html with index.html.
    Dir.chdir(File.join(File.dirname(File.expand_path(__FILE__)), "..", "ui", "build"))
    @bucket.put("report_viewer.html", File.open("report_viewer.html"), {}, 'public-read')
  else
    # Upload index.html for empty buckets.
    Dir.chdir(File.join(File.dirname(File.expand_path(__FILE__)), "..", "ui", "build"))
    @bucket.put("index.html", File.open("empty_bucket_index.html"), {}, 'public-read')
    puts "Uploaded empty bucket index.html."
  end
end