Class: Seira::Runner

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/seira.rb

Constant Summary collapse

CATEGORIES =
{
  'secrets' => Seira::Secrets,
  'config' => Seira::Config,
  'pods' => Seira::Pods,
  'jobs' => Seira::Jobs,
  'db' => Seira::Db,
  'app' => Seira::App,
  'cluster' => Seira::Cluster,
  'proxy' => Seira::Proxy,
  'setup' => Seira::Setup,
  'node-pools' => Seira::NodePools
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#gcloud, gcloud, kubectl, #kubectl, #tsh, tsh

Constructor Details

#initializeRunner

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/seira.rb', line 49

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

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



44
45
46
# File 'lib/seira.rb', line 44

def action
  @action
end

#appObject (readonly)

Returns the value of attribute app.



44
45
46
# File 'lib/seira.rb', line 44

def app
  @app
end

#argsObject (readonly)

Returns the value of attribute args.



44
45
46
# File 'lib/seira.rb', line 44

def args
  @args
end

#categoryObject (readonly)

Returns the value of attribute category.



44
45
46
# File 'lib/seira.rb', line 44

def category
  @category
end

#clusterObject (readonly)

Returns the value of attribute cluster.



44
45
46
# File 'lib/seira.rb', line 44

def cluster
  @cluster
end

#projectObject (readonly)

Returns the value of attribute project.



44
45
46
# File 'lib/seira.rb', line 44

def project
  @project
end

#settingsObject (readonly)

Returns the value of attribute settings.



45
46
47
# File 'lib/seira.rb', line 45

def settings
  @settings
end

Instance Method Details

#runObject



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/seira.rb', line 100

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 settings.use_teleport?(cluster)
    role_requirements = settings.teleport_role_requirements(cluster)
    filtered_requirements = role_requirements.filter { |rr| rr.matches?(category, action) }
    required_roles = filtered_requirements.map(&:roles).reduce([]) { |a, b| a + b }.uniq

    status = Seira::Teleport::Status.new

    unless required_roles.empty? || required_roles.any? { |r| status.has_role?(r) }
      reviewer = ENV['TELEPORT_REVIEWER']
      args.each do |arg|
        if arg.start_with? '--reviewer='
          reviewer = arg.split('=')[1]
        end
      end

      Seira::Teleport::Request.new(required_roles, reviewer: reviewer, context: passed_context).invoke
    end
  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