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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vidar/cli.rb', line 46

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



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

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



60
61
62
63
# File 'lib/vidar/cli.rb', line 60

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



218
219
220
# File 'lib/vidar/cli.rb', line 218

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

#deployObject



93
94
95
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
# File 'lib/vidar/cli.rb', line 93

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 ? 'all ' : ''}#{destination} container=#{container}..."
  Run.kubectl "set image #{destination} #{container}=#{Config.get!(:image)}:#{revision} #{all ? '--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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/vidar/cli.rb', line 191

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])
  containers = pod_set.containers.select(&:ready_and_running?).reject(&:istio?)

  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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vidar/cli.rb', line 162

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



241
242
243
# File 'lib/vidar/cli.rb', line 241

def notify_honeycomb
  HoneycombNotification.get.success
end

#notify_sentryObject



231
232
233
234
235
236
237
238
# File 'lib/vidar/cli.rb', line 231

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



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vidar/cli.rb', line 247

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vidar/cli.rb', line 66

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
# 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"

  docker_images = `docker images --format "{{.Repository}}:{{.Tag}}"`.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 docker_images.include?(base_image)

  Log.info "Docker images:"
  Log.info docker_images.join("\n")
end

#releaseObject



152
153
154
155
156
157
158
# File 'lib/vidar/cli.rb', line 152

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/vidar/cli.rb', line 139

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 ? 'all ' : ''}#{destination} container=#{container}..."
  Run.kubectl "set image #{destination} #{container}=#{Config.get!(:image)}:#{revision} #{all ? '--all' : ''}"
end

#sshObject



225
226
227
# File 'lib/vidar/cli.rb', line 225

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