Class: Pvectl::Services::PullConfig

Inherits:
Object
  • Object
show all
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.

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

#build_metadata(resource, type) ⇒ Hash[Symbol, untyped]

Parameters:

  • resource (Object)
  • type (Symbol)

Returns:

  • (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.tags
  }.compact
end

#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

  • type: (Symbol)
  • ids: (Array[Integer]) (defaults to: [])
  • all: (Boolean) (defaults to: false)
  • node: (String, nil) (defaults to: nil)
  • selector: (Object, nil) (defaults to: nil)

Returns:

  • (Hash)

    { manifests: Array, errors: Array }



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]

Parameters:

  • repo (Object)
  • resource (Object)
  • type (Symbol)

Returns:

  • (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.message}" }
end

#repository_for(type) ⇒ Object

Parameters:

  • type (Symbol)

Returns:

  • (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

Parameters:

  • type (Symbol)

Returns:

  • (String)


89
90
91
# File 'lib/pvectl/services/pull_config.rb', line 89

def type_label(type)
  type == :container ? "Container" : "VM"
end