Last active
March 13, 2019 09:30
-
-
Save sid226/e310f9c9bcfb30552bea5ff096a2c260 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Need handling for RHEL 6.10 as it doesn't have os-release file | |
if [ -f "/etc/os-release" ]; then | |
source "/etc/os-release" | |
else | |
cat /etc/redhat-release >>"${LOG_FILE}" | |
export ID="centos" | |
export VERSION_ID="6.x" | |
export PRETTY_NAME="Red Hat Enterprise Linux 6.x" | |
fi | |
DISTRO="$ID-$VERSION_ID" | |
VERSION="$ID" | |
CURDIR="$(pwd)" | |
#Check if directory exists | |
if [ ! -d "$CURDIR/logs/" ]; then | |
mkdir -p "$CURDIR/logs/" | |
fi | |
PACKAGE_LIST="$CURDIR/logs/${DISTRO}-$(date +"%F-%T").list" | |
DATA_FILE="$CURDIR/logs/${DISTRO}-$(date +"%F-%T").json" | |
case "$VERSION" in | |
"ubuntu") | |
# Commands for UBUNTU | |
printf -- " Get package data for %s \n" "$DISTRO" | |
apt update && apt install sudo vim curl | |
#Update repo for gitla-ce | |
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash | |
#Update repo for Docker | |
sudo apt-get update | |
sudo apt-get install -y \ | |
apt-transport-https \ | |
ca-certificates \ | |
curl \ | |
gnupg-agent \ | |
software-properties-common | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
printf -- " Adding docker repo for x86.\n" | |
sudo add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | |
$(lsb_release -cs) \ | |
stable" | |
apt update | |
apt list > aptlist.txt | |
sed '1,1d' aptlist.txt > "${DISTRO}"_temp.txt | |
awk '{printf("{\"packageName\":\"%s\", \"version\":\"%s\"},\n",$1,$2)}' "${DISTRO}"_temp.txt | sed -e '1s/^/[/' -e '$s/$/\]/' | sed '$ s/\,]/]/g' > xUbuntu_${VERSION_ID}_Package_List.json | |
;; | |
"centos") | |
# Commands for RHEL | |
printf -- " Generate package data for %s \n" "$DISTRO" | |
yum info | grep -e "^Name :" -e "^Version :" | sed 's/Name : /"packageName" : "/g' | sed 's/Version : /"version" : "/g' | sed '/^\"packageName\" :/{N;s/\n/", /;}' | sed 's/$/\"},/g' | sed 's/^/{/g' | LC_ALL=C sort | uniq | sed '1s/^/[/' | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' | sed -e '$s/,$/]/' > xRHEL_${VERSION_ID}_Package_List.json | |
;; | |
"opensuse-leap" | "sles") | |
# Commands for SLES | |
printf -- " Get package data for %s \n" "$DISTRO" | |
zypper refresh -s | |
zypper pa -u > one_un.txt | |
zypper pa -i > one_in.txt | |
awk '{$1=$2=$3=$4=$6=$8=$9=""; print $0}' one_in.txt > temp_in.txt | |
awk '{$1=$2=$3=$5=$7=$8=""; print $0}' one_un.txt > temp_un.txt | |
awk '{printf("{\"packageName\":\"%s\", \"version\":\"%s\"},\n",$1,$2)}' temp_in.txt | sed -e '1s/^/[/' -e '$s/$/\]/' > temp_in2.txt | |
awk '{printf("{\"packageName\":\"%s\", \"version\":\"%s\"},\n",$1,$2)}' temp_un.txt | sed -e '1s/^/[/' -e '$s/$/\]/' > temp_un2.txt | |
sed '1,6d' temp_in2.txt | sed '1s/^/[/' | sed '$ s/\,]/,/g' > final_in.txt | |
sed '1,6d' temp_un2.txt | sed '$ s/\,]/]/g' > final_un.txt | |
cat final_in.txt final_un.txt > xSuse_Linux_Enterprise_Server_${VERSION_ID}_Package_List.json | |
;; | |
"alpine") | |
# Commands for Alpine | |
printf -- " Get package data for %s \n" "$DISTRO" | |
apk update && apk search -a | sed 's/\(-[0-9]\)/ \1/g' | sed 's/ -/ \1/g' | sed '1 i\\n' | sed '1,1d' > "${DISTRO}"_packagelist.txt | |
awk -F ' ' 'BEGIN {printf("[")} NR > 2 {printf("{\"packageName\":\"%s\", \"version\":\"%s\"},\n",$1,$2)}' "${DISTRO}"_packagelist.txt | sed -e '$s/$/\]/' | sed '$ s/\,]/]/g' > xAlpine_${VERSION_ID}_Package_List.json | |
;; | |
"fedora") | |
# Commands for fedora | |
printf -- " Get package data for %s \n" "$DISTRO" | |
dnf list > "${DISTRO}"_packagelist.txt; | |
awk 'BEGIN {printf("[")} NF==3 { printf("{\"packageName\":\"%s\",\"version\":\"%s\"},",$1,$2)} END {printf("]")}' "${DISTRO}"_packagelist.txt | sed 's/,]$/]/' | sed 's/},/},\n/g' > xFedora_${VERSION_ID}_Package_List.json | |
;; | |
"debian") | |
# Commands for debian | |
printf -- " Get package data for %s \n" "$DISTRO" | |
apt-get update && apt list > aptlist.txt | |
sed '1,1d' aptlist.txt > "${DISTRO}"_temp.txt | |
awk '{$3=""; print $0}' "${DISTRO}"_temp.txt > "${DISTRO}"_temp2.txt | |
awk '{printf("{\"packageName\":\"%s\", \"version\":\"%s\"},\n",$1,$2)}' "${DISTRO}"_temp2.txt > "${DISTRO}"_temp3.txt | |
sed -e '1s/^/[/' -e '$s/$/\]/' "${DISTRO}"_temp3.txt > "${DISTRO}"_temp4.txt | |
sed '$ s/\,]/]/g' "${DISTRO}"_temp4.txt > xDebian_${VERSION_ID}_Package_List.json | |
;; | |
*) | |
printf -- "%s not supported \n" "$DISTRO" | tee -a "$LOG_FILE" | |
exit 1 | |
;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment