Class: Bosh::Director::Api::SnapshotManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/api/snapshot_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_snapshots(snapshots, options = {}) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/bosh/director/api/snapshot_manager.rb', line 57

def self.delete_snapshots(snapshots, options={})
  keep_snapshots_in_the_cloud = options.fetch(:keep_snapshots_in_the_cloud, false)
  snapshots.each do |snapshot|
    Config.cloud.delete_snapshot(snapshot.snapshot_cid) unless keep_snapshots_in_the_cloud
    snapshot.delete
  end
end

.take_snapshot(instance, options = {}) ⇒ Object



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
# File 'lib/bosh/director/api/snapshot_manager.rb', line 65

def self.take_snapshot(instance, options={})
  unless Config.enable_snapshots
    Config.logger.info('Snapshots are disabled; skipping')
    return []
  end

  clean = options.fetch(:clean, false)
  snapshot_cids = []
   = {
      deployment: instance.deployment.name,
      job: instance.job,
      index: instance.index,
      director_name: Config.name,
      director_uuid: Config.uuid,
      agent_id: instance.agent_id
  }

  instance.persistent_disks.each do |disk|
    cid = Config.cloud.snapshot_disk(disk.disk_cid, )
    snapshot = Models::Snapshot.new(persistent_disk: disk, snapshot_cid: cid, clean: clean)
    snapshot.save
    snapshot_cids << snapshot.snapshot_cid
  end

  snapshot_cids

rescue Bosh::Clouds::NotImplemented
  Config.logger.info('CPI does not support disk snapshots; skipping')
  []
end

Instance Method Details

#create_deployment_snapshot_task(username, deployment, options = {}) ⇒ Object



4
5
6
# File 'lib/bosh/director/api/snapshot_manager.rb', line 4

def create_deployment_snapshot_task(username, deployment, options = {})
  JobQueue.new.enqueue(username, Jobs::SnapshotDeployment, 'snapshot deployment', [deployment.name, options], deployment)
end

#create_snapshot_task(username, instance, options) ⇒ Object



8
9
10
# File 'lib/bosh/director/api/snapshot_manager.rb', line 8

def create_snapshot_task(username, instance, options)
  JobQueue.new.enqueue(username, Jobs::CreateSnapshot, 'create snapshot', [instance.id, options])
end

#delete_deployment_snapshots_task(username, deployment) ⇒ Object



12
13
14
# File 'lib/bosh/director/api/snapshot_manager.rb', line 12

def delete_deployment_snapshots_task(username, deployment)
  JobQueue.new.enqueue(username, Jobs::DeleteDeploymentSnapshots, 'delete deployment snapshots', [deployment.name], deployment)
end

#delete_snapshots_task(username, snapshot_cids) ⇒ Object



16
17
18
# File 'lib/bosh/director/api/snapshot_manager.rb', line 16

def delete_snapshots_task(username, snapshot_cids)
  JobQueue.new.enqueue(username, Jobs::DeleteSnapshots, 'delete snapshot', [snapshot_cids])
end

#find_by_cid(deployment, snapshot_cid) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
# File 'lib/bosh/director/api/snapshot_manager.rb', line 20

def find_by_cid(deployment, snapshot_cid)
  snapshot = Models::Snapshot.find(snapshot_cid: snapshot_cid)
  raise SnapshotNotFound, "snapshot #{snapshot_cid} not found" unless snapshot
  unless deployment == snapshot.persistent_disk.instance.deployment
    raise SnapshotNotFound, "snapshot #{snapshot_cid} not found in deployment #{deployment.name}"
  end
  snapshot
end

#snapshots(deployment, job = nil, index_or_id = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bosh/director/api/snapshot_manager.rb', line 29

def snapshots(deployment, job=nil, index_or_id=nil)
  filter = { deployment: deployment }
  filter[:job] = job if job
  if index_or_id
    filter_key = index_or_id.to_s =~ /^\d+$/ ? :index : :uuid
    filter[filter_key] = index_or_id
  end
  result = []
  instances = Models::Instance.filter(filter).all

  instances.each do |instance|
    instance.persistent_disks.each do |disk|
      disk.snapshots.each do |snapshot|
        result << {
            'job' => instance.job,
            'index' => instance.index,
            'uuid' => instance.uuid,
            'snapshot_cid' => snapshot.snapshot_cid,
            'created_at' => snapshot.created_at.to_s,
            'clean' => snapshot.clean
        }
      end
    end
  end

  result
end