Class: Pvectl::Services::PullConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/pull_config.rb

Overview

Orchestrates pulling resource configurations from the cluster and converting them to YAML manifests.

Examples:

Pull a single VM

service = PullConfig.new(vm_repository: vm_repo, container_repository: ct_repo)
result = service.execute(type: :vm, ids: [100])
result[:manifests].each { |m| puts m[:yaml] }

Instance Method Summary collapse

Constructor Details

#initialize(vm_repository:, container_repository:) ⇒ PullConfig

Returns a new instance of PullConfig.

Parameters:



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

#execute(type:, ids: [], all: false, node: nil, selector: nil) ⇒ Hash

Executes the pull operation.

Parameters:

  • type (Symbol)

    :vm or :container

  • ids (Array<Integer>) (defaults to: [])

    specific resource IDs (optional)

  • all (Boolean) (defaults to: false)

    pull all resources

  • node (String, nil) (defaults to: nil)

    limit to specific node

  • selector (Object, nil) (defaults to: nil)

    selector for filtering

Returns:

  • (Hash)

    { manifests: Array<Hash>, errors: Array<String> }



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