Module: KamalBackup::CLI::Helpers
- Included in:
- KamalBackup::CLI, CommandBase
- Defined in:
- lib/kamal_backup/cli.rb
Instance Method Summary collapse
- #accessory_name ⇒ Object
- #accessory_reboot_command ⇒ Object
- #bridge ⇒ Object
- #command_env ⇒ Object
- #confirm!(message) ⇒ Object
- #default_deploy_config? ⇒ Boolean
- #deploy_snippet ⇒ Object
- #deployment_mode? ⇒ Boolean
- #direct_app ⇒ Object
- #ensure_remote_version_match! ⇒ Object
- #exec_remote(argv, require_version_match: true) ⇒ Object
- #init_config_root ⇒ Object
- #local_app ⇒ Object
- #local_command_config ⇒ Object
- #local_preferences ⇒ Object
- #print_remote_version_status ⇒ Object
- #production_source_defaults ⇒ Object
- #prompt_required(label) ⇒ Object
- #redactor ⇒ Object
- #remote_version ⇒ Object
- #shared_config_path ⇒ Object
- #shared_config_source_defaults ⇒ Object
- #shared_config_template ⇒ Object
- #validate_deploy_config ⇒ Object
- #validate_deploy_mode? ⇒ Boolean
- #version_remote_mode? ⇒ Boolean
- #write_init_file(path, contents) ⇒ Object
Instance Method Details
#accessory_name ⇒ Object
87 88 89 |
# File 'lib/kamal_backup/cli.rb', line 87 def accessory_name @accessory_name ||= bridge.accessory_name(preferred: local_preferences.accessory_name) end |
#accessory_reboot_command ⇒ Object
116 117 118 119 120 121 |
# File 'lib/kamal_backup/cli.rb', line 116 def accessory_reboot_command argv = ["bin/kamal", "accessory", "reboot", accessory_name] argv.concat(["-c", [:config_file]]) if [:config_file] argv.concat(["-d", [:destination]]) if [:destination] Shellwords.join(argv) end |
#bridge ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/kamal_backup/cli.rb', line 63 def bridge @bridge ||= KamalBridge.new( redactor: redactor, config_file: [:config_file], destination: [:destination] ) end |
#command_env ⇒ Object
14 15 16 |
# File 'lib/kamal_backup/cli.rb', line 14 def command_env CLI.command_env || ENV end |
#confirm!(message) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/kamal_backup/cli.rb', line 141 def confirm!() return if [:yes] unless $stdin.tty? raise ConfigurationError, "confirmation required; rerun with --yes" end unless yes?("#{} [y/N]") raise ConfigurationError, "aborted" end end |
#default_deploy_config? ⇒ Boolean
75 76 77 |
# File 'lib/kamal_backup/cli.rb', line 75 def default_deploy_config? File.file?(File.(KamalBridge::DEFAULT_CONFIG_FILE)) end |
#deploy_snippet ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/kamal_backup/cli.rb', line 199 def deploy_snippet <<~YAML accessories: backup: image: ghcr.io/crmne/kamal-backup:#{VERSION} host: your-server.example.com files: - config/kamal-backup.yml:/app/config/kamal-backup.yml:ro env: secret: - PGPASSWORD - RESTIC_PASSWORD - AWS_ACCESS_KEY_ID - AWS_SECRET_ACCESS_KEY volumes: - "your_app_storage:/data/storage:ro" - "your_app_backup_state:/var/lib/kamal-backup" YAML end |
#deployment_mode? ⇒ Boolean
71 72 73 |
# File 'lib/kamal_backup/cli.rb', line 71 def deployment_mode? ![:destination].to_s.strip.empty? || ![:config_file].to_s.strip.empty? end |
#direct_app ⇒ Object
22 23 24 |
# File 'lib/kamal_backup/cli.rb', line 22 def direct_app @direct_app ||= App.new(config: Config.new(env: command_env), redactor: redactor) end |
#ensure_remote_version_match! ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/kamal_backup/cli.rb', line 106 def ensure_remote_version_match! return if remote_version == VERSION raise ConfigurationError, <<~MESSAGE.strip local gem version #{VERSION} does not match remote accessory version #{remote_version}. Reboot the backup accessory to pick up the latest image: #{accessory_reboot_command} MESSAGE end |
#exec_remote(argv, require_version_match: true) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/kamal_backup/cli.rb', line 95 def exec_remote(argv, require_version_match: true) ensure_remote_version_match! if require_version_match result = bridge.execute_on_accessory( accessory_name: accessory_name, command: Shellwords.join(argv) ) print(result.stdout) result end |
#init_config_root ⇒ Object
166 167 168 169 |
# File 'lib/kamal_backup/cli.rb', line 166 def init_config_root config_file = [:config_file] || KamalBridge::DEFAULT_CONFIG_FILE File.dirname(File.(config_file)) end |
#local_app ⇒ Object
26 27 28 |
# File 'lib/kamal_backup/cli.rb', line 26 def local_app @local_app ||= App.new(config: local_command_config, redactor: redactor) end |
#local_command_config ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kamal_backup/cli.rb', line 34 def local_command_config @local_command_config ||= begin if deployment_mode? Config.new( env: command_env, defaults: production_source_defaults, config_paths: [Config::LOCAL_CONFIG_PATH] ) else Config.new(env: command_env) end end end |
#local_preferences ⇒ Object
30 31 32 |
# File 'lib/kamal_backup/cli.rb', line 30 def local_preferences @local_preferences ||= Config.new(env: command_env) end |
#print_remote_version_status ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/kamal_backup/cli.rb', line 123 def print_remote_version_status status = remote_version == VERSION ? "in sync" : "out of sync" puts("local: #{VERSION}") puts("remote: #{remote_version}") puts("status: #{status}") puts("fix: #{accessory_reboot_command}") if status == "out of sync" end |
#production_source_defaults ⇒ Object
48 49 50 |
# File 'lib/kamal_backup/cli.rb', line 48 def production_source_defaults shared_config_source_defaults.merge(bridge.local_restore_defaults(accessory_name: accessory_name)) end |
#prompt_required(label) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/kamal_backup/cli.rb', line 153 def prompt_required(label) unless $stdin.tty? raise ConfigurationError, "#{label.downcase} is required; pass it on the command line" end value = ask("#{label}:").to_s.strip if value.empty? raise ConfigurationError, "#{label.downcase} is required" else value end end |
#redactor ⇒ Object
18 19 20 |
# File 'lib/kamal_backup/cli.rb', line 18 def redactor @redactor ||= Redactor.new(env: command_env) end |
#remote_version ⇒ Object
91 92 93 |
# File 'lib/kamal_backup/cli.rb', line 91 def remote_version @remote_version ||= bridge.remote_version(accessory_name: accessory_name) end |
#shared_config_path ⇒ Object
171 172 173 |
# File 'lib/kamal_backup/cli.rb', line 171 def shared_config_path File.join(init_config_root, "kamal-backup.yml") end |
#shared_config_source_defaults ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kamal_backup/cli.rb', line 52 def shared_config_source_defaults config = Config.new(env: {}, config_paths: [Config::SHARED_CONFIG_PATH], load_project_defaults: false) {}.tap do |defaults| defaults["APP_NAME"] = config.app_name if config.app_name defaults["DATABASE_ADAPTER"] = config.database_adapter if config.database_adapter defaults["RESTIC_REPOSITORY"] = config.restic_repository if config.restic_repository defaults["LOCAL_RESTORE_SOURCE_PATHS"] = config.backup_paths.join("\n") if config.backup_paths.any? end end |
#shared_config_template ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/kamal_backup/cli.rb', line 185 def shared_config_template <<~YAML accessory: backup app_name: your-app database_adapter: postgres database_url: postgres://your-app@your-db:5432/your_app_production backup_paths: - /data/storage restic_repository: s3:https://s3.example.com/your-app-backups restic_init_if_missing: true backup_schedule_seconds: 86400 YAML end |
#validate_deploy_config ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/kamal_backup/cli.rb', line 132 def validate_deploy_config config = Config.new( env: bridge.accessory_environment(accessory_name: accessory_name), config_paths: [Config::SHARED_CONFIG_PATH], load_project_defaults: false ) config.validate_backup(check_files: false) end |
#validate_deploy_mode? ⇒ Boolean
83 84 85 |
# File 'lib/kamal_backup/cli.rb', line 83 def validate_deploy_mode? deployment_mode? || default_deploy_config? end |
#version_remote_mode? ⇒ Boolean
79 80 81 |
# File 'lib/kamal_backup/cli.rb', line 79 def version_remote_mode? deployment_mode? || default_deploy_config? end |
#write_init_file(path, contents) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/kamal_backup/cli.rb', line 175 def write_init_file(path, contents) if File.exist?(path) say "Exists: #{path}", :yellow else FileUtils.mkdir_p(File.dirname(path)) File.write(path, contents) say "Created: #{path}", :green end end |