Class: Kuby::Tasks
- Inherits:
-
Object
- Object
- Kuby::Tasks
- Defined in:
- lib/kuby/tasks.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Instance Method Summary collapse
- #build ⇒ Object
- #deploy(tag = nil) ⇒ Object
- #dev_deployment_ok ⇒ Object
-
#initialize(environment) ⇒ Tasks
constructor
A new instance of Tasks.
- #kubectl(*cmd) ⇒ Object
- #print_dockerfile ⇒ Object
- #print_kubeconfig ⇒ Object
- #print_resources ⇒ Object
- #push ⇒ Object
- #remote_console ⇒ Object
- #remote_dbconsole ⇒ Object
- #remote_exec(cmd) ⇒ Object
- #remote_logs ⇒ Object
- #remote_restart ⇒ Object
- #remote_shell ⇒ Object
- #remote_status ⇒ Object
- #remote_system(cmd) ⇒ Object
- #rollback ⇒ Object
- #setup ⇒ Object
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
#environment ⇒ Object (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 ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/kuby/tasks.rb', line 24 def build build_args = {} unless ENV.fetch('RAILS_MASTER_KEY', '').empty? build_args['RAILS_MASTER_KEY'] = ENV['RAILS_MASTER_KEY'] end docker.cli.build( dockerfile: docker.to_dockerfile, image_url: docker..image_url, tags: docker.., build_args: build_args ) end |
#deploy(tag = nil) ⇒ Object
77 78 79 |
# File 'lib/kuby/tasks.rb', line 77 def deploy(tag = nil) environment.kubernetes.deploy(tag) end |
#dev_deployment_ok ⇒ Object
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 |
# File 'lib/kuby/tasks.rb', line 138 def dev_deployment_ok return true unless Kuby.environment.development? deployments = kubernetes_cli.get_objects( 'deployments', namespace, match_labels.serialize ) if deployments.empty? puts 'No development environment detected.' STDOUT.write('Set up development environment? (y/n): ') answer = STDIN.gets.strip.downcase return false unless answer =~ /ye?s?/ return DevSetup.new(environment).run else depl = deployments.first deployed_checksum = depl.dig('metadata', 'annotations', 'getkuby.io/dockerfile-checksum') current_checksum = docker.to_dockerfile.checksum if deployed_checksum != current_checksum puts "Development environment appears to be out-of-date." puts "Environment checksum: #{deployed_checksum}" puts "Current checksum: #{current_checksum}" STDOUT.write('Update development environment? (y/n): ') answer = STDIN.gets.strip.downcase # return true here to prevent letting an out-of-date deployment # stop us from running commands return true unless answer =~ /ye?s?/ return DevSetup.new(environment).run end end true end |
#kubectl(*cmd) ⇒ Object
99 100 101 |
# File 'lib/kuby/tasks.rb', line 99 def kubectl(*cmd) kubernetes_cli.run_cmd(cmd) end |
#print_dockerfile ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/kuby/tasks.rb', line 12 def print_dockerfile theme = Rouge::Themes::Base16::Solarized.new formatter = Rouge::Formatters::Terminal256.new(theme) lexer = Rouge::Lexers::Docker.new tokens = lexer.lex(Kuby.environment.docker.to_dockerfile.to_s) puts formatter.format(tokens) end |
#print_kubeconfig ⇒ Object
93 94 95 96 97 |
# File 'lib/kuby/tasks.rb', line 93 def print_kubeconfig path = kubernetes.provider.kubeconfig_path Kuby.logger.info("Printing contents of #{path}") puts File.read(path) end |
#print_resources ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/kuby/tasks.rb', line 85 def print_resources kubernetes.before_deploy kubernetes.resources.each do |res| puts res.to_resource.serialize.to_yaml end end |
#push ⇒ Object
39 40 41 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 |
# File 'lib/kuby/tasks.rb', line 39 def push if environment.development? fail 'Cannot push Docker images built for the development environment' end hostname = docker..image_hostname unless docker.cli.auths.include?(hostname) Kuby.logger.info("Attempting to log in to registry at #{hostname}") begin docker.cli.login( url: docker..image_host, username: docker.credentials.username, password: docker.credentials.password ) rescue Kuby::Docker::LoginError => e Kuby.logger.fatal("Couldn't log in to the registry at #{hostname}") Kuby.logger.fatal(e.) return end end image_url = docker..image_url begin docker..local..each do |tag| docker.cli.push(image_url, tag) end rescue Kuby::Docker::MissingTagError => e msg = "#{e.} Run rake kuby:build to build the"\ 'Docker image before running this task.' Kuby.logger.fatal(msg) Kuby.logger.fatal(e.) end end |
#remote_console ⇒ Object
125 126 127 |
# File 'lib/kuby/tasks.rb', line 125 def remote_console remote_exec('bundle exec rails console') end |
#remote_dbconsole ⇒ Object
129 130 131 |
# File 'lib/kuby/tasks.rb', line 129 def remote_dbconsole remote_exec('bundle exec rails dbconsole') end |
#remote_exec(cmd) ⇒ Object
111 112 113 114 |
# File 'lib/kuby/tasks.rb', line 111 def remote_exec(cmd) first_pod = get_first_pod kubernetes_cli.exec_cmd(cmd, namespace, first_pod.dig('metadata', 'name')) end |
#remote_logs ⇒ Object
103 104 105 |
# File 'lib/kuby/tasks.rb', line 103 def remote_logs kubernetes_cli.logtail(namespace, match_labels.serialize) end |
#remote_restart ⇒ Object
133 134 135 136 |
# File 'lib/kuby/tasks.rb', line 133 def remote_restart deployment = rails_app.deployment..name kubernetes_cli.restart_deployment(namespace, deployment) end |
#remote_shell ⇒ Object
121 122 123 |
# File 'lib/kuby/tasks.rb', line 121 def remote_shell remote_exec(docker.distro_spec.shell_exe) end |
#remote_status ⇒ Object
107 108 109 |
# File 'lib/kuby/tasks.rb', line 107 def remote_status kubernetes_cli.run_cmd(['-n', namespace, 'get', 'pods']) end |
#remote_system(cmd) ⇒ Object
116 117 118 119 |
# File 'lib/kuby/tasks.rb', line 116 def remote_system(cmd) first_pod = get_first_pod kubernetes_cli.system_cmd(cmd, namespace, first_pod.dig('metadata', 'name')) end |
#rollback ⇒ Object
81 82 83 |
# File 'lib/kuby/tasks.rb', line 81 def rollback environment.kubernetes.rollback end |
#setup ⇒ Object
20 21 22 |
# File 'lib/kuby/tasks.rb', line 20 def setup environment.kubernetes.setup end |