ask confirm shell

  1 #/bin/bash
  2 BASEDIR=$(cd $(dirname $0) && pwd)
  3 cd $BASEDIR>/dev/null
  4 usage="Usage: $0 -o/--org orgId[Must] -p/--prepare t[Option] -f/--force t[Option] -d/--dry t[Option]"
  5 force=0
  6 prepare=0
  7 step=0
  8 dry=0
  9 RED='33[0;31m'
 10 BLUE='33[1;32m'
 11 GREEN='33[1;34m'
 12 NC='33[0m' # No Color
 13 #read input parameters
 14 while [ "$1" != "" ]
 15 do
 16   case $1 in
 17       -o|--org) shift
 18          orgId=$1
 19          ;;
 20       -f|--force) shift
 21          if [ "$1" = "-p" ];then
 22            prepare=1
 23          fi
 24          force=1
 25           ;;
 26       -p|--prepare) shift
 27          if [ "$1" = "-f" ];then
 28            force=1
 29          fi
 30          prepare=1
 31           ;;
 32       -d|--dry) shift
 33         dry=1
 34         ;;
 35       *) echo $usage
 36          exit 1
 37          ;;
 38    esac
 39    shift
 40 done
 41 if [ -z $orgId ];then
 42    echo -e "$RED[Error] Missing orgId!$NC
$usage"
 43    exit
 44 fi
 45 logsdir=logs/${orgId}_`date +'%Y-%m-%d-%H-%M-%S'`
 46 mkdir -p $logsdir
 47 log="$logsdir/csc_migrage_$orgId.log"
 48 
 49 check_and_commit()
 50 {
 51    cmd=""
 52    step=`expr "$step" + 1`
 53    echo ""|tee -a $logs
 54    echo -e "$BLUE[`date +'%Y-%m-%d %H:%M:%S'`][Step $step] exec $1 $NC"|tee -a $log
 55    if [ $force -eq 0 ];then
 56        while true; do
 57             read -p "Do you confirm to execute step $1? [y/n]" yn
 58        case $yn in
 59             [Yy]* )
 60                 $2;
 61                 if [ $dry -eq 1 ];then
 62                     echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log
 63                 else
 64                     echo $cmd|tee -a $log
 65                     $cmd|tee  $logsdir/$1.log
 66                 fi
 67                 break;;
 68             [Nn]* ) echo "ignore step $1"|tee -a $log ;break;;
 69         esac
 70         done
 71    else
 72      $2
 73      if [ $dry -eq 1 ];then
 74          echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log
 75       else
 76          echo $cmd|tee -a $log
 77          $cmd|tee -a $logsdir/$1.log
 78       fi
 79    fi
 80 }
 81 
 82 prepare_message_confirm()
 83 {
 84    echo    "Please make sure next items be done"
 85    echo -e "${RED} 1.env.sh use correct environment information ${NC}"
 86    echo -e "${RED} 2.all gcs vm had added the onecloud replay URL and restarted${NC}"
 87    echo -e "${RED} 3.make sure this vm can connect to brown field mongo/redshift/CMC gateway ${NC}"
 88    echo -e "${RED} 4.had startup cloud-subscriber with correct version and expose port 3424 ${NC}"
 89    echo -e "${RED} 5.brown field subscrbier-sync pod had patched ${NC}"
 90    if [ $force -eq 0 ];then
 91        while true; do
 92             read -p "Do you confirm ? [y/n]" yn
 93        case $yn in
 94             [Yy]* )  echo "will continue to execute for org :$orgId";break;;
 95             [Nn]* )  exit -1 ;break;;
 96         esac
 97         done
 98    fi
 99 
100 }
101 
102 migrate_mongo_big_collections()
103 {
104    cmd="python ./mongo_to_psql_sxaccfs.py -m $compass_mongo -n $onecloud_postgres_host -d $onecloud_postgres_db -u $onecloud_postgres_username -p $onecloud_postgres_password"
105 }
106 
107 mongo_to_postgres_noorg()
108 {
109   cmd="python ./mongo_to_psql.py -m $compass_mongo -n $onecloud_postgres_host -d $onecloud_postgres_db -u $onecloud_postgres_username -p $onecloud_postgres_password --no-org"
110 }
111 
112 brown_sub_fullsync()
113 {
114   cmd="curl -s  $old_subscriber_api/sync?orgid=$orgId&mode=fsync-es&save-sync-logs=false&async=false"
115 }
116 
117 mongo_to_postgres_relay()
118 {
119   cmd="python ./mongo_to_psql.py -m $compass_mongo -n $onecloud_postgres_host -d $onecloud_postgres_db -u $onecloud_postgres_username -p $onecloud_postgres_password -O $orgId  -i in_collection_names.txt"
120 }
121 
122 sub_clean()
123 {
124   cmd="curl -s $local_subscriber_api/migrate-clear?orgId=$orgId"
125 }
126 
127 sub_soc()
128 {
129   cmd="curl -s $local_subscriber_api/migrate-usoc?orgId=$orgId"
130 }
131 
132 sub_cc()
133 {
134  cmd="curl -s $local_subscriber_api/migrate-subscriber-cc?orgId=$orgId"
135 }
136 
137 sub_billing()
138 {
139  cmd="curl -s $local_subscriber_api/migrate-subscriber-billing?orgId=$orgId"
140 }
141 
142 sub_update_fee()
143 {
144  cmd="curl -s $local_subscriber_api/calc-usoc-fee?orgId=$orgId"
145 }
146 
147 sub_reindex()
148 {
149  cmd="curl -s $onecloud_subscriber_api/index/reindex?org=$orgId"
150 }
151 
152 acs_relay()
153 {
154    sed -i "3i  "oneCloudRelay": true," org_$orgId.json
155    cat org_$orgId.json|tee -a $log
156    cmd="curl -s -X PUT -d @org_$orgId.json --url http://$old_gcs_ipaddress:8081/cc/organization/$orgId"
157 }
158 
159 check_org_exists()
160 {
161    curl -s http://$old_gcs_ipaddress:8081/cc/organization/$orgId>org_$orgId.json
162    if [ `cat org_$orgId.json|grep 'No organization found'|wc -l` -eq 1 ];then
163       echo -e "$RED org:$orgId is not exist! $NC"
164       rm -rf org_$orgId.json
165       exit -1
166    fi
167    if [ `cat org_$orgId.json|grep oneCloudRelay|grep true|wc -l` -eq 1 ];then
168        if [ $force -eq 0 ];then
169         while true; do
170             read -p "org:$orgId had already relay,Do you want to continue? [y/n]" yn
171         case $yn in
172             [Yy]* )
173                 sed -i '/oneCloudRelay/d' org_$orgId.json;
174                 echo "will continue to execute for org :$orgId";
175                 break;;
176             [Nn]* )  exit -1 ;break;;
177         esac
178         done
179         else
180                 sed -i '/oneCloudRelay/d' org_$orgId.json
181         fi
182    fi
183 }
184 
185 acs_workflow()
186 {
187   cmd="curl -s -X POST --url http://$onecloud_gcs_ipaddress:8081/cc/workflow/audit?orgId=$orgId"
188 }
189 
190 mongo_to_postgres_cloud()
191 {
192   cmd="python ./mongo_to_psql.py -m $cloud_mongo -n $onecloud_postgres_host -d $onecloud_postgres_db -u $onecloud_postgres_username -p $onecloud_postgres_password -O $orgId"
193 }
194 
195 mongo_to_postgres_compass_others()
196 {
197   cmd="python ./mongo_to_psql.py -m $compass_mongo -n $onecloud_postgres_host -d $onecloud_postgres_db -u $onecloud_postgres_username -p $onecloud_postgres_password -O $orgId -e ex_collection_names.txt"
198 }
199 
200 mongo_to_mongo()
201 {
202   cmd="./migrate_mongo_to_mongo.sh $orgId"
203 }
204 
205 copy_cc_org()
206 {
207    cmd="curl -s -X POST -d @org_$orgId.json --url http://$onecloud_gcs_ipaddress:8081/cc/organization/$orgId"
208 }
209 
210 main()
211 {
212    if [ $force -eq 0 ];then
213      prepare_message_confirm
214    fi
215    if [ $prepare -eq 1 ];then
216      check_and_commit "Migrate2BigCollectionOfMongo" migrate_mongo_big_collections
217      check_and_commit "Mongo2PostgresCrossOrgFiles" mongo_to_postgres_noorg
218    fi
219    check_org_exists
220 #   check_and_commit "Mongo2Postgres cross org files" mongo_to_postgres_noorg
221    check_and_commit "BrownSubscriberFullSync" brown_sub_fullsync
222    check_and_commit "MigrateCompassMongoforACSRelay" mongo_to_postgres_relay
223    check_and_commit "SubscriberMigrateClean" sub_clean
224    check_and_commit "SubscriberMigrateUSOC" sub_soc
225    check_and_commit "SubscriberMigrateCCSubscribers"  sub_cc
226    check_and_commit "SubscriberMigrateBilling" sub_billing
227    check_and_commit "SubscriberMigrateFEE" sub_update_fee
228    check_and_commit "GreenSubscriberReindex" sub_reindex
229    check_and_commit "MigrateCCorgToGreen" copy_cc_org
230    check_and_commit "EnableACSRelay" acs_relay
231    check_and_commit "MigrateACSWorkflow" acs_workflow
232    check_and_commit "CopyALLCloudCollectionsMongo2Postgres" mongo_to_postgres_cloud
233    check_and_commit "CopyLeftCompassCollectionsMongo2Postgres" mongo_to_postgres_compass_others
234    check_and_commit "MigrateCloudMongoToNewCloudMongo" mongo_to_mongo
235 }
236 source env.sh
237 main
原文地址:https://www.cnblogs.com/tben/p/10784090.html