Class: Indocker::Configurations::ConfigurationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/configurations/configuration_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repositories:, name:, registries:, servers:, build_servers:, volumes:, networks:, env_files:, containers:) ⇒ ConfigurationBuilder

Returns a new instance of ConfigurationBuilder.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/indocker/configurations/configuration_builder.rb', line 11

def initialize(repositories:, name:, registries:, servers:, build_servers:, volumes:, networks:, env_files:, containers:)
  @repositories = repositories
  @registries = registries
  @servers = servers
  @volumes = volumes
  @networks = networks
  @build_servers = build_servers
  @env_files = env_files
  @containers = containers
  @configuration = Indocker::Configurations::Configuration.new(name)
end

Instance Attribute Details

#build_serversObject (readonly)

Returns the value of attribute build_servers.



5
6
7
# File 'lib/indocker/configurations/configuration_builder.rb', line 5

def build_servers
  @build_servers
end

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/indocker/configurations/configuration_builder.rb', line 9

def configuration
  @configuration
end

#env_filesObject (readonly)

Returns the value of attribute env_files.



8
9
10
# File 'lib/indocker/configurations/configuration_builder.rb', line 8

def env_files
  @env_files
end

#networksObject (readonly)

Returns the value of attribute networks.



7
8
9
# File 'lib/indocker/configurations/configuration_builder.rb', line 7

def networks
  @networks
end

#registriesObject (readonly)

Returns the value of attribute registries.



3
4
5
# File 'lib/indocker/configurations/configuration_builder.rb', line 3

def registries
  @registries
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



2
3
4
# File 'lib/indocker/configurations/configuration_builder.rb', line 2

def repositories
  @repositories
end

#serversObject (readonly)

Returns the value of attribute servers.



4
5
6
# File 'lib/indocker/configurations/configuration_builder.rb', line 4

def servers
  @servers
end

#volumesObject (readonly)

Returns the value of attribute volumes.



6
7
8
# File 'lib/indocker/configurations/configuration_builder.rb', line 6

def volumes
  @volumes
end

Instance Method Details

#artifacts(server_artifacts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/indocker/configurations/configuration_builder.rb', line 42

def artifacts(server_artifacts)
  if !server_artifacts.is_a?(Hash)
    Indocker.logger.error("artifacts should be a hash for configuration :#{@configuration.name}")
    exit 1
  end

  server_artifacts.each do |artifact_name, server_names|
    artifact = Indocker.artifacts.detect do |a|
      a.name == artifact_name
    end

    if !artifact
      Indocker.logger.error("artifact :#{artifact_name} was not found")
      exit 1
    end

    if @configuration.servers.empty?
      Indocker.logger.error("artifacts should go after enabled_containers section")
      exit 1
    end

    servers = @configuration.servers.select do |s|
      server_names.include?(s.name)
    end

    servers += @configuration.build_servers.select do |s|
      server_names.include?(s.name)
    end

    extra_servers = server_names - servers.map(&:name)

    if !extra_servers.empty?
      Indocker.logger.error("invalid servers #{extra_servers.inspect} provided for artifact :#{artifact_name}")
      exit 1
    end

    @configuration.set_artifact_servers(artifact, servers)
  end

  self
end

#confirm_deploymentObject



37
38
39
40
# File 'lib/indocker/configurations/configuration_builder.rb', line 37

def confirm_deployment
  @configuration.set_confirm_deployment(true)
  self
end

#enabled_containers(container_list) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/indocker/configurations/configuration_builder.rb', line 126

def enabled_containers(container_list)
  containers = container_list.keys
  extra_containers = containers - Indocker.container_files.keys

  if !extra_containers.empty?
    Indocker.logger.error("unrecognised containers: #{extra_containers.inspect} for configuration :#{Indocker.configuration.name}")
    exit 1
  end

  containers.each do |name|
    path = Indocker.container_files.fetch(name) do
      Indocker.logger.error("invalid container name :#{name} provided in enabled containers for configuration :#{@configuration.name}")
    end

    require path
  end

  @configuration.set_enabled_containers(containers.map(&:to_sym))

  container_list.each do |container_name, opts|
    if !opts.is_a?(Hash)
      Indocker.logger.error("container options should be a Hash for :#{container_name} in configuration :#{@configuration.name}")
      exit 1
    end

    count = opts[:scale] || 1

    if count <= 0
      raise ArgumentError.new("count should be > 0")
    end

    container = @configuration.containers.detect {|c| c.name == container_name}

    builder = Indocker::Containers::ContainerBuilder
      .new(name: container.name, configuration: @configuration, container: container)

    builder.scale(count)

    if opts[:build_args]
      builder.build_args(opts[:build_args])
    end

    if opts[:start]
      builder.start(opts[:start])
    end

    if opts[:redeploy_schedule]
      builder.redeploy_schedule(opts[:redeploy_schedule])
    end

    if !opts[:servers] && !opts[:servers_from]
      Indocker.logger.error("servers or servers_from should be defined for container :#{container_name} in configuration :#{@configuration.name}")
      exit 1
    end

    servers = recursively_fetch_servers(container_list, container_name)
    builder.servers(*servers)
  end

  self
end

#global_build_args(hash) ⇒ Object



206
207
208
209
# File 'lib/indocker/configurations/configuration_builder.rb', line 206

def global_build_args(hash)
  @configuration.set_global_build_args(hash)
  self
end

#scale(opts) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/indocker/configurations/configuration_builder.rb', line 188

def scale(opts)
  opts.each do |container_name, count|
    if count <= 0
      raise ArgumentError.new("count should be > 0")
    end

    container = @containers.detect {|c| c.name == container_name}

    if !container
      Indocker.logger.error("container :#{name} is not presented in enabled containers for configuration :#{@configuration.name}")
    end

    Indocker::Containers::ContainerBuilder
      .new(name: container.name, configuration: @configuration, container: container)
      .scale(count)
  end
end

#use_build_server(name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/indocker/configurations/configuration_builder.rb', line 112

def use_build_server(name)
  build_server = @build_servers.detect do |bs|
    bs.name == name
  end

  if !build_server
    raise ArgumentError.new("build_server :#{name} was not found")
  end

  @configuration.add_build_server(build_server)

  self
end

#use_env_file(name, as:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/indocker/configurations/configuration_builder.rb', line 98

def use_env_file(name, as:)
  env_file = @env_files.detect do |ef|
    ef.name == name
  end

  if !env_file
    raise ArgumentError.new("env_file :#{name} was not found")
  end

  @configuration.set_env_file(as, env_file)

  self
end

#use_registry(name, as:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/indocker/configurations/configuration_builder.rb', line 84

def use_registry(name, as:)
  registry = @registries.detect do |r|
    r.repository_name == name
  end

  if !registry
    raise ArgumentError.new("registry :#{name} was not found")
  end

  @configuration.set_registry(as, registry)

  self
end

#use_repository(name, as:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/indocker/configurations/configuration_builder.rb', line 23

def use_repository(name, as:)
  repository = @repositories.detect do |repo|
    repo.name == name
  end

  if !repository
    raise ArgumentError.new("repository :#{name} was not found")
  end

  @configuration.set_repository(as, repository)

  self
end