Class: Seira::Cluster

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

Constant Summary collapse

VALID_ACTIONS =
%w[help upgrade-master].freeze
SUMMARY =
"For managing whole clusters.".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

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

Constructor Details

#initialize(action:, args:, context:, settings:) ⇒ Cluster

Returns a new instance of Cluster.



15
16
17
18
19
20
# File 'lib/seira/cluster.rb', line 15

def initialize(action:, args:, context:, settings:)
  @action = action
  @args = args
  @context = context
  @settings = settings
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



13
14
15
# File 'lib/seira/cluster.rb', line 13

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



13
14
15
# File 'lib/seira/cluster.rb', line 13

def args
  @args
end

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/seira/cluster.rb', line 13

def context
  @context
end

#settingsObject (readonly)

Returns the value of attribute settings.



13
14
15
# File 'lib/seira/cluster.rb', line 13

def settings
  @settings
end

Class Method Details

.current_clusterObject



69
70
71
# File 'lib/seira/cluster.rb', line 69

def self.current_cluster
  Seira::Commands.kubectl("config current-context", context: :none, return_output: true).chomp.strip
end

.current_projectObject



73
74
75
# File 'lib/seira/cluster.rb', line 73

def self.current_project
  Seira::Commands.gcloud("config get-value project", context: :none, return_output: true).chomp.strip
end

Instance Method Details

#currentObject



77
78
79
80
# File 'lib/seira/cluster.rb', line 77

def current
  puts current_project
  puts current_cluster
end

#runObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/seira/cluster.rb', line 22

def run
  case action
  when 'help'
    run_help
  when 'upgrade-master'
    run_upgrade_master
  else
    fail "Unknown command encountered"
  end
end

#switch(target_cluster:, verbose: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/seira/cluster.rb', line 33

def switch(target_cluster:, verbose: false)
  unless target_cluster && target_cluster != "" && settings.valid_cluster_names.include?(target_cluster)
    puts "Please specify cluster as first param to any seira command"
    puts "Cluster should be one of #{settings.valid_cluster_names}"
    exit(1)
  end

  # The context in kubectl are a bit more difficult to name. List by name only and search for the right one using a simple string match
   = settings.clusters[target_cluster]

  puts("Switching to gcloud config of '#{target_cluster}' and kubernetes cluster of '#{['cluster']}'") if verbose
  unless system("gcloud config configurations activate #{target_cluster}")
    puts("Try running `seira setup #{target_cluster}` if you have not yet configured this cluster.")
    exit(1)
  end

  if settings.use_teleport?(target_cluster)
    teleport_status = Seira::Teleport::Status.new

    unless teleport_status.is_valid?
      if teleport_status.active_cluster == settings.teleport_cluster(target_cluster)
        exit(1) unless system("tsh login")
      else
        exit(1) unless system("tsh login --proxy=#{settings.teleport_cluster(target_cluster)}:443 --auth=#{settings.teleport_auth(target_cluster)}")
      end
    end

    exit(1) unless system("tsh kube login #{settings.teleport_kubernetes_cluster(target_cluster)}")
  else
    exit(1) unless system("kubectl config use-context #{['cluster']}")
  end

  # If we haven't exited by now, it was successful
  true
end