1
|
import subprocess
|
2
|
import argparse
|
3
|
import os
|
4
|
import sys
|
5
|
import socket
|
6
|
|
7
|
def list_folders_and_files(path, max_depth):
|
8
|
try:
|
9
|
if os.path.exists(os.path.join(path, 'fileadmin')) and os.path.isdir(os.path.join(path, 'fileadmin')):
|
10
|
items = []
|
11
|
for root, dirs, files in os.walk(os.path.join(path, 'fileadmin')):
|
12
|
depth = root[len(os.path.join(path, 'fileadmin')) + len(os.path.sep):].count(os.path.sep)
|
13
|
if depth <= max_depth:
|
14
|
for dir in dirs:
|
15
|
if depth + 1 <= max_depth: # Check the next depth level
|
16
|
items.append(os.path.join(root, dir) + '/')
|
17
|
for file in files:
|
18
|
if depth <= max_depth: # Check the current depth level
|
19
|
items.append(os.path.join(root, file))
|
20
|
|
21
|
for idx, item in enumerate(items, start=1):
|
22
|
print(f"{idx}. {item}")
|
23
|
return items
|
24
|
|
25
|
else:
|
26
|
print("Error: 'fileadmin' directory not found")
|
27
|
except OSError:
|
28
|
print("Error: Invalid path")
|
29
|
return []
|
30
|
|
31
|
def get_user_selection(items):
|
32
|
selection = input("Select paths with enter comma-separated list of numbers: ")
|
33
|
selected_indices = [int(idx) for idx in selection.split(",") if idx.isdigit() and 1 <= int(idx) <= len(items)]
|
34
|
selected_paths = [items[idx - 1] for idx in selected_indices]
|
35
|
return selected_paths
|
36
|
|
37
|
def rsync_and_check_sync(selected_paths, project_dir_name, target_hostname):
|
38
|
syncable_paths = []
|
39
|
for idx, path in enumerate(selected_paths, start=1):
|
40
|
rsync_command = [
|
41
|
'rsync',
|
42
|
'-av',
|
43
|
'--dry-run',
|
44
|
os.path.join(path),
|
45
|
f'{target_hostname}:{path}'
|
46
|
]
|
47
|
|
48
|
result = subprocess.run(rsync_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
49
|
stdout = result.stdout.decode('utf-8')
|
50
|
stderr = result.stderr.decode('utf-8')
|
51
|
|
52
|
if "to-chk" in stderr:
|
53
|
print(f"{idx}. {path} {target_hostname}:{path} >> To sync")
|
54
|
print(stdout)
|
55
|
syncable_paths.append((idx, rsync_command))
|
56
|
else:
|
57
|
print(f"{idx}. {path} {target_hostname}:{path} >> Nothing to sync")
|
58
|
|
59
|
return syncable_paths
|
60
|
|
61
|
def execute_selected_syncs(syncable_paths):
|
62
|
input_str = input("Enter comma-separated list of numbers to execute: ")
|
63
|
selected_indices = [int(idx) for idx in input_str.split(",") if idx.isdigit()]
|
64
|
|
65
|
for idx, rsync_command in syncable_paths:
|
66
|
if idx in selected_indices:
|
67
|
print("Rsync command to execute:")
|
68
|
print(" ".join(rsync_command))
|
69
|
confirm = input("Do you want to execute this sync? (y/N): ").strip().lower()
|
70
|
if confirm == "y":
|
71
|
rsync_command.remove('--dry-run')
|
72
|
subprocess.run(rsync_command)
|
73
|
else:
|
74
|
print("Sync operation cancelled.")
|
75
|
|
76
|
if __name__ == "__main__":
|
77
|
|
78
|
if len(sys.argv) < 2:
|
79
|
print("Usage: python3 script.py project_dir_name")
|
80
|
sys.exit(1)
|
81
|
|
82
|
project_dir_name = sys.argv[1]
|
83
|
#max_depth = int(sys.argv[2])
|
84
|
max_depth = 1
|
85
|
target_hostname = 'w22a'
|
86
|
path = os.path.join('/var/www/html/', project_dir_name)
|
87
|
hostname = socket.gethostname()
|
88
|
|
89
|
print("Welcome to the migration live guide\n---------------------------------")
|
90
|
print("Project: " + path + "\nSource Host: " + hostname + "\nTarget Host: " + target_hostname)
|
91
|
print("--------------------------------------------------------\nStep 1: List of all files and folders from fileadmin (Level 1)")
|
92
|
|
93
|
# Step one: list all folders and files in fileadmin
|
94
|
items = list_folders_and_files(path, max_depth)
|
95
|
|
96
|
print("--------------------------------------------------------\nStep 2: Executing rsync with dry-run")
|
97
|
# Step two: allow user to input selection
|
98
|
if items:
|
99
|
selected_paths = get_user_selection(items)
|
100
|
print("Selected paths:")
|
101
|
for path in selected_paths:
|
102
|
print(path)
|
103
|
|
104
|
# Step four: perform rsync and check synchronization
|
105
|
syncable_paths = rsync_and_check_sync(selected_paths, project_dir_name, target_hostname)
|
106
|
|
107
|
print("--------------------------------------------------------\nStep 3: Executing rsync final")
|
108
|
if len(syncable_paths) == 0:
|
109
|
print("Nothing to sync.")
|
110
|
else:
|
111
|
execute_selected_syncs(syncable_paths)
|
112
|
|
113
|
|
114
|
print("--------------------------------------------------------\nStep 4: Starting table data transfer")
|
115
|
# Step four in seperate script
|
116
|
subprocess.run(['python3', 'table_data_transfer.py'])
|