-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapping.sh
190 lines (174 loc) · 6.71 KB
/
mapping.sh
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
#!/bin/bash
#############################################################################################################################
# File Name : mapping.sh
# Purpose : This Script contains all the required functions to manipulate a Mapping
# You can list, add, delete, rename, disable a Mapping or can view all the existing Mapping.
#############################################################################################################################
# Import the required files
source ws.sh
# Declare the variables
g_map_ws="" # Mapping's Workspace name
g_map_ws_dir="" # Mapping's Workspace directory
g_map_status="" # Mapping's Status
g_map_cfg="" # Mapping's configuration file
g_map_cfg_tmp="" # Temp file name
# Function to check/create the Mapping directory
create_map_dir(){
l_map
#echo "Checking if Directory exists : $l_ws_dir"
if [ ! -d "$l_ws_dir" ] ; then
echo "Creating Mapping directory ......"
mkdir "$l_ws_dir"
chmod 755 $l_ws_dir
fi
}
# Function to return Invalid Workspace message
function print_invalid_map() {
echo "$1 : Mapping does not exists. Please refer $g_map_cfg file to view existing Mappings."
}
# Function to return Invalid Workspace message
function print_valid_map() {
echo "$1 : Mapping already $g_map_status. Please refer $g_map_cfg file to view existing Mappings."
}
# This function will initialize the required parameters, Always run it first
function init_map() {
# Get Workspace name from User if not passed
if [ -z "$value1" ]; then
g_map_ws=$( uread "Enter Workspace name for Mapping" )
else
g_map_ws=$( get_upper $1 )
fi
#check Workspace status
set_ws_status $g_map_ws
if [ "$g_ws_status" = "INVALID" ]
then
echo "$g_map_ws : Invalid Workspace passed, Please LIST Workspace to view existing Workspaces."
exit 1
elif [ "$g_ws_status" = 'DISABLED' ]; then
g_map_ws_dir="$g_cfg_dir/$g_map_ws-DISABLED"
else
g_map_ws_dir="$g_cfg_dir/$g_map_ws"
fi
g_map_cfg="$g_map_ws_dir/mapping.cfg" #Set Mapping configuration
g_map_cfg_tmp="${g_map_cfg}_tmp.cfg" #Set Mapping configuration
}
# Function to list the Mapping
function list_mapping(){
init_map $1
#echo "Checking if file exists ... $g_map_cfg"
if [ -f "$g_map_cfg" ]; then
# File exists
list=`awk -F"," '{ if($0 !~ /#/) print $1 }' "$g_map_cfg"`
# Check if any Mapping exists or empty Workspace
if [ -z "$list" ]
then
echo "Nothing to list, No Mapping configured for the Workspace."
else
printf "%15s - %8s - %20s - %30s - %30s\n" "Mapping" "Status" "Host" "Remote Directory" "Local Directory"
for record in `grep -v "#" $g_map_cfg`; do
mapName=`echo $record | cut -d"," -f1`
mapStatus=`echo $record | cut -d"," -f2`
mapHost=`echo $record | cut -d"," -f3`
mapRemoteDir=`echo $record | cut -d"," -f4`
mapLocalDir=`echo $record | cut -d"," -f5`
printf "%15s - %8s - %20s - %30s - %30s\n" "$mapName" "$mapStatus" "$mapHost" "$mapRemoteDir" "$mapLocalDir"
done
fi
else
# File not Present."
echo "Nothing to list, No Mapping configuration found."
fi
}
# Function to get new mapping Id
# Format : <Workspace>_map_<id>
function get_mapping_id() {
# Check the highest mapping id availabe
max_id=`grep -v "#" $g_map_cfg | cut -d "," -f1 | cut -d "_" -f3 | uniq | sort -nr | head -n1| tr -s " "`
if [ -z "$max_id" ]
then
max_id=1
else
max_id=$[max_id+1]
fi
echo "${g_map_ws}_MAP_$max_id"
}
# Function to check the existence of a Mapping Id
function get_map_id_status(){
l_map_id=$( get_upper "$1" ) # Mapping
l_map_status=`grep -iw "$l_map_id" "${g_map_cfg}" | cut -d"," -f2`
if [ -z "$l_map_status" ]
then
l_map_status='INVALID'
fi
# Return value
echo $l_map_status
}
# Function to return the mapping status based on attributes
# 1. Host 2. Remote Directory 3. Local Directory
function get_map_status() {
l_map_status=""
list=`grep -iw "$1,$2,$3" $g_map_cfg`
if [ -z "$list" ]; then # if no value retrieved
l_map_status='INVALID'
else
# get the status
l_map_status=`echo $list| cut -d "," -f2`
fi
echo $l_map_status
}
# Function to add new Mapping
function add_mapping() {
init_map $1
#echo "Mapping Workspace directory : $g_map_ws_dir"
l_host=$( uread "Enter host" ) # Host
l_remote_dir=$( uread "Enter Remote directory" ) # Remote Directory
l_local_dir=$( uread "Enter Local directory" ) # Local Directory
l_user=$( uread "Enter Host User" ) # Host Password
l_pass=$( uread "Enter Host Password" ) # Host Password
# Check if Mapping attributes already exists
echo "Checking Mapping attributes for $l_host,$l_remote_dir,$l_local_dir"
#get_map_status $l_host $l_remote_dir $l_local_dir
if [ "$( get_map_status $l_host $l_remote_dir $l_local_dir )" = "INVALID" ] # Add the new mapping
then
# Generate the mapping id
l_map_id=$( get_mapping_id )
g_map_dir="$g_map_ws_dir/$l_map_id"
# Check if Directory exists
if [ -d "$g_map_dir" ]
then
echo "$l_map_id : Mapping Directory already present but missing in Configuration file."
else
# Directory does not exists # Create mapping directory
mkdir $g_map_dir
chmod 755 $g_map_dir
fi
# Make entry in mapping.sh
echo "$l_map_id,ENABLED,$l_host,$l_remote_dir,$l_local_dir,$l_user,$l_pass">>$g_map_cfg
sort $g_map_cfg | uniq > $g_map_cfg_tmp
mv $g_map_cfg_tmp $g_map_cfg
echo "$l_map_id : New mapping added Successfully."
else # Display error if Mapping already present
echo "Mapping already exists, Please list to view the existing mappings and its attributes."
fi
}
# Function to delete a Mapping
function delete_mapping() {
init_map $1
#echo "Mapping Workspace directory : $g_map_ws_dir"
l_map_id=$( uread "Enter Mapping id to Delete" )
# Check mapping status
if [ $( get_map_id_status $l_map_id ) != "INVALID" ]; then
# Delete the folder for Mapping
rm -r "$g_map_ws_dir/$l_map_id"
# Check the command status
if [ $? -eq 0 ]
then
# Delete entry from Mapping configuration file
grep -iwv "$l_map_id" $g_map_cfg > ${g_map_cfg_tmp}
mv $g_map_cfg_tmp $g_map_cfg
echo "$l_map_id : Mapping Deleted Successfully."
fi
else
print_invalid_map $l_map_id
fi
}