Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add production.rake helpers for IbmCloud #690

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions lib/tasks/production.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module Kubernetes
current_namespace == namespace || system("kubectl config set-context --current --namespace=#{namespace}")
end

def self.edit_config_map(name)
system("kubectl edit configmap #{name}")
def self.edit_secret(name)
system("kubectl edit secret #{name}")
end

def self.pod_from_deployment(deployment)
Expand Down Expand Up @@ -50,6 +50,34 @@ module Kubernetes
end
end

module IbmCloud
def self.available?
!`which ibmcloud`.chomp.empty?
end

def self.api_key
ENV["IBMCLOUD_BOT_API_KEY"]
end

def self.logged_in_as
`ibmcloud target --output json | jq -r .user.user_email`.chomp
end

def self.logged_in?
logged_in_as.start_with?("imbot")
end

def self.login
return false unless api_key

system({"IBMCLOUD_API_KEY" => api_key}, "ibmcloud login -r us-east -g manageiq", [:out, :err] => "/dev/null")
end

def self.ks_cluster_config(cluster_name)
system("ibmcloud ks cluster config --cluster #{cluster_name}", [:out, :err] => "/dev/null")
end
end

# rubocop:disable Style/StderrPuts

namespace :production do
Expand All @@ -60,22 +88,16 @@ namespace :production do
context = "#{cluster_name}/#{cluster_id}"
namespace = "bot"

unless Kubernetes.available?
$stderr.puts "ERROR: You must install kubectl command"
exit 1
end

unless Kubernetes.context?(context)
$stderr.puts "ERROR: kubernetes context does not exist"
$stderr.puts
$stderr.puts "1. Install the ibmcloud cli as per https://cloud.ibm.com/docs/containers?topic=containers-cli-install"
$stderr.puts "2. `ibmcloud login -r us-east -g manageiq --sso`"
$stderr.puts "3. `ibmcloud ks cluster config --cluster #{cluster_name}`"
exit 1
end

exit 1 unless Kubernetes.use_context(context) && Kubernetes.use_namespace(namespace)
puts
raise "kubectl command not installed" unless Kubernetes.available?
raise "ibmcloud command not installed" unless IbmCloud.available?
raise "Unable to login with ibmcloud command" unless IbmCloud.logged_in? || (IbmCloud.login && IbmCloud.logged_in?)
raise "Unable to configure the kubernetes cluster for ibmcloud command" unless IbmCloud.ks_cluster_config(cluster_name)
raise "Kubernetes context does not exist" unless Kubernetes.context?(context)
raise "Kubernetes context is invalid" unless Kubernetes.use_context(context)
raise "Unable to set kubernetes namespace" unless Kubernetes.use_namespace(namespace)
rescue => e
$stderr.puts "ERROR: #{e.message}"
exit 1
end

desc "Restart all production pods"
Expand All @@ -89,9 +111,9 @@ namespace :production do
puts "Restarting the ui pod...Complete"
end

desc "Edit the bot settings in production"
task :edit_settings => :set_context do
exit 1 unless Kubernetes.edit_config_map("bot-settings")
desc "Edit the bot token in production"
task :edit_token => :set_context do
exit 1 unless Kubernetes.edit_secret("config")

puts "Restarting the queue-worker pod..."
exit 1 unless Kubernetes.restart_deployment_pods("queue-worker")
Expand Down