bosh-director 1.5.0.pre.1113

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. data/CHANGELOG +34 -0
  2. data/bin/bosh-director +36 -0
  3. data/bin/bosh-director-console +84 -0
  4. data/bin/bosh-director-drain-workers +42 -0
  5. data/bin/bosh-director-migrate +58 -0
  6. data/bin/bosh-director-scheduler +27 -0
  7. data/bin/bosh-director-worker +76 -0
  8. data/db/migrations/README +1 -0
  9. data/db/migrations/director/20110209010747_initial.rb +118 -0
  10. data/db/migrations/director/20110406055800_add_task_user.rb +9 -0
  11. data/db/migrations/director/20110518225809_remove_cid_constrain.rb +13 -0
  12. data/db/migrations/director/20110617211923_add_deployments_release_versions.rb +32 -0
  13. data/db/migrations/director/20110622212607_add_task_checkpoint_timestamp.rb +9 -0
  14. data/db/migrations/director/20110628023039_add_state_to_instances.rb +21 -0
  15. data/db/migrations/director/20110709012332_add_disk_size_to_instances.rb +9 -0
  16. data/db/migrations/director/20110906183441_add_log_bundles.rb +11 -0
  17. data/db/migrations/director/20110907194830_add_logs_json_to_templates.rb +9 -0
  18. data/db/migrations/director/20110915205610_add_persistent_disks.rb +51 -0
  19. data/db/migrations/director/20111005180929_add_properties.rb +14 -0
  20. data/db/migrations/director/20111110024617_add_deployment_problems.rb +24 -0
  21. data/db/migrations/director/20111216214145_recreate_support_for_vms.rb +9 -0
  22. data/db/migrations/director/20120102084027_add_credentials_to_vms.rb +7 -0
  23. data/db/migrations/director/20120427235217_allow_multiple_releases_per_deployment.rb +36 -0
  24. data/db/migrations/director/20120524175805_add_task_type.rb +44 -0
  25. data/db/migrations/director/20120614001930_delete_redundant_deployment_release_relation.rb +34 -0
  26. data/db/migrations/director/20120822004528_add_fingerprint_to_templates_and_packages.rb +17 -0
  27. data/db/migrations/director/20120830191244_add_properties_to_templates.rb +9 -0
  28. data/db/migrations/director/20121106190739_persist_vm_env.rb +9 -0
  29. data/db/migrations/director/20130222232131_add_sha1_to_stemcells.rb +9 -0
  30. data/db/migrations/director/20130312211407_add_commit_hash_to_release_versions.rb +19 -0
  31. data/db/migrations/director/20130409235338_snapshot.rb +15 -0
  32. data/db/migrations/director/20130530164918_add_paused_flag_to_instance.rb +14 -0
  33. data/db/migrations/director/20130531172604_add_director_attributes.rb +13 -0
  34. data/db/migrations/dns/20120123234908_initial.rb +27 -0
  35. data/lib/bosh/director.rb +133 -0
  36. data/lib/bosh/director/agent_client.rb +78 -0
  37. data/lib/bosh/director/api.rb +29 -0
  38. data/lib/bosh/director/api/api_helper.rb +81 -0
  39. data/lib/bosh/director/api/backup_manager.rb +15 -0
  40. data/lib/bosh/director/api/controller.rb +639 -0
  41. data/lib/bosh/director/api/controller_helpers.rb +34 -0
  42. data/lib/bosh/director/api/deployment_lookup.rb +13 -0
  43. data/lib/bosh/director/api/deployment_manager.rb +60 -0
  44. data/lib/bosh/director/api/http_constants.rb +16 -0
  45. data/lib/bosh/director/api/instance_lookup.rb +44 -0
  46. data/lib/bosh/director/api/instance_manager.rb +63 -0
  47. data/lib/bosh/director/api/problem_manager.rb +40 -0
  48. data/lib/bosh/director/api/property_manager.rb +69 -0
  49. data/lib/bosh/director/api/release_manager.rb +59 -0
  50. data/lib/bosh/director/api/resource_manager.rb +69 -0
  51. data/lib/bosh/director/api/resurrector_manager.rb +15 -0
  52. data/lib/bosh/director/api/snapshot_manager.rb +94 -0
  53. data/lib/bosh/director/api/stemcell_manager.rb +50 -0
  54. data/lib/bosh/director/api/task_helper.rb +46 -0
  55. data/lib/bosh/director/api/task_manager.rb +64 -0
  56. data/lib/bosh/director/api/user_manager.rb +72 -0
  57. data/lib/bosh/director/api/vm_state_manager.rb +11 -0
  58. data/lib/bosh/director/app.rb +35 -0
  59. data/lib/bosh/director/blob_util.rb +87 -0
  60. data/lib/bosh/director/blobstores.rb +29 -0
  61. data/lib/bosh/director/client.rb +156 -0
  62. data/lib/bosh/director/cloudcheck_helper.rb +204 -0
  63. data/lib/bosh/director/compile_task.rb +157 -0
  64. data/lib/bosh/director/config.rb +370 -0
  65. data/lib/bosh/director/configuration_hasher.rb +114 -0
  66. data/lib/bosh/director/cycle_helper.rb +36 -0
  67. data/lib/bosh/director/db_backup.rb +22 -0
  68. data/lib/bosh/director/db_backup/adapter.rb +3 -0
  69. data/lib/bosh/director/db_backup/adapter/mysql2.rb +27 -0
  70. data/lib/bosh/director/db_backup/adapter/postgres.rb +36 -0
  71. data/lib/bosh/director/db_backup/adapter/sqlite.rb +17 -0
  72. data/lib/bosh/director/db_backup/error.rb +10 -0
  73. data/lib/bosh/director/deployment_plan.rb +26 -0
  74. data/lib/bosh/director/deployment_plan/assembler.rb +430 -0
  75. data/lib/bosh/director/deployment_plan/compilation_config.rb +54 -0
  76. data/lib/bosh/director/deployment_plan/compiled_package.rb +35 -0
  77. data/lib/bosh/director/deployment_plan/dynamic_network.rb +91 -0
  78. data/lib/bosh/director/deployment_plan/idle_vm.rb +109 -0
  79. data/lib/bosh/director/deployment_plan/instance.rb +413 -0
  80. data/lib/bosh/director/deployment_plan/job.rb +470 -0
  81. data/lib/bosh/director/deployment_plan/manual_network.rb +137 -0
  82. data/lib/bosh/director/deployment_plan/network.rb +74 -0
  83. data/lib/bosh/director/deployment_plan/network_subnet.rb +167 -0
  84. data/lib/bosh/director/deployment_plan/planner.rb +288 -0
  85. data/lib/bosh/director/deployment_plan/preparer.rb +52 -0
  86. data/lib/bosh/director/deployment_plan/release.rb +126 -0
  87. data/lib/bosh/director/deployment_plan/resource_pool.rb +143 -0
  88. data/lib/bosh/director/deployment_plan/resource_pools.rb +68 -0
  89. data/lib/bosh/director/deployment_plan/stemcell.rb +56 -0
  90. data/lib/bosh/director/deployment_plan/template.rb +94 -0
  91. data/lib/bosh/director/deployment_plan/update_config.rb +80 -0
  92. data/lib/bosh/director/deployment_plan/updater.rb +55 -0
  93. data/lib/bosh/director/deployment_plan/vip_network.rb +79 -0
  94. data/lib/bosh/director/dns_helper.rb +204 -0
  95. data/lib/bosh/director/download_helper.rb +44 -0
  96. data/lib/bosh/director/duration.rb +36 -0
  97. data/lib/bosh/director/encryption_helper.rb +10 -0
  98. data/lib/bosh/director/errors.rb +198 -0
  99. data/lib/bosh/director/event_log.rb +136 -0
  100. data/lib/bosh/director/ext.rb +64 -0
  101. data/lib/bosh/director/hash_string_vals.rb +13 -0
  102. data/lib/bosh/director/instance_deleter.rb +109 -0
  103. data/lib/bosh/director/instance_updater.rb +506 -0
  104. data/lib/bosh/director/ip_util.rb +67 -0
  105. data/lib/bosh/director/job_queue.rb +16 -0
  106. data/lib/bosh/director/job_runner.rb +162 -0
  107. data/lib/bosh/director/job_updater.rb +121 -0
  108. data/lib/bosh/director/jobs/backup.rb +86 -0
  109. data/lib/bosh/director/jobs/base_job.rb +66 -0
  110. data/lib/bosh/director/jobs/cloud_check/apply_resolutions.rb +46 -0
  111. data/lib/bosh/director/jobs/cloud_check/scan.rb +38 -0
  112. data/lib/bosh/director/jobs/cloud_check/scan_and_fix.rb +73 -0
  113. data/lib/bosh/director/jobs/create_snapshot.rb +23 -0
  114. data/lib/bosh/director/jobs/delete_deployment.rb +183 -0
  115. data/lib/bosh/director/jobs/delete_deployment_snapshots.rb +34 -0
  116. data/lib/bosh/director/jobs/delete_release.rb +219 -0
  117. data/lib/bosh/director/jobs/delete_snapshots.rb +23 -0
  118. data/lib/bosh/director/jobs/delete_stemcell.rb +102 -0
  119. data/lib/bosh/director/jobs/fetch_logs.rb +99 -0
  120. data/lib/bosh/director/jobs/scheduled_backup.rb +38 -0
  121. data/lib/bosh/director/jobs/snapshot_deployment.rb +61 -0
  122. data/lib/bosh/director/jobs/snapshot_deployments.rb +23 -0
  123. data/lib/bosh/director/jobs/snapshot_self.rb +43 -0
  124. data/lib/bosh/director/jobs/ssh.rb +59 -0
  125. data/lib/bosh/director/jobs/update_deployment.rb +110 -0
  126. data/lib/bosh/director/jobs/update_release.rb +672 -0
  127. data/lib/bosh/director/jobs/update_stemcell.rb +109 -0
  128. data/lib/bosh/director/jobs/vm_state.rb +89 -0
  129. data/lib/bosh/director/lock.rb +133 -0
  130. data/lib/bosh/director/lock_helper.rb +92 -0
  131. data/lib/bosh/director/models.rb +29 -0
  132. data/lib/bosh/director/models/compiled_package.rb +33 -0
  133. data/lib/bosh/director/models/deployment.rb +22 -0
  134. data/lib/bosh/director/models/deployment_problem.rb +49 -0
  135. data/lib/bosh/director/models/deployment_property.rb +21 -0
  136. data/lib/bosh/director/models/director_attribute.rb +9 -0
  137. data/lib/bosh/director/models/dns.rb +9 -0
  138. data/lib/bosh/director/models/dns/domain.rb +9 -0
  139. data/lib/bosh/director/models/dns/record.rb +7 -0
  140. data/lib/bosh/director/models/helpers/model_helper.rb +7 -0
  141. data/lib/bosh/director/models/instance.rb +28 -0
  142. data/lib/bosh/director/models/log_bundle.rb +10 -0
  143. data/lib/bosh/director/models/package.rb +30 -0
  144. data/lib/bosh/director/models/persistent_disk.rb +13 -0
  145. data/lib/bosh/director/models/release.rb +17 -0
  146. data/lib/bosh/director/models/release_version.rb +16 -0
  147. data/lib/bosh/director/models/snapshot.rb +13 -0
  148. data/lib/bosh/director/models/stemcell.rb +18 -0
  149. data/lib/bosh/director/models/task.rb +10 -0
  150. data/lib/bosh/director/models/template.rb +44 -0
  151. data/lib/bosh/director/models/user.rb +11 -0
  152. data/lib/bosh/director/models/vm.rb +42 -0
  153. data/lib/bosh/director/nats_rpc.rb +54 -0
  154. data/lib/bosh/director/network_reservation.rb +121 -0
  155. data/lib/bosh/director/next_rebase_version.rb +20 -0
  156. data/lib/bosh/director/package_compiler.rb +423 -0
  157. data/lib/bosh/director/problem_handlers/base.rb +153 -0
  158. data/lib/bosh/director/problem_handlers/inactive_disk.rb +112 -0
  159. data/lib/bosh/director/problem_handlers/invalid_problem.rb +28 -0
  160. data/lib/bosh/director/problem_handlers/missing_vm.rb +34 -0
  161. data/lib/bosh/director/problem_handlers/mount_info_mismatch.rb +62 -0
  162. data/lib/bosh/director/problem_handlers/out_of_sync_vm.rb +64 -0
  163. data/lib/bosh/director/problem_handlers/unbound_instance_vm.rb +85 -0
  164. data/lib/bosh/director/problem_handlers/unresponsive_agent.rb +78 -0
  165. data/lib/bosh/director/problem_resolver.rb +103 -0
  166. data/lib/bosh/director/problem_scanner.rb +268 -0
  167. data/lib/bosh/director/resource_pool_updater.rb +216 -0
  168. data/lib/bosh/director/scheduler.rb +57 -0
  169. data/lib/bosh/director/sequel.rb +13 -0
  170. data/lib/bosh/director/tar_gzipper.rb +47 -0
  171. data/lib/bosh/director/task_result_file.rb +19 -0
  172. data/lib/bosh/director/thread_pool.rb +8 -0
  173. data/lib/bosh/director/validation_helper.rb +55 -0
  174. data/lib/bosh/director/version.rb +7 -0
  175. data/lib/bosh/director/vm_creator.rb +80 -0
  176. data/lib/bosh/director/vm_data.rb +63 -0
  177. data/lib/bosh/director/vm_metadata_updater.rb +29 -0
  178. data/lib/bosh/director/vm_reuser.rb +63 -0
  179. data/lib/cloud/dummy.rb +149 -0
  180. metadata +664 -0
@@ -0,0 +1,219 @@
1
+ # Copyright (c) 2009-2012 VMware, Inc.
2
+
3
+ module Bosh::Director
4
+ module Jobs
5
+ class DeleteRelease < BaseJob
6
+ include LockHelper
7
+
8
+ @queue = :normal
9
+
10
+ def self.job_type
11
+ :delete_release
12
+ end
13
+
14
+ def initialize(name, options = {})
15
+ @name = name
16
+ @blobstore = options.fetch(:blobstore) { App.instance.blobstores.blobstore }
17
+ @errors = []
18
+ @force = !!options["force"]
19
+ @version = options["version"]
20
+ @release_manager = Api::ReleaseManager.new
21
+ end
22
+
23
+ def delete_release_version(release_version)
24
+ release = release_version.release
25
+
26
+ packages_to_keep = []
27
+ packages_to_delete = []
28
+ templates_to_keep = []
29
+ templates_to_delete = []
30
+
31
+ # We don't delete packages inside this loop b/c Sequel will also delete
32
+ # them from packages collection we're iterating on which will lead to
33
+ # skipping some packages
34
+ release_version.packages.each do |package|
35
+ if package.release_versions == [ release_version ]
36
+ packages_to_delete << package
37
+ else
38
+ packages_to_keep << package
39
+ end
40
+ end
41
+
42
+ release_version.templates.each do |template|
43
+ if template.release_versions == [ release_version ]
44
+ templates_to_delete << template
45
+ else
46
+ templates_to_keep << template
47
+ end
48
+ end
49
+
50
+ event_log.begin_stage("Deleting packages", packages_to_delete.count)
51
+ packages_to_delete.each do |package|
52
+ track_and_log("#{package.name}/#{package.version}") do
53
+ logger.info("Package #{package.name}/#{package.version} " +
54
+ "is only used by this release version " +
55
+ "and will be deleted")
56
+ delete_package(package)
57
+ end
58
+ end
59
+
60
+ packages_to_keep.each do |package|
61
+ logger.info("Keeping package #{package.name}/#{package.version} " +
62
+ "as it is used by other release versions")
63
+ package.remove_release_version(release_version)
64
+ end
65
+
66
+ event_log.begin_stage("Deleting templates", templates_to_delete.count)
67
+ templates_to_delete.each do |template|
68
+ track_and_log("#{template.name}/#{template.version}") do
69
+ logger.info("Template #{template.name}/#{template.version} " +
70
+ "is only used by this release version " +
71
+ "and will be deleted")
72
+ delete_template(template)
73
+ end
74
+ end
75
+
76
+ templates_to_keep.each do |template|
77
+ logger.info("Keeping template " +
78
+ "#{template.name}/#{template.version} as it is used " +
79
+ "by other release versions")
80
+ template.remove_release_version(release_version)
81
+ end
82
+
83
+ logger.info("Remove all deployments in release version")
84
+ release_version.remove_all_deployments
85
+
86
+ if @errors.empty? || @force
87
+ release_version.destroy
88
+ end
89
+
90
+ if release.versions.empty?
91
+ delete_release(release)
92
+ end
93
+ end
94
+
95
+ def delete_release(release)
96
+ event_log.begin_stage("Deleting packages", release.packages.count)
97
+ release.packages.each do |package|
98
+ track_and_log("#{package.name}/#{package.version}") do
99
+ delete_package(package)
100
+ end
101
+ end
102
+
103
+ event_log.begin_stage("Deleting templates", release.templates.count)
104
+ release.templates.each do |template|
105
+ track_and_log("#{template.name}/#{template.version}") do
106
+ delete_template(template)
107
+ end
108
+ end
109
+
110
+ if @errors.empty? || @force
111
+ event_log.begin_stage("Deleting release versions",
112
+ release.versions.count)
113
+
114
+ release.versions.each do |release_version|
115
+ track_and_log("#{release.name}/#{release_version.version}") do
116
+ release_version.destroy
117
+ end
118
+ end
119
+
120
+ release.destroy
121
+ end
122
+ end
123
+
124
+ def delete_package(package)
125
+ compiled_packages = package.compiled_packages
126
+
127
+ logger.info("Deleting package #{package.name}/#{package.version}")
128
+
129
+ compiled_packages.each do |compiled_package|
130
+ stemcell = compiled_package.stemcell
131
+ logger.info("Deleting compiled package " +
132
+ "(#{compiled_package.blobstore_id}) " +
133
+ "#{package.name}/#{package.version} " +
134
+ "for #{stemcell.name}/#{stemcell.version}")
135
+ delete_blobstore_id(compiled_package.blobstore_id) do
136
+ compiled_package.destroy
137
+ end
138
+ end
139
+
140
+ delete_blobstore_id(package.blobstore_id) do
141
+ package.remove_all_release_versions
142
+ package.destroy
143
+ end
144
+ end
145
+
146
+ def delete_template(template)
147
+ logger.info("Deleting template: #{template.name}/#{template.version}")
148
+
149
+ delete_blobstore_id(template.blobstore_id) do
150
+ template.remove_all_release_versions
151
+ template.destroy
152
+ end
153
+ end
154
+
155
+ def perform
156
+ logger.info("Processing delete release")
157
+
158
+ with_release_lock(@name, :timeout => 10) do
159
+ logger.info("Looking up release: #{@name}")
160
+ release = @release_manager.find_by_name(@name)
161
+ desc = "#{@name}/#{@version}"
162
+ logger.info("Found: #{release.name}")
163
+
164
+ if @version
165
+ logger.info("Looking up release version `#{desc}'")
166
+ release_version = @release_manager.find_version(release, @version)
167
+ logger.info("Found release version `#{desc}'")
168
+ logger.info("Checking for any deployments still using " +
169
+ "this particular release version")
170
+
171
+ deployments = release_version.deployments
172
+
173
+ unless deployments.empty?
174
+ names = deployments.map{ |d| d.name }.join(", ")
175
+ raise ReleaseVersionInUse,
176
+ "Release version `#{desc}' is still in use by: #{names}"
177
+ end
178
+
179
+ delete_release_version(release_version)
180
+ else
181
+ logger.info("Checking for any deployments still using the release")
182
+ deployments = release.versions.map { |version|
183
+ version.deployments
184
+ }.flatten.uniq
185
+
186
+ unless deployments.empty?
187
+ names = deployments.map { |d| d.name }.join(", ")
188
+ raise ReleaseInUse,
189
+ "Release `#{@name}' is still in use by: #{names}"
190
+ end
191
+
192
+ delete_release(release)
193
+ end
194
+ end
195
+
196
+ unless @errors.empty?
197
+ errors = @errors.map { |e| e.to_s }.join(", ")
198
+ raise ReleaseDeleteFailed, "Can't delete release: #{errors}"
199
+ end
200
+
201
+ "/release/#{@name}"
202
+ end
203
+
204
+ def delete_blobstore_id(blobstore_id)
205
+ deleted = false
206
+ begin
207
+ @blobstore.delete(blobstore_id)
208
+ deleted = true
209
+ rescue Exception => e
210
+ logger.warn("Could not delete from blobstore: #{e}\n " +
211
+ e.backtrace.join("\n"))
212
+ @errors << e
213
+ end
214
+ yield if deleted || @force
215
+ end
216
+
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,23 @@
1
+ module Bosh::Director
2
+ module Jobs
3
+ class DeleteSnapshots < BaseJob
4
+
5
+ @queue = :normal
6
+
7
+ def self.job_type
8
+ :delete_snapshot
9
+ end
10
+
11
+ def initialize(snapshots_cids)
12
+ @snapshot_cids = snapshots_cids
13
+ end
14
+
15
+ def perform
16
+ logger.info("deleting snapshots: #{@snapshot_cids.join(', ')}")
17
+ snapshots = Bosh::Director::Models::Snapshot.where(snapshot_cid: @snapshot_cids).to_a
18
+ Bosh::Director::Api::SnapshotManager.delete_snapshots(snapshots)
19
+ "snapshot(s) #{@snapshot_cids.join(', ')} deleted"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ # Copyright (c) 2009-2012 VMware, Inc.
2
+
3
+ module Bosh::Director
4
+ module Jobs
5
+ class DeleteStemcell < BaseJob
6
+ include LockHelper
7
+
8
+ @queue = :normal
9
+
10
+ def self.job_type
11
+ :delete_stemcell
12
+ end
13
+
14
+ # @param [String] name Stemcell name
15
+ # @param [String] version Stemcell version
16
+ def initialize(name, version, options = {})
17
+ @name = name
18
+ @version = version
19
+ @options = options
20
+ @cloud = Config.cloud
21
+ @blobstore = options.fetch(:blobstore) { App.instance.blobstores.blobstore }
22
+ @stemcell_manager = Api::StemcellManager.new
23
+ end
24
+
25
+ def perform
26
+ logger.info("Processing delete stemcell")
27
+ with_stemcell_lock(@name, @version) do
28
+ logger.info("Looking up stemcell: #{desc}")
29
+ @stemcell = @stemcell_manager.find_by_name_and_version(@name, @version)
30
+ logger.info("Found: #{@stemcell.pretty_inspect}")
31
+
32
+ validate_deletion
33
+ delete_from_cloud
34
+ cleanup_compiled_packages
35
+ delete_stemcell_metadata
36
+ end
37
+
38
+ "/stemcells/#{@name}/#{@version}"
39
+ end
40
+
41
+ def validate_deletion
42
+ logger.info("Checking for any deployments still using the stemcell")
43
+ deployments = @stemcell.deployments
44
+ unless deployments.empty?
45
+ names = deployments.map { |d| d.name }.join(", ")
46
+ raise StemcellInUse,
47
+ "Stemcell `#{desc}' is still in use by: #{names}"
48
+ end
49
+ end
50
+
51
+ def delete_from_cloud
52
+ event_log.begin_stage("Deleting stemcell from cloud", 1)
53
+
54
+ event_log.track("Delete stemcell") do
55
+ @cloud.delete_stemcell(@stemcell.cid)
56
+ end
57
+ rescue => e
58
+ raise unless force?
59
+ logger.warn(e.backtrace.join("\n"))
60
+ logger.info("Force deleting is set, ignoring exception: #{e.message}")
61
+ end
62
+
63
+ def cleanup_compiled_packages
64
+ logger.info("Looking for any compiled packages on this stemcell")
65
+ compiled_packages =
66
+ Models::CompiledPackage.filter(:stemcell_id => @stemcell.id)
67
+
68
+ event_log.begin_stage("Deleting compiled packages",
69
+ compiled_packages.count, [@name, @version])
70
+ logger.info("Deleting compiled packages " +
71
+ "(#{compiled_packages.count}) for `#{desc}'")
72
+
73
+ compiled_packages.each do |compiled_package|
74
+ next unless compiled_package
75
+
76
+ package = compiled_package.package
77
+ track_and_log("#{package.name}/#{package.version}") do
78
+ logger.info("Deleting compiled package: " +
79
+ "#{package.name}/#{package.version}")
80
+ @blobstore.delete(compiled_package.blobstore_id)
81
+ compiled_package.destroy
82
+ end
83
+ end
84
+ end
85
+
86
+ def delete_stemcell_metadata
87
+ event_log.begin_stage("Deleting stemcell metadata", 1)
88
+ event_log.track("Deleting stemcell metadata") do
89
+ @stemcell.destroy
90
+ end
91
+ end
92
+
93
+ def desc
94
+ "#@name/#@version"
95
+ end
96
+
97
+ def force?
98
+ @options["force"]
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,99 @@
1
+ # Copyright (c) 2009-2012 VMware, Inc.
2
+
3
+ module Bosh::Director
4
+ module Jobs
5
+ class FetchLogs < BaseJob
6
+ include LockHelper
7
+
8
+ DEFAULT_BUNDLE_LIFETIME = 86400 * 10 # 10 days
9
+
10
+ @queue = :normal
11
+
12
+ attr_accessor :bundle_lifetime
13
+
14
+ def self.job_type
15
+ :fetch_logs
16
+ end
17
+
18
+ def initialize(instance_id, options = {})
19
+ @blobstore = options.fetch(:blobstore) { App.instance.blobstores.blobstore }
20
+ @instance_id = instance_id
21
+ @log_type = options["type"] || "job"
22
+ @filters = options["filters"]
23
+ @instance_manager = Api::InstanceManager.new
24
+ @bundle_lifetime = DEFAULT_BUNDLE_LIFETIME
25
+ end
26
+
27
+ def perform
28
+ instance = @instance_manager.find_instance(@instance_id)
29
+ desc = "#{instance.job}/#{instance.index}"
30
+
31
+ deployment = instance.deployment
32
+
33
+ if deployment.nil?
34
+ raise InstanceDeploymentMissing,
35
+ "`#{desc}' doesn't belong to any deployment"
36
+ end
37
+
38
+ with_deployment_lock(deployment) do
39
+ cleanup_old_bundles
40
+
41
+ event_log.begin_stage("Fetching logs for #{desc}", 1)
42
+
43
+ agent = @instance_manager.agent_client_for(instance)
44
+ blobstore_id = nil
45
+
46
+ track_and_log("Finding and packing log files") do
47
+ logger.info("Fetching logs from agent: " +
48
+ "log_type='#{@log_type}', filters='#{@filters}'")
49
+
50
+ task = agent.fetch_logs(@log_type, @filters)
51
+ blobstore_id = task["blobstore_id"]
52
+ end
53
+
54
+ if blobstore_id.nil?
55
+ raise AgentTaskNoBlobstoreId,
56
+ "Agent didn't return a blobstore object id for packaged logs"
57
+ end
58
+
59
+ Models::LogBundle.create(:blobstore_id => blobstore_id,
60
+ :timestamp => Time.now)
61
+
62
+ # The returned value of this method is used as task result
63
+ # and gets extracted by CLI as a tarball blobstore id
64
+ blobstore_id
65
+ end
66
+ end
67
+
68
+ def cleanup_old_bundles
69
+ old_bundles = Models::LogBundle.filter("timestamp <= ?",
70
+ Time.now - bundle_lifetime)
71
+ count = old_bundles.count
72
+
73
+ if count == 0
74
+ logger.info("No old bundles to delete")
75
+ return
76
+ end
77
+
78
+ logger.info("Deleting #{count} old log bundle#{count > 1 ? "s" : ""}")
79
+
80
+ old_bundles.each do |bundle|
81
+ begin
82
+ logger.info("Deleting log bundle #{bundle.id}: " +
83
+ "#{bundle.blobstore_id}")
84
+ @blobstore.delete(bundle.blobstore_id)
85
+ bundle.delete
86
+ rescue Bosh::Blobstore::BlobstoreError => e
87
+ logger.warn("Could not delete #{bundle.blobstore_id}: #{e}")
88
+ # Assuming object has been deleted from blobstore by someone else,
89
+ # cleaning up DB record accordingly
90
+ if e.kind_of?(Bosh::Blobstore::NotFound)
91
+ bundle.delete
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,38 @@
1
+ module Bosh::Director
2
+ module Jobs
3
+ class ScheduledBackup < BaseJob
4
+ @queue = :normal
5
+
6
+ attr_reader :backup_job
7
+
8
+ def self.job_type
9
+ :scheduled_backup
10
+ end
11
+
12
+ def initialize(options={})
13
+ @backup_job = options.fetch(:backup_job) { Backup.new(backup_file) }
14
+ @backup_destination = options.fetch(:backup_destination) { App.instance.blobstores.backup_destination }
15
+ end
16
+
17
+ def perform
18
+ @backup_job.perform
19
+
20
+ blobstore_path = "backup-#{Time.now.utc.iso8601}.tgz"
21
+
22
+ File.open(@backup_job.backup_file) do |f|
23
+ @backup_destination.create(f, blobstore_path)
24
+ end
25
+
26
+ "Stored `#{blobstore_path}' in backup blobstore"
27
+ ensure
28
+ FileUtils.rm_f(@backup_job.backup_file)
29
+ end
30
+
31
+ private
32
+
33
+ def backup_file
34
+ File.join(Dir.tmpdir, "backup-#{task_id}.tgz")
35
+ end
36
+ end
37
+ end
38
+ end