Skip to content

Commit d71f8c2

Browse files
committed
fc
0 parents  commit d71f8c2

7 files changed

+627
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.rpm

Diff for: .rpm-install.sh.swp

16 KB
Binary file not shown.

Diff for: .yum-install.sh.swp

20 KB
Binary file not shown.

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Installation Scripts for DEB, RPM based distros.
2+
3+
The `yum-install.sh` script will replace the `rpm-install.sh`.

Diff for: deb-install.sh

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
# Function to check if a command exists
4+
command_exists() {
5+
command -v "$1" >/dev/null 2>&1
6+
}
7+
8+
# Check if required commands are available
9+
required_commands=("sudo" "mkdir" "touch" "tee" "date" "curl" "uname" "source" "sed" "tr" "systemctl" "chmod" "dpkg" "apt-get")
10+
missing_commands=()
11+
12+
for cmd in "${required_commands[@]}"; do
13+
if ! command_exists "$cmd"; then
14+
missing_commands+=("$cmd")
15+
fi
16+
done
17+
18+
if [ ${#missing_commands[@]} -gt 0 ]; then
19+
echo "Error: The following required commands are missing: ${missing_commands[*]}"
20+
echo "Please install them and run the script again."
21+
exit 1
22+
fi
23+
24+
LOG_FILE="/var/log/mw-agent/apt-installation-$(date +%s).log"
25+
sudo mkdir -p /var/log/mw-agent
26+
sudo touch "$LOG_FILE"
27+
28+
MW_TRACKING_TARGET="https://app.middleware.io"
29+
if [ -n "$MW_API_URL_FOR_CONFIG_CHECK" ]; then
30+
export MW_TRACKING_TARGET="$MW_API_URL_FOR_CONFIG_CHECK"
31+
fi
32+
33+
34+
function send_logs {
35+
status=$1
36+
message=$2
37+
host_id=$(eval hostname)
38+
39+
payload=$(cat <<EOF
40+
{
41+
"status": "$status",
42+
"metadata": {
43+
"script": "linux-deb",
44+
"status": "ok",
45+
"message": "$message",
46+
"host_id": "$host_id",
47+
"script_logs": "$(sed 's/$/\\n/' "$LOG_FILE" | tr -d '\n' | sed 's/"/\\\"/g')"
48+
}
49+
}
50+
EOF
51+
)
52+
53+
curl -s --location --request POST $MW_TRACKING_TARGET/api/v1/agent/tracking/$MW_API_KEY \
54+
--header 'Content-Type: application/json' \
55+
--data "$payload" > /dev/null
56+
}
57+
58+
function force_continue {
59+
read -p "Do you still want to continue? (y|N): " response
60+
case "$response" in
61+
[yY])
62+
echo "Continuing with the script..."
63+
;;
64+
[nN])
65+
echo "Exiting script..."
66+
exit 1
67+
;;
68+
*)
69+
echo "Invalid input. Please enter 'yes' or 'no'."
70+
force_continue # Recursively call the function until valid input is received.
71+
;;
72+
esac
73+
}
74+
75+
function on_exit {
76+
if [ $? -eq 0 ]; then
77+
send_logs "installed" "Script Completed"
78+
else
79+
send_logs "error" "Script Failed"
80+
fi
81+
}
82+
83+
trap on_exit EXIT
84+
85+
# recording agent installation attempt
86+
send_logs "tried" "Agent Installation Attempted"
87+
88+
# Check if the system is running Linux
89+
if [ "$(uname -s)" != "Linux" ]; then
90+
echo "This machine is not running Linux, The script is designed to run on a Linux machine."
91+
force_continue
92+
fi
93+
94+
MW_LATEST_VERSION="1.6.6"
95+
export MW_LATEST_VERSION
96+
# Check if MW_VERSION is provided
97+
if [ "${MW_VERSION}" = "" ]; then
98+
MW_VERSION=$MW_LATEST_VERSION
99+
fi
100+
export MW_VERSION
101+
echo -e "\nInstalling Middleware Agent version ${MW_VERSION} on hostname $(hostname) at $(date)" | sudo tee -a "$LOG_FILE"
102+
103+
# Check if /etc/os-release file exists
104+
if [ -f /etc/os-release ]; then
105+
source /etc/os-release
106+
case "$ID" in
107+
debian|ubuntu)
108+
echo "os-release ID is $ID"
109+
;;
110+
*)
111+
case "$ID_LIKE" in
112+
debian|ubuntu)
113+
echo "os-release ID_LIKE is $ID_LIKE"
114+
;;
115+
*)
116+
echo "This is not a Debian based Linux distribution."
117+
force_continue
118+
;;
119+
esac
120+
esac
121+
else
122+
echo "/etc/os-release file not found. Unable to determine the distribution."
123+
force_continue
124+
fi
125+
126+
if [ "${MW_DETECTED_ARCH}" = "" ]; then
127+
MW_DETECTED_ARCH=$(dpkg --print-architecture)
128+
echo -e "cpu architecture detected: '"${MW_DETECTED_ARCH}"'"
129+
else
130+
echo -e "cpu architecture provided: '"${MW_DETECTED_ARCH}"'"
131+
fi
132+
export MW_DETECTED_ARCH
133+
134+
MW_APT_LIST_ARCH=""
135+
if [[ $MW_DETECTED_ARCH == "arm64" || $MW_DETECTED_ARCH == "armhf" || $MW_DETECTED_ARCH == "armel" || $MW_DETECTED_ARCH == "armeb" ]]; then
136+
MW_APT_LIST_ARCH=arm64
137+
elif [[ $MW_DETECTED_ARCH == "amd64" || $MW_DETECTED_ARCH == "i386" || $MW_DETECTED_ARCH == "i486" || $MW_DETECTED_ARCH == "i586" || $MW_DETECTED_ARCH == "i686" || $MW_DETECTED_ARCH == "x32" ]]; then
138+
MW_APT_LIST_ARCH=amd64
139+
else
140+
echo ""
141+
fi
142+
143+
if [ "${MW_AGENT_HOME}" = "" ]; then
144+
MW_AGENT_HOME=/opt/mw-agent
145+
fi
146+
export MW_AGENT_HOME
147+
148+
if [ "${MW_KEYRING_LOCATION}" = "" ]; then
149+
MW_KEYRING_LOCATION=/usr/share/keyrings
150+
fi
151+
export MW_KEYRING_LOCATION
152+
153+
if [ "${MW_APT_LIST}" = "" ]; then
154+
MW_APT_LIST=mw-agent.list
155+
fi
156+
export MW_APT_LIST
157+
158+
MW_AGENT_BINARY=mw-agent
159+
if [ "${MW_AGENT_BINARY}" = "" ]; then
160+
MW_AGENT_BINARY=mw-agent
161+
fi
162+
163+
export MW_AGENT_BINARY
164+
165+
if [ "${MW_AUTO_START}" = "" ]; then
166+
MW_AUTO_START=true
167+
fi
168+
export MW_AUTO_START
169+
170+
if [ "${MW_API_KEY}" = "" ]; then
171+
echo "MW_API_KEY environment variable is required and is not set."
172+
force_continue
173+
fi
174+
export MW_API_KEY
175+
176+
if [ "${MW_TARGET}" = "" ]; then
177+
echo "MW_TARGET environment variable is required and is not set."
178+
force_continue
179+
fi
180+
export MW_TARGET
181+
182+
echo -e "\nThe host agent will monitor all '.log' files inside your /var/log directory recursively [/var/log/**/*.log]\n"
183+
184+
# Adding APT repo address & public key to system
185+
sudo curl -q -fs https://apt.middleware.io/gpg-keys/mw-agent-apt-public.key | sudo gpg --dearmor -o ${MW_KEYRING_LOCATION}/middleware-keyring.gpg
186+
sudo touch /etc/apt/sources.list.d/${MW_APT_LIST}
187+
188+
echo -e "Adding Middleware Agent APT Repository ...\n"
189+
echo "deb [arch=${MW_APT_LIST_ARCH} signed-by=${MW_KEYRING_LOCATION}/middleware-keyring.gpg] https://apt.middleware.io/public stable main" | sudo tee /etc/apt/sources.list.d/$MW_APT_LIST > /dev/null
190+
191+
# Updating apt list on system
192+
sudo apt-get update -o Dir::Etc::sourcelist="sources.list.d/${MW_APT_LIST}" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" > /dev/null
193+
194+
# Installing Agent
195+
echo -e "Installing Middleware Agent Service ...\n"
196+
sudo -E apt-get install -y ${MW_AGENT_BINARY}=${MW_VERSION}
197+
# Check for errors
198+
if [ $? -ne 0 ]; then
199+
echo "Error: Failed to install Middleware Agent."
200+
exit $?
201+
fi
202+
203+
sudo systemctl daemon-reload
204+
205+
sudo systemctl enable mw-agent
206+
#check for errors
207+
if [ $? -ne 0 ]; then
208+
echo "Error: Failed to enable Middleware Agent service."
209+
exit $?
210+
fi
211+
212+
if [ "${MW_AUTO_START}" = true ]; then
213+
sudo systemctl start mw-agent
214+
fi
215+
216+
echo -e "Middleware Agent installation completed successfully.\n"

0 commit comments

Comments
 (0)