Class: Controlplane

Inherits:
Object
  • Object
show all
Defined in:
lib/core/controlplane.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Controlplane

Returns a new instance of Controlplane.



6
7
8
9
10
11
# File 'lib/core/controlplane.rb', line 6

def initialize(config)
  @config = config
  @api = ControlplaneApi.new
  @gvc = config.app
  @org = config.org
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



4
5
6
# File 'lib/core/controlplane.rb', line 4

def api
  @api
end

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/core/controlplane.rb', line 4

def config
  @config
end

#gvcObject (readonly)

Returns the value of attribute gvc.



4
5
6
# File 'lib/core/controlplane.rb', line 4

def gvc
  @gvc
end

#orgObject (readonly)

Returns the value of attribute org.



4
5
6
# File 'lib/core/controlplane.rb', line 4

def org
  @org
end

Instance Method Details

#apply(data) ⇒ Object

apply



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/core/controlplane.rb', line 252

def apply(data) # rubocop:disable Metrics/MethodLength
  Tempfile.create do |f|
    f.write(data.to_yaml)
    f.rewind
    cmd = "cpln apply #{gvc_org} --file #{f.path} > /dev/null"
    if Shell.tmp_stderr
      cmd += " 2> #{Shell.tmp_stderr.path}"
      perform(cmd)
    else
      perform!(cmd)
    end
  end
end

#delete_workload(workload) ⇒ Object



187
188
189
# File 'lib/core/controlplane.rb', line 187

def delete_workload(workload)
  api.delete_workload(org: org, gvc: gvc, workload: workload)
end

#fetch_gvc(a_gvc = gvc, a_org = org) ⇒ Object



95
96
97
# File 'lib/core/controlplane.rb', line 95

def fetch_gvc(a_gvc = gvc, a_org = org)
  api.gvc_get(gvc: a_gvc, org: a_org)
end

#fetch_gvc!(a_gvc = gvc) ⇒ Object



99
100
101
102
103
104
# File 'lib/core/controlplane.rb', line 99

def fetch_gvc!(a_gvc = gvc)
  gvc_data = fetch_gvc(a_gvc)
  return gvc_data if gvc_data

  raise "Can't find app '#{gvc}', please create it with 'cpl setup-app -a #{config.app}'."
end

#fetch_gvcsObject

gvc



82
83
84
# File 'lib/core/controlplane.rb', line 82

def fetch_gvcs
  api.gvc_list(org: org)
end

#fetch_workload(workload) ⇒ Object



120
121
122
# File 'lib/core/controlplane.rb', line 120

def fetch_workload(workload)
  api.workload_get(workload: workload, gvc: gvc, org: org)
end

#fetch_workload!(workload) ⇒ Object



124
125
126
127
128
129
# File 'lib/core/controlplane.rb', line 124

def fetch_workload!(workload)
  workload_data = fetch_workload(workload)
  return workload_data if workload_data

  raise "Can't find workload '#{workload}', please create it with 'cpl apply-template #{workload} -a #{config.app}'."
end

#fetch_workload_deployments(workload) ⇒ Object



148
149
150
# File 'lib/core/controlplane.rb', line 148

def fetch_workload_deployments(workload)
  api.workload_deployments(workload: workload, gvc: gvc, org: org)
end

#fetch_workloads(a_gvc = gvc) ⇒ Object

workload



112
113
114
# File 'lib/core/controlplane.rb', line 112

def fetch_workloads(a_gvc = gvc)
  api.workload_list(gvc: a_gvc, org: org)
end

#fetch_workloads_by_org(a_org = org) ⇒ Object



116
117
118
# File 'lib/core/controlplane.rb', line 116

def fetch_workloads_by_org(a_org = org)
  api.workload_list_by_org(org: a_org)
end

#find_domain_for(workloads) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/core/controlplane.rb', line 217

def find_domain_for(workloads)
  domains = api.list_domains(org: org)["items"]
  domains.find do |domain_data|
    route = find_domain_route(domain_data)
    next false if route.nil?

    workloads.any? { |workload| route["workloadLink"].split("/").last == workload }
  end
end

#find_domain_route(data) ⇒ Object

domain



207
208
209
210
211
212
213
214
215
# File 'lib/core/controlplane.rb', line 207

def find_domain_route(data)
  port = data["spec"]["ports"].find { |current_port| current_port["number"] == 80 || current_port["number"] == 443 }
  return nil if port.nil? || port["routes"].nil?

  route = port["routes"].find { |current_route| current_route["prefix"] == "/" }
  return nil if route.nil?

  route
end

#get_domain_workload(data) ⇒ Object



227
228
229
230
# File 'lib/core/controlplane.rb', line 227

def get_domain_workload(data)
  route = find_domain_route(data)
  route["workloadLink"].split("/").last
end

#gvc_delete(a_gvc = gvc) ⇒ Object



106
107
108
# File 'lib/core/controlplane.rb', line 106

def gvc_delete(a_gvc = gvc)
  api.gvc_delete(gvc: a_gvc, org: org)
end

#gvc_query(app_name = config.app) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/core/controlplane.rb', line 86

def gvc_query(app_name = config.app)
  # When `match_if_app_name_starts_with` is `true`, we query for any gvc containing the name,
  # otherwise we query for a gvc with the exact name.
  op = config.current[:match_if_app_name_starts_with] ? "~" : "="

  cmd = "cpln gvc query --org #{org} -o yaml --prop name#{op}#{app_name}"
  perform_yaml(cmd)
end

#image_build(image, dockerfile:, build_args: [], push: true) ⇒ Object

image



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

def image_build(image, dockerfile:, build_args: [], push: true)
  cmd = "docker build -t #{image} -f #{dockerfile}"
  build_args.each { |build_arg| cmd += " --build-arg #{build_arg}" }
  cmd += " #{config.app_dir}"
  perform!(cmd)

  image_push(image) if push
end

#image_delete(image) ⇒ Object



52
53
54
# File 'lib/core/controlplane.rb', line 52

def image_delete(image)
  api.image_delete(org: org, image: image)
end

#image_login(org_name = config.org) ⇒ Object



56
57
58
59
60
# File 'lib/core/controlplane.rb', line 56

def (org_name = config.org)
  cmd = "cpln image docker-login --org #{org_name}"
  cmd += " > /dev/null 2>&1" if Shell.tmp_stderr
  perform!(cmd)
end

#image_pull(image) ⇒ Object



62
63
64
65
66
# File 'lib/core/controlplane.rb', line 62

def image_pull(image)
  cmd = "docker pull #{image}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#image_push(image) ⇒ Object



74
75
76
77
78
# File 'lib/core/controlplane.rb', line 74

def image_push(image)
  cmd = "docker push #{image}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#image_query(app_name = config.app, org_name = config.org) ⇒ Object



47
48
49
50
# File 'lib/core/controlplane.rb', line 47

def image_query(app_name = config.app, org_name = config.org)
  cmd = "cpln image query --org #{org_name} -o yaml --max -1 --prop repository=#{app_name}"
  perform_yaml(cmd)
end

#image_tag(old_tag, new_tag) ⇒ Object



68
69
70
71
72
# File 'lib/core/controlplane.rb', line 68

def image_tag(old_tag, new_tag)
  cmd = "docker tag #{old_tag} #{new_tag}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#log_get(workload:, from:, to:) ⇒ Object



246
247
248
# File 'lib/core/controlplane.rb', line 246

def log_get(workload:, from:, to:)
  api.log_get(org: org, gvc: gvc, workload: workload, from: from, to: to)
end

#logs(workload:) ⇒ Object

logs



241
242
243
244
# File 'lib/core/controlplane.rb', line 241

def logs(workload:)
  cmd = "cpln logs '{workload=\"#{workload}\"}' --org #{org} -t -o raw --limit 200"
  perform!(cmd)
end

#profile_create(profile, token) ⇒ Object



24
25
26
27
28
# File 'lib/core/controlplane.rb', line 24

def profile_create(profile, token)
  cmd = "cpln profile create #{profile} --token #{token}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#profile_delete(profile) ⇒ Object



30
31
32
33
34
# File 'lib/core/controlplane.rb', line 30

def profile_delete(profile)
  cmd = "cpln profile delete #{profile}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#profile_exists?(profile) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/core/controlplane.rb', line 19

def profile_exists?(profile)
  cmd = "cpln profile get #{profile} -o yaml"
  perform_yaml(cmd).length.positive?
end

#profile_switch(profile) ⇒ Object

profile



15
16
17
# File 'lib/core/controlplane.rb', line 15

def profile_switch(profile)
  ENV["CPLN_PROFILE"] = profile
end

#query_workloads(workload, partial_match: false) ⇒ Object



131
132
133
134
135
# File 'lib/core/controlplane.rb', line 131

def query_workloads(workload, partial_match: false)
  op = partial_match ? "~" : "="

  api.query_workloads(org: org, gvc: gvc, workload: workload, op_type: op)
end

#set_domain_workload(data, workload) ⇒ Object



232
233
234
235
236
237
# File 'lib/core/controlplane.rb', line 232

def set_domain_workload(data, workload)
  route = find_domain_route(data)
  route["workloadLink"] = "/org/#{org}/gvc/#{gvc}/workload/#{workload}"

  api.update_domain(org: org, domain: data["name"], data: data)
end

#set_workload_env_var(workload, container:, name:, value:) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/core/controlplane.rb', line 159

def set_workload_env_var(workload, container:, name:, value:)
  data = fetch_workload!(workload)
  data["spec"]["containers"].each do |container_data|
    next unless container_data["name"] == container

    container_data["env"].each do |env_data|
      next unless env_data["name"] == name

      env_data["value"] = value
    end
  end

  api.update_workload(org: org, gvc: gvc, workload: workload, data: data)
end

#set_workload_suspend(workload, value) ⇒ Object



174
175
176
177
178
179
# File 'lib/core/controlplane.rb', line 174

def set_workload_suspend(workload, value)
  data = fetch_workload!(workload)
  data["spec"]["defaultOptions"]["suspend"] = value

  api.update_workload(org: org, gvc: gvc, workload: workload, data: data)
end

#workload_connect(workload, location:, container: nil, shell: nil) ⇒ Object



191
192
193
194
195
196
# File 'lib/core/controlplane.rb', line 191

def workload_connect(workload, location:, container: nil, shell: nil)
  cmd = "cpln workload connect #{workload} #{gvc_org} --location #{location}"
  cmd += " --container #{container}" if container
  cmd += " --shell #{shell}" if shell
  perform!(cmd)
end

#workload_exec(workload, location:, container: nil, command: nil) ⇒ Object



198
199
200
201
202
203
# File 'lib/core/controlplane.rb', line 198

def workload_exec(workload, location:, container: nil, command: nil)
  cmd = "cpln workload exec #{workload} #{gvc_org} --location #{location}"
  cmd += " --container #{container}" if container
  cmd += " -- #{command}"
  perform!(cmd)
end

#workload_force_redeployment(workload) ⇒ Object



181
182
183
184
185
# File 'lib/core/controlplane.rb', line 181

def workload_force_redeployment(workload)
  cmd = "cpln workload force-redeployment #{workload} #{gvc_org}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end

#workload_get_replicas(workload, location:) ⇒ Object



137
138
139
140
# File 'lib/core/controlplane.rb', line 137

def workload_get_replicas(workload, location:)
  cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml"
  perform_yaml(cmd)
end

#workload_get_replicas_safely(workload, location:) ⇒ Object



142
143
144
145
146
# File 'lib/core/controlplane.rb', line 142

def workload_get_replicas_safely(workload, location:)
  cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml 2> /dev/null"
  result = `#{cmd}`
  $CHILD_STATUS.success? ? YAML.safe_load(result) : nil
end

#workload_set_image_ref(workload, container:, image:) ⇒ Object



152
153
154
155
156
157
# File 'lib/core/controlplane.rb', line 152

def workload_set_image_ref(workload, container:, image:)
  cmd = "cpln workload update #{workload} #{gvc_org}"
  cmd += " --set spec.containers.#{container}.image=/org/#{config.org}/image/#{image}"
  cmd += " > /dev/null" if Shell.tmp_stderr
  perform!(cmd)
end