Class: Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/dew/models/profile.rb

Constant Summary collapse

AWS_RESOURCES =
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'aws_resources.yaml'))
DEFAULT_RDS_STORAGE_SIZE =
5
DEFAULT_USERNAME =
'ubuntu'
TO_STRING_TEMPLATE =
"%{memory} GB memory, %{processor} ECUs processor, %{storage} GB storage, %{platform}-bit platform"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_name, init_yaml = nil) ⇒ Profile

Returns a new instance of Profile.



34
35
36
37
# File 'lib/dew/models/profile.rb', line 34

def initialize(profile_name, init_yaml = nil)
  @profile_name = profile_name
  populate_from_yaml(Cloud.region, init_yaml) if init_yaml
end

Instance Attribute Details

#amiObject

Returns the value of attribute ami.



6
7
8
# File 'lib/dew/models/profile.rb', line 6

def ami
  @ami
end

#countObject

Returns the value of attribute count.



6
7
8
# File 'lib/dew/models/profile.rb', line 6

def count
  @count
end

#elb_listener_portsObject

Returns the value of attribute elb_listener_ports.



7
8
9
# File 'lib/dew/models/profile.rb', line 7

def elb_listener_ports
  @elb_listener_ports
end

#instance_disk_sizeObject (readonly)

Returns the value of attribute instance_disk_size.



8
9
10
# File 'lib/dew/models/profile.rb', line 8

def instance_disk_size
  @instance_disk_size
end

#keypairObject

Returns the value of attribute keypair.



6
7
8
# File 'lib/dew/models/profile.rb', line 6

def keypair
  @keypair
end

#profile_nameObject (readonly)

Returns the value of attribute profile_name.



5
6
7
# File 'lib/dew/models/profile.rb', line 5

def profile_name
  @profile_name
end

#rds_sizeObject

Returns the value of attribute rds_size.



7
8
9
# File 'lib/dew/models/profile.rb', line 7

def rds_size
  @rds_size
end

#rds_storage_sizeObject

Returns the value of attribute rds_storage_size.



7
8
9
# File 'lib/dew/models/profile.rb', line 7

def rds_storage_size
  @rds_storage_size
end

#security_groupsObject

Returns the value of attribute security_groups.



6
7
8
# File 'lib/dew/models/profile.rb', line 6

def security_groups
  @security_groups
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/dew/models/profile.rb', line 6

def size
  @size
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/dew/models/profile.rb', line 7

def username
  @username
end

Class Method Details

.profile_path(profile_name) ⇒ Object



22
23
24
# File 'lib/dew/models/profile.rb', line 22

def self.profile_path(profile_name)
  File.join(ENV['HOME'], '.dew', 'profiles', "#{profile_name}.yaml")
end

.read(profile_name) ⇒ Object



15
16
17
18
19
20
# File 'lib/dew/models/profile.rb', line 15

def self.read(profile_name)
  new(
    profile_name,
    YAML.load_file(profile_path(profile_name))
  )
end

.size_to_s(size) ⇒ Object



64
65
66
67
68
# File 'lib/dew/models/profile.rb', line 64

def self.size_to_s(size)
  flavor = Cloud.compute.flavors.detect { |f| f.id == size }
  instance_hash = { :memory => flavor.ram.to_s, :processor => flavor.cores.to_s, :storage => flavor.disk.to_s, :platform => flavor.bits.to_s }
  instance_hash.inject(TO_STRING_TEMPLATE) { |res,(k,v)| res.gsub(/%\{#{k}\}/, v) }
end

Instance Method Details

#has_elb?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dew/models/profile.rb', line 26

def has_elb?
  elb_listener_ports != nil
end

#has_rds?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/dew/models/profile.rb', line 30

def has_rds?
  rds_size != nil
end

#instance_disk_size?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/dew/models/profile.rb', line 82

def instance_disk_size?
  !!@instance_disk_size
end

#populate_from_yaml(region, yaml) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dew/models/profile.rb', line 39

def populate_from_yaml(region, yaml)
  @username = DEFAULT_USERNAME    # do we actually need this?  There are no instances...

  if yaml['instances']
    @ami = yaml['instances'].fetch('amis', {})[region]
    @size = yaml['instances']['size']
    @security_groups = yaml['instances'].fetch('security-groups', ['default']) #TODO is this fallback tested?
    @keypair = yaml['instances']['keypair']
    @count = yaml['instances']['count'].to_i
    @username = yaml['instances'].fetch('username', DEFAULT_USERNAME) #TODO is this fallback tested?
    @instance_disk_size = yaml['instances'].fetch('disk-size', nil)
  end
  
  if yaml['elb']
    @elb_listener_ports = yaml['elb']['listener_ports']
  end
  
  if yaml['rds']
    @rds_size = yaml['rds']['size']
    @rds_storage_size = yaml['rds'].fetch('storage', DEFAULT_RDS_STORAGE_SIZE)
  end  
end

#to_sObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dew/models/profile.rb', line 70

def to_s
  db_instance_str = "%{memory} memory, %{processor} processor, %{platform} platform, %{io_performance} I/O performance"
  table { |t|
    t << [ "#{count} instance#{'s' if count > 1}", "#{size.inspect} (#{self.class.size_to_s(size)})"]
    t << ['disk image', ami.inspect]
    t << ['load balancer', "listener ports: #{elb_listener_ports.inspect}"] if has_elb?
    t << ['database', "#{rds_size.inspect} (#{rds_storage_size}Gb) (#{AWS_RESOURCES['db_instance_types'][rds_size].inject(db_instance_str) { |res,(k,v)| res.gsub(/%\{#{k}\}/, v) } })"] if has_rds?
    t << ['security groups', security_groups.inspect]
    t << ['keypair', keypair.inspect]
  }.to_s
end