-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-migrate-mysql.py
executable file
·101 lines (90 loc) · 4.81 KB
/
cf-migrate-mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3.6
import subprocess
import json
import argparse
import sys
class CfCli:
def __init__(self, instance):
self.instance = instance
def curl(self, url):
return json.loads(self.cmd("curl '" + url +"'"))
def cmd(self, command):
print("Running cf: " + command)
completed = subprocess.run("CF_HOME="+ self.instance + " cf " + command, shell=True, stdout=subprocess.PIPE)
if(completed.returncode != 0):
print("Failed running")
print(completed.stdout)
sys.exit(1)
return completed.stdout.decode().rstrip()
def main():
parser = argparse.ArgumentParser(description="Moves MySQL from one space to another including creating services.\
Service plans must be the same between environments.")
parser.add_argument("--src-api", dest="src_api",
help="URL for source API", required=True)
parser.add_argument("--src-user", dest="src_user",
help="username for source API", required=True)
parser.add_argument("--src-pass", dest="src_pass",
help="password for source API", required=True)
parser.add_argument("--src-org", dest="src_org",
help="org name for source", required=True)
parser.add_argument("--src-space", dest="src_space",
help="space name for source", required=True)
parser.add_argument("--dst-api", dest="dst_api",
help="URL for destination API", required=True)
parser.add_argument("--dst-user", dest="dst_user",
help="username for destination API", required=True)
parser.add_argument("--dst-pass", dest="dst_pass",
help="password for destination API", required=True)
parser.add_argument("--dst-org", dest="dst_org",
help="org name for destination", required=True)
parser.add_argument("--dst-space", dest="dst_space",
help="space name for destination", required=True)
args = parser.parse_args()
(src, org_guid, space_guid) = build_cli("src", vars(args))
(dst, dst_org_guid, dst_space_guid) = build_cli("dst", vars(args))
space_obj = src.curl("v2/spaces/" + space_guid + "/summary")
for service in space_obj['services']:
if service['service_plan']['service']['label'] == "p-mysql":
process_service(service, service['service_plan'], src, dst, dst_space_guid)
def build_cli (name, options):
dst = CfCli(name)
dst.cmd("api --skip-ssl-validation " + options[name + "_api"])
dst.cmd("auth " + options[name + "_user"] + " " + options[name + "_pass"])
dst.cmd("target -o " + options[name + "_org"])
org_guid = dst.cmd("org --guid " + options[name + "_org"])
dst.cmd("target -s " + options[name + "_space"])
space_guid = dst.cmd("space --guid " + options[name + "_space"])
return (dst, org_guid, space_guid)
def process_service(service, service_plan, src, dst, dst_space_guid):
# Get the database contents
src.cmd("create-service-key " + service['name'] + " migration_key")
sk_obj = src.curl("v2/service_instances/"+service['guid']+"/service_keys?q=name:migration_key")
entity = sk_obj['resources'][0]['entity']['credentials']
mysqldump(entity['hostname'], entity['port'], entity['username'], entity['password'], entity['name'])
src.cmd("delete-service-key -f " + service['name'] + " migration_key")
# Restore into new environment
dst.cmd("create-service p-mysql " + service_plan['name'] + " " + service['name'])
dst.cmd("create-service-key " + service['name'] + " migration_key")
dst_svc_guid = dst.cmd("service --guid " + service['name'])
dst_sk = dst.curl("v2/service_instances/"+dst_svc_guid+"/service_keys?q=name:migration_key")
dst_entity = dst_sk['resources'][0]['entity']['credentials']
mysqlimport(dst_entity['hostname'], dst_entity['port'], dst_entity['username'], dst_entity['password'], dst_entity['name'], entity['name'])
dst.cmd("delete-service-key -f " + service['name'] + " migration_key")
def mysqldump(hostname, port, username, password, db_name):
mysql_cmd = "".join(["mysqldump --user=", username, " --password=", password, " --host=", hostname, " --port=", str(port), " ", db_name, " --single-transaction --skip-add-locks > ", "/tmp/", db_name, ".dump"])
print(mysql_cmd)
completed = subprocess.run(mysql_cmd, shell=True, stdout=subprocess.PIPE)
if(completed.returncode != 0):
print(completed.stdout)
print(completed.stderr)
sys.exit(1)
def mysqlimport(hostname, port, username, password, db_name, old_db_name):
mysql_cmd = "".join(["mysql --user=", username, " --password=", password, " --host=", hostname, " --port=", str(port), " ", db_name, "< ", "/tmp/", old_db_name, ".dump"])
print(mysql_cmd)
completed = subprocess.run(mysql_cmd, shell=True, stdout=subprocess.PIPE)
if(completed.returncode != 0):
print(completed.stdout)
print(completed.err)
sys.exit(1)
if __name__ == '__main__':
main()