Class: Judo::Snapshot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, server_id) ⇒ Snapshot

Returns a new instance of Snapshot.



5
6
7
8
9
# File 'lib/judo/snapshot.rb', line 5

def initialize(base, name, server_id)
  @base = base
  @name = name
  @server_id = server_id
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/judo/snapshot.rb', line 3

def name
  @name
end

#server_idObject

Returns the value of attribute server_id.



3
4
5
# File 'lib/judo/snapshot.rb', line 3

def server_id
  @server_id
end

Instance Method Details

#animate(new_server_name) ⇒ Object

Raises:



71
72
73
74
75
76
# File 'lib/judo/snapshot.rb', line 71

def animate(new_server_name)
  raise JudoError, "cannot animate, snapshotting not complete" unless completed?
  @base.create_server(new_server_name, group_name,
    :version => version, :snapshots => devs, :virgin => virgin,
    :metadata =>  , :clone => name, :instance_type => server.instance_type)
end

#completed?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/judo/snapshot.rb', line 108

def completed?
  not ec2_data.detect { |s| s[:aws_status] != "completed" }
end

#createObject

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/judo/snapshot.rb', line 52

def create
  raise JudoError,"snapshot already exists" unless state.empty?
  raise JudoError,"server has no disks to clone: #{server.volumes}" if server.volumes.empty?
  @base.task("Snapshotting #{server.name}") do
    devs = server.volumes.map do |dev,vol|
      "#{dev}:#{@base.ec2.create_snapshot(vol)[:aws_id]}"
    end
    @base.sdb.put_attributes(@base.snapshot_domain, name, {
      "version" => server.version,
      "virgin" => server.virgin?,
      "metadata_json" => server.get("metadata_json"),
      "devs" => devs,
      "server" => server.id,
      "group" => server.group.name,
      "created_at" => Time.now.to_i.to_s
    }, :replace)
  end
end

#created_atObject



31
32
33
# File 'lib/judo/snapshot.rb', line 31

def created_at
  Time.at(get("created_at").to_i)
end

#deleteObject

Raises:



78
79
80
81
# File 'lib/judo/snapshot.rb', line 78

def delete
  raise JudoError, "cannot delete, snapshotting not complete" unless completed?
  @base.sdb.delete_attributes(@base.snapshot_domain, name)
end

#destroyObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/judo/snapshot.rb', line 87

def destroy
  devs.each do |dev,snapshot_id|
    @base.task("Deleting snapshot #{snapshot_id}") do
      begin
        @base.ec2.delete_snapshot(snapshot_id)
      rescue Object => e
        puts "Error destrotying snapshot #{e.message}"
      end
    end
  end
  delete
end

#devsObject



48
49
50
# File 'lib/judo/snapshot.rb', line 48

def devs
  (state["devs"] || []).inject({}) { |out, kv| k, v = kv.split(':'); out[k] = v; out }
end

#ec2_dataObject



104
105
106
# File 'lib/judo/snapshot.rb', line 104

def ec2_data
  @base.ec2_snapshots.select { |s| ec2_ids.include? s[:aws_id] }
end

#ec2_idsObject



100
101
102
# File 'lib/judo/snapshot.rb', line 100

def ec2_ids
  devs.values
end

#fetch_stateObject



19
20
21
# File 'lib/judo/snapshot.rb', line 19

def fetch_state
  @base.sdb.get_attributes(@base.snapshot_domain, name)[:attributes]
end

#get(key) ⇒ Object



83
84
85
# File 'lib/judo/snapshot.rb', line 83

def get(key)
  state[key] && [state[key]].flatten.first
end

#groupObject



125
126
127
# File 'lib/judo/snapshot.rb', line 125

def group
  @group ||= @base.groups.detect { |g| g.name == group_name }
end

#group_nameObject



27
28
29
# File 'lib/judo/snapshot.rb', line 27

def group_name
  get("group")
end

#metadataObject



39
40
41
42
# File 'lib/judo/snapshot.rb', line 39

def 
   = get("metadata_json")
  JSON.load() if 
end

#progressObject



112
113
114
115
# File 'lib/judo/snapshot.rb', line 112

def progress
  min_percent = ec2_data.map { |s| s[:aws_progress].to_i }.min
  "#{min_percent}%"
end

#serverObject



15
16
17
# File 'lib/judo/snapshot.rb', line 15

def server
  @server ||= @base.servers.detect { |s| s.id == server_id }
end

#server_nameObject



11
12
13
# File 'lib/judo/snapshot.rb', line 11

def server_name
  server.name rescue '(deleted)'
end

#size(snap_id) ⇒ Object



117
118
119
# File 'lib/judo/snapshot.rb', line 117

def size(snap_id)
  @base.ec2_snapshots.detect { |s| s[:aws_id] == snap_id }
end

#stateObject



23
24
25
# File 'lib/judo/snapshot.rb', line 23

def state
  @base.snapshots_state[name] ||= fetch_state
end

#versionObject



35
36
37
# File 'lib/judo/snapshot.rb', line 35

def version
  get("version").to_i
end

#version_descObject



121
122
123
# File 'lib/judo/snapshot.rb', line 121

def version_desc
  group.version_desc(version)
end

#virginObject



44
45
46
# File 'lib/judo/snapshot.rb', line 44

def virgin
  get("virgin").to_s == "true"
end