Class: EksCli::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/eks_cli/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name) ⇒ Config

Returns a new instance of Config.



14
15
16
# File 'lib/eks_cli/config.rb', line 14

def initialize(cluster_name)
  @cluster_name = cluster_name
end

Class Method Details

.[](cluster_name) ⇒ Object



8
9
10
# File 'lib/eks_cli/config.rb', line 8

def [](cluster_name)
  new(cluster_name)
end

Instance Method Details

#[](k) ⇒ Object



25
26
27
# File 'lib/eks_cli/config.rb', line 25

def [](k)
  read_from_disk[k]
end

#add_user(arn, username, groups) ⇒ Object



64
65
66
# File 'lib/eks_cli/config.rb', line 64

def add_user(arn, username, groups)
  write({"users" => {arn => {"username" => username, "groups" => groups}}})
end

#bootstrap(attrs) ⇒ Object



47
48
49
50
51
52
# File 'lib/eks_cli/config.rb', line 47

def bootstrap(attrs)
  write_to_file(attrs, config_path)
  write_to_file({}, state_path)
  write_to_file({}, groups_path)
  Log.info "written configuration files to:\n#{config_path}\n#{state_path}\n#{groups_path}"
end

#for_group(group_name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/eks_cli/config.rb', line 29

def for_group(group_name)
  all = read_from_disk
  group = group_defaults
    .merge(all["groups"][group_name])
    .merge(all.slice("cluster_name", "control_plane_sg_id", "nodes_sg_id", "vpc_id"))
  group["subnets"] = all["subnets"][0..(group["num_subnets"]-1)].join(",")
  group
end

#read_from_diskObject



18
19
20
21
22
23
# File 'lib/eks_cli/config.rb', line 18

def read_from_disk
  base = read(config_path)
  base["cluster_name"] = @cluster_name
  base = base.merge(read(state_path)).merge(read(groups_path))
  base
end

#set_iam_policies(policies) ⇒ Object



54
55
56
# File 'lib/eks_cli/config.rb', line 54

def set_iam_policies(policies)
  write({iam_policies: policies}, :groups)
end

#update_nodegroup(options) ⇒ Object



58
59
60
61
62
# File 'lib/eks_cli/config.rb', line 58

def update_nodegroup(options)
  options = options.slice("ami", "group_name", "instance_type", "num_subnets", "ssh_key_name", "taints", "min", "max")
  raise "bad nodegroup name #{options["group_name"]}" if options["group_name"] == nil || options["group_name"].empty?
  write({groups: { options["group_name"] => options }}, :groups)
end

#write(attrs, to = :state) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/eks_cli/config.rb', line 38

def write(attrs, to = :state)
  path = resolve_config_file(to)
  current = read(path) rescue {}
  Log.info "updating configuration file #{path}:\n#{attrs}"
  attrs = attrs.inject({}) {|h,(k,v)| h[k.to_s] = v; h}
  updated = current.deep_merge(attrs)
  write_to_file(updated, path)
end