-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileOps.py
More file actions
247 lines (188 loc) · 10.8 KB
/
Copy pathfileOps.py
File metadata and controls
247 lines (188 loc) · 10.8 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""
----------------------------------------------------------------------------------------------
This python script automates the process of following:
a. Copying a folder (along with its contents) from source to destination
b. Search for matching text in a file and replace it with new text
i. In this example, we are replacing text of current website address with new website address in the apache conf files
Pre-Requisites:
-----------------
This script needs Python environment 3.5 or above
Assumptions:
-----------------
1. Script expects two input parameters to be passed.
a. Name of current (source) folder using the --cf switch
b. Name of new (destination) folder using the --nf switch
----------------------------------------------------------------------------------------------
"""
from pathlib import Path
import argparse, os, shutil, sys
# Declaring variables
current_folder = ""
new_folder = ""
# Base path of the folders/directories
web_dir = '/var/www/'
# Base path of apache conf files
conf_files_dir = '/etc/apache/sites/'
print(f'\nBeginning the script execution...\n')
# -----------------------------------------------------------------------------------------
# Block to read the command line arguments - Begin
# if len(sys.argv) < 3:
# print(f'\nERROR: This script requires 2 arguments as inputs.You have specified less than 2. Aborting the operation..!\n')
# sys.exit()
try:
# Parsing the command line arguments
arg_parser = argparse.ArgumentParser(allow_abbrev=False)
arg_parser.add_argument('--cf', action='store', required=True)
arg_parser.add_argument('--nf', action='store', required=True)
args = arg_parser.parse_args()
current_folder = args.cf
new_folder = args.nf
# Printing the command line arguments received
print(f'\n-------------------------------------------------------------------------------')
print(f'Reading the input variables received as command line arguments...\n')
print(f'Current folder name is:', current_folder)
print(f'New folder name is:', new_folder)
print(f'-------------------------------------------------------------------------------\n')
except:
print(f'\n-------------------------------------------------------------------------------')
print(f'ERROR - Some error occured while parsing the command line arguments.')
print(f'Either the required number of arguments are missing or invalid arguments passed.')
print(f'Aborting the operation..!\n')
print(f'-------------------------------------------------------------------------------\n')
sys.exit()
# Block to read the command line arguments - End
# -----------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------
# Block to recursively copy source folder to destination - Begin
print(f'\n-------------------------------------------------------------------------------')
print(f'Copying the contents of current folder to new folder...\n')
try:
# Check if current folder exists or not
source_dir = os.path.join(web_dir, current_folder)
dest_dir = os.path.join(web_dir, new_folder)
if os.path.exists(source_dir):
print(f'\nCurrent folder [{source_dir}] exists.')
# Copy the contents of current folder to new folder
shutil.copytree(source_dir, dest_dir)
print(f'Copied the folder and its contents [{source_dir}] to [{dest_dir}]')
print(f'-------------------------------------------------------------------------------\n')
else:
print(f'\nERROR: Directory [{current_folder}] does not exist. Please check if you have specified the correct name.')
print(f'Aborting the execution here...\n')
sys.exit()
except OSError as err:
print(f'\nERROR: Error occured while copying the current folder to new folder. Please check.')
print(f'ERROR: Aborting the execution..!')
print(f'ERROR: % s\n' % err)
# Block to recursively copy source folder to destination - End
# -----------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------
# Block to copy and update the apache (port 80) conf file - Begin
# Variables for file name
port80_conf_file_current = current_folder + ".conf"
port80_conf_file_new = new_folder + ".conf"
# Building full system path for the files
port80_conf_file_current_path = os.path.join(conf_files_dir, port80_conf_file_current)
port80_conf_file_new_path = os.path.join(conf_files_dir, port80_conf_file_new)
try:
# Check if file exists or not
print(f'\n-------------------------------------------------------------------------------')
print(f'Checking if port 80 conf file of current website [{port80_conf_file_current}] exists...\n')
path = Path(port80_conf_file_current_path)
if path.is_file():
print(f'File [{port80_conf_file_current_path}] exist.\nCreating a copy of this file for the new website [{new_folder}]')
shutil.copyfile(port80_conf_file_current_path, port80_conf_file_new_path)
print(f'\nReading contents of the file [{port80_conf_file_new_path}]\n')
try:
# Reading the input file
file_in = open(f'{port80_conf_file_new_path}', 'r+')
#read content of file to string
data = file_in.read()
# Counting the number of text occurences
text_count = data.count(current_folder)
if text_count < 1:
print(f'No occurences of [{current_folder}] found in the file [{port80_conf_file_new_path}]...')
print(f'Nothing to do here. Exiting the file read operation.')
print(f'-------------------------------------------------------------------------------\n')
elif text_count > 0 :
print(f'Total of [{text_count}] occurences of current website address [{current_folder}] found in the file [{port80_conf_file_new}]')
print(f'Proceeding to update the current domain with new domain name...\n')
except:
sys.exit('Some error occured while reading the file [{port80_conf_file_new_path}]. Please check...!')
try:
# Reading file
with open(f'{port80_conf_file_new_path}', 'r') as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace(current_folder, new_folder)
# Write the file out again
with open(f'{port80_conf_file_new_path}', 'w') as file:
file.write(filedata)
print(f'\nNew domain name replaced successfully in the file [{port80_conf_file_new_path}].\nCheck the file to confirm...')
print(f'-------------------------------------------------------------------------------\n')
except:
sys.exit('Some error occured while updating the file with new domain name. Aborting the execution..!')
else:
print(f'The file [{port80_conf_file_new_path}] does not exist. Aborting the operation...!\n')
except OSError as err:
print(f'\nERROR: Error occured while updating the port 80 conf file with new domain name.')
print(f'ERROR: Aborting the execution..!')
print(f'ERROR: % s\n' % err)
# Block to copy and update the apache port 80 file - End
# -----------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------
# Block to copy and update the apache (ssl port 443) conf file - Begin
# Variables for file name
ssl_conf_file_current = current_folder + "-le-ssl.conf"
ssl_conf_file_new = new_folder + "-le-ssl.conf"
# Building full system path for the files
ssl_conf_file_current_path = os.path.join(conf_files_dir, ssl_conf_file_current)
ssl_conf_file_new_path = os.path.join(conf_files_dir, ssl_conf_file_new)
try:
# Check if file exists or not
print(f'\n-------------------------------------------------------------------------------')
print(f'Checking if ssl conf file of current domain [{ssl_conf_file_current}] exists...\n')
path = Path(ssl_conf_file_current_path)
if path.is_file():
print(f'File [{ssl_conf_file_current_path}] exist.\nCreating a copy of this file for the new domain [{new_folder}]')
shutil.copyfile(ssl_conf_file_current_path, ssl_conf_file_new_path)
print(f'\nReading contents of the file [{ssl_conf_file_new_path}]\n')
try:
# Reading the input file
file_in = open(f'{ssl_conf_file_new_path}', 'r+')
#read content of file to string
data = file_in.read()
# Counting the number of text occurences
text_count = data.count(current_folder)
if text_count < 1:
print(f'No occurences of the domain name [{current_folder}] found in the file [{ssl_conf_file_new_path}]...')
print(f'Nothing to do here. Exiting the file read operation.')
print(f'-------------------------------------------------------------------------------\n')
elif text_count > 0 :
print(f'Total of [{text_count}] occurences of current domain name [{current_folder}] found in the file [{ssl_conf_file_new}]')
print(f'Proceeding to update the current domain with new domain name...\n')
except:
sys.exit('Some error occured while reading the file [{ssl_conf_file_new_path}]. Please check...!')
try:
# Reading file
with open(f'{ssl_conf_file_new_path}', 'r') as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace(current_folder, new_folder)
# Write the file out again
with open(f'{ssl_conf_file_new_path}', 'w') as file:
file.write(filedata)
print(f'\nNew domain name replaced successfully in the file [{ssl_conf_file_new_path}].\nCheck the file to confirm...')
print(f'-------------------------------------------------------------------------------\n')
except:
sys.exit('Some error occured while updating the file with new domain name. Aborting the execution..!')
else:
print(f'The file [{ssl_conf_file_new_path}] does not exist. Aborting the operation...!\n')
except OSError as err:
print(f'\nERROR: Error occured while updating the ssl conf file with new domain name.')
print(f'ERROR: Aborting the execution..!')
print(f'ERROR: % s\n' % err)
# Block to copy and update the apache (ssl port 443) conf file - End
# -----------------------------------------------------------------------------------------
print(f'\nScript has finished its execution... Bye...\n')