Class: Kuby::Kubernetes::Spec

Inherits:
Object
  • Object
show all
Extended by:
KubeDSL::ValueFields
Defined in:
lib/kuby/kubernetes/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Spec

Returns a new instance of Spec.



13
14
15
16
17
18
19
20
# File 'lib/kuby/kubernetes/spec.rb', line 13

def initialize(environment)
  @environment = environment
  @plugins = TrailingHash.new

  # default plugins
  add_plugin(:system)
  add_plugin(:rails_app)
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



11
12
13
# File 'lib/kuby/kubernetes/spec.rb', line 11

def environment
  @environment
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



11
12
13
# File 'lib/kuby/kubernetes/spec.rb', line 11

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/kuby/kubernetes/spec.rb', line 11

def tag
  @tag
end

Instance Method Details

#after_configurationObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kuby/kubernetes/spec.rb', line 72

def after_configuration
  @plugins.each { |_, plg| plg.after_configuration }
  provider.after_configuration
  environment.docker.after_configuration

  spec = self

  # this must be done _after_ docker has been configured
  registry_secret do
    docker_config do
      registry_host spec.docker.image.image_hostname
      username spec.docker.image.credentials.username
      password spec.docker.image.credentials.password
      email spec.docker.image.credentials.email
    end
  end
end

#after_deployObject



128
129
130
131
132
133
134
135
# File 'lib/kuby/kubernetes/spec.rb', line 128

def after_deploy
  @tag ||= docker.image.current_version.main_tag

  @plugins.each { |_, plg| plg.after_deploy(resources) }
  provider.after_deploy(resources)
ensure
  @tag = nil
end

#before_deployObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/kuby/kubernetes/spec.rb', line 90

def before_deploy
  @tag ||= docker.image.current_version.main_tag

  check_dependencies!

  provider.before_deploy(resources)
  @plugins.each { |_, plg| plg.before_deploy(resources) }
ensure
  @tag = nil
end

#check_dependencies!(plugins = @plugins) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kuby/kubernetes/spec.rb', line 101

def check_dependencies!(plugins = @plugins)
  error_messages = []

  plugins.each do |plg_name, plg|
    plg.class.dependencies.each do |dependency|
      dependable = Kuby.dependables[dependency.name]

      unless dependable
        error_messages << "The #{plg_name} plugin depends on #{dependency.name}, "\
          "but that dependency has not been registered."

        next
      end

      unless dependency.satisfied_by?(dependable)
        error_messages << "The #{plg_name} plugin depends on #{dependency.name} "\
          "#{dependency.constraints}, but the available version is #{dependable.version}."
      end
    end
  end

  unless error_messages.empty?
    error_messages.each { |msg| Kuby.logger.fatal(msg) }
    exit 1
  end
end

#configure_plugin(plugin_name, &block) ⇒ Object Also known as: add_plugin



57
58
59
60
61
62
63
64
# File 'lib/kuby/kubernetes/spec.rb', line 57

def configure_plugin(plugin_name, &block)
  unless @plugins.include?(plugin_name)
    plugin_klass = Kuby.plugins.find(plugin_name)
    @plugins[plugin_name] = plugin_klass.new(environment)
  end

  @plugins[plugin_name].configure(&block) if block
end

#deploy(tag = nil) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/kuby/kubernetes/spec.rb', line 160

def deploy(tag = nil)
  @tag = tag

  before_deploy
  provider.deploy
  after_deploy
end

#dockerObject



233
234
235
# File 'lib/kuby/kubernetes/spec.rb', line 233

def docker
  environment.docker
end

#docker_imagesObject



223
224
225
226
227
# File 'lib/kuby/kubernetes/spec.rb', line 223

def docker_images
  @docker_images ||= [
    docker.image, *@plugins.flat_map { |_, plugin| plugin.docker_images }
  ]
end

#namespace(&block) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/kuby/kubernetes/spec.rb', line 188

def namespace(&block)
  spec = self

  @namespace ||= KubeDSL.namespace do
     do
      name "#{spec.selector_app}-#{spec.environment.name}"
    end
  end

  @namespace.instance_eval(&block) if block
  @namespace
end

#plugin(plugin_name) ⇒ Object



68
69
70
# File 'lib/kuby/kubernetes/spec.rb', line 68

def plugin(plugin_name)
  @plugins[plugin_name]
end

#provider(provider_name = nil, &block) ⇒ Object



22
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
51
52
53
54
55
# File 'lib/kuby/kubernetes/spec.rb', line 22

def provider(provider_name = nil, &block)
  if provider_name
    provider_klass = Kuby.providers[provider_name]

    unless provider_klass
      begin
        # attempt to auto-require
        require "kuby/#{provider_name}"
        provider_klass = Kuby.providers[provider_name]
      rescue LoadError
      end
    end

    if provider_klass
      if !@provider || !@provider.is_a?(provider_klass)
        @provider = provider_klass.new(environment)
      end

      @provider.configure(&block)
    else
      msg = if provider_name
        "no provider registered with name #{provider_name}, "\
          'do you need to add a gem to your Gemfile and/or a '\
          'require statement to your Kuby config?'
      else
        'no provider configured'
      end

      raise MissingProviderError, msg
    end
  end

  @provider
end

#registry_secret(&block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/kuby/kubernetes/spec.rb', line 201

def registry_secret(&block)
  spec = self

  @registry_secret ||= RegistrySecret.new do
     do
      name "#{spec.selector_app}-registry-secret"
      namespace spec.namespace..name
    end
  end

  @registry_secret.instance_eval(&block) if block
  @registry_secret
end

#resourcesObject



215
216
217
218
219
220
221
# File 'lib/kuby/kubernetes/spec.rb', line 215

def resources
  @resources ||= Manifest.new([
    namespace,
    registry_secret,
    *@plugins.flat_map { |_, plugin| plugin.resources }
  ].compact)
end

#rollbackObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/kuby/kubernetes/spec.rb', line 168

def rollback
  # it sucks that we have to reach into the rails app for this...
  depl = provider.kubernetes_cli.get_object(
    'deployment',
    namespace..name,
    plugin(:rails_app).deployment..name
  )

  image_url = depl.dig('spec', 'template', 'spec', 'containers', 0, 'image')

  unless image_url
    raise MissingDeploymentError, "couldn't find an existing deployment"
  end

  deployed_tag = image_url.split(':').last
  previous_tag = docker..previous_tag(deployed_tag)

  deploy(previous_tag)
end

#selector_appObject



229
230
231
# File 'lib/kuby/kubernetes/spec.rb', line 229

def selector_app
  @selector_app ||= environment.app_name.downcase
end

#setup(only: []) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kuby/kubernetes/spec.rb', line 137

def setup(only: [])
  plugins = if only.empty?
    @plugins
  else
    @plugins.each_with_object({}) do |(name, plg), memo|
      memo[name] = plg if only.include?(name)
    end
  end

  check_dependencies!(plugins)

  if only.empty?
    provider.before_setup
    provider.setup
  end

  plugins.each { |_, plg| plg.before_setup }
  plugins.each { |_, plg| plg.setup }
  plugins.each { |_, plg| plg.after_setup }

  provider.after_setup
end