Class: Pcrd::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pcrd/cli.rb

Constant Summary collapse

PASTEL =
Pastel.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/pcrd/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#analyzeObject



39
40
41
42
43
44
45
46
# File 'lib/pcrd/cli.rb', line 39

def analyze
  config = load_config!
  Commands::Analyze.new(config, options).run
rescue Commands::Analyze::Error => e
  raise Thor::Error, "ERROR: #{e.message}"
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
end

#cleanupObject



239
240
241
242
243
244
245
246
# File 'lib/pcrd/cli.rb', line 239

def cleanup
  config = load_config!
  Commands::Cleanup.new(config, options).run
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
rescue Pcrd::Error => e
  raise Thor::Error, "ERROR: #{e.message}"
end

#cutoverObject



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/pcrd/cli.rb', line 166

def cutover
  config = load_config!

  unless options[:"maintenance-confirmed"]
    say "\nThe application must be in maintenance mode before continuing."
    say "Maintenance mode options:"
    say "  pgBouncer:  PAUSE <database>"
    say "  Kubernetes: kubectl scale --replicas=0 deployment/app"
    say "  Rails:      enable maintenance middleware"
    say ""
    answer = ask("Is the application in maintenance mode? [y/N]")
    return unless answer.strip.downcase == "y"
  end

  source_pool = Connection::Client.new(config.source)
  target_pool = Connection::Client.new(config.target)
  printer     = Output::CutoverPrinter.new

  say "\nRunning cutover sequence..."
  orchestrator = Cutover::Orchestrator.new(
    source_pool: source_pool,
    target_pool: target_pool,
    config:      config
  )

  result = orchestrator.run(on_progress: ->(msg) { say "  #{msg}" })

  printer.print(result)

  source_pool.close
  target_pool.close

  exit(result.passed ? 0 : 1)
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
end

#migrateObject



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
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pcrd/cli.rb', line 73

def migrate
  config         = load_config!
  preflight_only = options[:"preflight-only"] || options[:"dry-run"]

  unless preflight_only
    raise Thor::Error,
          "ERROR: migrate requires a 'target' section in your config.\n\n" \
          "Use --preflight-only to validate without a target connection." if config.target.nil?

    raise Thor::Error,
          "ERROR: migrate requires a 'migrate' section in your config." if config.migrate.nil?
  end

  result = Preflight.new(config, options).run
  Output::PreflightPrinter.new.print(result)

  if preflight_only
    exit(result.passed ? 0 : 1)
    return
  end

  unless result.passed
    raise Thor::Error, "Preflight failed. Fix the issue(s) above before running migrate."
  end

  unless options[:yes]
    answer = ask("Proceed with migration? [y/N]")
    return unless answer.strip.downcase == "y"
  end

  orchestrator = Migration::Orchestrator.new(config: config, options: options)
  trap("INT")  { orchestrator.request_stop }
  trap("TERM") { orchestrator.request_stop }
  orchestrator.run
rescue Replication::Error => e
  raise Thor::Error, "ERROR: #{e.message}\n\nReplication stopped. Resume with --resume once the cause is resolved."
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
rescue Pcrd::Error => e
  raise Thor::Error, "ERROR: #{e.message}"
end

#readinessObject



143
144
145
146
147
148
149
150
151
# File 'lib/pcrd/cli.rb', line 143

def readiness
  config = load_config!
  result = Commands::Readiness.new(config, options).run
  Output::ReadinessPrinter.new.print(result)
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
rescue Pcrd::Error => e
  raise Thor::Error, "ERROR: #{e.message}"
end

#statusObject



121
122
123
124
125
126
# File 'lib/pcrd/cli.rb', line 121

def status
  config = load_config!
  Commands::Status.new(config, options).run
rescue Config::LoadError => e
  raise Thor::Error, "ERROR: #{e.message}"
end

#verifyObject



214
215
216
217
218
219
220
221
222
223
# File 'lib/pcrd/cli.rb', line 214

def verify
  config  = load_config!
  result  = Commands::Verify.new(config, options).run
  Output::CutoverPrinter.new.print_verify(result)
  exit(result.passed ? 0 : 1)
rescue Connection::Error => e
  raise Thor::Error, "Connection failed: #{e.message}"
rescue Pcrd::Error => e
  raise Thor::Error, "ERROR: #{e.message}"
end

#versionObject



19
20
21
# File 'lib/pcrd/cli.rb', line 19

def version
  say "pcrd #{Pcrd::VERSION}"
end