class Seira::Runner

Constants

CATEGORIES

Attributes

action[R]
app[R]
args[R]
category[R]
cluster[R]
project[R]
settings[R]

Public Class Methods

new() click to toggle source

Pop from beginning repeatedly for the first 4 main args, and then take the remaining back to original order for the remaining args

# File lib/seira.rb, line 48
def initialize
  @settings = Seira::Settings.new

  reversed_args = ARGV.reverse.map(&:chomp)

  # The cluster, node-pools and proxy command are not specific to any app, so that
  # arg is not in the ARGV array and should be skipped over
  if ARGV[0] == 'help'
    @category = reversed_args.pop
  elsif ARGV[0] == 'version'
    @category = reversed_args.pop
  elsif ARGV[1] == 'cluster'
    cluster = reversed_args.pop
    @category = reversed_args.pop
    @action = reversed_args.pop
    @args = reversed_args.reverse
  elsif ARGV[1] == 'node-pools'
    cluster = reversed_args.pop
    @category = reversed_args.pop
    @action = reversed_args.pop
    @args = reversed_args.reverse
  elsif ARGV[1] == 'proxy'
    cluster = reversed_args.pop
    @category = reversed_args.pop
  elsif ARGV[0] == 'setup'
    @category = reversed_args.pop
    cluster = reversed_args.pop
    @args = reversed_args.reverse
  else
    cluster = reversed_args.pop
    @app = reversed_args.pop
    @category = reversed_args.pop
    @action = reversed_args.pop
    @args = reversed_args.reverse
  end

  @cluster =
    if category == 'setup'
      cluster
    else
      @settings.full_cluster_name_for_shorthand(cluster)
    end

  # If cluster is nil, we'll show an error message later on.
  unless @cluster.nil?
    unless category == 'setup'
      @project = @settings.project_for_cluster(@cluster)
    end
  end
end

Public Instance Methods

run() click to toggle source
# File lib/seira.rb, line 99
def run
  if category == 'help'
    run_base_help
    exit(0)
  elsif category == 'version'
    puts "Seira version: #{Seira::VERSION}"
    exit(0)
  elsif category == 'setup'
    Seira::Setup.new(target: cluster, args: args, settings: settings).run
    exit(0)
  end

  base_validations

  command_class = CATEGORIES[category]

  unless command_class
    puts "Unknown command specified. Usage: 'seira <cluster> <app> <category> <action> <args..>'."
    puts "Valid categories are: #{CATEGORIES.keys.join(', ')}"
    exit(1)
  end

  if category == 'cluster'
    perform_action_validation(klass: command_class, action: action)
    command_class.new(action: action, args: args, context: passed_context, settings: settings).run
  elsif category == 'node-pools'
    perform_action_validation(klass: command_class, action: action)
    command_class.new(action: action, args: args, context: passed_context, settings: settings).run
  elsif category == 'proxy'
    command_class.new.run
  else
    perform_action_validation(klass: command_class, action: action)
    command_class.new(app: app, action: action, args: args, context: passed_context).run
  end
end

Private Instance Methods

base_validations() click to toggle source
# File lib/seira.rb, line 150
def base_validations
  # gcloud and kubectl is required, hard error if not installed
  unless system("gcloud version > /dev/null 2>&1")
    puts "Gcloud library not installed properly. Please install `gcloud` before using seira.".red
    exit(1)
  end

  unless system("kubectl version --client > /dev/null 2>&1")
    puts "Kubectl library not installed properly. Please install `kubectl` before using seira.".red
    exit(1)
  end

  # The first arg must always be the cluster. This ensures commands are not run by
  # accident on the wrong kubernetes cluster or gcloud project.
  exit(1) unless Seira::Cluster.new(action: nil, args: nil, context: nil, settings: settings).switch(target_cluster: cluster, verbose: false)
  exit(0) if simple_cluster_change?
end
passed_context() click to toggle source
# File lib/seira.rb, line 137
def passed_context
  {
    cluster: cluster,
    project: project,
    settings: settings,
    region: settings.region_for_cluster(cluster),
    zone: settings.zone_for_cluster(cluster),
    app: app,
    action: action,
    args: args
  }
end
perform_action_validation(klass:, action:) click to toggle source
# File lib/seira.rb, line 168
def perform_action_validation(klass:, action:)
  return true if simple_cluster_change?

  unless klass == Seira::Cluster || klass == Seira::NodePools || settings.applications.include?(app)
    puts "Invalid app name specified"
    exit(1)
  end

  unless klass::VALID_ACTIONS.include?(action)
    puts "Invalid action specified. Valid actions are: #{klass::VALID_ACTIONS.join(', ')}"
    exit(1)
  end
end
run_base_help() click to toggle source
# File lib/seira.rb, line 186
def run_base_help
  puts 'Seira is a library for managing Kubernetes as a PaaS.'
  puts 'All commands take the following form: `seira <cluster-name> <app-name> <category> <action> <args...>`'
  puts 'For example, `seira staging foo-app secrets list`'
  puts "Possible categories: \n\n"
  CATEGORIES.each do |key, klass|
    puts "#{key}: #{klass::SUMMARY}"
  end
  puts "\nTo get more help for a specific category, run `seira <cluster-name> <app-name> <category> help` command"
end
simple_cluster_change?() click to toggle source
# File lib/seira.rb, line 182
def simple_cluster_change?
  app.nil? && category.nil? # Special case where user is simply changing clusters
end