@@ -118,54 +118,67 @@ func runBuildPhase1(cmd *cobra.Command, args []string) error {
118118 if packerErr != nil {
119119 fmt .Printf ("\n ✗ Packer build failed: %v\n " , packerErr )
120120
121- // Try to find the packer instance by tag
122- ec2Client , err := aws .NewEC2Client (ctx , region )
123- if err != nil {
124- fmt .Printf ("⚠ Could not create EC2 client to find instance: %v\n " , err )
125- return fmt .Errorf ("packer build failed: %w" , packerErr )
121+ // Get state file path first
122+ stateFilePath := stateFile
123+ if stateFilePath == "" {
124+ var err error
125+ stateFilePath , err = state .GetDefaultStateFile ()
126+ if err != nil {
127+ fmt .Printf ("⚠ Could not get state file path: %v\n " , err )
128+ return fmt .Errorf ("packer build failed: %w" , packerErr )
129+ }
126130 }
127131
128- fmt . Println ( " \n ✓ Looking for packer instance..." )
129- instanceID , err := ec2Client . FindInstanceByTag ( ctx , "packerExecutionId" , executionID )
132+ // Load existing state or create new
133+ buildState , err := state . LoadState ( stateFilePath )
130134 if err != nil {
131- fmt .Printf ("⚠ Could not find packer instance: %v\n " , err )
132- fmt .Println ("\n Packer may have cleaned up the instance already." )
133- return fmt .Errorf ("packer build failed: %w" , packerErr )
135+ // Create new state if none exists
136+ buildState = & state.State {
137+ Region : region ,
138+ PostgresVersion : postgresVersion ,
139+ GitSHA : sha ,
140+ }
134141 }
135142
136- fmt .Printf ("✓ Found packer instance: %s\n " , instanceID )
137-
138- // Save state for debugging
139- stateFilePath := stateFile
140- if stateFilePath == "" {
141- stateFilePath , err = state .GetDefaultStateFile ()
143+ // Try to find the packer instance by tag
144+ var instanceID string
145+ ec2Client , err := aws .NewEC2Client (ctx , region )
146+ if err != nil {
147+ fmt .Printf ("⚠ Could not create EC2 client to find instance: %v\n " , err )
148+ } else {
149+ fmt .Println ("\n ✓ Looking for packer instance..." )
150+ instanceID , err = ec2Client .FindInstanceByTag (ctx , "packerExecutionId" , executionID )
142151 if err != nil {
143- return fmt .Errorf ("failed to get default state file: %w" , err )
152+ fmt .Printf ("⚠ Could not find packer instance: %v\n " , err )
153+ fmt .Println (" Packer may have cleaned up the instance already." )
154+ } else {
155+ fmt .Printf ("✓ Found packer instance: %s\n " , instanceID )
144156 }
145157 }
146158
147- buildState := & state.State {
148- Region : region ,
149- PostgresVersion : postgresVersion ,
150- GitSHA : sha ,
151- }
159+ // Save state with execution ID (and instance ID if found)
152160 buildState .SetPhaseState ("phase1" , & state.PhaseState {
153- InstanceID : instanceID ,
161+ InstanceID : instanceID , // Will be empty string if not found
154162 ExecutionID : executionID ,
155163 Timestamp : time .Now ().Format (time .RFC3339 ),
156164 })
157165
158166 if err := state .SaveState (stateFilePath , buildState ); err != nil {
159- return fmt .Errorf ("failed to save state: %w" , err )
167+ fmt .Printf ("⚠ Could not save state: %v\n " , err )
168+ } else {
169+ fmt .Printf ("\n ✓ State saved to: %s\n " , stateFilePath )
170+ fmt .Printf (" Execution ID: %s\n " , executionID )
171+ if instanceID != "" {
172+ fmt .Printf (" Instance ID: %s\n " , instanceID )
173+ fmt .Printf ("\n Next steps:\n " )
174+ fmt .Printf (" - SSH into instance: pg-ami-builder ssh\n " )
175+ fmt .Printf (" - Re-run ansible: pg-ami-builder ansible-rerun phase1 --sync-files\n " )
176+ fmt .Printf (" - Cleanup: pg-ami-builder cleanup\n " )
177+ } else {
178+ fmt .Printf ("\n No instance available for debugging (already cleaned up by packer)\n " )
179+ }
160180 }
161181
162- fmt .Printf ("\n Instance kept running for debugging:\n " )
163- fmt .Printf (" Instance ID: %s\n " , instanceID )
164- fmt .Printf (" State saved to: %s\n " , stateFilePath )
165- fmt .Printf ("\n Next steps:\n " )
166- fmt .Printf (" - SSH into instance: pg-ami-builder ssh\n " )
167- fmt .Printf (" - Re-run ansible: pg-ami-builder ansible-rerun phase1 --sync-files\n " )
168- fmt .Printf (" - Cleanup: pg-ami-builder cleanup\n " )
169182 return fmt .Errorf ("packer build failed: %w" , packerErr )
170183 }
171184
@@ -319,54 +332,67 @@ func runBuildPhase2(cmd *cobra.Command, args []string) error {
319332 if packerErr != nil {
320333 fmt .Printf ("\n ✗ Packer build failed: %v\n " , packerErr )
321334
322- // Try to find the packer instance by tag
323- ec2Client , err := aws .NewEC2Client (ctx , region )
324- if err != nil {
325- fmt .Printf ("⚠ Could not create EC2 client to find instance: %v\n " , err )
326- return fmt .Errorf ("packer build failed: %w" , packerErr )
335+ // Get state file path first
336+ stateFilePath := stateFile
337+ if stateFilePath == "" {
338+ var err error
339+ stateFilePath , err = state .GetDefaultStateFile ()
340+ if err != nil {
341+ fmt .Printf ("⚠ Could not get state file path: %v\n " , err )
342+ return fmt .Errorf ("packer build failed: %w" , packerErr )
343+ }
327344 }
328345
329- fmt . Println ( " \n ✓ Looking for packer instance..." )
330- instanceID , err := ec2Client . FindInstanceByTag ( ctx , "packerExecutionId" , executionID )
346+ // Load existing state or create new
347+ buildState , err := state . LoadState ( stateFilePath )
331348 if err != nil {
332- fmt .Printf ("⚠ Could not find packer instance: %v\n " , err )
333- fmt .Println ("\n Packer may have cleaned up the instance already." )
334- return fmt .Errorf ("packer build failed: %w" , packerErr )
349+ // Create new state if none exists
350+ buildState = & state.State {
351+ Region : region ,
352+ PostgresVersion : postgresVersion ,
353+ GitSHA : sha ,
354+ }
335355 }
336356
337- fmt .Printf ("✓ Found packer instance: %s\n " , instanceID )
338-
339- // Save state for debugging
340- stateFilePath := stateFile
341- if stateFilePath == "" {
342- stateFilePath , err = state .GetDefaultStateFile ()
357+ // Try to find the packer instance by tag
358+ var instanceID string
359+ ec2Client , err := aws .NewEC2Client (ctx , region )
360+ if err != nil {
361+ fmt .Printf ("⚠ Could not create EC2 client to find instance: %v\n " , err )
362+ } else {
363+ fmt .Println ("\n ✓ Looking for packer instance..." )
364+ instanceID , err = ec2Client .FindInstanceByTag (ctx , "packerExecutionId" , executionID )
343365 if err != nil {
344- return fmt .Errorf ("failed to get default state file: %w" , err )
366+ fmt .Printf ("⚠ Could not find packer instance: %v\n " , err )
367+ fmt .Println (" Packer may have cleaned up the instance already." )
368+ } else {
369+ fmt .Printf ("✓ Found packer instance: %s\n " , instanceID )
345370 }
346371 }
347372
348- buildState := & state.State {
349- Region : region ,
350- PostgresVersion : postgresVersion ,
351- GitSHA : sha ,
352- }
373+ // Save state with execution ID (and instance ID if found)
353374 buildState .SetPhaseState ("phase2" , & state.PhaseState {
354- InstanceID : instanceID ,
375+ InstanceID : instanceID , // Will be empty string if not found
355376 ExecutionID : executionID ,
356377 Timestamp : time .Now ().Format (time .RFC3339 ),
357378 })
358379
359380 if err := state .SaveState (stateFilePath , buildState ); err != nil {
360- return fmt .Errorf ("failed to save state: %w" , err )
381+ fmt .Printf ("⚠ Could not save state: %v\n " , err )
382+ } else {
383+ fmt .Printf ("\n ✓ State saved to: %s\n " , stateFilePath )
384+ fmt .Printf (" Execution ID: %s\n " , executionID )
385+ if instanceID != "" {
386+ fmt .Printf (" Instance ID: %s\n " , instanceID )
387+ fmt .Printf ("\n Next steps:\n " )
388+ fmt .Printf (" - SSH into instance: pg-ami-builder ssh\n " )
389+ fmt .Printf (" - Re-run ansible: pg-ami-builder ansible-rerun phase2 --sync-files\n " )
390+ fmt .Printf (" - Cleanup: pg-ami-builder cleanup\n " )
391+ } else {
392+ fmt .Printf ("\n No instance available for debugging (already cleaned up by packer)\n " )
393+ }
361394 }
362395
363- fmt .Printf ("\n Instance kept running for debugging:\n " )
364- fmt .Printf (" Instance ID: %s\n " , instanceID )
365- fmt .Printf (" State saved to: %s\n " , stateFilePath )
366- fmt .Printf ("\n Next steps:\n " )
367- fmt .Printf (" - SSH into instance: pg-ami-builder ssh\n " )
368- fmt .Printf (" - Re-run ansible: pg-ami-builder ansible-rerun phase2 --sync-files\n " )
369- fmt .Printf (" - Cleanup: pg-ami-builder cleanup\n " )
370396 return fmt .Errorf ("packer build failed: %w" , packerErr )
371397 }
372398
0 commit comments