Skip to content

Commit 3005ba7

Browse files
committed
Make migration script only support dry run
1 parent 058efce commit 3005ba7

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

helpers/migrate.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,12 @@ def moves(self):
5656
new = copy.deepcopy(old)
5757
new.module += migration["module"]
5858

59-
print("new", new.name)
60-
6159
# Update the copied resource with the "rename" value if it is set
6260
if "rename" in migration:
6361
new.name = migration["rename"]
6462

6563
pair = (old.path(), new.path())
6664
moves.append(pair)
67-
68-
print("moves", moves)
6965
return moves
7066

7167
def targets(self):
@@ -210,11 +206,11 @@ def group_by_module(resources):
210206
]
211207

212208

213-
def read_state(statefile):
209+
def read_state(statefile=None):
214210
"""
215211
Read the terraform state at the given path.
216212
"""
217-
argv = ["terraform", "state", "list", "-state", statefile]
213+
argv = ["terraform", "state", "list"]
218214
result = subprocess.run(argv,
219215
capture_output=True,
220216
check=True,
@@ -224,7 +220,7 @@ def read_state(statefile):
224220
return elements
225221

226222

227-
def state_changes_for_module(module, statefile):
223+
def state_changes_for_module(module, statefile=None):
228224
"""
229225
Compute the Terraform state changes (deletions and moves) for a single
230226
module.
@@ -235,13 +231,13 @@ def state_changes_for_module(module, statefile):
235231

236232
for (old, new) in migration.moves():
237233
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)]
239235
commands.append(argv)
240236

241237
return commands
242238

243239

244-
def migrate(statefile, dryrun=False):
240+
def migrate(statefile=None, dryrun=False):
245241
"""
246242
Migrate the terraform state in `statefile` to match the post-refactor
247243
resource structure.
@@ -263,7 +259,7 @@ def migrate(statefile, dryrun=False):
263259
# unique to a GKE module.
264260
modules_to_migrate = [
265261
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")
267263
]
268264

269265
print("---- Migrating the following modules:")
@@ -275,6 +271,7 @@ def migrate(statefile, dryrun=False):
275271
for module in modules_to_migrate:
276272
commands += state_changes_for_module(module, statefile)
277273

274+
print("---- Commands to run:")
278275
for argv in commands:
279276
if dryrun:
280277
print(" ".join(argv))
@@ -285,23 +282,21 @@ def main(argv):
285282
parser = argparser()
286283
args = parser.parse_args(argv[1:])
287284

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)
290287

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)
294289

295290
def argparser():
296291
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.')
305300
return parser
306301

307302

0 commit comments

Comments
 (0)