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.



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

def initialize(environment)
  @environment = environment
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

Instance Method Details

#build(build_args = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/kuby/tasks.rb', line 28

def build(build_args = {})
  kubernetes.docker_images.each do |image|
    image = image.new_version
    Kuby.logger.info("Building image #{image.image_url} with tags #{image.tags.join(', ')}")
    image.build(build_args)
  end
end

#deploy(tag = nil) ⇒ Object



72
73
74
# File 'lib/kuby/tasks.rb', line 72

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

#kubectl(*cmd) ⇒ Object



94
95
96
# File 'lib/kuby/tasks.rb', line 94

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


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

def print_dockerfiles
  kubernetes.docker_images.each do |image|
    image = image.current_version
    Kuby.logger.info("Dockerfile for 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


88
89
90
91
92
# File 'lib/kuby/tasks.rb', line 88

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


80
81
82
83
84
85
86
# File 'lib/kuby/tasks.rb', line 80

def print_resources
  kubernetes.before_deploy

  kubernetes.resources.each do |res|
    puts res.to_resource.serialize.to_yaml
  end
end

#pushObject



36
37
38
39
40
41
42
# File 'lib/kuby/tasks.rb', line 36

def push
  kubernetes.docker_images.each do |image|
    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



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
# File 'lib/kuby/tasks.rb', line 44

def push_image(image)
  if image.credentials.username && !image.docker_cli.auths.include?(image.image_host)
    Kuby.logger.info("Attempting to log in to registry at #{image.image_host}")

    begin
      image.docker_cli.(
        url: image.image_host,
        username: image.credentials.username,
        password: image.credentials.password
      )
    rescue Kuby::Docker::LoginError => e
      Kuby.logger.fatal("Couldn't log in to the registry at #{image.image_host}")
      Kuby.logger.fatal(e.message)
      return
    end
  end

  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



120
121
122
# File 'lib/kuby/tasks.rb', line 120

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

#remote_dbconsoleObject



124
125
126
# File 'lib/kuby/tasks.rb', line 124

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

#remote_exec(cmd) ⇒ Object



106
107
108
109
# File 'lib/kuby/tasks.rb', line 106

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

#remote_logsObject



98
99
100
# File 'lib/kuby/tasks.rb', line 98

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

#remote_restartObject



128
129
130
131
# File 'lib/kuby/tasks.rb', line 128

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

#remote_shellObject



116
117
118
# File 'lib/kuby/tasks.rb', line 116

def remote_shell
  remote_exec(docker.distro_spec.shell_exe)
end

#remote_statusObject



102
103
104
# File 'lib/kuby/tasks.rb', line 102

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

#remote_system(cmd) ⇒ Object



111
112
113
114
# File 'lib/kuby/tasks.rb', line 111

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

#rollbackObject



76
77
78
# File 'lib/kuby/tasks.rb', line 76

def rollback
  environment.kubernetes.rollback
end

#setupObject



24
25
26
# File 'lib/kuby/tasks.rb', line 24

def setup
  environment.kubernetes.setup
end