New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
make_usp_tar.sh in branches/2016/dev_r6393_NOC_WAD/NEMOGCM/TOOLS/MISCELLANEOUS – NEMO

source: branches/2016/dev_r6393_NOC_WAD/NEMOGCM/TOOLS/MISCELLANEOUS/make_usp_tar.sh @ 7117

Last change on this file since 7117 was 7117, checked in by acc, 8 years ago

Added script and README to help with unsupported configurations on systems without direct internet or wget access (TOOLS/MISCELLANEOUS/make_usp_tar.sh and README_uspcfg)

  • Property svn:executable set to *
File size: 8.3 KB
Line 
1#!/bin/bash
2#set -x
3set -o posix
4#set -u
5#set -e
6#+
7# ===============
8# make_usp_tar.sh
9# ===============
10# ---------------
11# Fetch and tar a unsupported configuration setup.
12# This script is only needed when target systems do
13# not have wget access to the internet. To configure
14# unsupported conigurations on these systems it will
15# first be necessary to run this script on a system that
16# does have access. Then copy access the resulting tar file,
17# unpack and run the enclosed set_local_uspcfg script in a
18# bash shell to complete the process. That script redefines/
19# defines wget as a bash function to perform local copies
20# from this unpacked tarball. If you wish to create a new
21# configuration based on this local copy of an unsupported
22# configuration in future sessions then you may need to
23# redefine wget again before running makenemo with the
24# appropriate -u setting. A simple script: def_wget is included
25# for such situations.
26# ---------------
27# SYNOPSIS
28# ========
29#  $ make_usp_tar.sh uspcfg.txt target_conf target_dir
30# Note target_dir.tar will be created
31#
32# DESCRIPTION
33# ===========
34# - Extract target configuration details from uspcfg.txt
35# - Create target directory
36# - Recursively use wget to retrieve remote configuration files
37#   into target directory
38# - Copy uspcfg.txt into target directory and alter remote paths
39#   to local (relative) versions
40# - tar target_directory and remove originals
41#----------------------------------------------------------------
42#
43#----------------------------------------------------------------
44# Check the correct number of arguments have been provided
45#----------------------------------------------------------------
46#
47   if [ "$#" != "3" ]; then
48    echo "Expected usage: make_usp_tar.sh uspcfg.txt target_conf target_dir"
49    exit
50   fi
51#
52#----------------------------------------------------------------
53# Check the named uspcfg.txt file exists
54#----------------------------------------------------------------
55#
56   if [ ! -f $1 ]; then
57    echo "named uspcfg.txt file does not exist ($1); attempt abandoned"
58    exit
59   fi
60#
61#----------------------------------------------------------------
62# Check the requested configuration is listed in the named uspcfg.txt file
63#----------------------------------------------------------------
64#
65   inthere=$( grep -c "$2" $1 )
66   if [ "$inthere" -lt 1 ]; then
67    echo "requested configuration is not in named uspcfg.txt file ($2); attempt abandoned"
68    exit
69   fi
70#
71#----------------------------------------------------------------
72# Create the target directory if it does not already exist
73# and cd into it
74#----------------------------------------------------------------
75#
76   if [ ! -d $3 ]; then
77    mkdir $3
78   else
79    echo "target directory already exists; attempt abandoned"
80    exit
81   fi
82   basedir=$(pwd)
83#
84   cd ${3}
85#
86#----------------------------------------------------------------
87# Copy named uspcfg.txt file into target directory
88#----------------------------------------------------------------
89#
90   cp $basedir/$1 .
91#
92#----------------------------------------------------------------
93# Extract information on target configuration and
94# retrieve full file list from remote server
95#----------------------------------------------------------------
96#
97   grep "$2 " $1 > ./cfg.tmp
98#
99   LOCAL_REF=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $2}')
100   TAB=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $3}')
101   REMOTE_CTL=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $4}')
102   wget ${REMOTE_CTL} -O remote_file.list
103#
104#----------------------------------------------------------------
105# Retrieve each remote file and create local directory structure
106# At the same time prepare a local version of the control file
107# by replacing http links with a string that will be replaced later
108# with a local directory path
109#----------------------------------------------------------------
110#
111   if [ -f remote_file.list ] ; then
112    cat remote_file.list | grep -v '^#' |
113     while
114      read remfile locfile
115      do
116       if [ $remfile == 'create_directory' ] ;then
117         mkdir $locfile
118         echo $remfile "                 " $locfile >> local.ctl
119       else
120         wget $remfile -O $locfile
121         remfile=$(echo $remfile | sed -e "s;.*$locfile;SET_LOCAL_DIRECTORY_HERE/$locfile;")
122         echo $remfile "  " $locfile >> local.ctl
123       fi
124      done
125   else
126    echo "Unable to find remote_file.list. Attempt abandoned"
127    echo "Files may be left in "$(pwd)
128    exit
129   fi
130#
131#----------------------------------------------------------------
132# Construct a modified version of the named uspcfg.txt file
133# First copy across all the other untouched configuration listed
134#----------------------------------------------------------------
135#
136   grep -v "$2 " $1 >  uspcfg_local.txt
137#
138#----------------------------------------------------------------
139# Now append the modified entry, replacing http links as before
140#----------------------------------------------------------------
141#
142   cat cfg.tmp | sed -e "s;http.*$;SET_LOCAL_DIRECTORY_HERE/local.ctl;" >> uspcfg_local.txt
143#
144#
145#----------------------------------------------------------------
146# Construct a script that can be used later to complete a local installation
147#----------------------------------------------------------------
148#
149cat > set_local_uspcfg << EOF
150#!/bin/bash
151   if [ "\$( echo $SHELL | grep -c bash )" -lt 1 ]; then
152    echo "WARNING: This is only going to be effective in a bash shell"
153    echo "since it redefines the wget command as a bash function."
154    echo "(ignore this comment if you are in a bash shell)"
155   fi
156   basedir=\$(pwd)
157#
158   echo "Enter full path to the CONFIG directory on your target system: "
159   read confdir
160#
161# Edit the local.ctl file to set the local directory path
162#
163   ed - local.ctl << EOFA
164g/SET_LOCAL_DIRECTORY_HERE/s;SET_LOCAL_DIRECTORY_HERE;\$basedir;
165w
166q
167EOFA
168#
169# Edit the uspcfg_local.txt file to set the local directory path
170#
171   ed - uspcfg_local.txt << EOFB
172/SET_LOCAL_DIRECTORY_HERE/s;SET_LOCAL_DIRECTORY_HERE;\$basedir;
173w
174q
175EOFB
176   if [ -f \$confdir/uspcfg.txt ] && [ ! -L \$confdir/uspcfg.txt ]; then
177    mv \$confdir/uspcfg.txt \$confdir/uspcfg_remote.txt
178    echo "\$confdir/uspcfg.txt moved to \$confdir/uspcfg_remote.txt"
179   fi
180   if [ -f \$confdir/uspcfg_local.txt ]; then
181    echo "Existing uspcfg_local.txt file found in \$confdir"
182    echo "This has been moved to: "\$confdir/uspcfg_local.txt\$\$
183    mv \$confdir/uspcfg_local.txt \$confdir/uspcfg_local.txt\$\$
184   fi
185   mv uspcfg_local.txt \$confdir/uspcfg_local.txt
186   ln -s \$confdir/uspcfg_local.txt \$confdir/uspcfg.txt
187#
188# define/redefine the wget command
189#
190function wget {
191   if [ "\$2" != "-O" ]; then
192    echo "Expected wget usage: wget src -O dest"
193    echo "-O not found. No action taken"
194   else
195    cp \$1 \$3
196   fi
197}
198EOF
199#----------------------------------------------------------------
200# Construct a script that can be used later to just redefine wget
201# in bash shell sessions
202#----------------------------------------------------------------
203#
204cat > def_wget << EOFC
205#!/bin/bash
206   if [ "\$( echo $SHELL | grep -c bash )" -lt 1 ]; then
207    echo "WARNING: This is only going to be effective in a bash shell"
208    echo "since it redefines the wget command as a bash function."
209    echo "(ignore this comment if you are in a bash shell)"
210   fi
211function wget {
212   if [ "\$2" != "-O" ]; then
213    echo "Expected wget usage: wget src -O dest"
214    echo "-O not found. No action taken"
215   else
216    cp \$1 \$3
217   fi
218}
219EOFC
220#----------------------------------------------------------------
221# Make sure these scripts have execute permission
222#----------------------------------------------------------------
223   chmod 755 set_local_uspcfg
224   chmod 755 def_wget
225#
226#----------------------------------------------------------------
227# Tidy up and tar the contents of the downloaded configuration
228#----------------------------------------------------------------
229   rm cfg.tmp
230   cd $basedir
231   tar cvf ${3}.tar $3
232   echo ${3}.tar " file successfully created and prepared for local references. Move this"
233   echo "tar file to your target system, unpack and run the set_local_uspcfg script in a "
234   echo "bash shell to complete the process. This script redefines/defines wget as a bash"
235   echo "function to perform local copies from this unpacked tarball. If you wish to create"
236   echo "a new configuration based on this local copy of an unsupported configuration in "
237   echo "future sessions then you may need to rerun the def_wget script"
238exit
Note: See TracBrowser for help on using the repository browser.