Class: Vidar::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/vidar/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/vidar/cli.rb', line 5

def self.exit_on_failure?
  true
end

Instance Method Details

#buildObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vidar/cli.rb', line 49

def build
  if options[:target]
    Log.info "Building #{options[:target]} image"
    Run.docker_compose "build #{options[: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_baseObject



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

#cacheObject



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

#consoleObject



222
223
224
# File 'lib/vidar/cli.rb', line 222

def console
  invoke :kube_exec, [], name: options[:name], command: options[:command] || Config.get!(:console_command)
end

#deployObject



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 = options[:revision] || Config.get!(:revision)
  kubectl_context = options[:kubectl_context] || Config.get!(:kubectl_context)
  max_tries = options[: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 = options[:destination]
  container = options[:container]
  all = options[: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

#execObject



12
13
14
15
# File 'lib/vidar/cli.rb', line 12

def exec
  target = options[:target] || Config.get!(:base_stage_name)
  Run.docker_compose("run #{target} #{options[:command]}") || exit(1)
end

#kube_execObject



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: options[: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 = options[: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 #{options[:command]} in #{container.pod_name}"
    Run.kubectl("exec -it #{container.pod_name} -- #{options[:command]}")
  end
end

#monitor_deploy_statusObject



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 = options[: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_honeycombObject



245
246
247
# File 'lib/vidar/cli.rb', line 245

def notify_honeycomb
  HoneycombNotification.get.success
end

#notify_sentryObject



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_slackObject



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: options[:message]) if slack_notification.configured?
end

#publishObject



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

#pullObject



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

#releaseObject



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_imageObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vidar/cli.rb', line 142

def set_image
  revision = options[:revision] || Config.get!(:revision)
  kubectl_context = options[:kubectl_context] || Config.get!(:kubectl_context)
  Log.info "Current kubectl context: #{kubectl_context}"

  destination = options[:destination]
  container = options[:container]
  all = options[: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

#sshObject



229
230
231
# File 'lib/vidar/cli.rb', line 229

def ssh
  invoke :kube_exec, [], name: options[:name], command: options[:command] || Config.get!(:shell_command)
end

#versionObject



18
19
20
# File 'lib/vidar/cli.rb', line 18

def version
  puts Vidar::VERSION
end