-
Notifications
You must be signed in to change notification settings - Fork 34
/
cloudrc
140 lines (128 loc) · 3.74 KB
/
cloudrc
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
# -*- mode: sh -*-
localrc="${BASH_SOURCE[0]}.local"
source "$localrc"
guest_userid_template=ZCC%05X
run_dir=/opt/cloudland/run
cache_dir=/opt/cloudland/cache
cache_tmp_dir=$cache_dir/tmp
backup_dir=$cache_dir/backup
volume_dir=/opt/cloudland/cache/volume
image_dir=$cache_dir/instance
image_cache=$cache_dir/image
xml_dir=/opt/cloudland/cache/xml
template_dir=/opt/cloudland/scripts/xml
container_dir=/var/lib/lxd/containers
dmasq_dir=/opt/cloudland/cache/dnsmasq
mudata_dir=/opt/cloudland/mudata
snapshot_dir=/var/snapshot
deploy_dir=/opt/cloudland/deploy
vxlan_db=/opt/cloudland/db/cloudland.db
cland_private_key=$deploy_dir/.ssh/cland.key
export JAEGER_SERVICE_NAME="SCI-Backend"
function die()
{
echo $1
exit -1
}
function inet_aton()
{
ip="$1"
hex=`printf '%02x' ${ip//./ }`
printf "%lu\n" "0x${hex}"
}
function inet_ntoa()
{
num="$1"
hex=`printf "%08x\n" ${num}`
for i in `echo ${hex} | sed "s/\(..\)/\1 /g"`; do
printf '%hu.' "0x${i}"
done | sed "s/\.$//g"
}
function get_tunip()
{
tunip=$(ifconfig $vxlan_interface | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1)
echo "tunip=$tunip" >> $localrc
}
function apply_fw()
{
action=$1
chain=$2
shift
shift
rule=$*
if [ "$action" = '-I' -o "$action" = '-A' ]; then
iptables -D $chain $rule 2>/dev/null
elif [ "$action" = '-N' ]; then
iptables -S $chain || iptables -N $chain
fi
iptables $action $chain $rule
}
function apply_vnic()
{
action=$1
vnic=$2
apply_fw $action FORWARD -m physdev --physdev-in $vnic --physdev-is-bridged -j ACCEPT
apply_fw $action FORWARD -m physdev --physdev-out $vnic --physdev-is-bridged -j ACCEPT
}
function apply_bridge()
{
action=$1
mybr=$2
apply_fw $action FORWARD -i $mybr -o $mybr -j ACCEPT
}
function sync_target()
{
target=$1
node=$2
cd $deploy_dir
group=hyper
if [ -n "$node" ]; then
group=$(cat hosts/hosts | grep client_id=$node | awk '{print $1}')
fi
ansible $group -u cland -b -a "chown -R cland.cland $target"
ansible $group -u cland -m synchronize -a "src=$target dest=$target"
cd -
}
function copy_target()
{
target=$1
node=$2
cd $deploy_dir
[ -n "$node" ] && hyper_node=$(cat hosts/hosts | grep client_id=$node | awk '{print $1}')
scp -i $cland_private_key -o StrictHostKeyChecking=no -r ${target} cland@${hyper_node}:${target}
cd -
}
function action_target()
{
node=$1
command=$2
cd $deploy_dir
[ -n "$node" ] && hyper_node=$(cat hosts/hosts | grep client_id=$node | awk '{print $1}')
ssh -i $cland_private_key -o StrictHostKeyChecking=no cland@$hyper_node "$command"
cd -
}
function sql_exec()
{
if [ ! -f $vxlan_db ]; then
mkdir /opt/cloudland/db
touch $vxlan_db
sqlite3 $vxlan_db "CREATE TABLE vxlan_rules (id INTEGER PRIMARY KEY AUTOINCREMENT, instance varchar(32), vni INTEGER, inner_ip varchar(32), inner_mac varchar(48), outer_ip varchar(32))"
fi
sqlite3 $vxlan_db "$*"
}
function wds_curl()
{
action=$1
path=$2
data=$3
[ -z "$data" ] && data='{}'
token="bearer $(curl -L -XPOST -k "$wds_address/api/v1/login" -H "Content-Type: application/json" -d "{\"name\": \"$wds_admin\",\"password\": \"$wds_pass\"}" | jq -r .access_token)"
curl -s -L -X$action -H 'Content-Type: application/json' -H "Authorization: $token" -k "$wds_address/$path" -d "$data"
}
function get_uss_gateway()
{
uss_name=$(hostname -s | tr -s '-' '_')
uss_id=$(wds_curl GET "api/v2/wds/uss?name=$uss_name" | jq -r '.uss_gateways[0].id')
[ -z "$uss_id" -o "$uss_id" = null ] && uss_id=$(wds_curl GET "api/v2/wds/uss" | jq --arg hname $(hostname -s) -r '.uss_gateways | .[] | select(.server_name == $hname) | .id')
[ -n "$uss_id" ] && echo $uss_id
}