Class: Seira::Cluster

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

Constant Summary collapse

VALID_ACTIONS =
%w[help bootstrap 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

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



53
54
55
# File 'lib/seira/cluster.rb', line 53

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

.current_projectObject



57
58
59
# File 'lib/seira/cluster.rb', line 57

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

Instance Method Details

#currentObject



61
62
63
64
# File 'lib/seira/cluster.rb', line 61

def current
  puts current_project
  puts current_cluster
end

#runObject



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

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/seira/cluster.rb', line 35

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
  exit(1) unless system("gcloud config configurations activate #{target_cluster}")
  exit(1) unless system("kubectl config use-context #{['cluster']}")

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