Class: Vidar::CLI
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #build_and_cache_base ⇒ Object
- #cache ⇒ Object
- #console ⇒ Object
- #deploy ⇒ Object
- #exec ⇒ Object
- #kube_exec ⇒ Object
- #monitor_deploy_status ⇒ Object
- #notify_honeycomb ⇒ Object
- #notify_sentry ⇒ Object
- #notify_slack ⇒ Object
- #publish ⇒ Object
- #pull ⇒ Object
- #release ⇒ Object
- #set_image ⇒ Object
- #ssh ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
5 6 7 |
# File 'lib/vidar/cli.rb', line 5 def self.exit_on_failure? true end |
Instance Method Details
#build ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vidar/cli.rb', line 49 def build if [:target] Log.info "Building #{[:target]} image" Run.docker_compose "build #{[:target]}" else Log.info "Building #{Config.get!(:base_stage_name)} image" Run.docker_compose "build #{Config.get!(:base_stage_name)}" Log.info "Building #{Config.get!(:release_stage_name)} image" Run.docker_compose "build #{Config.get!(:release_stage_name)}" end end |
#build_and_cache_base ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/vidar/cli.rb', line 39 def build_and_cache_base Log.info "Building #{Config.get!(:base_stage_name)} image" Run.docker_compose "build #{Config.get!(:base_stage_name)}" Log.info "Publishing #{Config.get!(:base_stage_name)} image" Run.docker "push #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)}" end |
#cache ⇒ Object
63 64 65 66 |
# File 'lib/vidar/cli.rb', line 63 def cache Log.info "Publishing #{Config.get!(:base_stage_name)} image" Run.docker "push #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)}" end |
#console ⇒ Object
222 223 224 |
# File 'lib/vidar/cli.rb', line 222 def console invoke :kube_exec, [], name: [:name], command: [:command] || Config.get!(:console_command) end |
#deploy ⇒ Object
96 97 98 99 100 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 127 128 129 130 131 132 133 134 |
# File 'lib/vidar/cli.rb', line 96 def deploy revision = [:revision] || Config.get!(:revision) kubectl_context = [:kubectl_context] || Config.get!(:kubectl_context) max_tries = [:max_tries].to_i Log.info "Current kubectl context: #{kubectl_context}" Log.info "Looking for deploy hook..." template_name, error, status = Run.kubectl_capture3("get cronjob deploy-hook-template -o name --ignore-not-found=true") slack_notification = SlackNotification.get honeycomb_notification = HoneycombNotification.get if status.success? if template_name.to_s.empty? Log.info "No deploy hook found" else deploy_status = run_deploy_hook(template_name:, revision:, max_tries:) unless deploy_status.success? Run.kubectl "describe job deploy-hook" Log.error "Error running deploy hook template" slack_notification.failure if slack_notification.configured? honeycomb_notification.failure exit(1) end end else Log.info "Error getting deploy hook template: #{error}" slack_notification.failure if slack_notification.configured? honeycomb_notification.failure exit(1) end destination = [:destination] container = [:container] all = [:all] Log.info "Set kubectl image for #{"all " if all}#{destination} container=#{container}..." Run.kubectl "set image #{destination} #{container}=#{Config.get!(:image)}:#{revision} #{"--all" if all}" end |
#exec ⇒ Object
12 13 14 15 |
# File 'lib/vidar/cli.rb', line 12 def exec target = [:target] || Config.get!(:base_stage_name) Run.docker_compose("run #{target} #{[:command]}") || exit(1) end |
#kube_exec ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/vidar/cli.rb', line 194 def kube_exec Log.info "Current kubectl context: #{Config.get!(:kubectl_context)}" deploy_config = Config.deploy_config Log.error "ERROR: could not find deployment config for #{Config.get!(:kubectl_context)} context" unless deploy_config pod_set = K8s::PodSet.new(namespace: Config.get!(:namespace), filter: [:name]) sidecar_names = Array(Config.get(:sidecar_container_names)) containers = pod_set.containers.select(&:ready_and_running?).reject { |c| c.sidecar?(sidecar_names) } if containers.empty? name = [:name] || "any" Log.error "No running containers found with *#{name}* name" exit(1) else Log.info "Available containers:" containers.each(&:print) container = containers.detect { |c| c.name == "console" } || containers.last Log.info "Running #{[:command]} in #{container.pod_name}" Run.kubectl("exec -it #{container.pod_name} -- #{[:command]}") end end |
#monitor_deploy_status ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/vidar/cli.rb', line 165 def monitor_deploy_status max_tries = [:max_tries].to_i Log.info "Current kubectl context: #{Config.get!(:kubectl_context)}" Log.info "Checking if all containers in #{Config.get!(:namespace)} namespace(s) are ready (#{max_tries} tries)..." slack_notification = SlackNotification.get honeycomb_notification = HoneycombNotification.get deploy_status = Vidar::DeployStatus.new(namespace: Config.get!(:namespace), max_tries:) deploy_status.wait_until_completed if deploy_status.success? Log.info "OK: All containers are ready" slack_notification.success if slack_notification.configured? honeycomb_notification.success invoke :notify_sentry, [], {} else Log.error "ERROR: Some of containers are errored or not ready" slack_notification.failure if slack_notification.configured? honeycomb_notification.failure exit(1) end end |
#notify_honeycomb ⇒ Object
245 246 247 |
# File 'lib/vidar/cli.rb', line 245 def notify_honeycomb HoneycombNotification.get.success end |
#notify_sentry ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/vidar/cli.rb', line 235 def notify_sentry sentry_notification = SentryNotification.new( revision: Config.get!(:revision), deploy_config: Config.deploy_config ) sentry_notification.call if sentry_notification.configured? end |
#notify_slack ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/vidar/cli.rb', line 251 def notify_slack slack_notification = SlackNotification.new( github: Config.get!(:github), revision: Config.get!(:revision), revision_name: Config.get!(:revision_name), build_url: Config.build_url, deploy_config: Config.deploy_config ) slack_notification.deliver(message: [:message]) if slack_notification.configured? end |
#publish ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/vidar/cli.rb', line 69 def publish revision_image_tag = "#{Config.get!(:image)}:#{Config.get!(:revision)}" release_image_tag = "#{Config.get!(:image)}:#{Config.get!(:release_stage_name)}" latest_image_tag = "#{Config.get!(:image)}:latest" Log.info "Publishing #{revision_image_tag}" Run.docker "tag #{release_image_tag} #{revision_image_tag}" Run.docker "push #{revision_image_tag}" return unless Config.default_branch? Log.info "Publishing #{Config.get!(:base_stage_name)} image" Run.docker "push #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)}" Log.info "Publishing #{release_image_tag}" Run.docker "tag #{release_image_tag} #{latest_image_tag}" Run.docker "push #{release_image_tag}" Run.docker "push #{latest_image_tag}" end |
#pull ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vidar/cli.rb', line 23 def pull Log.info "Pulling #{Config.get!(:image)} tags" Run.docker "pull #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)} 2> /dev/null || true" image_names_stdout, _stderr, _status = Open3.capture3('docker images --format "{{.Repository}}:{{.Tag}}"') image_names = image_names_stdout.split("\n") base_image = "#{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:default_branch)}" Run.docker "pull #{base_image} 2> /dev/null || true" unless image_names.include?(base_image) Log.info "Docker images:" image_names.each do |image_name| puts image_name end end |
#release ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/vidar/cli.rb', line 155 def release Log.info "Build and release #{Config.get!(:image)}:#{Config.get!(:revision)}" pull Log.info "Building #{Config.get!(:release_stage_name)} image" Run.docker_compose "build #{Config.get!(:release_stage_name)}" publish end |
#set_image ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vidar/cli.rb', line 142 def set_image revision = [:revision] || Config.get!(:revision) kubectl_context = [:kubectl_context] || Config.get!(:kubectl_context) Log.info "Current kubectl context: #{kubectl_context}" destination = [:destination] container = [:container] all = [:all] Log.info "Set kubectl image for #{"all " if all}#{destination} container=#{container}..." Run.kubectl "set image #{destination} #{container}=#{Config.get!(:image)}:#{revision} #{"--all" if all}" end |