Class: WideEvents::Generators::StoreGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/wide_events/store/store_generator.rb

Overview

Wires the bundled wide_events store container into a host app's Kamal deployment: an accessory in config/deploy.yml plus durable ingest/query tokens under .kamal/. Delegates every structural decision to WideEvent::Kamal::DeployEditor and WideEvent::Kamal::SecretsEditor so this class only handles option parsing, Thor plumbing, and applying both edits as a single transaction: nothing is written to disk unless every preflight check on both editors has already succeeded.

Constant Summary collapse

DNS_NAME_RE =
/\A(?=.{1,253}\z)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/

Instance Method Summary collapse

Instance Method Details

#generate_accessoryObject



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
# File 'lib/generators/wide_events/store/store_generator.rb', line 30

def generate_accessory
  host = options["host"]
  hostname = options["hostname"]
  retention_days = options["retention-days"]

  return unless valid_options?(host, hostname, retention_days)

  deploy_path = File.join(destination_root, "config/deploy.yml")
  unless File.exist?(deploy_path)
    say_status :error, "config/deploy.yml not found - run `kamal init` first, then re-run this generator", :red
    return
  end

  service = options["service"] || default_service_name(deploy_path)
  image = "ghcr.io/adammiribyan/wide-events-store:#{WideEvent::VERSION}"

  deploy_result = WideEvent::Kamal::DeployEditor.new(
    File.read(deploy_path),
    service: service,
    host: host,
    hostname: hostname,
    environment: options["environment"],
    retention_days: retention_days.to_i,
    image: image
  ).apply

  if deploy_result.conflicts.any?
    say_status :refused, "config/deploy.yml was left unchanged - merge this by hand:", :red
    say deploy_result.snippet
    return
  end

  begin
    secrets_result = WideEvent::Kamal::SecretsEditor.new(root: destination_root).apply
  rescue WideEvent::Kamal::SecretsEditor::UnsafeTokenFile => e
    say_status :error, e.message, :red
    return
  end

  if deploy_result.changed?
    atomic_write(deploy_path, deploy_result.content)
    say_status :update, "config/deploy.yml"
  else
    say_status :identical, "config/deploy.yml"
  end

  secrets_result.write!
  say_status :kamal, "wide_events store accessory ready - run `kamal setup` then `kamal telemetry`"
end