Class: Kdep::Commands::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/commands/migrate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_options:, command_options:, args:) ⇒ Migrate

Returns a new instance of Migrate.



17
18
19
20
21
22
# File 'lib/kdep/commands/migrate.rb', line 17

def initialize(global_options:, command_options:, args:)
  @global_options = global_options
  @command_options = command_options
  @args = args
  @ui = Kdep::UI.new
end

Class Method Details

.option_parserObject



8
9
10
11
12
13
14
15
# File 'lib/kdep/commands/migrate.rb', line 8

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep migrate [path]"
    opts.separator ""
    opts.separator "Converts old deploy files (.app-name + app.yml) to kdep format."
    opts.separator "Verifies byte-for-byte match between old materialized and new rendered output."
  end
end

Instance Method Details

#executeObject



24
25
26
27
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
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
100
101
102
# File 'lib/kdep/commands/migrate.rb', line 24

def execute
  path = @args[0]
  unless path
    @ui.error("Missing path argument")
    @ui.info("Usage: kdep migrate [path]")
    exit 1
  end

  unless File.directory?(path)
    @ui.error("Path not found: #{path}")
    exit 1
  end

  # Parse old format
  old = Kdep::OldFormat.new(path)
  begin
    name = old.app_name
    kdep_config = old.to_kdep_config
  rescue => e
    @ui.error(e.message)
    exit 1
  end

  # Create kdep/[name]/ directory with app.yml
  target_dir = File.join(Dir.pwd, "kdep", name)
  if File.exist?(target_dir)
    @ui.error("kdep/#{name} already exists")
    exit 1
  end

  FileUtils.mkdir_p(target_dir)
  File.write(File.join(target_dir, "app.yml"), YAML.dump(kdep_config))
  @ui.file_written("kdep/#{name}/app.yml")

  # Warn about secrets
  @ui.warn("Secrets must be manually migrated to kdep/#{name}/secrets.yml")

  # Render via existing pipeline
  config = Kdep::Config.new(target_dir).load
  preset = Kdep::Preset.new(config["preset"], target_dir)
  resources = preset.resources

  output_dir = File.join(Dir.pwd, ".rendered")
  writer = Kdep::Writer.new(output_dir)
  writer.clean
  renderer = Kdep::Renderer.new(config, target_dir)

  rendered_files = []
  resources.each_with_index do |resource_name, idx|
    content = renderer.render_resource(resource_name)
    written_path = writer.write(resource_name, content, idx + 1)
    rendered_files << written_path if written_path
  end

  # Compare byte-for-byte against old materialized files
  old_files = old.materialized_files
  if old_files.empty?
    @ui.warn("No materialized files found to verify against")
    @ui.success("Migration complete: kdep/#{name}/")
    return
  end

  mismatches = compare_files(old_files, rendered_files)

  if mismatches.empty?
    @ui.success("Migration verified: #{rendered_files.length} files match byte-for-byte")
    @ui.success("Migration complete: kdep/#{name}/")
  else
    @ui.error("Migration verification failed: #{mismatches.length} file(s) differ")
    mismatches.each do |m|
      @ui.error("  #{m[:file]}:")
      m[:diff].each { |line| @ui.info("    #{line}") }
    end
    # Clean up failed migration
    FileUtils.rm_rf(target_dir)
    @ui.error("Migration aborted. Generated config removed.")
    exit 1
  end
end