Class: Cumulus::EC2::EbsManager

Inherits:
Common::Manager show all
Defined in:
lib/ec2/managers/EbsManager.rb

Defined Under Namespace

Classes: EbsMigrationData

Instance Method Summary collapse

Methods inherited from Common::Manager

#diff, #diff_one, #filter_local, #list, #sync, #sync_one

Constructor Details

#initializeEbsManager

Returns a new instance of EbsManager.



15
16
17
18
19
# File 'lib/ec2/managers/EbsManager.rb', line 15

def initialize
  super()
  @create_asset = true
  @client = Aws::EC2::Client.new(Configuration.instance.client)
end

Instance Method Details

#added_diff(local) ⇒ Object



37
38
39
# File 'lib/ec2/managers/EbsManager.rb', line 37

def added_diff(local)
  EbsGroupDiff.added(local)
end

#aws_resourcesObject



29
30
31
# File 'lib/ec2/managers/EbsManager.rb', line 29

def aws_resources
  @aws_resources ||= EC2::group_ebs_volumes
end

#create(local) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/ec2/managers/EbsManager.rb', line 99

def create(local)
  local.volume_groups.each do |vg|
    vg.count.times do
      create_volume(vg, local.availability_zone, local.name)
    end
  end
end

#diff_resource(local, aws) ⇒ Object



41
42
43
# File 'lib/ec2/managers/EbsManager.rb', line 41

def diff_resource(local, aws)
  local.diff(aws)
end

#local_resourcesObject



25
26
27
# File 'lib/ec2/managers/EbsManager.rb', line 25

def local_resources
  @local_resources ||= Hash[EbsLoader.groups.map { |local| [local.name, local] }]
end

#migrateObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ec2/managers/EbsManager.rb', line 46

def migrate
  puts Colors.blue("Migrating EBS Volume Groups...")

  # Create the directories
  ec2_dir = "#{@migration_root}/ec2"
  ebs_dir = "#{ec2_dir}/ebs"

  if !Dir.exists?(@migration_root)
    Dir.mkdir(@migration_root)
  end
  if !Dir.exists?(ec2_dir)
    Dir.mkdir(ec2_dir)
  end
  if !Dir.exists?(ebs_dir)
    Dir.mkdir(ebs_dir)
  end

  puts "Would you like Cumulus to automatically update your volumes to have a Group tag that matches the name of the instance they are attached to? (y/n)"
  update_tags = (STDIN.getc.downcase[0] == "y")

  # Migrate any volumes that have already been grouped
  vol_groups = Hash[EC2.group_ebs_volumes.map { |group_name, cumulus_group| [group_name, EbsMigrationData.new(cumulus_group, nil)] }]

  # Use the instance name to group volumes if they do not have a group.  Anything
  # not attached should not get migrated
  migratable_vols = EC2.ebs_volumes.select { |vol| vol.group.nil? and !vol.attachments.empty? and vol.attached? }
  instance_grouped = migratable_vols.group_by do |vol|
    attachments = vol.attachments.select { |att| att.state == "attached" || att.state == "attaching" }
    attachments.first.instance_id
  end

  instance_grouped.each do |instance_id, vols|
    instance_name = EC2.id_instances[instance_id].name || instance_id
    vol_groups[instance_name] = EbsMigrationData.new(EbsGroupConfig.new(instance_name).populate!(vols), vols)
  end

  vol_groups.each do |group_name, data|
    puts "Migrating group #{group_name}"

    if update_tags and data.set_group_vols
      data.set_group_vols.each do |vol|
        if vol.group.nil?
          set_group_name(vol.volume_id, group_name)
        end
      end
    end

    json = JSON.pretty_generate(data.cumulus_group.to_hash)
    File.open("#{ebs_dir}/#{group_name}.json", "w") { |f| f.write(json) }
  end

end

#resource_nameObject



21
22
23
# File 'lib/ec2/managers/EbsManager.rb', line 21

def resource_name
  "EBS Volume Group"
end

#unmanaged_diff(aws) ⇒ Object



33
34
35
# File 'lib/ec2/managers/EbsManager.rb', line 33

def unmanaged_diff(aws)
  EbsGroupDiff.unmanaged(aws)
end

#update(local, diffs) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ec2/managers/EbsManager.rb', line 107

def update(local, diffs)

  # If they tried to update AZ, use the old value
  availability_zone = (diffs.select { |d| d.type == EbsGroupChange::AZ }.first.aws rescue local.availability_zone)

  diffs.each do |diff|
    case diff.type
    when EbsGroupChange::AZ
      puts Colors.blue("Availability zone cannot be updated")
    when EbsGroupChange::VG_REMOVED
      puts Colors.blue("Cumulus does not delete or detach volumes. Manually update #{diff.local.description}")
    when EbsGroupChange::VG_ADDED
      added_vg = diff.local
      puts Colors.blue("Creating #{added_vg.count} x #{added_vg.description}...")
      added_vg.count.times do
        create_volume(added_vg, availability_zone, local.name)
      end
    when EbsGroupChange::VG_COUNT
      if diff.local.count < diff.aws.count
        puts Colors.blue("Cumulus will not delete or detach volumes. Manually update #{diff.local.description}")
      else
        num_added = diff.local.count - diff.aws.count
        puts Colors.blue("Adding #{num_added} x #{diff.local.description}...")
        num_added.times do
          create_volume(diff.local, availability_zone, local.name)
        end
      end
    end
  end
end