summaryrefslogtreecommitdiff
path: root/scripts/xorp_tmpl_tool
blob: 014a53b995dd18d90d3ec5035c49a5d4decc8ce0 (plain)
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
#!/bin/bash

if grep -q union=aufs /proc/cmdline || grep -q aufs /proc/filesystems ; then
    export UNIONFS=aufs
else
    export UNIONFS=unionfs
fi

UMASK_SAVE_G=`umask`
umask 0111
XORPLOGFILE=/tmp/xorp_tmpl_tool.log
touch ${XORPLOGFILE}

umask 0002

#need to pass in value to change... as part of set command...
## cli ENV_EDIT_LEVEL
export VYATTA_EDIT_LEVEL=/;
## cli ENV_TEMPLATE_LEVEL
export VYATTA_TEMPLATE_LEVEL=/;

## cli ENV_A_DIR
export VYATTA_ACTIVE_CONFIGURATION_DIR=/opt/vyatta/config/active;
mkdir -p $VYATTA_ACTIVE_CONFIGURATION_DIR

#now need to grab the parent pid.
## XXX eventually, we will use each session's bash shell pid for this.
##     however, for now, to interact with XORP we will rely on a global lock
##     instead of separate config dirs.
#export VTID=$PPID
export VTID=XORP

# lock for XORP
export XORP_LOCK="/opt/vyatta/config/active/.xorp.lck"

## cli ENV_C_DIR
export VYATTA_CHANGES_ONLY_DIR=/opt/vyatta/config/tmp/changes_only_$VTID;
mkdir -p $VYATTA_CHANGES_ONLY_DIR

## cli ENV_M_DIR
export VYATTA_TEMP_CONFIG_DIR=/opt/vyatta/config/tmp/new_config_$VTID;
if [ ! -d $VYATTA_TEMP_CONFIG_DIR ]
then
	mkdir -p $VYATTA_TEMP_CONFIG_DIR
	sudo mount -t $UNIONFS -o dirs=${VYATTA_CHANGES_ONLY_DIR}=rw:/opt/vyatta/config/active=ro $UNIONFS ${VYATTA_TEMP_CONFIG_DIR}
fi

## cli ENV_TMP_DIR
export VYATTA_CONFIG_TMP=/opt/vyatta/config/tmp/tmp_$VTID;
mkdir -p $VYATTA_CONFIG_TMP

RET_STATUS=0
#this needs to be the array string of commands, something like $[*] or whatever

echo "Command: ${@}" | grep -v -i password >> ${XORPLOGFILE}

#echo "ConfigDirectories BEFORE ========>>>>>>" >> ${XORPLOGFILE}
#find /opt/vyatta/config -name "*" -print | grep interface >> ${XORPLOGFILE}
#echo "<=========ConfigDirectories BEFORE" >> ${XORPLOGFILE}

## for tracing command-line XRL calls.
## 1 => info level
## 2 => warning level
#export CL_XRLTRACE=2

UMASK_SAVE=`umask`
umask 0111
MYCMDERRLOGFILE=/tmp/my_cmd_err_${RANDOM}.log
rm -rf ${MYCMDERRLOGFILE}
umask ${UMASK_SAVE}

case "$1" in 
  set)
    /opt/vyatta/sbin/my_set "${@:2}" >>${XORPLOGFILE} 2>>${MYCMDERRLOGFILE}
    RET_STATUS=$?
    if [ $RET_STATUS != 0 ]; then
      rm -rf $XORP_LOCK >&/dev/null
    fi
    ;;
  delete)
    /opt/vyatta/sbin/my_delete "${@:2}" >>${XORPLOGFILE} 2>>${MYCMDERRLOGFILE}
    RET_STATUS=$?
    if [ $RET_STATUS != 0 ]; then
      rm -rf $XORP_LOCK >&/dev/null
    fi
    ;;
  commit)
    /opt/vyatta/sbin/my_commit >>${XORPLOGFILE} 2>>${MYCMDERRLOGFILE}
    RET_STATUS=$?
    rm -rf $XORP_LOCK >&/dev/null
    ;;
  test)
    "${@:2}"
    RET_STATUS=$?
    ;;
  cleanup)

    LOCKTRYCOUNTER=0
    LOCKTRYSTATUS=-1

    while [[ ${LOCKTRYCOUNTER} -lt 60 && ${LOCKTRYSTATUS} -ne 0 ]] ; do

	if mkdir $XORP_LOCK >&/dev/null ; then
	    LOCKTRYSTATUS=0
	else
	    LOCKTRYCOUNTER=`expr ${LOCKTRYCOUNTER} + 1`
	    sleep 1;
	fi
    done

    if [ ${LOCKTRYCOUNTER} -ge 60 ] ; then
	echo "Cannot unlock configuration" >> ${MYCMDERRLOGFILE}
	rm -rf ${XORP_LOCK}
	mkdir $XORP_LOCK >&/dev/null
    fi

    sudo umount ${VYATTA_TEMP_CONFIG_DIR}
    sudo rm -rf $VYATTA_CHANGES_ONLY_DIR/* $VYATTA_CHANGES_ONLY_DIR/.modified
    sudo mount -t $UNIONFS -o dirs=${VYATTA_CHANGES_ONLY_DIR}=rw:/opt/vyatta/config/active=ro $UNIONFS ${VYATTA_TEMP_CONFIG_DIR}
    RET_STATUS=0
    ;;
  end_loading)
    sudo umount ${VYATTA_TEMP_CONFIG_DIR}
    sudo rm -rf ${VYATTA_CHANGES_ONLY_DIR}
    sudo rm -rf ${VYATTA_CONFIG_TMP}
    sudo rm -rf ${VYATTA_TEMP_CONFIG_DIR}
    RET_STATUS=0
    ;;
  rtrmgr_indirect_cleanup)
    # do nothing now that we handle XORP interaction differently.
    RET_STATUS=0
    ;;
  *)
    rm -rf ${MYCMDERRLOGFILE}
    exit 1  
    ;;
esac

if [ -f ${MYCMDERRLOGFILE} ] ; then
    
    echo -n "STDERR:" >>${XORPLOGFILE}
    cat ${MYCMDERRLOGFILE} >>${XORPLOGFILE}
    echo "end of STDERR" >>${XORPLOGFILE}
    
    cat ${MYCMDERRLOGFILE} 1>&2
    
    rm -rf ${MYCMDERRLOGFILE}
    
fi

#echo "ConfigDirectories AFTER ========>>>>>>" >> ${XORPLOGFILE}
#find /opt/vyatta/config -name "*" -print | grep interface >> ${XORPLOGFILE}
#echo "<=========ConfigDirectories AFTER" >> ${XORPLOGFILE}

echo "ret=${RET_STATUS}" >> ${XORPLOGFILE}
umask ${UMASK_SAVE_G}
exit $RET_STATUS

# Local Variables:
# mode: shell-script
# sh-indentation: 4
# End: