nucleus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +1 -0
  3. data/.gitignore +19 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +44 -0
  6. data/.travis.yml +21 -0
  7. data/CHANGELOG.md +19 -0
  8. data/CONTRIBUTING.md +13 -0
  9. data/Gemfile +16 -0
  10. data/Guardfile +22 -0
  11. data/LICENSE +21 -0
  12. data/README.md +675 -0
  13. data/Rakefile +137 -0
  14. data/bin/nucleus +91 -0
  15. data/bin/nucleus.bat +1 -0
  16. data/config.ru +18 -0
  17. data/config/adapters/cloud_control.yml +32 -0
  18. data/config/adapters/cloud_foundry_v2.yml +61 -0
  19. data/config/adapters/heroku.yml +13 -0
  20. data/config/adapters/openshift_v2.yml +20 -0
  21. data/config/nucleus_config.rb +47 -0
  22. data/lib/nucleus.rb +13 -0
  23. data/lib/nucleus/adapter_resolver.rb +115 -0
  24. data/lib/nucleus/adapters/base_adapter.rb +109 -0
  25. data/lib/nucleus/adapters/buildpack_translator.rb +79 -0
  26. data/lib/nucleus/adapters/v1/cloud_control/application.rb +108 -0
  27. data/lib/nucleus/adapters/v1/cloud_control/authentication.rb +27 -0
  28. data/lib/nucleus/adapters/v1/cloud_control/buildpacks.rb +23 -0
  29. data/lib/nucleus/adapters/v1/cloud_control/cloud_control.rb +153 -0
  30. data/lib/nucleus/adapters/v1/cloud_control/data.rb +76 -0
  31. data/lib/nucleus/adapters/v1/cloud_control/domains.rb +68 -0
  32. data/lib/nucleus/adapters/v1/cloud_control/lifecycle.rb +27 -0
  33. data/lib/nucleus/adapters/v1/cloud_control/log_poller.rb +71 -0
  34. data/lib/nucleus/adapters/v1/cloud_control/logs.rb +103 -0
  35. data/lib/nucleus/adapters/v1/cloud_control/regions.rb +32 -0
  36. data/lib/nucleus/adapters/v1/cloud_control/scaling.rb +17 -0
  37. data/lib/nucleus/adapters/v1/cloud_control/semantic_errors.rb +31 -0
  38. data/lib/nucleus/adapters/v1/cloud_control/services.rb +162 -0
  39. data/lib/nucleus/adapters/v1/cloud_control/token.rb +17 -0
  40. data/lib/nucleus/adapters/v1/cloud_control/vars.rb +88 -0
  41. data/lib/nucleus/adapters/v1/cloud_foundry_v2/app_states.rb +28 -0
  42. data/lib/nucleus/adapters/v1/cloud_foundry_v2/application.rb +111 -0
  43. data/lib/nucleus/adapters/v1/cloud_foundry_v2/authentication.rb +17 -0
  44. data/lib/nucleus/adapters/v1/cloud_foundry_v2/buildpacks.rb +23 -0
  45. data/lib/nucleus/adapters/v1/cloud_foundry_v2/cloud_foundry_v2.rb +141 -0
  46. data/lib/nucleus/adapters/v1/cloud_foundry_v2/data.rb +97 -0
  47. data/lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb +149 -0
  48. data/lib/nucleus/adapters/v1/cloud_foundry_v2/lifecycle.rb +41 -0
  49. data/lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb +303 -0
  50. data/lib/nucleus/adapters/v1/cloud_foundry_v2/regions.rb +33 -0
  51. data/lib/nucleus/adapters/v1/cloud_foundry_v2/scaling.rb +15 -0
  52. data/lib/nucleus/adapters/v1/cloud_foundry_v2/semantic_errors.rb +27 -0
  53. data/lib/nucleus/adapters/v1/cloud_foundry_v2/services.rb +286 -0
  54. data/lib/nucleus/adapters/v1/cloud_foundry_v2/vars.rb +80 -0
  55. data/lib/nucleus/adapters/v1/heroku/app_states.rb +57 -0
  56. data/lib/nucleus/adapters/v1/heroku/application.rb +93 -0
  57. data/lib/nucleus/adapters/v1/heroku/authentication.rb +27 -0
  58. data/lib/nucleus/adapters/v1/heroku/buildpacks.rb +27 -0
  59. data/lib/nucleus/adapters/v1/heroku/data.rb +78 -0
  60. data/lib/nucleus/adapters/v1/heroku/domains.rb +43 -0
  61. data/lib/nucleus/adapters/v1/heroku/heroku.rb +146 -0
  62. data/lib/nucleus/adapters/v1/heroku/lifecycle.rb +51 -0
  63. data/lib/nucleus/adapters/v1/heroku/logs.rb +108 -0
  64. data/lib/nucleus/adapters/v1/heroku/regions.rb +42 -0
  65. data/lib/nucleus/adapters/v1/heroku/scaling.rb +28 -0
  66. data/lib/nucleus/adapters/v1/heroku/semantic_errors.rb +23 -0
  67. data/lib/nucleus/adapters/v1/heroku/services.rb +168 -0
  68. data/lib/nucleus/adapters/v1/heroku/vars.rb +65 -0
  69. data/lib/nucleus/adapters/v1/openshift_v2/app_states.rb +68 -0
  70. data/lib/nucleus/adapters/v1/openshift_v2/application.rb +108 -0
  71. data/lib/nucleus/adapters/v1/openshift_v2/authentication.rb +21 -0
  72. data/lib/nucleus/adapters/v1/openshift_v2/data.rb +96 -0
  73. data/lib/nucleus/adapters/v1/openshift_v2/domains.rb +37 -0
  74. data/lib/nucleus/adapters/v1/openshift_v2/lifecycle.rb +60 -0
  75. data/lib/nucleus/adapters/v1/openshift_v2/logs.rb +106 -0
  76. data/lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb +125 -0
  77. data/lib/nucleus/adapters/v1/openshift_v2/regions.rb +58 -0
  78. data/lib/nucleus/adapters/v1/openshift_v2/scaling.rb +39 -0
  79. data/lib/nucleus/adapters/v1/openshift_v2/semantic_errors.rb +40 -0
  80. data/lib/nucleus/adapters/v1/openshift_v2/services.rb +173 -0
  81. data/lib/nucleus/adapters/v1/openshift_v2/vars.rb +49 -0
  82. data/lib/nucleus/adapters/v1/stub_adapter.rb +464 -0
  83. data/lib/nucleus/core/adapter_authentication_inductor.rb +62 -0
  84. data/lib/nucleus/core/adapter_extensions/auth/auth_client.rb +44 -0
  85. data/lib/nucleus/core/adapter_extensions/auth/authentication_retry_wrapper.rb +79 -0
  86. data/lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb +53 -0
  87. data/lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb +37 -0
  88. data/lib/nucleus/core/adapter_extensions/auth/o_auth2_auth_client.rb +95 -0
  89. data/lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb +36 -0
  90. data/lib/nucleus/core/adapter_extensions/http_client.rb +177 -0
  91. data/lib/nucleus/core/adapter_extensions/http_tail_client.rb +26 -0
  92. data/lib/nucleus/core/adapter_extensions/tail_stopper.rb +25 -0
  93. data/lib/nucleus/core/common/errors/ambiguous_adapter_error.rb +7 -0
  94. data/lib/nucleus/core/common/errors/file_existence_error.rb +7 -0
  95. data/lib/nucleus/core/common/errors/startup_error.rb +12 -0
  96. data/lib/nucleus/core/common/exit_codes.rb +25 -0
  97. data/lib/nucleus/core/common/files/application_repo_sanitizer.rb +52 -0
  98. data/lib/nucleus/core/common/files/archive_extractor.rb +112 -0
  99. data/lib/nucleus/core/common/files/archiver.rb +91 -0
  100. data/lib/nucleus/core/common/link_generator.rb +46 -0
  101. data/lib/nucleus/core/common/logging/logging.rb +52 -0
  102. data/lib/nucleus/core/common/logging/multi_logger.rb +59 -0
  103. data/lib/nucleus/core/common/logging/request_log_formatter.rb +48 -0
  104. data/lib/nucleus/core/common/ssh_handler.rb +108 -0
  105. data/lib/nucleus/core/common/stream_callback.rb +27 -0
  106. data/lib/nucleus/core/common/thread_config_accessor.rb +85 -0
  107. data/lib/nucleus/core/common/url_converter.rb +28 -0
  108. data/lib/nucleus/core/enums/application_states.rb +26 -0
  109. data/lib/nucleus/core/enums/logfile_types.rb +28 -0
  110. data/lib/nucleus/core/error_messages.rb +127 -0
  111. data/lib/nucleus/core/errors/adapter_error.rb +13 -0
  112. data/lib/nucleus/core/errors/adapter_missing_implementation_error.rb +12 -0
  113. data/lib/nucleus/core/errors/adapter_request_error.rb +10 -0
  114. data/lib/nucleus/core/errors/adapter_resource_not_found_error.rb +10 -0
  115. data/lib/nucleus/core/errors/endpoint_authentication_error.rb +10 -0
  116. data/lib/nucleus/core/errors/platform_specific_semantic_error.rb +12 -0
  117. data/lib/nucleus/core/errors/platform_timeout_error.rb +10 -0
  118. data/lib/nucleus/core/errors/platform_unavailable_error.rb +10 -0
  119. data/lib/nucleus/core/errors/semantic_adapter_request_error.rb +19 -0
  120. data/lib/nucleus/core/errors/unknown_adapter_call_error.rb +10 -0
  121. data/lib/nucleus/core/file_handling/archive_converter.rb +29 -0
  122. data/lib/nucleus/core/file_handling/file_manager.rb +64 -0
  123. data/lib/nucleus/core/file_handling/git_deployer.rb +133 -0
  124. data/lib/nucleus/core/file_handling/git_repo_analyzer.rb +23 -0
  125. data/lib/nucleus/core/import/adapter_configuration.rb +53 -0
  126. data/lib/nucleus/core/import/vendor_parser.rb +28 -0
  127. data/lib/nucleus/core/import/version_detector.rb +18 -0
  128. data/lib/nucleus/core/models/abstract_model.rb +29 -0
  129. data/lib/nucleus/core/models/endpoint.rb +30 -0
  130. data/lib/nucleus/core/models/provider.rb +26 -0
  131. data/lib/nucleus/core/models/vendor.rb +22 -0
  132. data/lib/nucleus/ext/kernel.rb +5 -0
  133. data/lib/nucleus/ext/regexp.rb +49 -0
  134. data/lib/nucleus/os.rb +15 -0
  135. data/lib/nucleus/root_dir.rb +13 -0
  136. data/lib/nucleus/scripts/finalize.rb +8 -0
  137. data/lib/nucleus/scripts/initialize.rb +9 -0
  138. data/lib/nucleus/scripts/initialize_config_defaults.rb +26 -0
  139. data/lib/nucleus/scripts/load.rb +17 -0
  140. data/lib/nucleus/scripts/load_dependencies.rb +43 -0
  141. data/lib/nucleus/scripts/setup_config.rb +28 -0
  142. data/lib/nucleus/scripts/shutdown.rb +11 -0
  143. data/lib/nucleus/version.rb +3 -0
  144. data/nucleus.gemspec +88 -0
  145. data/public/robots.txt +2 -0
  146. data/public/swagger-ui/css/reset.css +125 -0
  147. data/public/swagger-ui/css/screen.css +1224 -0
  148. data/public/swagger-ui/images/apple-touch-icon-114x114.png +0 -0
  149. data/public/swagger-ui/images/apple-touch-icon-120x120.png +0 -0
  150. data/public/swagger-ui/images/apple-touch-icon-144x144.png +0 -0
  151. data/public/swagger-ui/images/apple-touch-icon-152x152.png +0 -0
  152. data/public/swagger-ui/images/apple-touch-icon-57x57.png +0 -0
  153. data/public/swagger-ui/images/apple-touch-icon-60x60.png +0 -0
  154. data/public/swagger-ui/images/apple-touch-icon-72x72.png +0 -0
  155. data/public/swagger-ui/images/apple-touch-icon-76x76.png +0 -0
  156. data/public/swagger-ui/images/explorer_icons.png +0 -0
  157. data/public/swagger-ui/images/favicon-128.png +0 -0
  158. data/public/swagger-ui/images/favicon-16x16.png +0 -0
  159. data/public/swagger-ui/images/favicon-196x196.png +0 -0
  160. data/public/swagger-ui/images/favicon-32x32.png +0 -0
  161. data/public/swagger-ui/images/favicon-96x96.png +0 -0
  162. data/public/swagger-ui/images/favicon.ico +0 -0
  163. data/public/swagger-ui/images/logo_small.png +0 -0
  164. data/public/swagger-ui/images/mstile-144x144.png +0 -0
  165. data/public/swagger-ui/images/mstile-150x150.png +0 -0
  166. data/public/swagger-ui/images/mstile-310x150.png +0 -0
  167. data/public/swagger-ui/images/mstile-310x310.png +0 -0
  168. data/public/swagger-ui/images/mstile-70x70.png +0 -0
  169. data/public/swagger-ui/images/pet_store_api.png +0 -0
  170. data/public/swagger-ui/images/throbber.gif +0 -0
  171. data/public/swagger-ui/images/wordnik_api.png +0 -0
  172. data/public/swagger-ui/index.html +107 -0
  173. data/public/swagger-ui/lib/backbone-min.js +38 -0
  174. data/public/swagger-ui/lib/handlebars-1.0.0.js +2278 -0
  175. data/public/swagger-ui/lib/highlight.7.3.pack.js +1 -0
  176. data/public/swagger-ui/lib/jquery-1.8.0.min.js +2 -0
  177. data/public/swagger-ui/lib/jquery.ba-bbq.min.js +18 -0
  178. data/public/swagger-ui/lib/jquery.slideto.min.js +1 -0
  179. data/public/swagger-ui/lib/jquery.wiggle.min.js +8 -0
  180. data/public/swagger-ui/lib/shred.bundle.js +2765 -0
  181. data/public/swagger-ui/lib/shred/content.js +193 -0
  182. data/public/swagger-ui/lib/swagger-oauth.js +211 -0
  183. data/public/swagger-ui/lib/swagger.js +1653 -0
  184. data/public/swagger-ui/lib/underscore-min.js +32 -0
  185. data/public/swagger-ui/o2c.html +15 -0
  186. data/public/swagger-ui/redirect.html +14 -0
  187. data/public/swagger-ui/swagger-ui.js +2324 -0
  188. data/public/swagger-ui/swagger-ui.min.js +1 -0
  189. data/schemas/api.adapter.schema.yml +31 -0
  190. data/schemas/api.requirements.schema.yml +17 -0
  191. data/spec/factories/models.rb +61 -0
  192. data/spec/integration/api/auth_spec.rb +58 -0
  193. data/spec/integration/api/endpoints_spec.rb +167 -0
  194. data/spec/integration/api/errors_spec.rb +47 -0
  195. data/spec/integration/api/providers_spec.rb +157 -0
  196. data/spec/integration/api/swagger_schema_spec.rb +64 -0
  197. data/spec/integration/api/vendors_spec.rb +45 -0
  198. data/spec/integration/integration_spec_helper.rb +27 -0
  199. data/spec/integration/test_data_generator.rb +55 -0
  200. data/spec/nucleus_git_key.pem +51 -0
  201. data/spec/spec_helper.rb +98 -0
  202. data/spec/support/shared_example_request_types.rb +99 -0
  203. data/spec/test_suites.rake +31 -0
  204. data/spec/unit/adapters/archive_converter_spec.rb +25 -0
  205. data/spec/unit/adapters/file_manager_spec.rb +93 -0
  206. data/spec/unit/adapters/git_deployer_spec.rb +262 -0
  207. data/spec/unit/adapters/v1/stub_spec.rb +14 -0
  208. data/spec/unit/common/helpers/auth_helper_spec.rb +73 -0
  209. data/spec/unit/common/oauth2_auth_client_spec.rb +108 -0
  210. data/spec/unit/common/regexp_spec.rb +33 -0
  211. data/spec/unit/common/request_log_formatter_spec.rb +108 -0
  212. data/spec/unit/common/thread_config_accessor_spec.rb +97 -0
  213. data/spec/unit/models/endpoint_spec.rb +83 -0
  214. data/spec/unit/models/provider_spec.rb +102 -0
  215. data/spec/unit/models/vendor_spec.rb +100 -0
  216. data/spec/unit/schemas/adapter_schema_spec.rb +16 -0
  217. data/spec/unit/schemas/adapter_validation_spec.rb +56 -0
  218. data/spec/unit/schemas/requirements_schema_spec.rb +16 -0
  219. data/spec/unit/unit_spec_helper.rb +11 -0
  220. data/tasks/compatibility.rake +113 -0
  221. data/tasks/evaluation.rake +162 -0
  222. data/wiki/adapter_tests.md +99 -0
  223. data/wiki/implement_new_adapter.md +155 -0
  224. metadata +836 -0
@@ -0,0 +1,76 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # cloud control data management operations
6
+ module Data
7
+ # @see Stub#deploy
8
+ def deploy(application_id, file, compression_format)
9
+ # get deployment, also serves as 404 check for application
10
+ deployment = default_deployment(application_id)
11
+ current_state = application_state(deployment)
12
+
13
+ user = get('/user').body[0]
14
+ name = "nucleus.app.repo.cloudControl.deploy.#{application_id}.#{SecureRandom.uuid}"
15
+ # push to the deployment branch, here: nucleus
16
+ with_ssh_key do
17
+ deployer = GitDeployer.new(name, deployment[:branch], user[:email], NUCLEUS_DEPLOYMENT)
18
+ deployer.deploy(file, compression_format)
19
+ end
20
+
21
+ return if current_state == Enums::ApplicationStates::CREATED ||
22
+ current_state == Enums::ApplicationStates::DEPLOYED
23
+
24
+ # Deploy via the API, use version identifier -1 to refer a new build,
25
+ # but ONLY (!) if the application is not in the CREATED or DEPLOYED state
26
+ put("app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}", body: { version: '-1' })
27
+ end
28
+
29
+ # @see Stub#download
30
+ def download(application_id, compression_format)
31
+ # get deployment, also serves as 404 check for application
32
+ deployment = default_deployment(application_id)
33
+ if application_state(deployment) == Enums::ApplicationStates::CREATED
34
+ fail Errors::SemanticAdapterRequestError, 'Application must be deployed before data can be downloaded'
35
+ end
36
+
37
+ # compress files to archive but exclude the .git repo
38
+ name = "nucleus.app.repo.cloudControl.download.#{application_id}.#{SecureRandom.uuid}"
39
+ with_ssh_key do
40
+ GitDeployer.new(name, deployment[:branch], nil, NUCLEUS_DEPLOYMENT).download(compression_format, true)
41
+ end
42
+ end
43
+
44
+ # @see Stub#rebuild
45
+ def rebuild(application_id)
46
+ # get deployment, also serves as 404 check for application
47
+ deployment = default_deployment(application_id)
48
+ if application_state(deployment) == Enums::ApplicationStates::CREATED
49
+ fail Errors::SemanticAdapterRequestError, 'Application must be deployed before data can be rebuild'
50
+ end
51
+
52
+ user = get('/user').body[0]
53
+ name = "nucleus.app.repo.cloudControl.rebuild.#{application_id}.#{SecureRandom.uuid}"
54
+
55
+ with_ssh_key do
56
+ GitDeployer.new(name, deployment[:branch], user[:email], NUCLEUS_DEPLOYMENT).trigger_build
57
+ end
58
+
59
+ # now deploy via the API, use version identifier -1 to refer a new build
60
+ put("app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}", body: { version: '-1' })
61
+
62
+ # return with updated application
63
+ application(application_id)
64
+ end
65
+
66
+ private
67
+
68
+ def register_key(user, type, key)
69
+ key_name = "nucleus-#{SecureRandom.uuid}"
70
+ post("/user/#{user}/key", body: { key: [type, key, key_name].join(' ') }).body[:key_id]
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,68 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # cloud control, CRUD operations for the application's domain object
6
+ module Domains
7
+ # cloud control URLs that are automatically assigned to applications as domain but can't be managed
8
+ CC_URLS = %w(cloudcontrolapp.com cloudcontrolled.com)
9
+
10
+ # @see Stub#domains
11
+ def domains(application_id)
12
+ # no conversion needed, cc domains already have :name value
13
+ cc_domains = get("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/alias").body
14
+ # the domain shall NOT be a CC system domain
15
+ cc_domains = cc_domains.find_all do |domain|
16
+ !CC_URLS.any? { |cc_domain| domain[:name].include? cc_domain }
17
+ end
18
+ # the list does not include the timestamps, fetch all
19
+ cc_domains.compact.collect { |domain| domain(application_id, domain[:name]) }
20
+ end
21
+
22
+ # @see Stub#domain
23
+ def domain(application_id, alias_name)
24
+ # no conversion needed, cc domains already have :name value
25
+ cc_domain = get("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/alias/#{alias_name}").body
26
+ to_nucleus_domain(cc_domain)
27
+ end
28
+
29
+ # @see Stub#create_domain
30
+ def create_domain(application_id, domain)
31
+ # check if name is available
32
+ if domain?(application_id, domain[:name])
33
+ fail Errors::SemanticAdapterRequestError,
34
+ "Domain '#{domain[:name]}' is already assigned to the application"
35
+ end
36
+
37
+ # no conversion needed, cc domains already have :name value
38
+ cc_domain = post("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/alias",
39
+ body: { name: domain[:name] }).body
40
+ log.info("Please use this code to verify your custom application domain: #{cc_domain[:verification_code]}")
41
+ log.info('More information about the domain verification can be found at: '\
42
+ 'https://www.cloudcontrol.com/dev-center/add-on-documentation/alias')
43
+ to_nucleus_domain(cc_domain)
44
+ end
45
+
46
+ # @see Stub#delete_domain
47
+ def delete_domain(application_id, alias_name)
48
+ delete("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/alias/#{alias_name}")
49
+ end
50
+
51
+ private
52
+
53
+ def domain?(application_id, alias_name)
54
+ cc_domains = get("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/alias").body
55
+ cc_domains.any? { |domain| domain[:name] == alias_name }
56
+ end
57
+
58
+ def to_nucleus_domain(domain)
59
+ domain[:id] = domain[:name]
60
+ domain[:created_at] = domain.delete(:date_created)
61
+ domain[:updated_at] = domain.delete(:date_modified)
62
+ domain
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ module Lifecycle
6
+ # @see Stub#start
7
+ def start(application_id)
8
+ deployment = default_deployment(application_id)
9
+ # fail if there is no deployment
10
+ unless data_uploaded?(deployment)
11
+ fail Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be started'
12
+ end
13
+
14
+ # if no cloudControl deployment has been made, trigger it
15
+ if deployment[:version] == '-1'
16
+ # deploy via the API, use version identifier -1 to refer a new build
17
+ put("app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}", body: { version: '-1' })
18
+ end
19
+
20
+ # return the application object
21
+ to_nucleus_app(get("/app/#{application_id}").body, default_deployment(application_id))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,71 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # cloud control application's log management operations
6
+ module Logs
7
+ class LogPoller
8
+ # Initialize a new instance
9
+ # @param [BaseAdapter] adapter the adapter that needs the {LogPoller}
10
+ # @param [Hash] headers_to_use adapter headers, e.g. containing the authentication, that shall be used
11
+ # for requests when the headers can't be resolved line in deferred actions
12
+ def initialize(adapter, headers_to_use)
13
+ @adapter = adapter
14
+ @headers_to_use = headers_to_use
15
+ @last_log_entry = {}
16
+ end
17
+
18
+ # Start the continuous polling of the logs.
19
+ # @param [String] application_name the name (the ID) of the application
20
+ # @param [Array<String>] logs_to_poll IDs of the logs to poll
21
+ # @param [StreamCallback] stream stream callback to push messages
22
+ # @return [void]
23
+ def start(application_name, logs_to_poll, stream)
24
+ @polling_active = true
25
+ # 1 log: wait 4 seconds between polls
26
+ # 4 logs: 1 seconds
27
+ timeout = logs_to_poll.length == 1 ? 4 : 1
28
+ logs_to_poll.each { |log_to_poll| @last_log_entry[log_to_poll] = nil }
29
+
30
+ fetch_action = lambda do
31
+ update_log(application_name, logs_to_poll, stream)
32
+ # start next iteration if we are still supposed to be active
33
+ EM.add_timer(timeout) { fetch_action.call } if @polling_active
34
+ end
35
+ # start the loop to poll the logs
36
+ EM.add_timer(timeout) { fetch_action.call }
37
+ end
38
+
39
+ # Stop the polling at the next shot
40
+ def stop
41
+ @polling_active = false
42
+ end
43
+
44
+ private
45
+
46
+ def update_log(application_name, logs_to_poll, stream)
47
+ logs_to_poll.each do |log_to_poll|
48
+ # check again if we are still supposed to be active
49
+ break unless @polling_active
50
+ lines = @adapter.send(:cc_log_entries, application_name, log_to_poll,
51
+ @last_log_entry[log_to_poll], @headers_to_use)
52
+ next if lines.empty?
53
+ send_lines(lines, log_to_poll, stream)
54
+ end
55
+ end
56
+
57
+ def send_lines(lines, log_to_poll, stream)
58
+ # now sort by time
59
+ lines.sort_by! { |line| line[:time].to_f }
60
+ @last_log_entry[log_to_poll] = lines.last[:time] if lines
61
+ lines.each do |line|
62
+ line[:nucleus_origin] = log_to_poll
63
+ stream.send_message(@adapter.send(:format_log_entry, line[:nucleus_origin], line))
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,103 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # cloud control application's log management operations
6
+ module Logs
7
+ # Cloud control log types. The +key+ and +id+ shall match the Nucleus definitions of log files,
8
+ # whereas the +name+ shall match the cloud control log id.
9
+ LOG_TYPES = {
10
+ all: { id: 'all', name: 'all', type: Enums::ApplicationLogfileType::OTHER },
11
+ request: { id: 'request', name: 'access', type: Enums::ApplicationLogfileType::REQUEST },
12
+ application: { id: 'application', name: 'error', type: Enums::ApplicationLogfileType::APPLICATION },
13
+ api: { id: 'api', name: 'deploy', type: Enums::ApplicationLogfileType::API },
14
+ system: { id: 'system', name: 'worker', type: Enums::ApplicationLogfileType::SYSTEM }
15
+ }
16
+
17
+ # @see Stub#logs
18
+ def logs(application_name)
19
+ # fails with 404 if application is not available and serves for timestamps
20
+ app = get("/app/#{application_name}").body
21
+
22
+ LOG_TYPES.values.collect do |log|
23
+ log[:created_at] = app[:date_created]
24
+ log[:updated_at] = app[:date_modified]
25
+ log
26
+ end
27
+ end
28
+
29
+ # @see Stub#log?
30
+ def log?(application_name, log_id)
31
+ # fails with 404 if application is not available
32
+ get("/app/#{application_name}")
33
+
34
+ LOG_TYPES.key? log_id.to_sym
35
+ end
36
+
37
+ # cloud control shows the last 500 log messages if applicable
38
+ # @see Stub#tail
39
+ def tail(application_name, log_id, stream)
40
+ # cache headers as they are bound to a request and could be lost with the next tick
41
+ headers_to_use = headers
42
+ logs_to_poll = log_id == 'all' ? LOG_TYPES.keys - [:all] : [log_id]
43
+ poller = LogPoller.new(self, headers_to_use)
44
+ poller.start(application_name, logs_to_poll, stream)
45
+ TailStopper.new(poller, :stop)
46
+ end
47
+
48
+ # cloud control shows the last 500 log messages if applicable
49
+ # @see Stub#log_entries
50
+ def log_entries(application_name, log_id)
51
+ unless log?(application_name, log_id)
52
+ fail Errors::AdapterResourceNotFoundError,
53
+ "Invalid log file '#{log_id}', not available for application '#{application_name}'"
54
+ end
55
+ if log_id == 'all'
56
+ fetched_lines = []
57
+ (LOG_TYPES.keys - [:all]).each do |current_log_id|
58
+ cc_log_entries(application_name, current_log_id).each do |line|
59
+ line[:nucleus_origin] = current_log_id
60
+ fetched_lines.push(line)
61
+ end
62
+ end
63
+ fetched_lines.sort_by! { |line| line[:time] }
64
+ fetched_lines.collect { |line| format_log_entry(line[:nucleus_origin], line) }
65
+ else
66
+ cc_log_entries(application_name, log_id).collect { |line| format_log_entry(log_id, line) }
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def cc_log_entries(app_name, log_id, time = nil, headers_to_use = nil)
73
+ log_name = LOG_TYPES[log_id.to_sym][:name]
74
+ # Hack, do not create fresh headers (which would fail) when in a deferred action
75
+ headers_to_use = headers unless headers_to_use
76
+ if time
77
+ get("/app/#{app_name}/deployment/#{NUCLEUS_DEPLOYMENT}/log/#{log_name}?timestamp=#{time}",
78
+ headers: headers_to_use).body
79
+ else
80
+ get("/app/#{app_name}/deployment/#{NUCLEUS_DEPLOYMENT}/log/#{log_name}", headers: headers_to_use).body
81
+ end
82
+ end
83
+
84
+ def format_log_entry(log_id, line)
85
+ # format according to: https://github.com/cloudControl/cctrl/blob/master/cctrl/output.py
86
+ case log_id.to_sym
87
+ when :request
88
+ "#{line[:remote_host]} #{line[:remote_user]} #{line[:remote_logname]} [#{Time.at(line[:time]).iso8601}] "\
89
+ "#{line[:first_request_line]} #{line[:status]} #{line[:response_size_CLF]} #{line[:referer]} "\
90
+ "#{line[:user_agent]}"
91
+ when :system
92
+ "#{line[:time]} #{line[:wrk_id]} #{line[:message]}"
93
+ when :build
94
+ "#{Time.at(line[:time]).iso8601} [#{line[:hostname]}/#{line[:depl_id]}] #{line[:level]} #{line[:message]}"
95
+ when :error
96
+ "#{line[:time]} #{line[:type]} #{line[:message]}"
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,32 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ module Regions
6
+ # @see Stub#regions
7
+ def regions
8
+ [default_region]
9
+ end
10
+
11
+ # @see Stub#region
12
+ def region(region_name)
13
+ fail Errors::AdapterResourceNotFoundError,
14
+ "Region '#{region_name}' does not exist at the endpoint" unless region_name.casecmp('default') == 0
15
+ default_region
16
+ end
17
+
18
+ private
19
+
20
+ def default_region
21
+ {
22
+ id: 'default',
23
+ description: 'Default region, cloudControl does not support multi regions yet.',
24
+ created_at: Time.at(0).to_datetime,
25
+ updated_at: Time.at(0).to_datetime
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ module Scaling
6
+ # @see Stub#scale
7
+ def scale(application_id, instances)
8
+ # update the number of instances on the application's deployment
9
+ scale_response = put("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}",
10
+ body: { min_boxes: instances }).body
11
+ to_nucleus_app(get("/app/#{application_id}").body, scale_response)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # Semantic error messages that are specific for cloudControl
6
+ module SemanticErrors
7
+ # Get all cloudControl specific semantic error definitions.
8
+ # @return [Hash<Symbol,Hash<Symbol,String>>] the error message definitions, including the error +code+,
9
+ # e.g. +422_200_1+ and the +message+ that shall be formatted when used.
10
+ def semantic_error_messages
11
+ {
12
+ # Error code '300_1': Only one runtime is allowed per cloudControl application
13
+ only_one_runtime: { code: 422_300_1, message: 'cloudControl only allows 1 runtime per application' },
14
+ # Error code '300_2': Billing details required
15
+ billing_required: { code: 422_300_2,
16
+ message: 'cloudControl requires a billing account to allow this action: %s' },
17
+ # Error code '300_3': Malformed name, please follow the requirements of cloudControl app names
18
+ bad_name: { code: 422_300_3, message: '%s' },
19
+ # Error code '300_3': Malformed name, please follow the requirements of cloudControl app names
20
+ ambiguous_deployments: { code: 422_300_4, message: 'Unable to identify the deployment that shall be '\
21
+ 'used. Nucleus require to find: a) exactly one deployment, b) a "default" deployment or '\
22
+ 'c) a "nucleus" deployment' },
23
+ no_deployment: { code: 422_300_5, message: 'No deployment found. Nucleus requires to find: a) '\
24
+ 'exactly one deployment, b) a "default" deployment or c) a "nucleus" deployment' }
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,162 @@
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudControl < Stub
5
+ # cloud control, operations for the application's addons
6
+ module Services
7
+ # @see Stub#services
8
+ def services
9
+ get('/addon').body.collect { |cc_service| to_nucleus_service(cc_service) }
10
+ end
11
+
12
+ # @see Stub#service
13
+ def service(service_name)
14
+ to_nucleus_service(get("/addon/#{service_name}").body)
15
+ end
16
+
17
+ # @see Stub#service_plans
18
+ def service_plans(service_name)
19
+ get("/addon/#{service_name}").body[:options].collect { |plan| to_nucleus_plan(plan) }
20
+ end
21
+
22
+ # @see Stub#service_plan
23
+ def service_plan(service_name, plan_name)
24
+ plan_name = plan_name?(plan_name) ? plan_name : "#{service_name}.#{plan_name}"
25
+ plan = get("/addon/#{service_name}").body[:options].find do |cc_plan|
26
+ cc_plan[:name] == plan_name
27
+ end
28
+ fail Errors::AdapterResourceNotFoundError,
29
+ "No such plan '#{plan_name}' for service '#{service_name}'" unless plan
30
+ to_nucleus_plan(plan)
31
+ end
32
+
33
+ # @see Stub#installed_services
34
+ def installed_services(application_id)
35
+ load_installed_addons(application_id).collect do |assignment|
36
+ # ignore config and alias addons, for us they are core parts of the application
37
+ next if %w(config.free alias.free).include?(assignment[:addon_option][:name])
38
+ service = service(parse_service_name(assignment[:addon_option][:name]))
39
+ to_nucleus_installed_service(service, assignment)
40
+ end.compact
41
+ end
42
+
43
+ # @see Stub#installed_service
44
+ def installed_service(application_id, service_name)
45
+ # we also require the installed plan to retrieve the service, the list does not include all properties :(
46
+ plan_name = active_plan(application_id, service_name)
47
+ assignment = get("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/addon/#{plan_name}").body
48
+ installed_service = service(parse_service_name(assignment[:addon_option][:name]))
49
+ to_nucleus_installed_service(installed_service, assignment)
50
+ end
51
+
52
+ # @see Stub#add_service
53
+ def add_service(application_id, service_entity, plan_entity)
54
+ plan_name = plan_name?(plan_entity[:id]) ? plan_entity[:id] : "#{service_entity[:id]}.#{plan_entity[:id]}"
55
+ created = post("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/addon",
56
+ body: { addon: plan_name }).body
57
+ to_nucleus_installed_service(service(service_entity[:id]), created)
58
+ end
59
+
60
+ # @see Stub#change_service
61
+ def change_service(application_id, service_id, plan_entity)
62
+ plan_name = active_plan(application_id, service_id)
63
+ fail Errors::SemanticAdapterRequestError,
64
+ "Plan '#{plan_entity[:id]}' is already active for service '#{service_id}' of application "\
65
+ "'#{application_id}'" if plan_name == plan_entity[:id]
66
+
67
+ updated = put("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/addon/#{plan_name}",
68
+ body: { addon: plan_entity[:id] }).body
69
+ to_nucleus_installed_service(service(service_id), updated)
70
+ end
71
+
72
+ # @see Stub#remove_service
73
+ def remove_service(application_id, service_id)
74
+ plan_name = active_plan(application_id, service_id)
75
+ delete("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/addon/#{plan_name}")
76
+ end
77
+
78
+ private
79
+
80
+ def plan_name?(plan_name)
81
+ parts = plan_name.split('.')
82
+ # must have 2 parts and both must not be empty
83
+ parts.length == 2 && parts.all { |part| part.length > 0 }
84
+ end
85
+
86
+ def parse_service_name(plan_name)
87
+ parts = plan_name.split('.')
88
+ fail Errors::SemanticAdapterRequestError, 'Invalid service plan name: Name must contain only one dot, '\
89
+ "which separates the service and plan, e.g. 'mysql.free'" if parts.length != 2
90
+ parts[0]
91
+ end
92
+
93
+ def load_installed_addons(application_id)
94
+ get("/app/#{application_id}/deployment/#{NUCLEUS_DEPLOYMENT}/addon").body
95
+ end
96
+
97
+ def active_plan(application_id, service_id)
98
+ all_installed = load_installed_addons(application_id)
99
+ installed_service = all_installed.find do |service|
100
+ service[:addon_option][:name].start_with?("#{service_id}.") || service[:addon_option][:name] == service_id
101
+ end
102
+ fail Errors::AdapterResourceNotFoundError,
103
+ "No such service '#{service_id}' for application '#{application_id}'" unless installed_service
104
+ installed_service[:addon_option][:name]
105
+ end
106
+
107
+ def free_plan?(service)
108
+ service[:options].any? { |plan| plan[:thirty_days_price].to_i == 0 }
109
+ end
110
+
111
+ # The currency that is used for the prices is not stored within the API.
112
+ # However, we can identify the currency based on a list of known providers.
113
+ # As fallback, we assume the currency is EURO.
114
+ def currency
115
+ return 'USD' if endpoint_url.to_s.include?('dotcloudapp.com')
116
+ return 'CHF' if endpoint_url.to_s.include?('app.exo.io')
117
+ # EUR used for cloudControl, CLOUD & HEAT and as fallback
118
+ 'EUR'
119
+ end
120
+
121
+ def to_nucleus_plan(plan)
122
+ plan[:id] = plan[:name]
123
+ plan[:free] = plan[:thirty_days_price].to_i == 0
124
+ plan[:description] = nil
125
+ # TODO: extract payment period to enum
126
+ plan[:costs] = [{ period: 'month', per_instance: plan[:price_is_per_box],
127
+ price: [amount: plan[:thirty_days_price].to_f, currency: currency] }]
128
+ plan[:created_at] = nil
129
+ plan[:updated_at] = nil
130
+ plan
131
+ end
132
+
133
+ def to_nucleus_service(service)
134
+ service[:id] = service[:name]
135
+ service[:release] = service.delete(:stage)
136
+ # the API does not contain any information, but the homepage does
137
+ service[:documentation_url] = "https://www.cloudcontrol.com/add-ons/#{service[:name]}"
138
+ service[:description] = nil
139
+ service[:created_at] = nil
140
+ service[:updated_at] = nil
141
+ service[:required_services] = []
142
+ service[:free_plan] = free_plan?(service)
143
+ service
144
+ end
145
+
146
+ def to_nucleus_installed_service(service, installed_service)
147
+ # settings ? ? ?
148
+ service[:active_plan] = installed_service[:addon_option][:name]
149
+ if installed_service[:settings] && !installed_service[:settings].empty?
150
+ properties = installed_service[:settings].collect do |key, value|
151
+ { key: key, value: value, description: nil }
152
+ end
153
+ end
154
+ service[:properties] = properties ? properties : []
155
+ service[:web_url] = nil
156
+ service
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end