Class: Pvectl::Services::PushConfig
- Inherits:
-
Object
- Object
- Pvectl::Services::PushConfig
- Defined in:
- lib/pvectl/services/push_config.rb
Overview
Orchestrates pushing YAML manifests to the Proxmox cluster. Implements a two-phase approach: prepare (validate + diff) then apply.
Constant Summary collapse
- DEFAULT_TASK_TIMEOUT =
120
Instance Method Summary collapse
-
#apply(plans) ⇒ Hash
Applies prepared plans (executes API calls).
-
#initialize(vm_repository:, container_repository:, task_repository: nil) ⇒ PushConfig
constructor
A new instance of PushConfig.
-
#prepare(yaml_string) ⇒ Hash
Prepares a push plan from a single YAML manifest string.
-
#prepare_batch(yaml_contents, filter_type: nil) ⇒ Hash
Prepares push plans from multiple YAML contents.
Constructor Details
#initialize(vm_repository:, container_repository:, task_repository: nil) ⇒ PushConfig
Returns a new instance of PushConfig.
18 19 20 21 22 |
# File 'lib/pvectl/services/push_config.rb', line 18 def initialize(vm_repository:, container_repository:, task_repository: nil) @vm_repository = vm_repository @container_repository = container_repository @task_repository = task_repository end |
Instance Method Details
#apply(plans) ⇒ Hash
Applies prepared plans (executes API calls). Tracks async task completion for resize and create operations.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/pvectl/services/push_config.rb', line 154 def apply(plans) results = [] errors = [] plans.each do |plan| begin repo = repository_for(plan[:type]) if plan[:action] == :update config_params = plan[:params].reject { |k, _| k == :digest } unless config_params.empty? repo.update(plan[:vmid], plan[:node], plan[:params]) end resize_errors = apply_resize_ops(repo, plan) if resize_errors.any? resize_errors.each { |e| errors << "Error resizing #{type_label(plan[:type])} #{plan[:vmid]}: #{e}" } results << { action: :update, vmid: plan[:vmid], type: plan[:type], success: false, error: resize_errors.join("; ") } else results << { action: :update, vmid: plan[:vmid], type: plan[:type], success: true } end elsif plan[:action] == :create upid = repo.create(plan[:node], plan[:vmid], plan[:params]) task = wait_for_task(upid) if task&.failed? error_msg = task.exitstatus errors << "Error creating #{type_label(plan[:type])} #{plan[:vmid]}: #{error_msg}" results << { action: :create, vmid: plan[:vmid], type: plan[:type], success: false, error: error_msg } else results << { action: :create, vmid: plan[:vmid], type: plan[:type], success: true, auto_id: plan[:auto_id], source_path: plan[:source_path] } end end rescue ProxmoxAPI::ApiException => e detail = extract_api_error(e) errors << "Error applying #{plan[:action]} for #{type_label(plan[:type])} #{plan[:vmid]}: #{detail}" results << { action: plan[:action], vmid: plan[:vmid], type: plan[:type], success: false, error: detail } rescue StandardError => e errors << "Error applying #{plan[:action]} for #{type_label(plan[:type])} #{plan[:vmid]}: #{e.}" results << { action: plan[:action], vmid: plan[:vmid], type: plan[:type], success: false, error: e. } end end { results: results, errors: errors } end |
#prepare(yaml_string) ⇒ Hash
Prepares a push plan from a single YAML manifest string. Validates the manifest, determines update vs create, computes diff.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pvectl/services/push_config.rb', line 29 def prepare(yaml_string) errors = ManifestSerializer.validate(yaml_string) return { plans: [], errors: errors } unless errors.empty? manifest = ManifestSerializer.from_yaml(yaml_string) type = manifest[:type] = manifest[:metadata] spec = manifest[:spec] vmid = [:vmid] repo = repository_for(type) # Convert nested spec to flat config flat_from_manifest = ConfigSerializer.from_nested(spec, type: type) # No VMID → always create with auto-allocated ID unless vmid return prepare_create(type, , flat_from_manifest, repo, auto_id: true) end # Check if resource exists (update) or not (create) resource = repo.get(vmid) if resource # UPDATE path: fetch current config, compute diff current_config = repo.fetch_config(resource.node, vmid) original_flat = ConfigSerializer.from_nested( ConfigSerializer.to_nested(current_config, type: type), type: type ) # Filter nil/empty values from manifest (treated as "not specified"). # YAML null or empty strings mean the user didn't set the value. flat_from_manifest = flat_from_manifest.reject { |_, v| v.nil? || (v.is_a?(String) && v.empty?) } # Complete manifest's complex values with sub-properties from API. # When a manifest omits sub-properties (volume, MAC, size) the API # values fill them in, preventing false diffs from partial specs. flat_from_manifest = ConfigSerializer.complete_from_api(flat_from_manifest, original_flat, type: type) # Collect readonly keys and strip them from both sides. readonly_keys = collect_readonly_keys(flat_from_manifest.merge(original_flat), type) comparable_manifest = flat_from_manifest.reject { |k, _| readonly_keys.include?(k) } # Only compare API keys that are also present in manifest. # Keys only in API are "not specified" and should not generate diffs. comparable_original = original_flat.select { |k, _| comparable_manifest.key?(k) } diff = ConfigSerializer.diff(comparable_original, comparable_manifest) if diff[:changed].empty? && diff[:added].empty? && diff[:removed].empty? return { plans: [], errors: [], no_changes: true, vmid: vmid, type: type } end update_result = build_update_params(diff, current_config, type) plan = { action: :update, type: type, vmid: vmid, node: resource.node, diff: diff, params: update_result[:params], resize_ops: update_result[:resize_ops] } { plans: [plan], errors: [] } else prepare_create(type, , flat_from_manifest, repo, vmid: vmid) end rescue StandardError => e { plans: [], errors: [e.] } end |
#prepare_batch(yaml_contents, filter_type: nil) ⇒ Hash
Prepares push plans from multiple YAML contents.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pvectl/services/push_config.rb', line 106 def prepare_batch(yaml_contents, filter_type: nil) plans = [] errors = [] skipped = [] unchanged = [] yaml_contents.each do |entry| filename = entry[:filename] content = entry[:content] # Pre-check kind filter before full prepare if filter_type begin parsed = YAML.safe_load(content) kind = ManifestSerializer::KINDS_REVERSE[parsed&.dig("kind")] if kind && kind != filter_type skipped << "#{filename}: skipped (kind #{parsed['kind']} doesn't match filter)" next end rescue Psych::SyntaxError # Will be caught by prepare end end result = prepare(content) if result[:no_changes] skipped << "#{filename}: no changes" unchanged << { vmid: result[:vmid], type: result[:type], source_path: entry[:path] } next end result[:plans].each do |p| p[:filename] = filename p[:source_path] = entry[:path] end plans.concat(result[:plans]) errors.concat(result[:errors].map { |e| "#{filename}: #{e}" }) end { plans: plans, errors: errors, skipped: skipped, unchanged: unchanged } end |