forked from alfetopito/dokku-ethereum-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
executable file
·206 lines (156 loc) · 3.98 KB
/
functions
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
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
COMMAND=
ETHEREUM_OPTIONS=
ETHEREUM_DIR=
DEBUG=
ID=
IP=
DOCKER_ETHEREUM_IMAGE="alfetopito/ethereum-stable"
function info() {
local msg="$1"
echo "-----> $msg"
}
function failure() {
local msg="$1"
echo "FAILURE: $msg"
exit 1
}
function debug() {
local msg="$1"
if [ ! -z "$DEBUG" ];then
echo "---------> $msg"
fi
}
function check_required_params() {
debug "Check required params"
local command_param="$1"
COMMAND="$command_param"
debug "set COMMAND to $COMMAND"
APP="$2"
debug "set APP to $APP"
# if [[ $command_param == eth:* ]]; then
# fi
}
function check_ethereum_dir() {
debug "Check ethereum dir"
if [ -z $ETHEREUM_DIR ]; then
failure "Call init_vars first (ETHEREUM_DIR not set)"
fi
if [ ! -d $ETHEREUM_DIR ]; then
mkdir -p $ETHEREUM_DIR
chown -R dokku: $ETHEREUM_DIR
debug "Created $ETHEREUM_DIR"
else
debug "$ETHEREUM_DIR exists"
fi
}
function init_vars() {
debug "Init vars"
ETHEREUM_DIR="$DOKKU_ROOT/.ethereum"
check_ethereum_dir
ETHEREUM_OPTIONS_PROPERTY="$ETHEREUM_DIR/options"
if [ -z "$ETHEREUM_OPTIONS" ]; then
ETHEREUM_OPTIONS='-j -b'
fi
echo $ETHEREUM_OPTIONS > "$ETHEREUM_OPTIONS_PROPERTY"
}
function check_image() {
debug "Check image"
local image=$(docker images | grep "$DOCKER_ETHEREUM_IMAGE" | awk '{print $3}')
if [[ -z $image ]]; then
failure "Ethereum image not found... Did you run 'dokku plugins-install'?"
fi
}
function get_container_id() {
local id=$(docker ps -a | grep "$DOCKER_ETHEREUM_IMAGE":latest | awk '{print $1}')
eval "$1=$id"
}
function get_ip() {
debug "Get IP"
get_container_id ID
local ip=$(docker inspect $ID | grep IPAddress | cut -d '"' -f 4)
eval "$1=$ip"
}
function check_is_running() {
debug "Check is running"
local container="$1"
if docker ps -a | grep "$DOCKER_ETHEREUM_IMAGE":latest | grep Exited -q ; then
failure "Ethereum not running. Check eth:logs for details."
fi
}
function eth_logs() {
echo
info "Logs for ethereum"
get_container_id ID
if [[ -z $ID ]]; then
failure "Ethereum not running. Make sure you start it with eth:start"
else
docker logs $ID | tail -n 100
fi
}
function eth_start() {
# Creates a new container or starts the existing
echo
info "Starting ethereum"
check_image
get_container_id ID
if [[ -z $ID ]]; then
debug "Creating a new container"
ID=$(docker run -d $DOCKER_ETHEREUM_IMAGE $ETHEREUM_OPTIONS)
else
debug "Starting existing container"
docker start $ID
fi
sleep 1
check_is_running
info "Ethereum running"
eth_info
}
function eth_info() {
get_ip IP
echo
echo " Options: $ETHEREUM_OPTIONS"
echo
echo " Host: $IP"
echo " Json rpc port: 8080"
echo
}
function eth_version() {
echo
info "Ethereum version"
docker run -i --rm=true $DOCKER_ETHEREUM_IMAGE -V
}
function eth_link() {
if [[ -z $APP ]]; then
failure "eth:link requires an app argument"
fi
echo
info "Linking ethereum to $APP"
get_ip IP
dokku config:set $APP ETHEREUM_HOST="$IP"
info "$APP linked to Ethereum"
}
function eth_stop() {
# Stops the container if running and removes it
echo
info "Stopping ethereum"
get_container_id ID
if [[ ! -z $ID ]]; then
docker kill $ID > /dev/null
sleep 1
docker rm $ID > /dev/null
sleep 1
info "Ethereum stopped"
fi
}
function print_help() {
cat << EOF
eth:start Creates a Ethereum container or starts it if any
eth:stop Stops and removes the Ethereum container if any
eth:logs Displays the last logs from Ethereum container
eth:version Displays Ethereum version
eth:info Displays Ethereum port, ip and options
eth:link <app> Links <app> to Ethereum container
EOF
}