File: create-uid-gid-mappings.sh

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (276 lines) | stat: -rwxr-xr-x 6,805 bytes parent folder | download | duplicates (8)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh

#
# This script creates the virtual id <-> group mappings in the LFC for the existing groups
#
# Author : 	- Sophie Lemaitre (sophie.lemaitre@cern.ch)
#		- James Casey (james.casey@cern.ch)
#
# Date : 27/10/2005
#

base=$(cd $(dirname $0); pwd)
TEMP=$(getopt -o omu:p:d:hv --long oracle,mysql,user:,password-file:,database:,host:,help,verbose -- "$@")
eval set -- "$TEMP"

VERBOSE='false'
DB_TYPE="mysql"
DB_USER=""
DB_DATABASE=""
DB_HOST=`hostname`

function usage() {
echo "Usage : $(basename $0) (--oracle|--mysql) --user user --password-file \"/path/to/password/file\""
echo "          --database database [--host mysql-server] [--help] [--verbose]"
echo 

exit 1

}

function max_insert_statement() {
  echo "INSERT INTO Cns_unique_$1 VALUES($2);"
  return 0
}

function call_database() {
    if [ "x$1" == "x" ] ; then # no file provided
      if [ $DB_TYPE == "mysql" ] ; then
	  mysql -N -u $DB_USER -p$DB_PASSWORD --database $DB_DATABASE --host $DB_HOST
      else
	  sqlplus $DB_USER/$DB_PASSWORD@$DB_DATABASE
      fi


    else 
      if [ ! -f $1 ] ; then
	  echo "[ERROR] : File does not exist : $1"
	  return 1
      fi
      if [ $DB_TYPE == "mysql" ] ; then
	  mysql -N -u $DB_USER -p$DB_PASSWORD --database $DB_DATABASE --host $DB_HOST < $1
      else
	  sqlplus -S $DB_USER/$DB_PASSWORD@$DB_DATABASE < $1
      fi
   fi
}

while true; do
    case "$1" in
        -o|--oracle)
            DB_TYPE='oracle'
            shift
            ;;
        -m|--mysql)
            BD_TYPE='mysql'
            shift
            ;;
        -u|--user)
            shift
            DB_USER="$1"
            shift
            ;;
        -p|--password-file)
            shift
            PASSWORD_FILE="$1"
            shift
            ;;
        -d|--database)
            shift
            DB_DATABASE="$1"
            shift
            ;;
        --host)
            shift
            DB_HOST="$1"
            shift
            ;;
        -v|--verbose)
            VERBOSE='true'
            shift
            ;;
        -h|--help)
            usage
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Unknown option '$1'"
            exit
            ;;
    esac
done


#
# Check for reqd variables
if [ "$DB_USER" == "" ] ||  [ "$PASSWORD_FILE" == "" ] || [ "$DB_DATABASE" == "" ] ; then
    usage "Database name, database user and file containing database password must be specified"
fi

#
# Read database user password from file
DB_PASSWORD=`cat $PASSWORD_FILE`

OUTPUT_FILE=`mktemp "/tmp/lfc_upgrade.XXXXXX"`
if [ $VERBOSE == 'true' ]; then
    echo "Using Output File : $OUTPUT_FILE"
fi

QUERY_FILE=`mktemp "/tmp/lfc_upgrade.XXXXXX"`
GROUP_MAP=`mktemp "/tmp/lfc_upgrade.XXXXXX"`
CHECK_FILE1=`mktemp "/tmp/lfc_upgrade.XXXXXX"`
CHECK_FILE2=`mktemp "/tmp/lfc_upgrade.XXXXXX"`

#
# find /grid entry and generate group map from it's children
#
if [ "$DB_TYPE" == "oracle" ] ; then
    echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
else
    echo "" > $QUERY_FILE
fi
echo "select fileid from Cns_file_metadata where parent_fileid = '2' and name = 'grid';" >> $QUERY_FILE
GRID_ENTRY=$(call_database $QUERY_FILE)

if [ "$DB_TYPE" == "oracle" ] ; then
    echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
    echo "COLUMN name FORMAT A32" >> $QUERY_FILE
else
    echo "" > $QUERY_FILE
fi

# check that all existing gids are different
echo "select count(distinct gid) from Cns_file_metadata where parent_fileid = $GRID_ENTRY;" >> $CHECK_FILE1
NUMBER1=$(call_database $CHECK_FILE1  | tr -d "\t ")
echo "select count(gid) from Cns_file_metadata where parent_fileid = $GRID_ENTRY;" >> $CHECK_FILE2
NUMBER2=$(call_database $CHECK_FILE2  | tr -d "\t ")

if [ $NUMBER2 != $NUMBER1 ] ; then
    echo "ERROR : different groups have the same group id.\n"
    echo "ERROR : change the permissions on the /grid/vo directories and rerun the script."
    exit 1;
fi

echo "select gid, ':', name from Cns_file_metadata where parent_fileid = $GRID_ENTRY;" >> $QUERY_FILE
$(call_database $QUERY_FILE  | tr -d "\t " > $GROUP_MAP)

#
# find users from db
#

if [ "$DB_TYPE" == "oracle" ] ; then
    echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
else
    echo "" > $QUERY_FILE
fi
echo "select distinct owner_uid from Cns_file_metadata order by owner_uid;" >> $QUERY_FILE
UID_LIST=$(call_database $QUERY_FILE)
IFS='
'

MAX=-1
for uid in $UID_LIST ; do
  if [ $uid == "owner_uid" ]; then
    continue
  fi
  # strip whitespace
  uid=$(echo $uid | tr -d "\t ")
  username=`cut -d':' -f1,3,4 /etc/passwd|grep :$uid:|cut -d':' -f1`
  if [ "x$username" != "x" ] ; then
      echo "INSERT INTO Cns_userinfo(username, userid) VALUES('$username', $uid);" >> $OUTPUT_FILE
      if [ $VERBOSE == 'true' ] ; then
	    echo "Adding User  : $username (uid=$uid)"
	fi
      if [ $(($uid > $MAX)) == 1 ] ; then
        MAX=$uid
      fi

  fi
done

if [ $(($MAX > -1)) == 1 ] ; then
  MAX=`expr $MAX + 1`
  echo $(max_insert_statement "uid" $MAX) >> $OUTPUT_FILE
  if [ $VERBOSE == 'true' ] ; then
      echo "Setting Maximum User ID : $MAX"
  fi
else
  echo "[WARNING] No Users added"
fi


#
# get all the group names and gids used in the LFC
# We map the group names to VO names using the 'group-mappings' file
#

if [ "$DB_TYPE" == "oracle" ] ; then
    echo "SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON" > $QUERY_FILE
else
    echo "" > $QUERY_FILE
fi
echo "select distinct gid from Cns_file_metadata order by gid;" >> $QUERY_FILE
GID_LIST=$(call_database $QUERY_FILE)
IFS='
'
MAX=-1
for gid in $GID_LIST; do
    if [ "x$gid" == "xgid" ]; then
	continue
    fi
    # strip whitespace
    gid=$(echo $gid | tr -d "\t ")
    voname=`grep ^$gid: $GROUP_MAP| cut -d':' -f2`
    if [ "x$voname" != "x" ] ; then
        echo "INSERT INTO Cns_groupinfo(groupname,gid) VALUES('$voname', $gid);"  >> $OUTPUT_FILE
    	if [ $VERBOSE == 'true' ] ; then
	    echo "Adding Group : $voname (gid=$gid)"
	fi
    fi
    if [ $(($gid > $MAX )) == 1 ] ; then
        MAX=$gid
    fi
done

if [ $(($MAX > -1)) == 1 ] ; then
    echo $(max_insert_statement "gid" $MAX) >> $OUTPUT_FILE
  if [ $VERBOSE == 'true' ] ; then
      echo "Setting Maximum User ID : $MAX"
  fi

else
    echo "[WARNING] No groups added"
fi

if [ ! -s $OUTPUT_FILE ] ; then
   echo "[WARNING] No SQL to insert into database."
   exit 1;
fi

#/bin/rm $QUERY_FILE
#/bin/rm $GROUP_MAP

#
# stop the LFC
#
/sbin/service lfcdaemon stop && sleep 10

#
# Migrate the database schema 
#
call_database $base/migrate-$DB_TYPE-schema-to-2-0-0.sql

#
# Create the existing user and group mappings
#
call_database $OUTPUT_FILE 


#
# Migration over...
#
/sbin/service lfcdaemon start
#/bin/rm $OUTPUT_FILE