-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbundle.sh
195 lines (180 loc) · 5.32 KB
/
bundle.sh
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
bashfoo_require flag
##
## bashfoo budler - almost bash-package-manager or bash-pack
##
##
## Build transferable scripts in fashion similar to
## c/c++ linker and/or javascript webpack.
##
## Usage:
##
## Modularized code
##
## log_exec() {
## echo "$0: $@"
## "$@"
## }
## my_function() {
## : bashfoo-import-var DATA_ROOT_DIR
## : bashfoo-import-fun log_exec
## : bashfoo-import-fun timeout from bashfoo/timeout # future
##
## log_exec mkdir -p $DATA_ROOT_DIR/$1
## timeout 2 curl http://example.com/ > $DATA_ROOT_DIR/$1/index.html
## }
##
## Bundle and run code on remote machine
##
## DATA_ROOT_DIR="/tmp/foo"
## ssh foo@hot "$(bundle my_function) ; my_function foobar"
##
## Call to bundle, will "export" `my_function` and all it's dependencies:
## - `DATA_ROOT_DIR` variable content
## - `log_exec` local function
## - `timeout` function as imported from bashfoo/timeout using bashfoo_require
##
## In such a way that remote bash can interpret it, and `my_function` .can be called.
##
## Problem
##
## One might ask what problem does it solve. It solves
## problem or running short (at beginning) scripts using tools like ssh on "remote"
## shells/machines. Writing script inline works for short one-liners, but as one-liner grows,
## escaping and \backslashing and semicoloning; everything makes script very awful e.g
##
## ssh "$target" "set -x ; \
## tf=\$(readlink -f $rollback_link) ; \
## tv=\$(basename \$tf) ; \
## if [ -e \$tf ] ; then \
## ln -snf \$tf '$current_link' ; \
## if [ -x '$activate_script' ] ; then echo 'rdt: invoking activate script $activate_script' ; '$activate_script' ; fi \
## else \
## echo rdt: unable to find version to rollback: \$tf ; \
## exit 1 ; \
## fi ; \
## echo rdt: last version activated was \$tv"
## then
## echo "$0: activated $app_name@$version on $target"
## fi
##
## So with this script, we can write it as it is normal script:
##
## function activate_app() {
## : bashfoo-import-var rollback_link
## : bashfoo-import-var activate_script
##
## local tf=$(readlink -f $rollback_link)
## local tv=$(basename $tf)
## if [ -e $tf ] ; then
## ln -snf \$tf '$current_link'
## if [ -x '$activate_script' ] ; then
## echo 'rdt: invoking activate script $activate_script'
## '$activate_script'
## fi
## else
## echo rdt: unable to find version to rollback: $tf
## exit 1
## fi
## echo "rdt: last version activated was $tv"
## }
##
## And use like this:
##
## ssh $target "$(bundle activate_app) ; activate_app"
##
export_function()
## use_function VAR [ALIAS]
##
## emits eval-able function source code so it can
## be used in target environment
{
type $1 | tail -n +2
}
export_variable()
## use_variable VAR [ALIAS]
##
## emits eval-able script that
## sets VAR (or ALIAS) to actual value
## of $VAR from current execution context
{
local name=${2-$1}
local value="$(eval echo "\$""$1" | sed -e "s|\([\\\"\\\\]\)|\\\\\1|g")"
echo "${2-$1}=\"$value\""
}
export_function_recursive_int()
{
local body="$(export_function "$1")"
while read type imports ; do
echo "$1 -> $type $import" >&2
if check_flag _imported $import ; then
echo "$1 -> $$import already included" >&2
break
fi
set_flag _imported $import
if [ $type == 'bashfoo-import-var' ] ; then
export_variable $import
elif [ $type == 'bashfoo-import-fun' ] ; then
export_function_recursive_int $import
else
echo "$1 -> unknown import type: $type" >&2
return 1
fi
done < <(echo "$body" | egrep --only-matching 'bashfoo-import-(var|fun)\s+([a-z_A-Z0-9-]+)')
echo "$body"
}
export_module_recursive_int()
{
local body="$(cat "$1")"
pushd $(dirname $1) >& /dev/null
local import
while read type import ; do
#echo "$1 -> $type $import" >&2
if check_flag _imported $import ; then
#echo "$1 -> $$import already included" >&2
break
fi
set_flag _imported $import
if [ $type == 'bashfoo-source' ] ; then
echo "## bashfoo-bundled-start $import"
export_module_recursive_int $import
echo "## bashfoo-bundled-end $import"
else
echo "$1 -> unknown import type: $type" >&2
return 1
fi
done < <(echo "$body" | egrep --only-matching 'bashfoo-source\s+([.a-z_A-Z0-9-]+)')
popd >& /dev/null
echo "## bashfoo-module-body $1"
echo "$body"
}
export_module_recursive()
{
_imported=""
export_module_recursive_int "$1"
_imported=""
}
export_modules_recursive()
{
_imported=""
for module in "$@" ; do
export_module_recursive_int "$1"
done
_imported=""
}
export_function_recursive()
##
## export_function_recursive NAME
##
## exports eval-able script which will
## contain function NAME and all it's
## dependencies as declared by : bashfoo-import-* directives
##
{
_imported=""
use_function_recursive_int "$1"
_imported=""
}
bundle()
{
export_function_recursive "$1"
}