Class: Dockistrano::ServiceDependency
- Inherits:
-
Object
- Object
- Dockistrano::ServiceDependency
- Defined in:
- lib/dockistrano/service_dependency.rb
Defined Under Namespace
Classes: ContainerConfigurationMissing, DefaultEnvironmentMissingInConfiguration, HostDirectoriesMissing, NoTagFoundForImage
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
- .clear_cache ⇒ Object
-
.factory(service, name, config) ⇒ Object
Creates a new service instance based on the name and configuration.
Instance Method Summary collapse
- #backing_service ⇒ Object
-
#initialize(service, name, config) ⇒ ServiceDependency
constructor
A new instance of ServiceDependency.
- #load_config ⇒ Object
- #load_from_cache ⇒ Object
- #load_from_image ⇒ Object
- #tag_with_fallback(tag) ⇒ Object
Constructor Details
#initialize(service, name, config) ⇒ ServiceDependency
Returns a new instance of ServiceDependency.
17 18 19 20 21 |
# File 'lib/dockistrano/service_dependency.rb', line 17 def initialize(service, name, config) @service = service @name = name @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/dockistrano/service_dependency.rb', line 15 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/dockistrano/service_dependency.rb', line 15 def name @name end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
15 16 17 |
# File 'lib/dockistrano/service_dependency.rb', line 15 def service @service end |
Class Method Details
.clear_cache ⇒ Object
109 110 111 |
# File 'lib/dockistrano/service_dependency.rb', line 109 def self.clear_cache `rm -rf tmp/configuration_cache/` end |
.factory(service, name, config) ⇒ Object
Creates a new service instance based on the name and configuration. When configuration is not local, the configuration is fetched from Github and processed.
8 9 10 |
# File 'lib/dockistrano/service_dependency.rb', line 8 def self.factory(service, name, config) ServiceDependency.new(service, name, config).backing_service end |
Instance Method Details
#backing_service ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dockistrano/service_dependency.rb', line 23 def backing_service @backing_service ||= begin backing_service = Service.new("default" => { "registry" => service.registry, "image_name" => name, "tag" => service.tag, "backing_service_env" => config }) backing_service.tag = tag_with_fallback(service.tag) begin loaded_config = load_config if loaded_config and loaded_config["default"] backing_service.config = loaded_config["default"] else raise DefaultEnvironmentMissingInConfiguration.new("No 'default' configuration found in /dockistrano.yml file in #{name} container.") end rescue ContainerConfigurationMissing puts "Warning: no configuration file found for service #{name}." rescue HostDirectoriesMissing puts "Error: missing host directory configuration for #{name}. Please execute `doc setup`" exit 1 end backing_service end end |
#load_config ⇒ Object
52 53 54 |
# File 'lib/dockistrano/service_dependency.rb', line 52 def load_config load_from_cache || load_from_image end |
#load_from_cache ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/dockistrano/service_dependency.rb', line 56 def load_from_cache image_id = backing_service.image_id if image_id and File.exists?("tmp/configuration_cache/#{image_id}") YAML.load_file("tmp/configuration_cache/#{image_id}") else nil end end |
#load_from_image ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dockistrano/service_dependency.rb', line 71 def load_from_image raw_config = Docker.run(backing_service.full_image_name, command: "cat /dockistrano.yml") if raw_config.empty? or raw_config.include?("No such file or directory") if raw_config.include?("failed to mount") raise HostDirectoriesMissing else raise ContainerConfigurationMissing end else FileUtils.mkdir_p("tmp/configuration_cache") file = File.open("tmp/configuration_cache/#{backing_service.image_id}", "w+") file.write(raw_config) file.close config = YAML.load(raw_config) end end |
#tag_with_fallback(tag) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dockistrano/service_dependency.rb', line 92 def tag_with_fallback(tag) = [tag, "develop", "master", "latest"] = Docker.("#{backing_service.registry}/#{backing_service.image_name}") begin tag_suggestion = .shift final_tag = tag_suggestion if .include?(tag_suggestion) end while !final_tag and .any? if final_tag final_tag else raise NoTagFoundForImage.new("No tag found for image #{backing_service.image_name}, locally available tags: #{available_tags} `doc pull` for more tags from repository.") end end |