forked from cuulee/picluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pictl
executable file
·177 lines (157 loc) · 8.03 KB
/
pictl
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
#!/usr/bin/env bash
clear
# Test if jq is installed
if type jq &>/dev/null; then
# Config file
config="config.json"
# Set config parameters to assign to variables
config_token=$(jq .token $config | sed -e 's/\"//g')
config_server_port=$(jq .server_port $config | sed -e 's/\"//g')
config_ssl=$(jq .ssl $config | sed -e 's/\"//g')
config_web_connect=$(jq .web_connect $config | sed -e 's/\"//g')
hb(){
clear
echo "Heartbeat is now checking....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/heartbeat?token=${config_token}"
else curl -s "http://${config_web_connect}:${config_server_port}/heartbeat?token=${config_token}"
fi
logs
}
nodes(){
clear
echo "Active Nodes:"
if [ "${config_ssl}" == "true" ]; then echo;curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname)"';echo
else echo;curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname)"';echo
fi
}
metrics(){
clear
echo "Nodes Metrics"
if [ "${config_ssl}" == "true" ]; then echo;curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) CPU: \(.cpu_percent) Memory: \(.memory_percentage) Disk: \(.disk_percentage) Running: \(.total_running_containers) "'
else echo;curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) CPU: \(.cpu_percent) Memory: \(.memory_percentage) Disk: \(.disk_percentage) Running: \(.total_running_containers) Network In : \(.network_rx) Network Out : \(.network_tx)"';echo
fi
}
stop(){
clear
echo "Stopping the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=stop" "https://${config_web_connect}:${config_server_port}/containers/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=stop" "http://${config_web_connect}:${config_server_port}/containers/manage"
fi
}
images(){
clear
echo "Gathering a list of images....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.images)"'
else curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.images)"'
fi
}
addimage(){
clear
if [ "${config_ssl}" == "true" ]; then
echo "Uploading container zip file to Server......."
curl -X POST -F "file=@$1" -F "token=${config_token}" "https://${config_web_connect}:${config_server_port}/receive-file"
echo
else
echo "Uploading container Zip file to Server......."
curl -X POST -F "file=@$1" -F "token=${config_token}" "http://${config_web_connect}:${config_server_port}/receive-file"
echo
fi
}
shell(){
clear
echo "Running the command....."
if [ "${config_ssl}" == "true" ]; then curl -H "Content-Type: application/json" -X POST -d '{"command":"'"$1"'","token":"'"${config_token}"'"}' "https://${config_web_connect}:${config_server_port}/exec"
else curl -H "Content-Type: application/json" -X POST -d '{"command":"'"$1"'","token":"'"${config_token}"'"}' "http://${config_web_connect}:${config_server_port}/exec"
fi
}
clean(){
clear
echo "Cleaning stale Docker images and containers...."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/prune?token=${config_token}"
else curl -H "Content-Type: application/json" -X POST -d '{"token":"'"${config_token}"'"}' "http://${config_web_connect}:${config_server_port}/prune"
fi
}
build() {
clear
echo "Building the container(s). This may take a while......"
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&no_cache=1&operation=build" "https://${config_web_connect}:${config_server_port}/image/manage"
else curl -s -G -d "token=${config_token}&container=$1&no_cache=1&operation=build" "http://${config_web_connect}:${config_server_port}/image/manage"
fi
}
logs(){
sleep 5
clear
echo "Checking the logs....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/log?token=${config_token}"
else curl -s "http://${config_web_connect}:${config_server_port}/log?token=${config_token}"
fi
}
create() {
clear
echo "Running the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1" "https://${config_web_connect}:${config_server_port}/create"
else curl -s -G -d "token=${config_token}&container=$1" "http://${config_web_connect}:${config_server_port}/create"
fi
}
start(){
clear
echo "Starting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=start" "https://${config_web_connect}:${config_server_port}/containers/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=start" "http://${config_web_connect}:${config_server_port}/containers/manage"
fi
}
restart(){
clear
echo "Restarting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=restart" "https://${config_web_connect}:${config_server_port}/containers/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=restart" "http://${config_web_connect}:${config_server_port}/containers/manage"
fi
}
changehost(){
clear
echo "Changing the host....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&newhost=$2&container=$1" "https://${config_web_connect}:${config_server_port}/changehost"
else curl -s -G -d "token=${config_token}&newhost=$2&container=$1" "http://${config_web_connect}:${config_server_port}/changehost"
fi
}
delete(){
clear
echo "Deleting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=rm" "https://${config_web_connect}:${config_server_port}/containers/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=rm" "http://${config_web_connect}:${config_server_port}/containers/manage"
fi
}
status() {
clear
echo "Checking the status....."
if [ "${config_ssl}" == "true" ]; then curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq ".data[].running_containers"
else curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.running_containers)"'
fi
}
if [ -z "$1" ]; then
echo "Error, no args specified!"
echo
arguments="build metrics addimage nodes images start create status exec start stop clean "
echo "Valid arguments are: $arguments"
echo
exit
fi;
if [ "$1" = "hb" ]; then hb;exit; fi;
if [ "$1" = "exec" ]; then shell "$2";exit; fi;
if [ "$1" = "metrics" ]; then metrics "$2";exit; fi;
if [ "$1" = "status" ]; then echo "Gathering Cluster Status....";status;exit; fi;
if [ "$1" = "nodes" ]; then nodes;exit; fi;
if [ "$1" = "addimage" ]; then addimage "$2";exit; fi;
if [ "$1" = "clean" ]; then clean;exit; fi;
if [ "$1" = "images" ]; then images;exit; fi;
if [ "$1" = "log" ]; then echo "Gathering Logs....";logs;exit; fi;
if [ "$1" = "restart" ]; then restart "$2";exit; fi;
if [ "$1" = "changehost" ]; then changehost "$2" "$3";exit; fi;
if [ "$1" = "delete" ]; then delete "$2";exit; fi;
if [ "$1" = "build" ]; then echo "Building Images....";build "$2"; fi;
if [ "$1" = "start" ]; then echo "Running container(s)....";start "$2"; fi;
if [ "$1" = "stop" ]; then echo "Stopping container(s)....";stop "$2"; fi;
if [ "$1" = "create" ]; then echo "Creating container(s)....";create "$2"; fi;
else
echo "Please install jq"
fi