Class: EksCli::Config

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

Constant Summary collapse

AZS =
{"us-east-1" => ["us-east-1a", "us-east-1b", "us-east-1c"],
"us-west-2" => ["us-west-2a", "us-west-2b", "us-west-2c"],
"us-east-2" => ["us-east-2a", "us-east-2b", "us-east-2c"],
"us-west-1" => ["us-west-1b", "us-west-1b", "us-west-1c"]}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name) ⇒ Config

Returns a new instance of Config.



29
30
31
# File 'lib/eks_cli/config.rb', line 29

def initialize(cluster_name)
  @cluster_name = cluster_name
end

Class Method Details

.[](cluster_name) ⇒ Object



16
17
18
# File 'lib/eks_cli/config.rb', line 16

def [](cluster_name)
  new(cluster_name)
end

.s3_bucketObject



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

def s3_bucket
  @s3_bucket || raise("no s3 bucket set")
end

.s3_bucket=(bucket) ⇒ Object



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

def s3_bucket=(bucket)
  @s3_bucket = bucket
end

Instance Method Details

#[](k) ⇒ Object



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

def [](k)
  read_from_disk[k]
end

#add_user(arn, username, groups) ⇒ Object



86
87
88
# File 'lib/eks_cli/config.rb', line 86

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

#bootstrap(attrs) ⇒ Object



69
70
71
72
73
74
# File 'lib/eks_cli/config.rb', line 69

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

#deleteObject



33
34
35
36
37
38
39
# File 'lib/eks_cli/config.rb', line 33

def delete
  Log.info "deleting configuration for #{@cluster_name} at #{dir}"
  s3.delete_object(bucket: s3_bucket, key: config_path)
  s3.delete_object(bucket: s3_bucket, key: state_path)
  s3.delete_object(bucket: s3_bucket, key: groups_path)
  s3.delete_object(bucket: s3_bucket, key: dir)
end

#for_group(group_name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/eks_cli/config.rb', line 52

def for_group(group_name)
  all = read_from_disk
  group = all["groups"][group_name]
    .merge(all.slice("cluster_name", "control_plane_sg_id", "nodes_sg_id", "vpc_id"))
  group["subnets"] = group["subnets"].map {|s| all["subnets"][s-1]}.join(",")
  group
end

#read_from_diskObject



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

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



76
77
78
# File 'lib/eks_cli/config.rb', line 76

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

#update_nodegroup(options) ⇒ Object



80
81
82
83
84
# File 'lib/eks_cli/config.rb', line 80

def update_nodegroup(options)
  options = options.slice("ami", "group_name", "instance_type", "subnets", "ssh_key_name", "volume_size", "taints", "min", "max", "enable_docker_bridge", "desired", "spotinst")
  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



60
61
62
63
64
65
66
67
# File 'lib/eks_cli/config.rb', line 60

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