forked from keploy/keploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeploy.sh
178 lines (157 loc) · 6.16 KB
/
keploy.sh
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
#!/bin/bash
installKeploy (){
IS_CI=false
for arg in "$@"
do
case $arg in
-isCI)
IS_CI=true
shift
;;
*)
;;
esac
done
install_keploy_arm() {
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin/keploybin
set_alias 'sudo -E env PATH="$PATH" keploybin'
}
check_sudo(){
if groups | grep -q '\bdocker\b'; then
return 1
else
return 0
fi
}
install_keploy_amd() {
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin/keploybin
set_alias 'sudo -E env PATH="$PATH" keploybin'
}
append_to_rc() {
last_byte=$(tail -c 1 $2)
if [[ "$last_byte" != "" ]]; then
echo -e "\n$1" >> $2
else
echo "$1" >> $2
fi
source $2
}
# Get the alias to set and set it
set_alias() {
# Check if the command is for docker or not
if [[ "$1" == *"docker"* ]]; then
# Check if the user is a member of the docker group
check_sudo
sudoCheck=$?
if [ "$sudoCheck" -eq 0 ] && [ $OS_NAME = "Linux" ]; then
# Add sudo to the alias.
ALIAS_CMD="alias keploy='sudo $1'"
else
ALIAS_CMD="alias keploy='$1'"
fi
else
ALIAS_CMD="alias keploy='$1'"
fi
current_shell="$(basename "$SHELL")"
if [[ "$current_shell" = "zsh" || "$current_shell" = "-zsh" ]]; then
if [ -f ~/.zshrc ]; then
if grep -q "alias keploy=" ~/.zshrc; then
if [ "$OS_NAME" = "Darwin" ]; then
sed -i '' '/alias keploy/d' ~/.zshrc
else
sed -i '/alias keploy/d' ~/.zshrc
fi
fi
append_to_rc "$ALIAS_CMD" ~/.zshrc
else
alias keploy="$1"
fi
elif [[ "$current_shell" = "bash" || "$current_shell" = "-bash" ]]; then
if [ -f ~/.bashrc ]; then
if grep -q "alias keploy=" ~/.bashrc; then
if [ "$OS_NAME" = "Darwin" ]; then
sed -i '' '/alias keploy/d' ~/.bashrc
else
sed -i '/alias keploy/d' ~/.bashrc
fi
fi
append_to_rc "$ALIAS_CMD" ~/.bashrc
else
alias keploy="$1"
fi
else
alias keploy="$1"
fi
}
install_docker() {
check_sudo
sudoCheck=$?
network_alias=""
if [ "$sudoCheck" -eq 0 ] && [ $OS_NAME = "Linux" ]; then
# Add sudo to docker
network_alias="sudo"
fi
if ! $network_alias docker network ls | grep -q 'keploy-network'; then
$network_alias docker network create keploy-network
fi
if [ "$OS_NAME" = "Darwin" ]; then
if ! docker volume inspect debugfs &>/dev/null; then
docker volume create --driver local --opt type=debugfs --opt device=debugfs debugfs
fi
set_alias 'docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v debugfs:/sys/kernel/debug:rw -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock -v '"$HOME"'/.keploy-config:/root/.keploy-config -v '"$HOME"'/.keploy:/root/.keploy --rm ghcr.io/keploy/keploy'
else
set_alias 'docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock -v '"$HOME"'/.keploy-config:/root/.keploy-config -v '"$HOME"'/.keploy:/root/.keploy --rm ghcr.io/keploy/keploy'
fi
}
ARCH=$(uname -m)
if [ "$IS_CI" = false ]; then
OS_NAME="$(uname -s)"
if [ "$OS_NAME" = "Darwin" ]; then
if ! which docker &> /dev/null; then
echo -n "Docker not found on device, please install docker to use Keploy"
return
fi
# Check if docker is running
if ! docker info &> /dev/null; then
echo "Keploy only supports intercepting and replaying docker containers on macOS, and requires Docker to be installed and running. Please start Docker and try again."
return
fi
install_docker
return
elif [ "$OS_NAME" = "Linux" ]; then
if ! sudo mountpoint -q /sys/kernel/debug; then
sudo mount -t debugfs debugfs /sys/kernel/debug
fi
if [ "$ARCH" = "x86_64" ]; then
install_keploy_amd
elif [ "$ARCH" = "aarch64" ]; then
install_keploy_arm
else
echo "Unsupported architecture: $ARCH"
return
fi
elif [[ "$OS_NAME" == MINGW32_NT* ]]; then
echo "\e]8;; https://pureinfotech.com/install-windows-subsystem-linux-2-windows-10\aWindows not supported please run on WSL2\e]8;;\a"
elif [[ "$OS_NAME" == MINGW64_NT* ]]; then
echo "\e]8;; https://pureinfotech.com/install-windows-subsystem-linux-2-windows-10\aWindows not supported please run on WSL2\e]8;;\a"
else
echo "Unknown OS, install Linux to run Keploy"
fi
else
if [ "$ARCH" = "x86_64" ]; then
install_keploy_amd
elif [ "$ARCH" = "aarch64" ]; then
install_keploy_arm
else
echo "Unsupported architecture: $ARCH"
return
fi
fi
}
installKeploy
if command -v keploy &> /dev/null; then
keploy example
rm keploy.sh
fi