Class: Drebs::Cloud

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Cloud

Returns a new instance of Cloud.



6
7
8
# File 'lib/drebs/cloud.rb', line 6

def initialize(config)
  @config = config
end

Instance Method Details

#check_cloudObject



10
11
12
13
# File 'lib/drebs/cloud.rb', line 10

def check_cloud
  ec2
  find_local_instance
end

#create_local_snapshot(pre_snapshot_tasks, post_snapshot_tasks, mount_point) ⇒ Object



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
# File 'lib/drebs/cloud.rb', line 51

def create_local_snapshot(pre_snapshot_tasks, post_snapshot_tasks, mount_point)
  local_instance=find_local_instance
  ip = local_instance[:ip_address]
  instance_id = local_instance[:aws_instance_id]
  instance_tags = ec2.describe_tags(:filters => {"resource-id" => instance_id})
  instance_name_tag = instance_tags.find{|t| t[:key] == "Name"}
  instance_desc = instance_name_tag.nil? ? ip : instance_name_tag[:value]
  volume_id = local_instance[:block_device_mappings].select{|m| m[:device_name]==mount_point}.first[:ebs_volume_id]
  timestamp = DateTime.now.strftime("%Y%m%d%H%M%S")
  return nil if not ebs = find_local_ebs(mount_point)
  pre_snapshot_tasks.each do |task|
    result, stdout, stderr = systemu(task)
    unless result.exitstatus == 0
      raise Exception.new(
        "Error while executing pre-snapshot task: #{task} on #{instance_desc}:#{mount_point} #{instance_id}:#{volume_id} at #{timestamp}"
      )
    end
  end if pre_snapshot_tasks
  snapshot = ec2.create_snapshot(ebs[:ebs_volume_id], "DREBS #{instance_desc}:#{mount_point} #{instance_id}:#{volume_id} at #{timestamp}")
  Thread.new(snapshot[:aws_id], post_snapshot_tasks) do |snapshot_id, post_snapshot_tasks|
    1.upto(500) do |a|
      sleep(3)
      break if get_snapshot(snapshot_id)[:aws_status] == 'completed'
    end
    post_snapshot_tasks.each do |task|
      result = systemu(task)
      unless result.exitstatus == 0
        raise Exception.new(
          "Error while executing post-snapshot task: #{task} on #{instance_desc}:#{mount_point} #{instance_id}:#{volume_id} at #{timestamp}"
        )
      end
    end if post_snapshot_tasks
  end
  return snapshot
end

#ec2Object



15
16
17
18
19
20
# File 'lib/drebs/cloud.rb', line 15

def ec2
  key_id = @config["aws_access_key_id"]
  key = @config["aws_secret_access_key"]
  region = @config["region"]
  return RightAws::Ec2.new(key_id, key, {:region=>region})
end

#find_local_ebs(mount_point) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/drebs/cloud.rb', line 31

def find_local_ebs(mount_point)
  return nil if not local_instance = find_local_instance
  local_instance[:block_device_mappings].each do |volume|
    return volume if volume[:device_name] == mount_point
  end
  return nil
end

#find_local_instanceObject



22
23
24
25
26
27
28
29
# File 'lib/drebs/cloud.rb', line 22

def find_local_instance
  #find a better way... right-aws?
  private_ip = UDPSocket.open{|s| s.connect("8.8.8.8", 1); s.addr.last}
  ec2.describe_instances.each do |instance|
    return instance if instance[:private_ip_address] == private_ip
  end
  return nil
end

#find_local_snapshots(mount_point) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/drebs/cloud.rb', line 87

def find_local_snapshots(mount_point)
  return nil if not ebs = find_local_ebs(mount_point)
  snapshots = []
  ec2.describe_snapshots.each {|snapshot|
    snapshots.push(snapshot) if snapshot[:aws_volume_id] == ebs[:ebs_volume_id]
  }
  return snapshots
end

#get_snapshot(snapshot_id) ⇒ Object



45
46
47
48
49
# File 'lib/drebs/cloud.rb', line 45

def get_snapshot(snapshot_id)
  ec2.describe_snapshots {|a_snapshot|
    return a_snapshot if a_snapshot[:aws_id] == snapshot_id
  }
end

#local_ebs_idsObject



39
40
41
42
43
# File 'lib/drebs/cloud.rb', line 39

def local_ebs_ids
  @ebs_ids ||= find_local_instance[:block_device_mappings].map do |volume| 
    volume[:ebs_volume_id]
  end rescue nil
end