@@ -56,16 +56,12 @@ def moves(self):
56
56
new = copy .deepcopy (old )
57
57
new .module += migration ["module" ]
58
58
59
- print ("new" , new .name )
60
-
61
59
# Update the copied resource with the "rename" value if it is set
62
60
if "rename" in migration :
63
61
new .name = migration ["rename" ]
64
62
65
63
pair = (old .path (), new .path ())
66
64
moves .append (pair )
67
-
68
- print ("moves" , moves )
69
65
return moves
70
66
71
67
def targets (self ):
@@ -210,11 +206,11 @@ def group_by_module(resources):
210
206
]
211
207
212
208
213
- def read_state (statefile ):
209
+ def read_state (statefile = None ):
214
210
"""
215
211
Read the terraform state at the given path.
216
212
"""
217
- argv = ["terraform" , "state" , "list" , "-state" , statefile ]
213
+ argv = ["terraform" , "state" , "list" ]
218
214
result = subprocess .run (argv ,
219
215
capture_output = True ,
220
216
check = True ,
@@ -224,7 +220,7 @@ def read_state(statefile):
224
220
return elements
225
221
226
222
227
- def state_changes_for_module (module , statefile ):
223
+ def state_changes_for_module (module , statefile = None ):
228
224
"""
229
225
Compute the Terraform state changes (deletions and moves) for a single
230
226
module.
@@ -235,13 +231,13 @@ def state_changes_for_module(module, statefile):
235
231
236
232
for (old , new ) in migration .moves ():
237
233
wrapper = '"{0}"'
238
- argv = ["terraform" , "state" , "mv" , "-state" , statefile , wrapper .format (old ), wrapper .format (new )]
234
+ argv = ["terraform" , "state" , "mv" , wrapper .format (old ), wrapper .format (new )]
239
235
commands .append (argv )
240
236
241
237
return commands
242
238
243
239
244
- def migrate (statefile , dryrun = False ):
240
+ def migrate (statefile = None , dryrun = False ):
245
241
"""
246
242
Migrate the terraform state in `statefile` to match the post-refactor
247
243
resource structure.
@@ -263,7 +259,7 @@ def migrate(statefile, dryrun=False):
263
259
# unique to a GKE module.
264
260
modules_to_migrate = [
265
261
module for module in modules
266
- if module .has_resource ("google_container_cluster" , "zonal_primary " )
262
+ if module .has_resource ("google_container_cluster" , "zonal_pools " )
267
263
]
268
264
269
265
print ("---- Migrating the following modules:" )
@@ -275,6 +271,7 @@ def migrate(statefile, dryrun=False):
275
271
for module in modules_to_migrate :
276
272
commands += state_changes_for_module (module , statefile )
277
273
274
+ print ("---- Commands to run:" )
278
275
for argv in commands :
279
276
if dryrun :
280
277
print (" " .join (argv ))
@@ -285,23 +282,21 @@ def main(argv):
285
282
parser = argparser ()
286
283
args = parser .parse_args (argv [1 :])
287
284
288
- print ("cp {} {}" .format (args .oldstate , args .newstate ))
289
- shutil .copy (args .oldstate , args .newstate )
285
+ # print("cp {} {}".format(args.oldstate, args.newstate))
286
+ # shutil.copy(args.oldstate, args.newstate)
290
287
291
- migrate (args .newstate , dryrun = args .dryrun )
292
- print ("State migration complete, verify migration with "
293
- "`terraform plan -state '{}'`" .format (args .newstate ))
288
+ migrate (dryrun = True )
294
289
295
290
def argparser ():
296
291
parser = argparse .ArgumentParser (description = 'Migrate Terraform state' )
297
- parser .add_argument ('oldstate' , metavar = 'oldstate.json' ,
298
- help = 'The current Terraform state (will not be '
299
- 'modified)' )
300
- parser .add_argument ('newstate' , metavar = 'newstate.json' ,
301
- help = 'The path to the new state file' )
302
- parser .add_argument ('--dryrun' , action = 'store_true' ,
303
- help = 'Print the `terraform state mv` commands instead '
304
- 'of running the commands.' )
292
+ # parser.add_argument('oldstate', metavar='oldstate.json',
293
+ # help='The current Terraform state (will not be '
294
+ # 'modified)')
295
+ # parser.add_argument('newstate', metavar='newstate.json',
296
+ # help='The path to the new state file')
297
+ # parser.add_argument('--dryrun', action='store_true',
298
+ # help='Print the `terraform state mv` commands instead '
299
+ # 'of running the commands.')
305
300
return parser
306
301
307
302
0 commit comments