Class: Dockistrano::ServiceDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/dockistrano/service_dependency.rb

Defined Under Namespace

Classes: ContainerConfigurationMissing, DefaultEnvironmentMissingInConfiguration, HostDirectoriesMissing, NoTagFoundForImage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, name, config) ⇒ ServiceDependency

Returns a new instance of ServiceDependency.



24
25
26
27
28
# File 'lib/dockistrano/service_dependency.rb', line 24

def initialize(service, name, config)
  @service = service
  @name = name
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/dockistrano/service_dependency.rb', line 22

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/dockistrano/service_dependency.rb', line 22

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service.



22
23
24
# File 'lib/dockistrano/service_dependency.rb', line 22

def service
  @service
end

Class Method Details

.clear_cacheObject



116
117
118
# File 'lib/dockistrano/service_dependency.rb', line 116

def self.clear_cache
  `rm -rf tmp/configuration_cache/`
end

.factory(service, name, config, initialize = true) ⇒ 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. In some cases, you only need an uninitialized version of a dependency, for example when pulling new versions of images. Provide initialize=false to skip tag detection and configuration loading from the image.



11
12
13
14
15
16
17
# File 'lib/dockistrano/service_dependency.rb', line 11

def self.factory(service, name, config, initialize=true)
  if initialize
    ServiceDependency.new(service, name, config).initialized_backing_service
  else
    ServiceDependency.new(service, name, config).backing_service
  end
end

Instance Method Details

#backing_serviceObject



30
31
32
33
34
35
36
37
# File 'lib/dockistrano/service_dependency.rb', line 30

def backing_service
  @backing_service ||= Service.new("default" => {
    "registry"    => service.registry,
    "image_name"  => name,
    "tag"         => service.tag,
    "backing_service_env" =>  config
  })
end

#initialized_backing_serviceObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dockistrano/service_dependency.rb', line 39

def initialized_backing_service
  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

#load_configObject



59
60
61
# File 'lib/dockistrano/service_dependency.rb', line 59

def load_config
  load_from_cache || load_from_image
end

#load_from_cacheObject



63
64
65
66
67
68
69
70
# File 'lib/dockistrano/service_dependency.rb', line 63

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_imageObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dockistrano/service_dependency.rb', line 78

def load_from_image
  raw_config = Docker.run(backing_service.full_image_name, command: "cat /dockistrano.yml", rm: true)
  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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dockistrano/service_dependency.rb', line 99

def tag_with_fallback(tag)
  fallback_tags = [tag, "develop", "master", "latest"]

  available_tags = Docker.tags_for_image("#{backing_service.registry}/#{backing_service.image_name}")

  begin
    tag_suggestion = fallback_tags.shift
    final_tag = tag_suggestion if available_tags.include?(tag_suggestion)
  end while !final_tag and fallback_tags.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