Class: Pvectl::Services::PullConfig
- Inherits:
-
Object
- Object
- Pvectl::Services::PullConfig
- Defined in:
- lib/pvectl/services/pull_config.rb,
sig/pvectl/services/pull_config.rbs
Overview
Orchestrates pulling resource configurations from the cluster and converting them to YAML manifests.
Instance Method Summary collapse
- #build_metadata(resource, type) ⇒ Hash[Symbol, untyped]
-
#execute(type:, ids: [], all: false, node: nil, selector: nil) ⇒ Hash
Executes the pull operation.
-
#initialize(vm_repository:, container_repository:) ⇒ PullConfig
constructor
A new instance of PullConfig.
- #pull_single(repo, resource, type) ⇒ Hash[Symbol, untyped]
- #repository_for(type) ⇒ Object
- #type_label(type) ⇒ String
Constructor Details
#initialize(vm_repository:, container_repository:) ⇒ PullConfig
Returns a new instance of PullConfig.
15 16 17 18 |
# File 'lib/pvectl/services/pull_config.rb', line 15 def initialize(vm_repository:, container_repository:) @vm_repository = vm_repository @container_repository = container_repository end |
Instance Method Details
#build_metadata(resource, type) ⇒ Hash[Symbol, untyped]
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pvectl/services/pull_config.rb', line 78 def (resource, type) name = type == :container ? resource.hostname : resource.name { vmid: resource.vmid, name: name, node: resource.node, status: resource.status, tags: resource. }.compact end |
#execute(type:, ids: [], all: false, node: nil, selector: nil) ⇒ Hash
Executes the pull operation.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pvectl/services/pull_config.rb', line 28 def execute(type:, ids: [], all: false, node: nil, selector: nil) repo = repository_for(type) manifests = [] errors = [] if all || selector resources = repo.list(node: node) resources = selector.apply(resources) if selector resources.reject!(&:template?) else resources = ids.filter_map do |id| resource = repo.get(id.to_i) unless resource errors << "#{type_label(type)} #{id} not found" nil else resource end end end resources.each do |resource| result = pull_single(repo, resource, type) if result[:error] errors << result[:error] else manifests << result end end { manifests: manifests, errors: errors } end |
#pull_single(repo, resource, type) ⇒ Hash[Symbol, untyped]
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pvectl/services/pull_config.rb', line 67 def pull_single(repo, resource, type) config = repo.fetch_config(resource.node, resource.vmid) nested = ConfigSerializer.to_nested(config, type: type) = (resource, type) yaml = ManifestSerializer.to_yaml(nested, type: type, metadata: ) { metadata: , yaml: yaml, vmid: resource.vmid } rescue StandardError => e { error: "Error pulling #{type_label(type)} #{resource.vmid}: #{e.}" } end |
#repository_for(type) ⇒ Object
63 64 65 |
# File 'lib/pvectl/services/pull_config.rb', line 63 def repository_for(type) type == :container ? @container_repository : @vm_repository end |
#type_label(type) ⇒ String
89 90 91 |
# File 'lib/pvectl/services/pull_config.rb', line 89 def type_label(type) type == :container ? "Container" : "VM" end |