summaryrefslogtreecommitdiffstats
path: root/dev-shell
blob: dfe8db2ea91750a8f4a54bf809e37ca5272dc7ea (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
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
#!/bin/bash

# Copyright (c) 2012-2013 Kaarle Ritvanen
# See LICENSE file for license details

function _acf_req {
    local url=$ACF_URL$1
    shift

    local resp
    resp=$(curl -s -w $'\n\nStatus: %{response_code}\r\n' \
	${ACF_Auth_Token:+-H "X-ACF-Auth-Token: $ACF_Auth_Token"} \
	${ACF_Transaction_ID:+-H "X-ACF-Transaction-ID: $ACF_Transaction_ID"} \
	"$@" $url)
    local code=$?

    if [ $code -gt 0 ]; then
	echo "Request failed, code $code" >&2
	return 1
    fi

    resp=$(echo "$resp" | sed $'s/\r//')
    echo "$resp"

    local status=$(echo "$resp" | sed 's/^Status: //;ta;d;:a;q')
    [ "${status:0:1}" = 2 ] && return

    echo "Request failed" >&2
    return 1
}

function _acf_start_req {
    local url=$1
    shift

    local resp
    resp=$(_acf_req "$url" "$@" -D /proc/self/fd/1 -o /proc/self/fd/3)
    [ $? -eq 0 ] || return

    local txn_id=$ACF_Transaction_ID

    while read line; do
	eval export "$line"
    done < <(echo "$resp" | \
	sed 's/^X-\(ACF-[-A-Za-z]\+\): /\1=/;ta;d;:a;y/-/_/')

    bash --rcfile "$ACF_QD_CLI"
    [ $? -eq 254 ] || _acf_req $url -X DELETE

    ACF_Transaction_ID=$txn_id
}

if [ "$ACF_Auth_Token" ]; then

    if [ "$ACF_Transaction_ID" ]; then
	cat >&2 <<EOF
Transaction $ACF_Transaction_ID started
Type 'commit' to commit, 'exit' to abort

EOF
    else
	cat >&2 <<EOF
Available commands:
  Fetch object:                              get <path>
  Create/update object:                      put <path> <JSON content>
  Add member to list/set or perform action:  post <path> <JSON content>
  Delete object:                             delete <path>
  Fetch metadata:                            meta <path>
  Start transaction:                         start
EOF
	if [ $ACF_Save_Required = 1 ]; then
	    echo "  Save changes persistently:                 save" >&2
	fi
cat >&2 <<EOF

Example: put /awall/zone/internet '{"iface": ["eth0"]}'

EOF
    fi

    PS1="$ACF_USER@acf2-dev-shell${ACF_Transaction_ID:+($ACF_Transaction_ID)}> "

    if [ $ACF_Save_Required = 1 ]; then
	function save {
	    if [ "$ACF_Transaction_ID" ]; then
		echo "Transaction not committed" >&2
		return 1
	    fi
	    _acf_req /save -X POST
	}
    fi

    function start {
	_acf_start_req /transaction -X POST
    }

    function meta {
	_acf_req "/meta$1"
    }

    function _acf_obj_req {
	local path=/config$1
	shift
	_acf_req "$path" "$@"
    }

    function _acf_post_req {
	_acf_obj_req "$2" -d "$3" -X $1
    }

    function get {
	_acf_obj_req "$1"
    }

    function put {
	_acf_post_req PUT "$@"
    }

    function post {
	_acf_post_req POST "$@"
    }

    function delete {
	_acf_obj_req "$1" -X DELETE
    }

    function commit {
	if [ "$ACF_Transaction_ID" ]; then
	    if _acf_req /transaction -X PUT; then
		echo Committed >&2
		exit 254
	    fi
	else
	    echo "No transaction started" >&2
	fi
	return 1
    }

else
    cat >&2 <<EOF
Development shell for ACF2
Copyright (c) 2012-2013 Kaarle Ritvanen

EOF

    exec 3>&1
    export ACF_QD_CLI=$0

    export ACF_URL=http://localhost:8000
    export ACF_USER=`whoami`
    PASSWORD=

    function usage {
	echo "Usage: $0 [-H <server_url>] [-u <username>] [-p <password>]" >&2
	exit $1
    }

    ARGS=$(getopt -o hH:p:u: -- "$@")
    [ $? -gt 0 ] && usage 1
    eval set -- $ARGS

    while :; do
	case $1 in
	    -h)
		usage 0
		;;
	    -H)
		shift
		ACF_URL=$1
		;;
	    -p)
		shift
		PASSWORD=$1
		;;
	    -u)
		shift
		ACF_USER=$1
		;;
	    --)
		break
	esac
	shift
    done

    if [ -z "$PASSWORD" ]; then
	echo -n "Password: " >&2
	read PASSWORD
	echo >&2
    fi

    ACF_Transaction_ID=

    _acf_start_req /login \
	-d "{\"username\": \"$ACF_USER\", \"password\": \"$PASSWORD\"}"
fi