- made common behaviour both for installer and acc

This commit is contained in:
Stanislav Ievlev 2008-04-24 17:30:09 +04:00
parent 59dcbd39a4
commit a6c56ac85a
5 changed files with 282 additions and 207 deletions

@ -1,14 +1,45 @@
#!/bin/sh
cachedir="/var/cache/alterator/net-eth"
po_domain="alterator-net-eth"
iftab_file="/etc/iftab"
. alterator-sh-functions
. shell-config
PATH="/usr/lib/alterator-net-common:$PATH"
################### interface modificators
### cache
init_cache()
{
local ifacedir="/etc/net/ifaces/$1"
if [ ! -d "$cachedir/$1" ] ;then
[ -d "$ifacedir" ] && cp -a "$ifacedir" "$cachedir"
mkdir -p "$cachedir/$1"
fi
[ -f "$cachedir/iftab" ] || cp /etc/iftab "$cachedir/iftab"
}
clear_cache()
{
rm -rf "$cachedir"
mkdir "$cachedir"
}
commit_cache()
{
[ -f "$cachedir/iftab" ] && mv -f "$cachedir/iftab" /etc/iftab
find $cachedir -maxdepth 1 -mindepth 1 -type d|
while read iface; do
[ -n "$iface" ] || exit
rm -rf "/etc/net/ifaces/${iface##*/}"
mv -f "$iface" "/etc/net/ifaces/${iface##*/}"
done
clear_cache
}
### interface work
list_mask()
{
@ -34,21 +65,17 @@ list_hw_binding()
printf '("businfo" label "%s")' "`_ "by bus location"`"
}
restart_iface()
{
ifdown "$1" && ifup "$1"
}
restart_all_ifaces() {
iflist|
while read iface mac; do
restart_iface "$iface"
done >&2
}
read_hw_binding()
{
local line="$(grep "^[[:space:]]*$1[[:space:]]" "$iftab_file")"
local config=
if [ -f "$cachedir/iftab" ];then
config="$cachedir/iftab"
else
config="/etc/iftab"
fi
local line="$(grep "^[[:space:]]*$1[[:space:]]" "$config")"
if [ -z "$line" ]; then
echo "missing"
@ -63,12 +90,8 @@ read_hw_binding()
fi
}
read_iface()
read_info()
{
local name="$1" && shift
echo "("
#collect general information
local info=
ifcheckup "$name" && info="`_ "interface is up"`" || info="`_ "interface is down"`"
@ -79,14 +102,29 @@ read_iface()
3) info="$info, `_ "unplugged"`" ;;
esac
fi
echo "$info"
}
printf 'info "%s" ' "$info"
read_iface()
{
local name="$1"; shift
local ifacedir=
if [ -d "$cachedir/$name" ];then
ifacedir="$cachedir/$name"
else
ifacedir="/etc/net/ifaces/$name"
fi
#collect general information
printf 'info "%s" ' "$(read_info)"
printf 'wireless %s ' "$(ifcheckwireless "$name" && echo "#t" || echo "#f")"
printf 'hw_binding "%s" ' "$(read_hw_binding "$name")"
local addr= ip= mask= bootproto=
[ ! -s "/etc/net/ifaces/$name/ipv4address" ] ||
addr=$(grep '^[0-9]' "/etc/net/ifaces/$name/ipv4address" | head -n1 || echo "")
[ ! -s "/$ifacedir/ipv4address" ] ||
addr=$(grep '^[0-9]' "$ifacedir/ipv4address" | head -n1 || echo "")
if [ -n "$addr" ]; then
ip="${addr%%/*}"
@ -95,39 +133,43 @@ read_iface()
printf 'ip "%s" mask "%s"\n' "$ip" "${mask:-32}"
fi
[ ! -s "/etc/net/ifaces/$name/ipv4route" ] ||
[ ! -s "/$ifacedir/ipv4route" ] ||
printf 'default "%s"\n' \
$(grep '^default' "/etc/net/ifaces/$name/ipv4route" | sed -r 's,default[[:space:]]+via[[:space:]],,' || echo "")
$(grep '^default' "$ifacedir/ipv4route" | sed -r 's,default[[:space:]]+via[[:space:]],,' || echo "")
bootproto="$(ifvar "$name" "BOOTPROTO")"
[ "$bootproto" = "dhcp" ] && echo "dhcp #t" || echo "dhcp #f"
echo ")"
local bootproto="$(ifvar "$name" "BOOTPROTO")"
case "$bootproto" in
dhcp) echo "dhcp #t" ;;
static) echo "dhcp #f" ;;
*) echo 'dhcp #t' ;;
esac
}
write_hw_binding()
{
local name="$1";shift
local value="$1";shift
local config="$cachedir/iftab"
local mac="$(ifread "$name"|cut -f1)"
local businfo="$(ifread "$name"|cut -f2)"
if [ -s "$iftab_file" ]; then
if [ -s "$config" ]; then
sed -r \
-e "/^$name[[:space:]]/ d" \
-i "$iftab_file"
-i "$config"
[ -z "$mac" ] ||
[ "$mac" = "00:00:00:00:00:00" ] ||
sed -r \
-e "/[[:space:]]mac[[:space:]]+$mac([[:space:]]|\$)/ d" \
-i "$iftab_file"
-i "$config"
[ -z "$businfo" ] ||
sed -r \
-e "/[[:space:]]businfo[[:space:]]+$businfo([[:space:]]|\$)/ d" \
-i "$iftab_file"
-i "$config"
fi
case "$value" in
@ -135,34 +177,34 @@ write_hw_binding()
mac)
[ -z "$mac" ] ||
[ "$mac" = "00:00:00:00:00:00" ] ||
printf "%s\tmac %s\n" "$name" "$mac" >>"$iftab_file"
printf "%s\tmac %s\n" "$name" "$mac" >>"$config"
;;
businfo)
[ -z "$businfo" ] ||
printf "%s\tbusinfo %s\n" "$name" "$businfo" >>"$iftab_file"
printf "%s\tbusinfo %s\n" "$name" "$businfo" >>"$config"
;;
esac
}
write_iface()
{
local name="$1" && shift
local ifacedir="/etc/net/ifaces/$name"
local options="$ifacedir/options"
local ifacedir="$cachedir/$name"
[ -d "$ifacedir" ] || mkdir "$ifacedir"
init_cache "$name"
[ -n "$in_mask" ] && [ -n "$in_ip" ] && \
printf '%s/%s\n' "$in_ip" "$in_mask" >"/etc/net/ifaces/$name/ipv4address"
printf '%s/%s\n' "$in_ip" "$in_mask" >"/$ifacedir/ipv4address"
[ -n "$in_default" ] && [ -s "/etc/net/ifaces/$name/ipv4address" ] && \
printf 'default via %s\n' "$in_default" >"/etc/net/ifaces/$name/ipv4route"
[ -n "$in_default" ] && [ -s "/$ifacedir/ipv4address" ] && \
printf 'default via %s\n' "$in_default" >"/$ifacedir/ipv4route"
shell_config_set "$options" TYPE eth
shell_config_set "$ifacedir/options" TYPE eth
[ -n "$in_dhcp" ] &&
shell_config_set \
"$options" \
"$ifacedir/options" \
BOOTPROTO \
$(test_bool "$in_dhcp" && echo "dhcp" || echo "static")
@ -170,25 +212,42 @@ write_iface()
write_hw_binding "$name" "$in_hw_binding"
}
commit_iface()
{
commit_cache
iflist|
while read iface rest; do
ifdown "$iface" && ifup "$iface"
done >&2
}
reset_iface()
{
clear_cache
}
#initial actions
ifup lo
clear_cache
on_message()
{
case "$in_action" in
constraints)
echo '('
printf 'info (label "%s")' "`_ "Interface state"`"
printf 'dhcp (exclude (#t ip) exclude (#t default) exclude (#t mask) default #f label "%s")' \
"`_ "Use DHCP"`"
printf 'ip (required #t ipv4-address #t label "%s")' \
"`_ "IP address"`"
printf 'default (ipv4-address #t label "%s")' \
"`_ "Default gateway"`"
printf 'mask (label "%s")' \
"`_ "NetMask"`"
printf 'hw_binding (label "%s")' \
"`_ "Hardware binding"`"
if [ "$in__objects" != "/" ];then
printf 'info (label "%s")' "`_ "Interface state"`"
printf 'dhcp (exclude (#t ip) exclude (#t default) exclude (#t mask) default #f label "%s")' \
"`_ "Use DHCP"`"
printf 'ip (required #t ipv4-address #t label "%s")' \
"`_ "IP address"`"
printf 'default (ipv4-address #t label "%s")' \
"`_ "Default gateway"`"
printf 'mask (label "%s")' \
"`_ "NetMask"`"
printf 'hw_binding (label "%s")' \
"`_ "Hardware binding"`"
fi
echo ')'
;;
list)
@ -201,19 +260,16 @@ on_message()
echo ')'
;;
read)
if [ "$in__objects" == "/" ];then
write_nop
else
read_iface "${in__objects}"
fi
echo "("
local name="$in__objects"
[ "$name" == "/" ] || read_iface "$name"
echo ")"
;;
write)
write_iface "${in__objects}"
[ "$in_restart" = "#f" ] || restart_iface "$in__objects"
write_nop
;;
restart)
restart_all_ifaces
local name="${in__objects}"
[ "$name" == "/" ] || write_iface "$name"
test_bool "$in_commit" && commit_iface
test_bool "$in_reset" && reset_iface
write_nop
;;
*)

@ -1,4 +0,0 @@
(define (base-behaviour)
(ifaces (when selected (update-interface (current-interface))))
(c-button (when clicked (commit-interface "/net-eth" (current-interface))))
(r-button (when clicked (update-interface (current-interface)))))

@ -1,58 +0,0 @@
(define avail-ifaces (woo-list/name+label "/net-eth"))
(define avail-masks (woo-list/name+label "/net-eth/eth0/avail_masks"))
(define avail-hw-bindings (woo-list/name+label "/net-eth/eth0/avail_hw_bindings"))
(define (update-interface name)
(and (not-empty-string? name)
(let ((cmd (woo-read-first (string-append "/net-eth" "/" name))))
(iface-info text (string-append "<small>(" (woo-get-option cmd 'info) ")</small>"))
(iface-dhcp state (woo-get-option cmd 'dhcp #f) toggled)
(iface-ip text (woo-get-option cmd 'ip))
(iface-mask current (string-list-index (woo-get-option cmd 'mask "24") (map car avail-masks)))
(iface-hw-binding current (string-list-index (woo-get-option cmd 'hw_binding) (map car avail-hw-bindings)))
(w-button activity (woo-get-option cmd 'wireless))
(iface-gw text (woo-get-option cmd 'default)))))
(define (commit-interface path name . args)
(if (or (iface-dhcp state) (not-empty-string? (iface-ip text)))
(and (not-empty-string? name)
(woo-catch/message
(thunk
(apply
woo-write/constraints (string-append path "/" name)
'dhcp (iface-dhcp state)
'ip (iface-ip text)
'mask (current-mask)
'hw_binding (current-hw-binding)
'default (iface-gw text) args))))
#t))
(define (current-interface)
(and (number? (ifaces current))
(>= (ifaces current) 0)
(car (list-ref avail-ifaces (ifaces current)))))
(define (current-mask)
(car (list-ref avail-masks (iface-mask current))))
(define (current-hw-binding)
(car (list-ref avail-hw-bindings (iface-hw-binding current))))
(define (net-wifi name)
(and (not-empty-string? name)
(document:popup "/net-wifi/" 'interface name)))
(define (common-behaviour)
(w-button (when clicked (net-wifi (current-interface))))
(ifaces rows (map cdr avail-ifaces))
(and (positive? (ifaces count))
(begin (ifaces current 0)
(update-interface (current-interface))))
;;constraints
(iface-dhcp (when toggled
((widgets iface-ip
iface-gw
iface-mask)
activity (not (iface-dhcp state)))))
(iface-dhcp toggled))

@ -1,70 +1,179 @@
(document:surround "/std/frame")
(document:insert "/std/functions")
(document:insert "/net-eth/common-functions")
(document:insert "/net-eth/base-functions")
(document:insert "/net-eth/install-functions")
(document:envelop with-translation _ "alterator-net-eth")
;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Functions
(define *avail-ifaces* (make-cell '()))
(define *avail-masks* (make-cell '()))
(define *avail-hw-bindings* (make-cell '()))
(define *prev-current* (make-cell 0))
(define (prev-interface)
(car (list-ref (cell-ref *avail-ifaces*) (cell-ref *prev-current*))))
(define (mask-index cmd)
(or (string-list-index (woo-get-option cmd 'mask "24") (map car (cell-ref *avail-masks*)))
0))
(define (hw-binding-index cmd)
(format #t "cmd=~S,list=~S~%" (woo-get-option cmd 'hw_binding) (map car (cell-ref *avail-hw-bindings*)))
(or (string-list-index (woo-get-option cmd 'hw_binding) (map car (cell-ref *avail-hw-bindings*)))
0))
(define (current-interface)
(let ((c (ifaces current)))
(and (number? c)
(>= c 0)
(car (list-ref (cell-ref *avail-ifaces*) c)))))
(define (current-mask)
(let ((c (iface-mask current)))
(if (>= c 0)
(car (list-ref (cell-ref *avail-masks*) c))
"")))
(define (current-hw-binding)
(let ((c (iface-hw-binding current)))
(if (>= c 0)
(car (list-ref (cell-ref *avail-hw-bindings*) c))
"")))
(define (read-interface name)
(and (not-empty-string? name)
(let ((cmd (woo-read-first (string-append "/net-eth" "/" name))))
(iface-info text (string-append "<small>(" (woo-get-option cmd 'info) ")</small>"))
(iface-dhcp state (woo-get-option cmd 'dhcp #f) toggled)
(iface-ip text (woo-get-option cmd 'ip))
(iface-mask current (mask-index cmd))
(iface-hw-binding current (hw-binding-index cmd))
(w-button activity (woo-get-option cmd 'wireless))
(iface-gw text (woo-get-option cmd 'default)))))
(define (write-interface path name)
(and (not-empty-string? name)
(woo-write/constraints
(string-append path "/" name)
'dhcp (iface-dhcp state)
'ip (iface-ip text)
'mask (current-mask)
'hw_binding (current-hw-binding)
'default (iface-gw text))))
(define (commit-interface)
(woo-catch/message
(thunk
(write-interface "/net-eth/" (current-interface))
(and (global 'frame:next)
(write-interface "/autoinstall/net-eth/" (current-interface)))
(woo-write "/net-eth" 'commit #t)
(and (global 'frame:next)
(woo-write "/net-eth" 'commit #t)))))
(define (reset-interface)
(woo-catch/message
(thunk
(woo-write "/net-eth" 'reset #t)
(and (global 'frame:next)
(woo-write "/net-eth" 'reset #t))
(let ((avail-masks (woo-list/name+label "/net-eth/eth0/avail_masks")))
(cell-set! *avail-masks* avail-masks)
(iface-mask rows (map cdr avail-masks)))
(let ((avail-hw-bindings (woo-list/name+label "/net-eth/eth0/avail_hw_bindings")))
(cell-set! *avail-hw-bindings* avail-hw-bindings)
(iface-hw-binding rows (map cdr avail-hw-bindings)))
(let ((avail-ifaces (woo-list/name+label "/net-eth")))
(cell-set! *avail-ifaces* avail-ifaces)
(ifaces rows (map cdr avail-ifaces))
(or (null? avail-ifaces)
(begin (ifaces current 0)
(cell-set! *prev-current* 0)
(read-interface (current-interface))))))))
;;; UI
(gridbox
columns "20;20;40;20"
;;
(spacer)
(label (_ "Interface") align "right")
(document:id ifaces (combobox))
(spacer)
columns "20;20;40;20"
;;
(spacer)
(label (_ "Interface") align "right")
(document:id ifaces (combobox
(when selected
(or (woo-catch/message
(thunk
(write-interface "/net-eth" (prev-interface))
(and (global 'frame:next) (write-interface "/autoinstall/net-eth" (prev-interface)))
(read-interface (current-interface))
(cell-set! *prev-current* (ifaces current))))
(ifaces current (cell-ref *prev-current*))))))
(spacer)
;;
(spacer)
(spacer)
(document:id iface-info (label ""))
(spacer)
;;
(spacer)
(spacer)
(document:id iface-info (label ""))
(spacer)
;;
(spacer)
(spacer)
(document:id iface-dhcp (checkbox (_ "Use DHCP") widget-name "dhcp"))
(spacer)
;;
(spacer)
(label (_ "IP address") align "right")
(document:id iface-ip (edit "" widget-name "ip"))
(spacer)
;;
(spacer)
(label (_ "Netmask") align "right")
(document:id iface-mask (combobox "" rows (map cdr avail-masks) widget-name "mask"))
(spacer)
;;
(spacer)
(label (_ "Default gateway") align "right")
(document:id iface-gw (edit "" widget-name "default"))
(spacer)
;;
(spacer)
(label (_ "Hardware binding") align "right")
(document:id iface-hw-binding (combobox "" rows (map cdr avail-hw-bindings) widget-name "hw_binding"))
;;
(spacer)
(spacer)
(spacer)
(document:id w-button (button (_ "Wireless settings")))
;;
(spacer)
(spacer)
(document:id iface-dhcp (checkbox (_ "Use DHCP")
(when toggled
((widgets iface-ip
iface-gw
iface-mask)
activity (not (iface-dhcp state))))))
(spacer)
)
;;
(spacer)
(label (_ "IP address") align "right")
(document:id iface-ip (edit ""))
(spacer)
;;
(spacer)
(label (_ "Netmask") align "right")
(document:id iface-mask (combobox))
(spacer)
;;
(spacer)
(label (_ "Default gateway") align "right")
(document:id iface-gw (edit ""))
(spacer)
;;
(spacer)
(label (_ "Hardware binding") align "right")
(document:id iface-hw-binding (combobox))
;;
(spacer)
(spacer)
(spacer)
(document:id w-button (button (_ "Wireless settings")
activity #f
(when clicked
(let ((name (current-interface)))
(and (not-empty-string? name)
(document:popup "/net-wifi/" 'interface name))))))
)
;;;;;;;;;;;;;;;
(or (global 'frame:next)
(hbox align "center"
(document:id c-button (button (_ "Apply")))
(document:id r-button (button (_ "Reset")))))
(document:id c-button (button (_ "Apply") (when clicked (commit-interface))))
(document:id r-button (button (_ "Reset") (when clicked (reset-interface))))))
;;;;;;;;;;;;;;;;;;
(common-behaviour)
(if (global 'frame:next) (install-behaviour) (base-behaviour))
(document:root
(when loaded (reset-interface)))
(frame:on-back (thunk (or (commit-interface) 'cancel)))
(frame:on-next (thunk (or (commit-interface) 'cancel)))

@ -1,28 +0,0 @@
(define prev-current (make-cell 0))
(define (prev-interface)
(car (list-ref avail-ifaces (cell-ref prev-current))))
(define (commit-current-interface)
(and (commit-interface "/net-eth" (current-interface) 'restart #f)
(commit-interface "/autoinstall/net-eth" (current-interface) 'restart #f)))
(define (restart-interfaces)
(and (commit-current-interface)
(woo-catch/message
(thunk
(woo-try "restart" "/net-eth")
(woo-try "restart" "/autoinstall/net-eth")))))
(define (install-behaviour)
(ifaces (when selected
(and (commit-interface "/net-eth" (prev-interface) 'restart #f)
(commit-interface "/autoinstall/net-eth" (prev-interface) 'restart #f)
(update-interface (current-interface))
(cell-set! prev-current (ifaces current)))))
(and (global 'frame:auto-save)
(cell-set! (global 'frame:auto-save) commit-current-interface))
(frame:on-back (thunk (or (restart-interfaces) 'cancel)))
(frame:on-next (thunk (or (restart-interfaces) 'cancel))))