Class: Kuby::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/kuby/tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Tasks

Returns a new instance of Tasks.



9
10
11
# File 'lib/kuby/tasks.rb', line 9

def initialize(environment)
  @environment = environment
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



7
8
9
# File 'lib/kuby/tasks.rb', line 7

def environment
  @environment
end

Instance Method Details

#build(build_args = {}, docker_args = [], only: [], ignore_missing_args: false, context: nil, cache_from_latest: true) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kuby/tasks.rb', line 83

def build(build_args = {}, docker_args = [], only: [], ignore_missing_args: false, context: nil, cache_from_latest: true)
  check_platform(docker_args)

  if master_key = rails_app.master_key
    build_args['RAILS_MASTER_KEY'] = master_key
  end

  check_build_args(build_args) unless ignore_missing_args

  kubernetes.docker_images.each do |image|
    next unless only.empty? || only.include?(image.identifier)
    return unless (image)

    build_kwargs = {}

    if cache_from_latest
      latest_image_url = "#{image.image_url}:#{Kuby::Docker::LATEST_TAG}"
      Kuby.logger.info("Pulling image #{latest_image_url}")

      begin
        image.pull(Kuby::Docker::LATEST_TAG)
        build_kwargs[:cache_from] = latest_image_url
      rescue Kuby::Docker::PullError
      end
    end

    image = image.new_version
    Kuby.logger.info("Building image #{image.image_url} with tags #{image.tags.join(', ')}")
    image.build(build_args, docker_args, context: context, **build_kwargs)
  end
end

#deploy(tag = nil) ⇒ Object



139
140
141
# File 'lib/kuby/tasks.rb', line 139

def deploy(tag = nil)
  environment.kubernetes.deploy(tag)
end

#kubectl(*cmd) ⇒ Object



179
180
181
# File 'lib/kuby/tasks.rb', line 179

def kubectl(*cmd)
  kubernetes_cli.run_cmd(cmd)
end

#list_plugins(all: false) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/kuby/tasks.rb', line 53

def list_plugins(all: false)
  Kuby.plugins.each do |name, _|
    if environment.kubernetes.plugins.include?(name)
      Kuby.logger.info("* #{name}")
    elsif all
      Kuby.logger.info("  #{name}")
    end
  end
end

#list_rake_tasksObject



73
74
75
76
77
78
79
80
81
# File 'lib/kuby/tasks.rb', line 73

def list_rake_tasks
  Kuby.load_rake_tasks!

  rows = Rake::Task.tasks.map do |task|
    [task.name, task.full_comment || '']
  end

  puts Kuby::Utils::Table.new(%w(NAME DESCRIPTION), rows).to_s
end


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kuby/tasks.rb', line 13

def print_dockerfiles(only: [])
  kubernetes.docker_images.each do |image|
    next unless only.empty? || only.include?(image.identifier)

    image = image.current_version
    identifier = image.identifier ? " ##{image.identifier}" : ""
    Kuby.logger.info("Dockerfile for#{identifier} image #{image.image_url} with tags #{image.tags.join(', ')}")
    theme = Rouge::Themes::Base16::Solarized.new
    formatter = Rouge::Formatters::Terminal256.new(theme)
    lexer = Rouge::Lexers::Docker.new
    tokens = lexer.lex(image.dockerfile.to_s)
    puts formatter.format(tokens)
  end
end


167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kuby/tasks.rb', line 167

def print_images
  rows = kubernetes.docker_images.flat_map do |image|
    image = image.current_version

    image.tags.map do |tag|
      [image.identifier, "#{image.image_url}:#{tag}"]
    end
  end

  puts Kuby::Utils::Table.new(%w(IDENTIFIER URL), rows).to_s
end


161
162
163
164
165
# File 'lib/kuby/tasks.rb', line 161

def print_kubeconfig
  path = kubernetes.provider.kubeconfig_path
  Kuby.logger.info("Printing contents of #{path}")
  puts File.read(path)
end


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kuby/tasks.rb', line 147

def print_resources(kind = nil, name_pattern = nil)
  kubernetes.before_deploy

  name_rxp = Regexp.new(name_pattern) if name_pattern

  kubernetes.resources.each do |res|
    next if kind && res.kind_sym.to_s != kind

    next if name_rxp && !name_rxp.match?(res..name)

    puts res.to_resource.serialize.to_yaml
  end
end

#push(only: []) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/kuby/tasks.rb', line 115

def push(only: [])
  kubernetes.docker_images.each do |image|
    next unless only.empty? || only.include?(image.identifier)

    image = image.current_version
    Kuby.logger.info("Pushing image #{image.image_url} with tags #{image.tags.join(', ')}")
    push_image(image)
  end
end

#push_image(image) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kuby/tasks.rb', line 125

def push_image(image)
  return unless (image)

  begin
    image.tags.each { |tag| image.push(tag) }
  rescue Kuby::Docker::MissingTagError => e
    msg = "#{e.message} Run kuby build to build the "\
      'Docker images before running this task.'

    Kuby.logger.fatal(msg)
    Kuby.logger.fatal(e.message)
  end
end

#remote_consoleObject



205
206
207
# File 'lib/kuby/tasks.rb', line 205

def remote_console
  remote_exec('bundle exec rails console')
end

#remote_dbconsoleObject



209
210
211
# File 'lib/kuby/tasks.rb', line 209

def remote_dbconsole
  remote_exec('bundle exec rails dbconsole')
end

#remote_exec(cmd) ⇒ Object



191
192
193
194
# File 'lib/kuby/tasks.rb', line 191

def remote_exec(cmd)
  first_pod = get_first_pod
  kubernetes_cli.exec_cmd(cmd, namespace, first_pod.dig('metadata', 'name'))
end

#remote_logsObject



183
184
185
# File 'lib/kuby/tasks.rb', line 183

def remote_logs
  kubernetes_cli.logtail(namespace, match_labels.serialize)
end

#remote_restartObject



213
214
215
216
# File 'lib/kuby/tasks.rb', line 213

def remote_restart
  deployment = rails_app.deployment..name
  kubernetes_cli.restart_deployment(namespace, deployment)
end

#remote_shellObject



201
202
203
# File 'lib/kuby/tasks.rb', line 201

def remote_shell
  remote_exec(docker.distro_spec.shell_exe)
end

#remote_statusObject



187
188
189
# File 'lib/kuby/tasks.rb', line 187

def remote_status
  kubernetes_cli.run_cmd(['-n', namespace, 'get', 'pods'])
end

#remote_system(cmd) ⇒ Object



196
197
198
199
# File 'lib/kuby/tasks.rb', line 196

def remote_system(cmd)
  first_pod = get_first_pod
  kubernetes_cli.system_cmd(cmd, namespace, first_pod.dig('metadata', 'name'))
end

#remove_plugin(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kuby/tasks.rb', line 32

def remove_plugin(name)
  plugin = environment.kubernetes.plugin(name)

  # Allow running uninstallation procedures for plugins that are no longer part
  # of the environment. This is especially useful for plugins Kuby adds by default
  # in one version and removes in another, like KubeDB.
  plugin ||= if plugin_class = Kuby.plugins.find(name.to_sym)
    plugin_class.new(environment)
  end

  unless plugin
    Kuby.logger.fatal(
      "No plugin found named '#{name}'. Run `kuby plugin list -a` to show a list of all available plugins."
    )

    exit 1
  end

  plugin.remove
end

#rollbackObject



143
144
145
# File 'lib/kuby/tasks.rb', line 143

def rollback
  environment.kubernetes.rollback
end

#run_rake_tasks(tasks) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/kuby/tasks.rb', line 63

def run_rake_tasks(tasks)
  if tasks.empty?
    Kuby.logger.fatal('Please specify at least one task to run.')
    exit 1
  end

  Kuby.load_rake_tasks!
  Rake::Task[tasks.join(' ')].invoke
end

#setup(only: []) ⇒ Object



28
29
30
# File 'lib/kuby/tasks.rb', line 28

def setup(only: [])
  environment.kubernetes.setup(only: only.map(&:to_sym))
end