summaryrefslogtreecommitdiff
path: root/functions/wrapper/script-template
blob: 4318fe5fef3169a99c0c7fffd5ccd4cfff369a7f (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
163
164
165
166
#!/bin/vbash
#
# Set up aliases and functions for running Vyatta commands from scripts
#
# Copyright (C) 2012 Vyatta, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Authors: John Southworth, Daniil Baturin
#
# Usage: source /opt/vyatta/etc/functions/script-template

export _OFR_CONFIGURE='ok'
source /etc/bash_completion.d/vyatta-cfg
unset _OFR_CONFIGURE

shopt -s expand_aliases

SBIN_PATH=/opt/vyatta/sbin
BIN_PATH=/opt/vyatta/bin

API=/bin/cli-shell-api

# "pipe" functions
count ()
{
  wc -l
}

match ()
{
  grep -E -e "$1"
}

no-match ()
{
  grep -E -v -e "$1"
}

no-more ()
{
  cat
}

function vyatta_configure()
{
  session_env=$($API getSessionEnv $PPID)
  eval $session_env
  $API setupSession
  echo $session_env
}

function list()
{
   local -a expanded_api_args
   local -a args=( "$@" )
   local path='/opt/vyatta/share/vyatta-cfg/templates'
   vyatta_config_expand_compwords show "${args[@]}"
   for elem in "${expanded_api_args[@]:1}"; do
     path+="/$elem"
   done
   if [[ ! -f $path/node.def ]]; then
     return 1
   fi
   if grep -q -e 'tag:' "$path/node.def"; then
     echo $($API listEffectiveNodes "${expanded_api_args[@]:1}")
   elif grep -q -e 'multi:' "$path/node.def"; then
     echo $($API returnEffectiveValues "${expanded_api_args[@]:1}")  
   else
     echo $($API returnEffectiveValue "${expanded_api_args[@]:1}")  
   fi
}

function vyatta_exit_configure ()
{
  $API teardownSession
  echo -n 'export -n VYATTA_CONFIG_TMP; '
  echo -n 'export -n VYATTA_CHANGES_ONLY_DIR; '
  echo -n 'export -n VYATTA_ACTIVE_CONFIGURATION_DIR; '
  echo -n 'export -n VYATTA_TEMPLATE_LEVEL; '
  echo -n 'export -n VYATTA_CONFIG_TEMPLATE; '
  echo -n 'export -n VYATTA_TEMP_CONFIG_DIR; '
  echo -n 'export -n VYATTA_EDIT_LEVEL; '
}

function load ()
{
  if [[ $# -eq 0 ]]; then
    echo -e "You must provide a file name to load the config from"
    return 1
  fi 

  # don't load if there are uncommitted changes.
  if $API sessionChanged; then
    echo "Cannot load: configuration modified."
    echo "Commit or discard the changes before loading a config file."
    return 1
  fi
  # return to top level.
  reset_edit_level
  ${vyos_libexec_dir}/vyos-load-config.py "$@"
} 

function save ()
{
  if [[ $# -eq 0 ]]; then
    echo -e "You must provide a file name to save the config to"
    return 1
  fi 
  if $API sessionChanged; then
    echo -e "Warning: you have uncommitted changes that will not be saved.\n"
  fi
  # return to top level.
  reset_edit_level
  # transform individual args into quoted strings
  local arg=''
  local save_cmd="${vyos_libexec_dir}/vyos-save-config.py"
  for arg in "$@"; do
    save_cmd+=" '$arg'"
  done
  eval "sudo sg vyattacfg \"umask 0002 ; $save_cmd\""
  $API unmarkSessionUnsaved
} 

function reset_edit_level ()
{
  edit_env=$($API getEditResetEnv)
  eval $edit_env
  return $?
} 

function edit ()
{
  edit_env=$($API getEditEnv "$@")
  eval $edit_env
}

function top ()
{
  if $API editLevelAtRoot; then
    echo "Already at the top level"
    return 0
  fi

  # go to the top level.
  reset_edit_level
}

function up ()
{
  edit_env=$($API getEditUpEnv "$@")
  eval $edit_env
}

alias configure='eval $(vyatta_configure)'
alias exit='eval $(vyatta_exit_configure)'