Module: DLDInternet::AWS::EC2::Instance_Types::MixIns::EC2_Instance_Types

Included in:
DLDInternet::AWS::EC2::Instance_Types
Defined in:
lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb

Instance Method Summary collapse

Instance Method Details

#abort!(msg) ⇒ Object



82
83
84
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 82

def abort!(msg)
  raise DLDInternet::AWS::EC2::Instance_Types::Error.new msg
end

#getEC2_Instance_Types(mechanize = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 64

def getEC2_Instance_Types(mechanize=nil)
  unless mechanize
    require 'mechanize'
    mechanize = ::Mechanize.new
    mechanize.open_timeout = 5
    mechanize.read_timeout = 10
  end

  scraper = DLDInternet::AWS::EC2::Instance_Types::Scraper.new()

  begin
    return scraper.getInstanceTypes(:mechanize => mechanize)
  rescue Timeout::Error => e
    puts "Unable to retrieve instance type details in a reasonable time (#{mechanize.open_timeout}s). Giving up ...".light_red
    return nil
  end
end

#getFileFormat(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 14

def getFileFormat(path)
  format = case File.extname(File.basename(path)).downcase
             when /json|js/
               'json'
             when /yaml|yml/
               'yaml'
             else
               raise DLDInternet::AWS::EC2::Instance_Types::Error.new("Unsupported file type: #{path}")
           end
end

#loadEC2_Instance_Types(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 46

def loadEC2_Instance_Types(path)
  format = getFileFormat(path)
  spec = File.read(path)
  case format
    when /json/
      JSON.parse(spec)
    #when /yaml/
    else
      begin
        YAML.load(spec)
      rescue Psych::SyntaxError => e
        abort! "Error in the template specification: #{e.message}\n#{spec.split(/\n/).map{|l| "#{i+=1}: #{l}"}.join("\n")}"
      end
    # else
    #   abort! "Unsupported file type: #{path}"
  end
end

#saveEC2_Instance_Types(path, it) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 25

def saveEC2_Instance_Types(path,it)
  format = getFileFormat(path)
  begin
    File.open path, File::CREAT|File::TRUNC|File::RDWR, 0644 do |f|
      case format
        when /yaml/
          f.write it.to_yaml line_width: 1024, indentation: 4, canonical: false
        when /json/
          f.write JSON.pretty_generate(it, { indent: "\t", space: ' '})
        else
          f.write YAML::dump(it)
          # abort! "Unsupported save format #{format}!"
      end
      f.close
    end
  rescue
    abort! "!!! Could not write file #{path}: \nException: #{$!}\nParent directory exists? #{File.directory?(File.dirname(path))}\n"
  end
  0
end