-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclone_wp_site.py
More file actions
183 lines (152 loc) · 7.96 KB
/
clone_wp_site.py
File metadata and controls
183 lines (152 loc) · 7.96 KB
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
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
134
135
136
137
138
139
140
141
142
143
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
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
import sys, os
import logging.config
import argparse
import opalstack
opalapi = opalstack.Api(token='0000000000000000000000000000000000000000')
from opalstack.util import run, filt_one
SRC_APP_NAME = 'myapp' # The application files to clone (~/apps/myapp).
DST_WEB_SERVER_HOSTNAME = 'vps1.opalstack.com' # The webserver to clone to. May be the same as source webserver.
DST_DOMAIN_NAME = 'www.mynewsite.com' # The domain to create. Will host the cloned site.
DST_OSUSER_NAME = 'mynewuser' # The osuser (shell user) to which the source app will be cloned.
DST_OSUSER_PASS = None # The osuser password if the user already exists. If None, the osuser will be created instead.
DST_APP_NAME = 'mynewapp' # The new app name cloned on the destination server (~/apps/mynewapp). May be the same as the source app name.
DST_SITE_NAME = 'mynewsite' # The new site name to create (visible in the dashboard). Often the same as DST_APP_NAME.
DST_SITE_HTTPS = False # Whether or not to enable HTTPS and generate an SSL certificate. Can also be enabled on the site in the dashboard later.
logging.config.dictConfig({
'version': 1,
'formatters': {
'standard': {
'format': '%(asctime)s : %(levelname)-5s : %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
'formatter': 'standard',
'level': 'DEBUG',
},
},
'loggers': {
'opalstack': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': True,
},
'__main__': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
},
})
log = logging.getLogger(__name__)
def get_args():
parser = argparse.ArgumentParser(description='Wordpress Site Cloner')
return parser.parse_args(sys.argv[1:])
def main(args):
# Set some derived variables
userhost = f'{DST_OSUSER_NAME}@{DST_WEB_SERVER_HOSTNAME}'
src_app_path = os.path.expanduser(f'~/apps/{SRC_APP_NAME}')
dst_app_path = f'/home/{DST_OSUSER_NAME}/apps/{DST_APP_NAME}'
proto = 'https' if DST_SITE_HTTPS else 'http'
site_url = f'{proto}://{DST_DOMAIN_NAME}/'
ssh_password_filepath = f'{DST_OSUSER_NAME}.sshpass'
sqlpasswd_filepath_local = os.path.expanduser(f'~/{DST_APP_NAME}.sqlpasswd')
sqlpasswd_filepath_remote = f'/home/{DST_OSUSER_NAME}/{DST_APP_NAME}.sqlpasswd'
sql_filepath_local = os.path.expanduser(f'~/{DST_APP_NAME}.sql')
sql_filepath_remote = f'/home/{DST_OSUSER_NAME}/{DST_APP_NAME}_remote.sql'
# Retrieve web_server and primary IP entries for later use
log.info(f'Retrieving webserver information for {DST_WEB_SERVER_HOSTNAME}')
web_server = filt_one(
opalapi.servers.list_all()['web_servers'], {'hostname': DST_WEB_SERVER_HOSTNAME},
)
webserver_primary_ip = filt_one(
opalapi.ips.list_all(embed=['server']), {'server.hostname': web_server['hostname'], 'primary': True},
)
# Retrieve existing app information for later use
log.info(f'Retrieving app information for {SRC_APP_NAME}')
src_app = filt_one(
opalapi.apps.list_all(), {'name': SRC_APP_NAME}
)
# Either retrieve or create destination osuser, depending on whether or not a password was specified.
if DST_OSUSER_PASS:
log.info(f'Retrieving existing osuser {DST_OSUSER_NAME}')
dst_osuser = filt_one(
opalapi.osusers.list_all(), {'name': DST_OSUSER_NAME, 'server': web_server['id']},
)
ssh_password = DST_OSUSER_PASS
else:
log.info(f'Creating new osuser {DST_OSUSER_NAME}')
dst_osuser = opalapi.osusers.create_one({
'name': DST_OSUSER_NAME,
'server': web_server['id'],
})
ssh_password = dst_osuser['default_password']
sshrunner = opalstack.util.SshRunner(userhost, ssh_password=ssh_password, ssh_password_filepath=ssh_password_filepath)
sshrunner.run_passbased_ssh('/bin/true')
# Create domain
log.info(f'Creating domain {DST_DOMAIN_NAME}')
created_domain = opalapi.domains.create_one({
'name': DST_DOMAIN_NAME,
})
# Create app
log.info(f'Creating application {DST_APP_NAME}')
created_app = opalapi.apps.create_one({
'name': DST_APP_NAME,
'osuser': dst_osuser['id'],
'type': 'APA',
'installer_url': 'https://raw.githubusercontent.com/opalstack/installers/master/core/wordpress/install.sh',
'json': src_app['json'],
})
# Create site
log.info(f'Creating site {DST_SITE_NAME}')
created_site = opalapi.sites.create_one({
'name': DST_SITE_NAME,
'ip4': webserver_primary_ip['id'],
'domains': [created_domain['id']],
'routes': [{'app': created_app['id'], 'uri': '/'}],
'generate_le': DST_SITE_HTTPS,
'redirect': DST_SITE_HTTPS,
})
# Clone data
log.info(f'Retrieving database configurations')
src_db_host, _, _ = run(f'~/bin/wp --path={src_app_path} config get DB_HOST')
src_db_user, _, _ = run(f'~/bin/wp --path={src_app_path} config get DB_USER')
src_db_pass, _, _ = run(f'~/bin/wp --path={src_app_path} config get DB_PASSWORD')
src_db_name, _, _ = run(f'~/bin/wp --path={src_app_path} config get DB_NAME')
assert src_db_host and src_db_user and src_db_pass and src_db_name
dst_db_host, _, _ = sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config get DB_HOST')
dst_db_user, _, _ = sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config get DB_USER')
dst_db_pass, _, _ = sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config get DB_PASSWORD')
dst_db_name, _, _ = sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config get DB_NAME')
assert dst_db_host and dst_db_user and dst_db_pass and dst_db_name
mariatool_local = opalstack.util.MariaTool(src_db_host, 3306, src_db_user, src_db_pass, src_db_name, sqlpasswd_filepath_local)
mariatool_remote = opalstack.util.MariaTool(dst_db_host, 3306, dst_db_user, dst_db_pass, dst_db_name, sqlpasswd_filepath_local, sqlpasswd_filepath_remote)
log.info(f'Exporting database')
mariatool_local.export_local_db(sql_filepath_local)
log.info(f'Copying files')
sshrunner.run_passbased_rsync(f'{src_app_path}/', f'{userhost}:{dst_app_path}/')
log.info(f'Updating database configuration')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config set DB_HOST {dst_db_host}')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config set DB_USER {dst_db_user}')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config set DB_PASSWORD {dst_db_pass}')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} config set DB_NAME {dst_db_name}')
log.info(f'Copying database content')
sshrunner.run_passbased_scp(sql_filepath_local, f'{userhost}:{sql_filepath_remote}')
os.remove(sql_filepath_local)
log.info(f'Importing database')
mariatool_remote.import_remote_db(sshrunner, sql_filepath_remote)
sshrunner.run_passbased_ssh(f'rm {sql_filepath_remote}')
log.info(f'Updating site url')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} option set siteurl {site_url}')
sshrunner.run_passbased_ssh(f'/home/{DST_OSUSER_NAME}/bin/wp --path={dst_app_path} option set home {site_url}')
if DST_SITE_HTTPS:
log.info(f'Site cloned: {site_url} (Note: Please allow a few minutes for an SSL certificate to be generated.)')
else:
log.info(f'Site cloned: {site_url}')
if __name__ == '__main__':
args = get_args()
main(args)