Class: Belt::CLI::DeployCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/deploy_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, auto_approve: false, skip_backup: false, extra_args: []) ⇒ DeployCommand

Returns a new instance of DeployCommand.



135
136
137
138
139
140
141
142
# File 'lib/belt/cli/deploy_command.rb', line 135

def initialize(env, auto_approve: false, skip_backup: false, extra_args: [])
  @env = env
  @auto_approve = auto_approve
  @skip_backup = skip_backup
  @extra_args = extra_args
  @infra_dir = TerraformCommand.find_infrastructure_dir
  @project_root = find_project_root
end

Class Method Details

.help_textObject



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/belt/cli/deploy_command.rb', line 82

def self.help_text
  <<~HELP
    Deploy your Belt application to AWS.

    Usage: belt deploy [environment] [options]
           belt deploy frontend <environment>

    This runs the full deployment lifecycle:
      1. Ensure Gemfile.lock is consistent (fix stale PATH refs)
      2. Regenerate route manifests
      3. Run pre-deploy backups (if configured in belt.rb)
      4. terraform init    (initialize providers/modules)
      5. terraform plan    (preview changes)
      6. Prompt for confirmation (unless --auto)
      7. terraform apply   (deploy changes)

    Options:
      --auto, --yes, -y    Skip confirmation prompt (auto-approve)
      --rebuild            Rebuild and push Lambda code directly (bypasses Terraform).
                           Much faster for code-only changes — packages gems via Docker,
                           zips, and pushes with `aws lambda update-function-code`.
      --skip-backup        Skip pre-deploy backup phase
      --no-backup          Alias for --skip-backup
      --backup-only        Run backups only, do not deploy
      -h, --help           Show this help

    Environment:
      Defaults to BELT_ENV if set, otherwise the first available environment.

    Backup Configuration:
      Create `infrastructure/<env>/belt.rb` to configure backups:

        Belt.configure do |config|
          config.backups do
            dynamodb :all
            cognito :users, :pool_config
            s3 :legal_documents
            retention snapshots: 90, cognito: 10, s3: 10
          end
        end

    Examples:
      belt deploy                # Deploy dev (or BELT_ENV)
      belt deploy prod           # Deploy to prod
      belt deploy dev --auto     # Deploy without confirmation (CI mode)
      belt deploy --rebuild      # Fast code push to dev (no infra changes)
      belt deploy prod --rebuild # Fast code push to prod
      belt deploy prod --skip-backup  # Skip backups for this deploy
      belt deploy prod --backup-only  # Run backups without deploying
      belt deploy frontend dev   # Deploy frontend assets only
  HELP
end

.run(args) ⇒ Object



16
17
18
19
20
21
22
23
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
# File 'lib/belt/cli/deploy_command.rb', line 16

def self.run(args)
  # Handle `belt deploy frontend <env>` as before
  if args.first == 'frontend'
    args.shift
    Belt::CLI::FrontendDeployCommand.run(args)
    return
  end

  auto_approve = false
  rebuild = false
  skip_backup = false
  backup_only = false
  filtered_args = []

  args.each do |arg|
    case arg
    when '--auto', '--yes', '-y'
      auto_approve = true
    when '--rebuild'
      rebuild = true
    when '--skip-backup', '--no-backup'
      skip_backup = true
    when '--backup-only'
      backup_only = true
    when '-h', '--help'
      puts help_text
      exit 0
    else
      filtered_args << arg
    end
  end

  env = EnvResolver.resolve(filtered_args)

  # Default to the first available environment when none specified
  if env.nil?
    available = TerraformCommand.list_environments
    if available.any?
      env = available.first
    else
      puts 'Usage: belt deploy [environment] [options]'
      puts "\nDefaults to BELT_ENV or the first available environment."
      puts "\nOptions:"
      puts '  --auto, --yes, -y    Skip confirmation prompt (auto-approve)'
      puts '  --rebuild            Rebuild and push Lambda code directly (bypasses Terraform)'
      puts '  -h, --help           Show this help'
      puts "\nExamples:"
      puts '  belt deploy                # Deploy dev (or BELT_ENV)'
      puts '  belt deploy prod           # Deploy to prod'
      puts '  belt deploy dev --auto     # Deploy without confirmation'
      puts '  belt deploy --rebuild      # Fast code push (no infra changes)'
      puts '  belt deploy frontend dev   # Deploy frontend only'
      puts "\nNo environments found. Run `belt generate environment dev` first."
      exit 1
    end
  end

  if backup_only
    new(env, auto_approve: auto_approve, skip_backup: false, extra_args: filtered_args).run_backup_only
  elsif rebuild
    new(env, auto_approve: auto_approve, skip_backup: skip_backup, extra_args: filtered_args).run_rebuild
  else
    new(env, auto_approve: auto_approve, skip_backup: skip_backup, extra_args: filtered_args).run
  end
end

Instance Method Details

#runObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/belt/cli/deploy_command.rb', line 144

def run
  validate!
  env_dir = File.join(@infra_dir, @env)

  load_and_apply_env_config!

  puts "belt → deploying #{@env} (in #{env_dir}/)\n\n"

  ensure_lockfile_consistent!
  generate_routes_if_needed
  run_backups unless @skip_backup

  Dir.chdir(env_dir) do
    run_init
    run_plan
    return unless confirm_apply?

    run_apply
  end

  puts "\n✅ Deployed #{@env} successfully!"
  print_outputs(env_dir)

  deploy_frontend_if_exists

  puts "\n   Run `belt server` to view your app locally (auto-connects to the deployed API)."
end

#run_backup_onlyObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/belt/cli/deploy_command.rb', line 172

def run_backup_only
  validate!
  load_and_apply_env_config!

  puts "belt → running backups for #{@env}\n\n"

  backup_config = load_backup_config
  if backup_config.nil?
    puts "No backup configuration found for #{@env}."
    puts "Create infrastructure/#{@env}/belt.rb to configure backups."
    puts "\nExample:"
    puts '  Belt.configure do |config|'
    puts '    config.backups do'
    puts '      dynamodb :all'
    puts '    end'
    puts '  end'
    exit 1
  end

  run_backup_phase(backup_config)
  puts "\n✅ Backups complete for #{@env}!"
end

#run_rebuildObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/belt/cli/deploy_command.rb', line 195

def run_rebuild
  validate!
  load_and_apply_env_config!
  validate_aws!

  puts "belt → rebuilding Lambda for #{@env}\n\n"

  lambda_dir = find_lambda_dir
  abort 'Error: Cannot find lambda/ directory.' unless lambda_dir

  function_name = find_lambda_function_name
  abort 'Error: Cannot determine Lambda function name from Terraform state.' unless function_name

  ensure_lockfile_consistent!
  generate_routes(lambda_dir)
  build_and_deploy(lambda_dir, function_name)
end