trucker-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (294) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +1277 -0
  3. data/Rakefile +13 -0
  4. data/bin/truck +18 -0
  5. data/lib/admin/README.md +30 -0
  6. data/lib/admin/curl.rb +59 -0
  7. data/lib/admin/guid.rb +89 -0
  8. data/lib/admin/plugin.rb +8 -0
  9. data/lib/admin/service_auth_token.rb +93 -0
  10. data/lib/admin/service_broker/add.rb +54 -0
  11. data/lib/admin/service_broker/remove.rb +33 -0
  12. data/lib/admin/service_broker/service_brokers.rb +29 -0
  13. data/lib/admin/service_broker/update.rb +60 -0
  14. data/lib/admin/set_quota.rb +44 -0
  15. data/lib/cf.rb +14 -0
  16. data/lib/cf/cli.rb +495 -0
  17. data/lib/cf/cli/app/app.rb +43 -0
  18. data/lib/cf/cli/app/apps.rb +87 -0
  19. data/lib/cf/cli/app/base.rb +72 -0
  20. data/lib/cf/cli/app/delete.rb +95 -0
  21. data/lib/cf/cli/app/deprecated.rb +11 -0
  22. data/lib/cf/cli/app/env.rb +99 -0
  23. data/lib/cf/cli/app/events.rb +45 -0
  24. data/lib/cf/cli/app/files.rb +137 -0
  25. data/lib/cf/cli/app/health.rb +26 -0
  26. data/lib/cf/cli/app/instances.rb +53 -0
  27. data/lib/cf/cli/app/logs.rb +76 -0
  28. data/lib/cf/cli/app/push.rb +104 -0
  29. data/lib/cf/cli/app/push/create.rb +108 -0
  30. data/lib/cf/cli/app/push/interactions.rb +90 -0
  31. data/lib/cf/cli/app/push/sync.rb +57 -0
  32. data/lib/cf/cli/app/rename.rb +35 -0
  33. data/lib/cf/cli/app/restart.rb +31 -0
  34. data/lib/cf/cli/app/scale.rb +63 -0
  35. data/lib/cf/cli/app/start.rb +175 -0
  36. data/lib/cf/cli/app/stats.rb +66 -0
  37. data/lib/cf/cli/app/stop.rb +27 -0
  38. data/lib/cf/cli/domain/base.rb +9 -0
  39. data/lib/cf/cli/domain/domains.rb +40 -0
  40. data/lib/cf/cli/domain/map.rb +55 -0
  41. data/lib/cf/cli/domain/unmap.rb +56 -0
  42. data/lib/cf/cli/help.rb +15 -0
  43. data/lib/cf/cli/interactive.rb +104 -0
  44. data/lib/cf/cli/login_requirements.rb +13 -0
  45. data/lib/cf/cli/organization/base.rb +12 -0
  46. data/lib/cf/cli/organization/create.rb +36 -0
  47. data/lib/cf/cli/organization/delete.rb +61 -0
  48. data/lib/cf/cli/organization/org.rb +45 -0
  49. data/lib/cf/cli/organization/orgs.rb +30 -0
  50. data/lib/cf/cli/organization/rename.rb +36 -0
  51. data/lib/cf/cli/populators/base.rb +16 -0
  52. data/lib/cf/cli/populators/organization.rb +32 -0
  53. data/lib/cf/cli/populators/populator_methods.rb +64 -0
  54. data/lib/cf/cli/populators/space.rb +33 -0
  55. data/lib/cf/cli/populators/target.rb +14 -0
  56. data/lib/cf/cli/route/base.rb +9 -0
  57. data/lib/cf/cli/route/delete.rb +28 -0
  58. data/lib/cf/cli/route/map.rb +68 -0
  59. data/lib/cf/cli/route/routes.rb +26 -0
  60. data/lib/cf/cli/route/unmap.rb +56 -0
  61. data/lib/cf/cli/service/base.rb +9 -0
  62. data/lib/cf/cli/service/bind.rb +43 -0
  63. data/lib/cf/cli/service/create.rb +158 -0
  64. data/lib/cf/cli/service/delete.rb +82 -0
  65. data/lib/cf/cli/service/rename.rb +35 -0
  66. data/lib/cf/cli/service/service.rb +40 -0
  67. data/lib/cf/cli/service/service_helper.rb +25 -0
  68. data/lib/cf/cli/service/service_instance_helper.rb +100 -0
  69. data/lib/cf/cli/service/services.rb +110 -0
  70. data/lib/cf/cli/service/unbind.rb +36 -0
  71. data/lib/cf/cli/space/base.rb +27 -0
  72. data/lib/cf/cli/space/create.rb +66 -0
  73. data/lib/cf/cli/space/delete.rb +55 -0
  74. data/lib/cf/cli/space/rename.rb +38 -0
  75. data/lib/cf/cli/space/space.rb +66 -0
  76. data/lib/cf/cli/space/spaces.rb +57 -0
  77. data/lib/cf/cli/space/switch.rb +18 -0
  78. data/lib/cf/cli/start/base.rb +35 -0
  79. data/lib/cf/cli/start/colors.rb +13 -0
  80. data/lib/cf/cli/start/login.rb +104 -0
  81. data/lib/cf/cli/start/logout.rb +17 -0
  82. data/lib/cf/cli/start/target.rb +50 -0
  83. data/lib/cf/cli/start/target_prettifier.rb +35 -0
  84. data/lib/cf/cli/start/targets.rb +16 -0
  85. data/lib/cf/cli/user/base.rb +29 -0
  86. data/lib/cf/cli/user/create.rb +51 -0
  87. data/lib/cf/cli/user/passwd.rb +36 -0
  88. data/lib/cf/cli/user/register.rb +42 -0
  89. data/lib/cf/cli/user/users.rb +32 -0
  90. data/lib/cf/constants.rb +10 -0
  91. data/lib/cf/errors.rb +19 -0
  92. data/lib/cf/plugin.rb +56 -0
  93. data/lib/cf/spacing.rb +89 -0
  94. data/lib/cf/spec_helper.rb +1 -0
  95. data/lib/cf/test_support.rb +6 -0
  96. data/lib/cf/version.rb +3 -0
  97. data/lib/console/README.md +16 -0
  98. data/lib/console/console.rb +187 -0
  99. data/lib/console/plugin.rb +32 -0
  100. data/lib/manifests/errors.rb +35 -0
  101. data/lib/manifests/loader.rb +31 -0
  102. data/lib/manifests/loader/builder.rb +39 -0
  103. data/lib/manifests/loader/normalizer.rb +145 -0
  104. data/lib/manifests/loader/resolver.rb +79 -0
  105. data/lib/manifests/manifests.rb +343 -0
  106. data/lib/manifests/plugin.rb +140 -0
  107. data/lib/micro/README.md +25 -0
  108. data/lib/micro/errors.rb +4 -0
  109. data/lib/micro/micro.rb +56 -0
  110. data/lib/micro/plugin.rb +197 -0
  111. data/lib/micro/switcher/base.rb +79 -0
  112. data/lib/micro/switcher/darwin.rb +21 -0
  113. data/lib/micro/switcher/dummy.rb +15 -0
  114. data/lib/micro/switcher/linux.rb +16 -0
  115. data/lib/micro/switcher/windows.rb +31 -0
  116. data/lib/micro/vmrun.rb +175 -0
  117. data/lib/tasks/gem_release.rake +56 -0
  118. data/lib/tasks/windows_build.rake +14 -0
  119. data/lib/tunnel/README.md +29 -0
  120. data/lib/tunnel/config/clients.yml +17 -0
  121. data/lib/tunnel/helper-app/Gemfile +10 -0
  122. data/lib/tunnel/helper-app/Gemfile.lock +48 -0
  123. data/lib/tunnel/helper-app/server.rb +43 -0
  124. data/lib/tunnel/plugin.rb +183 -0
  125. data/lib/tunnel/tunnel.rb +295 -0
  126. data/spec/admin/curl_spec.rb +52 -0
  127. data/spec/admin/guid_spec.rb +85 -0
  128. data/spec/admin/service_broker/add_spec.rb +64 -0
  129. data/spec/admin/service_broker/remove_spec.rb +46 -0
  130. data/spec/admin/service_broker/service_brokers_spec.rb +34 -0
  131. data/spec/admin/service_broker/update_spec.rb +51 -0
  132. data/spec/admin/set_quota_spec.rb +89 -0
  133. data/spec/assets/env/Gemfile +4 -0
  134. data/spec/assets/env/Gemfile.lock +20 -0
  135. data/spec/assets/env/env_test.rb +58 -0
  136. data/spec/assets/hello-sinatra/Gemfile +3 -0
  137. data/spec/assets/hello-sinatra/Gemfile.lock +17 -0
  138. data/spec/assets/hello-sinatra/config.ru +3 -0
  139. data/spec/assets/hello-sinatra/fat-cat-makes-app-larger.png +0 -0
  140. data/spec/assets/hello-sinatra/main.rb +8 -0
  141. data/spec/assets/rails328_ruby187_app/Gemfile +39 -0
  142. data/spec/assets/rails328_ruby187_app/README.rdoc +261 -0
  143. data/spec/assets/rails328_ruby187_app/Rakefile +7 -0
  144. data/spec/assets/rails328_ruby187_app/app/assets/images/rails.png +0 -0
  145. data/spec/assets/rails328_ruby187_app/app/assets/javascripts/application.js +15 -0
  146. data/spec/assets/rails328_ruby187_app/app/assets/stylesheets/application.css +13 -0
  147. data/spec/assets/rails328_ruby187_app/app/controllers/application_controller.rb +3 -0
  148. data/spec/assets/rails328_ruby187_app/app/helpers/application_helper.rb +2 -0
  149. data/spec/assets/rails328_ruby187_app/app/views/layouts/application.html.erb +14 -0
  150. data/spec/assets/rails328_ruby187_app/config.ru +4 -0
  151. data/spec/assets/rails328_ruby187_app/config/application.rb +62 -0
  152. data/spec/assets/rails328_ruby187_app/config/boot.rb +6 -0
  153. data/spec/assets/rails328_ruby187_app/config/database.yml +25 -0
  154. data/spec/assets/rails328_ruby187_app/config/environment.rb +5 -0
  155. data/spec/assets/rails328_ruby187_app/config/environments/development.rb +37 -0
  156. data/spec/assets/rails328_ruby187_app/config/environments/production.rb +67 -0
  157. data/spec/assets/rails328_ruby187_app/config/environments/test.rb +37 -0
  158. data/spec/assets/rails328_ruby187_app/config/initializers/backtrace_silencers.rb +7 -0
  159. data/spec/assets/rails328_ruby187_app/config/initializers/inflections.rb +15 -0
  160. data/spec/assets/rails328_ruby187_app/config/initializers/mime_types.rb +5 -0
  161. data/spec/assets/rails328_ruby187_app/config/initializers/secret_token.rb +7 -0
  162. data/spec/assets/rails328_ruby187_app/config/initializers/session_store.rb +8 -0
  163. data/spec/assets/rails328_ruby187_app/config/initializers/wrap_parameters.rb +14 -0
  164. data/spec/assets/rails328_ruby187_app/config/locales/en.yml +5 -0
  165. data/spec/assets/rails328_ruby187_app/config/routes.rb +58 -0
  166. data/spec/assets/rails328_ruby187_app/db/seeds.rb +7 -0
  167. data/spec/assets/rails328_ruby187_app/doc/README_FOR_APP +2 -0
  168. data/spec/assets/rails328_ruby187_app/manifest.yml +7 -0
  169. data/spec/assets/rails328_ruby187_app/public/404.html +26 -0
  170. data/spec/assets/rails328_ruby187_app/public/422.html +26 -0
  171. data/spec/assets/rails328_ruby187_app/public/500.html +25 -0
  172. data/spec/assets/rails328_ruby187_app/public/assets/application-7270767b2a9e9fff880aa5de378ca791.css +0 -0
  173. data/spec/assets/rails328_ruby187_app/public/assets/application-7270767b2a9e9fff880aa5de378ca791.css.gz +0 -0
  174. data/spec/assets/rails328_ruby187_app/public/assets/application-ccab98dc1abdf097c0af693e20aed861.js +17 -0
  175. data/spec/assets/rails328_ruby187_app/public/assets/application-ccab98dc1abdf097c0af693e20aed861.js.gz +0 -0
  176. data/spec/assets/rails328_ruby187_app/public/assets/application.css +0 -0
  177. data/spec/assets/rails328_ruby187_app/public/assets/application.css.gz +0 -0
  178. data/spec/assets/rails328_ruby187_app/public/assets/application.js +17 -0
  179. data/spec/assets/rails328_ruby187_app/public/assets/application.js.gz +0 -0
  180. data/spec/assets/rails328_ruby187_app/public/assets/manifest.yml +4 -0
  181. data/spec/assets/rails328_ruby187_app/public/assets/rails-be8732dac73d845ac5b142c8fb5f9fb0.png +0 -0
  182. data/spec/assets/rails328_ruby187_app/public/assets/rails.png +0 -0
  183. data/spec/assets/rails328_ruby187_app/public/favicon.ico +0 -0
  184. data/spec/assets/rails328_ruby187_app/public/index.html +241 -0
  185. data/spec/assets/rails328_ruby187_app/public/robots.txt +5 -0
  186. data/spec/assets/rails328_ruby187_app/script/rails +6 -0
  187. data/spec/assets/rails328_ruby187_app/test/performance/browsing_test.rb +12 -0
  188. data/spec/assets/rails328_ruby187_app/test/test_helper.rb +13 -0
  189. data/spec/cf/cli/app/base_spec.rb +17 -0
  190. data/spec/cf/cli/app/delete_spec.rb +197 -0
  191. data/spec/cf/cli/app/env_spec.rb +261 -0
  192. data/spec/cf/cli/app/events_spec.rb +72 -0
  193. data/spec/cf/cli/app/help_spec.rb +39 -0
  194. data/spec/cf/cli/app/instances_spec.rb +69 -0
  195. data/spec/cf/cli/app/push/create_spec.rb +444 -0
  196. data/spec/cf/cli/app/push/interactions_spec.rb +32 -0
  197. data/spec/cf/cli/app/push_spec.rb +332 -0
  198. data/spec/cf/cli/app/rename_spec.rb +108 -0
  199. data/spec/cf/cli/app/restart_spec.rb +47 -0
  200. data/spec/cf/cli/app/scale_spec.rb +63 -0
  201. data/spec/cf/cli/app/start_spec.rb +255 -0
  202. data/spec/cf/cli/app/stats_spec.rb +66 -0
  203. data/spec/cf/cli/domain/help_spec.rb +39 -0
  204. data/spec/cf/cli/domain/map_spec.rb +140 -0
  205. data/spec/cf/cli/domain/unmap_spec.rb +77 -0
  206. data/spec/cf/cli/help_spec.rb +21 -0
  207. data/spec/cf/cli/organization/create_spec.rb +121 -0
  208. data/spec/cf/cli/organization/delete_spec.rb +105 -0
  209. data/spec/cf/cli/organization/help_spec.rb +39 -0
  210. data/spec/cf/cli/organization/orgs_spec.rb +116 -0
  211. data/spec/cf/cli/organization/rename_spec.rb +116 -0
  212. data/spec/cf/cli/populators/organization_spec.rb +179 -0
  213. data/spec/cf/cli/populators/space_spec.rb +156 -0
  214. data/spec/cf/cli/populators/target_spec.rb +39 -0
  215. data/spec/cf/cli/route/delete_spec.rb +92 -0
  216. data/spec/cf/cli/route/help_spec.rb +32 -0
  217. data/spec/cf/cli/route/map_spec.rb +148 -0
  218. data/spec/cf/cli/route/unmap_spec.rb +114 -0
  219. data/spec/cf/cli/service/bind_spec.rb +25 -0
  220. data/spec/cf/cli/service/create_spec.rb +147 -0
  221. data/spec/cf/cli/service/delete_spec.rb +22 -0
  222. data/spec/cf/cli/service/help_spec.rb +39 -0
  223. data/spec/cf/cli/service/rename_spec.rb +103 -0
  224. data/spec/cf/cli/service/service_helper_spec.rb +66 -0
  225. data/spec/cf/cli/service/service_instance_helper_spec.rb +155 -0
  226. data/spec/cf/cli/service/service_spec.rb +23 -0
  227. data/spec/cf/cli/service/services_spec.rb +156 -0
  228. data/spec/cf/cli/service/unbind_spec.rb +25 -0
  229. data/spec/cf/cli/space/base_spec.rb +49 -0
  230. data/spec/cf/cli/space/create_spec.rb +144 -0
  231. data/spec/cf/cli/space/delete_spec.rb +109 -0
  232. data/spec/cf/cli/space/help_spec.rb +39 -0
  233. data/spec/cf/cli/space/rename_spec.rb +110 -0
  234. data/spec/cf/cli/space/space_spec.rb +80 -0
  235. data/spec/cf/cli/space/spaces_spec.rb +107 -0
  236. data/spec/cf/cli/space/switch_space_spec.rb +57 -0
  237. data/spec/cf/cli/start/help_spec.rb +39 -0
  238. data/spec/cf/cli/start/login_spec.rb +142 -0
  239. data/spec/cf/cli/start/logout_spec.rb +49 -0
  240. data/spec/cf/cli/start/target_prettifier_spec.rb +78 -0
  241. data/spec/cf/cli/start/target_spec.rb +123 -0
  242. data/spec/cf/cli/user/create_spec.rb +98 -0
  243. data/spec/cf/cli/user/help_spec.rb +32 -0
  244. data/spec/cf/cli/user/passwd_spec.rb +94 -0
  245. data/spec/cf/cli/user/register_spec.rb +145 -0
  246. data/spec/cf/cli_spec.rb +474 -0
  247. data/spec/console/console_spec.rb +189 -0
  248. data/spec/factories/cfoundry/v2/apps_factory.rb +12 -0
  249. data/spec/factories/cfoundry/v2/clients_factory.rb +7 -0
  250. data/spec/factories/cfoundry/v2/domains_factory.rb +8 -0
  251. data/spec/factories/cfoundry/v2/managed_service_instances_factory.rb +13 -0
  252. data/spec/factories/cfoundry/v2/organizations_factory.rb +14 -0
  253. data/spec/factories/cfoundry/v2/quota_definitions.rb +13 -0
  254. data/spec/factories/cfoundry/v2/routes_factory.rb +8 -0
  255. data/spec/factories/cfoundry/v2/service_bindings_factory.rb +11 -0
  256. data/spec/factories/cfoundry/v2/service_brokers_factory.rb +11 -0
  257. data/spec/factories/cfoundry/v2/service_plans_factory.rb +17 -0
  258. data/spec/factories/cfoundry/v2/services_factory.rb +14 -0
  259. data/spec/factories/cfoundry/v2/spaces_factory.rb +13 -0
  260. data/spec/factories/cfoundry/v2/stacks_factory.rb +11 -0
  261. data/spec/factories/cfoundry/v2/user_provided_service_instances_factory.rb +12 -0
  262. data/spec/factories/cfoundry/v2/users_factory.rb +7 -0
  263. data/spec/features/backspace_spec.rb +46 -0
  264. data/spec/features/create_user_spec.rb +74 -0
  265. data/spec/features/delete_orphaned_service_spec.rb +64 -0
  266. data/spec/features/login_spec.rb +65 -0
  267. data/spec/features/manifests_spec.rb +86 -0
  268. data/spec/features/org_spec.rb +50 -0
  269. data/spec/features/push_flow_spec.rb +169 -0
  270. data/spec/features/service_brokers_spec.rb +173 -0
  271. data/spec/features/services_spec.rb +122 -0
  272. data/spec/features/space_spec.rb +107 -0
  273. data/spec/features/switching_targets_spec.rb +70 -0
  274. data/spec/manifests/errors_spec.rb +43 -0
  275. data/spec/manifests/loader/builder_spec.rb +80 -0
  276. data/spec/manifests/loader/normalizer_spec.rb +158 -0
  277. data/spec/manifests/manifests_spec.rb +335 -0
  278. data/spec/manifests/plugin_spec.rb +410 -0
  279. data/spec/micro/plugin_spec.rb +64 -0
  280. data/spec/spec_helper.rb +69 -0
  281. data/spec/support/cli_helper.rb +87 -0
  282. data/spec/support/config_helper.rb +15 -0
  283. data/spec/support/factory_girl.rb +6 -0
  284. data/spec/support/fake_home_dir.rb +55 -0
  285. data/spec/support/features_helper.rb +99 -0
  286. data/spec/support/interact_helper.rb +27 -0
  287. data/spec/support/matchers.rb +16 -0
  288. data/spec/support/mock_commands/mock_restart.rb +16 -0
  289. data/spec/support/mock_commands/mock_start.rb +17 -0
  290. data/spec/support/shared_examples/errors.rb +40 -0
  291. data/spec/support/shared_examples/input.rb +14 -0
  292. data/spec/tunnel/plugin_spec.rb +31 -0
  293. data/spec/tunnel/tunnel_spec.rb +54 -0
  294. metadata +814 -0
@@ -0,0 +1,32 @@
1
+ require "cf/plugin"
2
+ require "console/console"
3
+
4
+ module CFConsolePlugin
5
+ class Console < CF::CLI
6
+ desc "Open a console connected to your app"
7
+ group :apps, :manage
8
+ input :app, :argument => :required, :from_given => by_name("app"),
9
+ :desc => "App to connect to"
10
+ input :port, :default => 10000
11
+ def console
12
+ app = input[:app]
13
+
14
+ console = CFConsole.new(client, app)
15
+ port = console.pick_port!(input[:port])
16
+
17
+ with_progress("Opening console on port #{c(port, :name)}") do
18
+ console.open!
19
+ console.wait_for_start
20
+ end
21
+
22
+ console.start_console
23
+ end
24
+
25
+ ::ManifestsPlugin.default_to_app_from_manifest :console, true
26
+
27
+ filter(:start, :start_app) do |app|
28
+ app.console = true
29
+ app
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ require "cf/errors"
2
+
3
+ module CFManifests
4
+ class InvalidManifest < CF::UserFriendlyError
5
+ attr_reader :file
6
+
7
+ def initialize(file)
8
+ @file = file
9
+ end
10
+
11
+ def to_s
12
+ "Manifest file '#{@file}' is malformed."
13
+ end
14
+ end
15
+
16
+ class CircularDependency < CF::UserFriendlyError
17
+ def initialize(app)
18
+ @app = app
19
+ end
20
+
21
+ def to_s
22
+ "Circular dependency in application '#@app'"
23
+ end
24
+ end
25
+
26
+ class UnknownSymbol < CF::UserFriendlyError
27
+ def initialize(sym)
28
+ @sym = sym
29
+ end
30
+
31
+ def to_s
32
+ "Undefined symbol in manifest: '#@sym'"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require "manifests/loader/builder"
2
+ require "manifests/loader/normalizer"
3
+ require "manifests/loader/resolver"
4
+
5
+ module CFManifests
6
+ class Loader
7
+ include Builder
8
+ include Normalizer
9
+ include Resolver
10
+
11
+ def initialize(file, resolver)
12
+ @file = file
13
+ @resolver = resolver
14
+ end
15
+
16
+ def manifest
17
+ info = build(@file)
18
+ normalize! info
19
+ resolve info, @resolver
20
+ end
21
+
22
+ private
23
+
24
+ # expand a path relative to the manifest file's directory
25
+ def from_manifest(path)
26
+ return path unless @file
27
+
28
+ File.expand_path(path, File.dirname(@file))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ require "manifests/errors"
2
+
3
+ module CFManifests
4
+ module Builder
5
+ # parse a manifest and merge with its inherited manifests
6
+ def build(file)
7
+ manifest = YAML.load_file file
8
+ raise CFManifests::InvalidManifest.new(file) unless manifest
9
+
10
+ Array(manifest["inherit"]).each do |path|
11
+ manifest = merge_parent(path, manifest)
12
+ end
13
+
14
+ manifest.delete("inherit")
15
+
16
+ manifest
17
+ end
18
+
19
+ private
20
+
21
+ # merge the manifest at `parent_path' into the `child'
22
+ def merge_parent(parent_path, child)
23
+ merge_manifest(build(from_manifest(parent_path)), child)
24
+ end
25
+
26
+ # deep hash merge
27
+ def merge_manifest(parent, child)
28
+ merge = proc do |_, old, new|
29
+ if new.is_a?(Hash) && old.is_a?(Hash)
30
+ old.merge(new, &merge)
31
+ else
32
+ new
33
+ end
34
+ end
35
+
36
+ parent.merge(child, &merge)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,145 @@
1
+ module CFManifests
2
+ module Normalizer
3
+ MANIFEST_META = ["applications", "properties"]
4
+
5
+ def normalize!(manifest)
6
+ toplevel = toplevel_attributes(manifest)
7
+
8
+ apps = manifest["applications"]
9
+ apps ||= [{}]
10
+
11
+ default_paths_to_keys!(apps)
12
+
13
+ apps = convert_to_array(apps)
14
+
15
+ merge_toplevel!(toplevel, manifest, apps)
16
+ normalize_apps!(apps)
17
+
18
+ manifest["applications"] = apps
19
+
20
+ normalize_paths!(apps)
21
+
22
+ keyval = normalize_key_val(manifest)
23
+ manifest.clear.merge!(keyval)
24
+
25
+ nil
26
+ end
27
+
28
+ private
29
+
30
+ def normalize_paths!(apps)
31
+ apps.each do |app|
32
+ app["path"] = from_manifest(app["path"])
33
+ end
34
+ end
35
+
36
+ def convert_to_array(apps)
37
+ return apps if apps.is_a?(Array)
38
+
39
+ ordered_by_deps(apps)
40
+ end
41
+
42
+ # sort applications in dependency order
43
+ # e.g. if A depends on B, B will be listed before A
44
+ def ordered_by_deps(apps, processed = Set[])
45
+ ordered = []
46
+ apps.each do |tag, info|
47
+ next if processed.include?(tag)
48
+
49
+ if deps = Array(info["depends-on"])
50
+ dep_apps = {}
51
+ deps.each do |dep|
52
+ dep_apps[dep] = apps[dep]
53
+ end
54
+
55
+ processed.add(tag)
56
+
57
+ ordered += ordered_by_deps(dep_apps, processed)
58
+ ordered << info
59
+ else
60
+ ordered << info
61
+ processed.add(tag)
62
+ end
63
+ end
64
+
65
+ ordered.each { |app| app.delete("depends-on") }
66
+
67
+ ordered
68
+ end
69
+
70
+ def default_paths_to_keys!(apps)
71
+ return if apps.is_a?(Array)
72
+
73
+ apps.each do |tag, app|
74
+ app["path"] ||= tag
75
+ end
76
+ end
77
+
78
+ def normalize_apps!(apps)
79
+ apps.each do |app|
80
+ normalize_app!(app)
81
+ end
82
+ end
83
+
84
+ def merge_toplevel!(toplevel, manifest, apps)
85
+ return if toplevel.empty?
86
+
87
+ apps.collect! do |a|
88
+ toplevel.merge(a)
89
+ end
90
+
91
+ toplevel.each do |k, _|
92
+ manifest.delete k
93
+ end
94
+ end
95
+
96
+ def normalize_app!(app)
97
+ if app.key?("mem")
98
+ app["memory"] = app.delete("mem")
99
+ end
100
+
101
+ if app.key?("url") && app["url"].nil?
102
+ app["url"] = "none"
103
+ end
104
+
105
+ if app.key?("subdomain")
106
+ if app.key?("host")
107
+ app.delete("subdomain")
108
+ else
109
+ app["host"] = app.delete("subdomain")
110
+ end
111
+ end
112
+ end
113
+
114
+ def toplevel_attributes(manifest)
115
+ top =
116
+ manifest.reject { |k, _|
117
+ MANIFEST_META.include? k
118
+ }
119
+
120
+ # implicit toplevel path of .
121
+ top["path"] ||= "."
122
+
123
+ top
124
+ end
125
+
126
+ def normalize_key_val(val)
127
+ case val
128
+ when Hash
129
+ stringified = {}
130
+
131
+ val.each do |k, v|
132
+ stringified[k.to_sym] = normalize_key_val(v)
133
+ end
134
+
135
+ stringified
136
+ when Array
137
+ val.collect { |x| normalize_key_val(x) }
138
+ when nil
139
+ nil
140
+ else
141
+ val.to_s
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,79 @@
1
+ module CFManifests
2
+ module Resolver
3
+ def resolve(manifest, resolver)
4
+ new = {}
5
+
6
+ new[:applications] = manifest[:applications].collect do |app|
7
+ resolve_lexically(resolver, app, [manifest])
8
+ end
9
+
10
+ resolve_lexically(resolver, new, [new])
11
+ end
12
+
13
+ private
14
+
15
+ # resolve symbols, with hashes introducing new lexical symbols
16
+ def resolve_lexically(resolver, val, ctx)
17
+ case val
18
+ when Hash
19
+ new = {}
20
+
21
+ val.each do |k, v|
22
+ new[k] = resolve_lexically(resolver, v, [val] + ctx)
23
+ end
24
+
25
+ new
26
+ when Array
27
+ val.collect do |v|
28
+ resolve_lexically(resolver, v, ctx)
29
+ end
30
+ when String
31
+ val.gsub(/\$\{([^\}]+)\}/) do
32
+ resolve_symbol(resolver, $1, ctx)
33
+ end
34
+ else
35
+ val
36
+ end
37
+ end
38
+
39
+ # resolve a symbol to its value, and then resolve that value
40
+ def resolve_symbol(resolver, sym, ctx)
41
+ if found = find_symbol(sym.to_sym, ctx)
42
+ resolve_lexically(resolver, found, ctx)
43
+ found
44
+ elsif dynamic = resolver.resolve_symbol(sym)
45
+ dynamic
46
+ else
47
+ fail("Unknown symbol in manifest: #{sym}")
48
+ end
49
+ end
50
+
51
+ # search for a symbol introduced in the lexical context
52
+ def find_symbol(sym, ctx)
53
+ ctx.each do |h|
54
+ if val = resolve_in(h, sym)
55
+ return val
56
+ end
57
+ end
58
+
59
+ nil
60
+ end
61
+
62
+ # find a value, searching in explicit properties first
63
+ def resolve_in(hash, *where)
64
+ find_in_hash(hash, [:properties] + where) ||
65
+ find_in_hash(hash, where)
66
+ end
67
+
68
+ # helper for following a path of values in a hash
69
+ def find_in_hash(hash, where)
70
+ what = hash
71
+ where.each do |x|
72
+ return nil unless what.is_a?(Hash)
73
+ what = what[x]
74
+ end
75
+
76
+ what
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,343 @@
1
+ require "yaml"
2
+ require "set"
3
+ require "cf/cli/service/create"
4
+
5
+ require "manifests/loader"
6
+
7
+ module CFManifests
8
+ MANIFEST_FILE = "manifest.yml"
9
+
10
+ @@showed_manifest_usage = false
11
+ @@manifest = nil
12
+
13
+ def manifest
14
+ return @@manifest if @@manifest
15
+
16
+ if manifest_file && File.exists?(manifest_file)
17
+ @@manifest = load_manifest(manifest_file)
18
+ end
19
+ end
20
+
21
+ def save_manifest(save_to = manifest_file)
22
+ fail "No manifest to save!" unless @@manifest
23
+
24
+ File.open(save_to, "w") do |io|
25
+ YAML.dump(@@manifest, io)
26
+ end
27
+ end
28
+
29
+ # find the manifest file to work with
30
+ def manifest_file
31
+ return @manifest_file if @manifest_file
32
+
33
+ unless path = input[:manifest]
34
+ where = Dir.pwd
35
+ while true
36
+ if File.exists?(File.join(where, MANIFEST_FILE))
37
+ path = File.join(where, MANIFEST_FILE)
38
+ break
39
+ elsif File.basename(where) == "/"
40
+ path = nil
41
+ break
42
+ else
43
+ where = File.expand_path("../", where)
44
+ end
45
+ end
46
+ end
47
+
48
+ return unless path
49
+
50
+ @manifest_file = File.expand_path(path)
51
+ end
52
+
53
+ # load and resolve a given manifest file
54
+ def load_manifest(file)
55
+ check_manifest! Loader.new(file, self).manifest
56
+ end
57
+
58
+ def check_manifest!(manifest_hash, output = $stdout)
59
+ manifest_hash[:applications].each{ |app| check_attributes!(app, output) }
60
+ manifest_hash
61
+ end
62
+
63
+ def check_attributes!(app, output = $stdout)
64
+ app.each do |k, v|
65
+ output.puts error_message_for_attribute(k) unless known_manifest_attributes.include? k
66
+ end
67
+ end
68
+
69
+ def error_message_for_attribute(attribute)
70
+ "\e[31mWarning: #{attribute} is not a valid manifest attribute. Please " +
71
+ "remove this attribute from your manifest to get rid of this warning\e[0m"
72
+ end
73
+
74
+ def known_manifest_attributes
75
+ [:applications, :buildpack, :command, :disk, :domain, :env,
76
+ :host, :inherit, :instances, :mem, :memory, :name,
77
+ :path, :properties, :runtime, :services, :stack]
78
+ end
79
+
80
+ # dynamic symbol resolution
81
+ def resolve_symbol(sym)
82
+ case sym
83
+ when "target-url"
84
+ client_target
85
+
86
+ when "target-base"
87
+ target_base
88
+
89
+ when "random-word"
90
+ sprintf("%04x", rand(0x0100000))
91
+
92
+ when /^ask (.+)/
93
+ ask($1)
94
+ end
95
+ end
96
+
97
+ # find apps by an identifier, which may be either a tag, a name, or a path
98
+ def find_apps(identifier)
99
+ return [] unless manifest
100
+
101
+ apps = apps_by(:name, identifier)
102
+
103
+ if apps.empty?
104
+ apps = apps_by(:path, from_manifest(identifier))
105
+ end
106
+
107
+ apps
108
+ end
109
+
110
+ # return all the apps described by the manifest
111
+ def all_apps
112
+ manifest[:applications]
113
+ end
114
+
115
+ def current_apps
116
+ manifest[:applications].select do |app|
117
+ next unless app[:path]
118
+ from_manifest(app[:path]) == Dir.pwd
119
+ end
120
+ end
121
+
122
+ # splits the user's input, resolving paths with the manifest,
123
+ # into internal/external apps
124
+ #
125
+ # internal apps are returned as their data in the manifest
126
+ #
127
+ # external apps are the strings that the user gave, to be
128
+ # passed along wholesale to the wrapped command
129
+ def apps_in_manifest(input = nil, use_name = true, &blk)
130
+ names_or_paths =
131
+ if input.has?(:apps)
132
+ # names may be given but be [], which will still cause
133
+ # interaction, so use #direct instead of #[] here
134
+ input.direct(:apps)
135
+ elsif input.has?(:app)
136
+ [input.direct(:app)]
137
+ elsif input.has?(:name)
138
+ [input.direct(:name)]
139
+ else
140
+ []
141
+ end
142
+
143
+ internal = []
144
+ external = []
145
+
146
+ names_or_paths.each do |x|
147
+ if x.is_a?(String)
148
+ if x =~ %r([/\\])
149
+ apps = find_apps(File.expand_path(x))
150
+
151
+ if apps.empty?
152
+ fail("Path #{b(x)} is not present in manifest #{b(relative_manifest_file)}.")
153
+ end
154
+ else
155
+ apps = find_apps(x)
156
+ end
157
+
158
+ if !apps.empty?
159
+ internal += apps
160
+ else
161
+ external << x
162
+ end
163
+ else
164
+ external << x
165
+ end
166
+ end
167
+
168
+ [internal, external]
169
+ end
170
+
171
+ def create_manifest_for(app, path)
172
+ meta = {
173
+ "name" => app.name,
174
+ "memory" => human_size(app.memory * 1024 * 1024, 0),
175
+ "instances" => app.total_instances,
176
+ "host" => app.host || "none",
177
+ "domain" => app.domain ? app.domain : "none",
178
+ "path" => path
179
+ }
180
+
181
+ services = app.services
182
+
183
+ unless services.empty?
184
+ meta["services"] = {}
185
+
186
+ services.each do |service_instance|
187
+ if service_instance.is_a?(CFoundry::V2::UserProvidedServiceInstance)
188
+ meta["services"][service_instance.name] = {
189
+ "label" => "user-provided",
190
+ "credentials" => service_instance.credentials.stringify_keys,
191
+ }
192
+ else
193
+ service_plan = service_instance.service_plan
194
+ service = service_plan.service
195
+
196
+ meta["services"][service_instance.name] = {
197
+ "label" => service.label,
198
+ "provider" => service.provider,
199
+ "version" => service.version,
200
+ "plan" => service_plan.name
201
+ }
202
+ end
203
+ end
204
+ end
205
+
206
+ if cmd = app.command
207
+ meta["command"] = cmd
208
+ end
209
+
210
+ if buildpack = app.buildpack
211
+ meta["buildpack"] = buildpack
212
+ end
213
+
214
+ meta
215
+ end
216
+
217
+ private
218
+
219
+ def relative_manifest_file
220
+ Pathname.new(manifest_file).relative_path_from(Pathname.pwd)
221
+ end
222
+
223
+ def show_manifest_usage
224
+ return if @@showed_manifest_usage
225
+
226
+ path = relative_manifest_file
227
+ line "Using manifest file #{c(path, :name)}"
228
+ line
229
+
230
+ @@showed_manifest_usage = true
231
+ end
232
+
233
+ def no_apps
234
+ fail "No applications or manifest to operate on."
235
+ end
236
+
237
+ def warn_reset_changes
238
+ line c("Not applying manifest changes without --reset", :warning)
239
+ line
240
+ end
241
+
242
+ def apps_by(attr, val)
243
+ manifest[:applications].select do |info|
244
+ info[attr] == val
245
+ end
246
+ end
247
+
248
+ # expand a path relative to the manifest file's directory
249
+ def from_manifest(path)
250
+ File.expand_path(path, File.dirname(manifest_file))
251
+ end
252
+
253
+
254
+ def ask_to_save(input, app)
255
+ return if manifest_file
256
+ return unless ask("Save configuration?", :default => false)
257
+
258
+ manifest = create_manifest_for(app, input[:path])
259
+
260
+ with_progress("Saving to #{c("manifest.yml", :name)}") do
261
+ File.open("manifest.yml", "w") do |io|
262
+ YAML.dump(
263
+ { "applications" => [manifest] },
264
+ io)
265
+ end
266
+ end
267
+ end
268
+
269
+ def env_hash(val)
270
+ if val.is_a?(Hash)
271
+ val
272
+ else
273
+ hash = {}
274
+
275
+ val.each do |pair|
276
+ name, val = pair.split("=", 2)
277
+ hash[name] = val
278
+ end
279
+
280
+ hash
281
+ end
282
+ end
283
+
284
+ def setup_env(app, info)
285
+ return unless info[:env]
286
+ app.env = env_hash(info[:env])
287
+ end
288
+
289
+ def setup_services(app, info)
290
+ return if !info[:services] || info[:services].empty?
291
+
292
+ offerings = client.services
293
+
294
+ to_bind = []
295
+
296
+ info[:services].each do |name, svc|
297
+ name = name.to_s
298
+
299
+ if instance = client.service_instance_by_name(name)
300
+ to_bind << instance
301
+ else
302
+ if svc[:label] == "user-provided"
303
+ invoke :create_service,
304
+ name: name,
305
+ offering: CF::Service::UPDummy.new,
306
+ app: app,
307
+ credentials: svc[:credentials]
308
+ else
309
+ offering = offerings.find { |o|
310
+ o.label == (svc[:label] || svc[:type] || svc[:vendor]) &&
311
+ (!svc[:version] || o.version == svc[:version]) &&
312
+ (o.provider == (svc[:provider] || "core"))
313
+ }
314
+
315
+ fail "Unknown service offering: #{svc.inspect}." unless offering
316
+
317
+ plan = offering.service_plans.find { |p|
318
+ p.name == (svc[:plan] || "D100")
319
+ }
320
+
321
+ fail "Unknown service plan: #{svc[:plan]}." unless plan
322
+
323
+ invoke :create_service,
324
+ :name => name,
325
+ :offering => offering,
326
+ :plan => plan,
327
+ :app => app
328
+ end
329
+ end
330
+ end
331
+
332
+ to_bind.each do |s|
333
+ next if app.binds?(s)
334
+
335
+ # TODO: splat
336
+ invoke :bind_service, :app => app, :service => s
337
+ end
338
+ end
339
+
340
+ def target_base
341
+ client_target.sub(/^[^\.]+\./, "")
342
+ end
343
+ end