Class: Cloud::Sh::Providers::DigitalOcean
- Inherits:
-
Base
- Object
- Base
- Cloud::Sh::Providers::DigitalOcean
show all
- Defined in:
- lib/cloud/sh/providers/digital_ocean.rb
Instance Attribute Summary
Attributes inherited from Base
#account
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#initialize
#command_chain
Class Method Details
.kube_config ⇒ Object
23
24
25
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 23
def self.kube_config
File.expand_path(".kube/cloud_sh_config", "~/")
end
|
.refresh_k8s_configs(force: false) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 10
def self.refresh_k8s_configs(force: false)
return if !force && File.exist?(kube_config) && (Time.now.to_i - File.mtime(kube_config).to_i) < 3600
configs = Cloud::Sh.config.accounts.map { |account| new(account).k8s_configs }.flatten.compact
config = configs.shift
configs.each do |cfg|
config["clusters"] += cfg["clusters"]
config["contexts"] += cfg["contexts"]
config["users"] += cfg["users"]
end
config["current-context"] = Cloud::Sh.config.raw["default_kubectl_context"] || config["contexts"].first["name"]
File.write(kube_config, YAML.dump(config))
end
|
Instance Method Details
#clusters ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 50
def clusters
doctl.k8s.cluster.list.(true).map(:id, :name).map do |cluster|
account.find_cluster(cluster.name)&.enrich(cluster)
next if cluster.ignore
cluster.context = k8s_context(account, cluster)
cluster.pods = kubectl.context(cluster.context).get.pod.all_namespaces(true).(true).map(:namespace, :name).map do |pod|
pod.name = k8s_pod_name(pod.name)
pod
end.group_by(&:itself).keys.group_by(&:namespace)
yield cluster if block_given?
end.compact
end
|
#databases ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 33
def databases
list = []
doctl.db.list.(true).map(:id, :name, :engine).each do |cluster|
account.find_database(cluster.name)&.enrich(cluster)
defaultdb_uri = doctl.db.conn.with(cluster.id).format("URI").(true).map(:uri).first.uri
dbs = [OpenStruct.new(name: "")] if cluster.engine == "redis"
dbs ||= doctl.db.db.list.with(cluster.id).(true).map(:name)
dbs.each do |db|
uri = URI.parse(defaultdb_uri).tap { |uri| uri.path = "/#{db.name}" }.to_s
database = OpenStruct.new(cluster: cluster, name: db.name, uri: uri)
yield database if block_given?
list << database
end
end
list
end
|
#doctl ⇒ Object
88
89
90
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 88
def doctl
command_chain("doctl").context(account.context)
end
|
#k8s_configs ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 63
def k8s_configs
doctl.k8s.cluster.list.(true).map(:id, :name).map do |cluster|
account.find_cluster(cluster.name)&.enrich(cluster)
next if cluster.ignore
cluster_config = YAML.load(doctl.k8s.cluster.config.show.with(cluster.id).execute)
cluster_config["contexts"].first["name"] = k8s_context(account, cluster)
cluster_config
end
end
|
#k8s_context(account, cluster) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 73
def k8s_context(account, cluster)
[
account.name,
(cluster.alias unless cluster.default)
].compact.join("-").tr("._", "-")
end
|
#k8s_pod_name(name) ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 80
def k8s_pod_name(name)
parts = name.split("-")
parts.pop if parts.last =~ /^[a-z0-9]{5}$/
parts.pop if parts.last =~ /^[a-f0-9]{8,10}$/
parts.pop if parts.last =~ /^[a-f0-9]{8,10}$/
parts.join("-")
end
|
#kubectl ⇒ Object
92
93
94
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 92
def kubectl
command_chain("kubectl").kubeconfig(Cloud::Sh::Providers::DigitalOcean.kube_config)
end
|
#servers ⇒ Object
27
28
29
30
31
|
# File 'lib/cloud/sh/providers/digital_ocean.rb', line 27
def servers
doctl.compute.droplet.list.format("Name,PublicIPv4").(true).map(:name, :ip).each do |server|
yield server if block_given?
end
end
|