#!/usr/bin/env bash
#########################################
#Copyright (c) 2023 ABLECLOUD Co. Ltd.
#
#mold 패키징하는 스크립트
#
#최초작성자 : 배태주 책임
#최초작성일 : 2023-07-12
#########################################
set -x

# 파일 위치
wget -P /root https://images.ablecloud.io/mold/security_certification/ablestack.spec
wget -P /root https://images.ablecloud.io/mold/security_certification/ablestack_install_preparation.spec
wget -P /root https://images.ablecloud.io/mold/security_certification/ablestack_install_hypervisor_preparation.spec
ablestack_install_preparation_spec_file="/root/ablestack_install_preparation.spec"
ablestack_install_preparation_package_path="/root/install_prepare-4.0.15"

#필요 폴더 생성 및 삭제
rm -rf /root/rpmbuild
rm -rf $ablestack_install_preparation_package_path
mkdir $ablestack_install_preparation_package_path
mkdir $ablestack_install_preparation_package_path/rpms
mkdir $ablestack_install_preparation_package_path/rpms_temp
mkdir $ablestack_install_preparation_package_path/pip3

# rpm-build 다운로드 및 설치
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install rpm-build
dnf -y install rpm-build

# openssl build에 필요한 rpm 다운로드
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install make gcc perl-core pcre-devel wget zlib-devel
#dnf -y install make gcc perl-core pcre-devel wget zlib-devel

#mysql 8.0.36 다운로드
wget -P $ablestack_install_preparation_package_path https://images.ablecloud.io/mold/security_certification/mysql/mysql-8.0.36-1.el9.x86_64.rpm-bundle.tar
tar -xf $ablestack_install_preparation_package_path/mysql-8.0.36-1.el9.x86_64.rpm-bundle.tar -C $ablestack_install_preparation_package_path/rpms_temp
rm -rf $ablestack_install_preparation_package_path/mysql-8.0.36-1.el9.x86_64.rpm-bundle.tar

#mold 의존 RPM 다운로드
# dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install mysql mysql-server
#dnf -y install mysql-server
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install expect
#dnf -y install expect
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install genisoimage
#dnf -y install genisoimage
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install nfs-utils
#dnf -y install nfs-utils
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install qemu-img
#dnf -y install qemu-img
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install ipmitool
#dnf -y install ipmitool
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install python3-pip
#dnf -y install python3-pip
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install rng-tools
#dnf -y install rng-tools
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install chkconfig
#dnf -y install chkconfig
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install tzdata-java
#dnf -y install tzdata-java
dnf -y --downloadonly --downloaddir=$ablestack_install_preparation_package_path/rpms_temp install mount.nfs
mv $ablestack_install_preparation_package_path/rpms_temp/* $ablestack_install_preparation_package_path/rpms/
#dnf -y install mount.nfs

#dnf -y install python3-pip
#pip3 download -d $ablestack_install_preparation_package_path/pip3 --no-deps wheel
#pip3 download -d $ablestack_install_preparation_package_path/pip3 --no-deps easy_install
wget -P $ablestack_install_preparation_package_path/pip3 https://images.ablecloud.io/mold/security_certification/pip3/easy_install-66.0.2.tar.gz
wget -P $ablestack_install_preparation_package_path/pip3 https://images.ablecloud.io/mold/security_certification/pip3/wheel-0.41.1-py3-none-any.whl

# mold java 호환 문제로 다운그레이드
# rm -f $ablestack_install_preparation_package_path/rpms/java-11-openjdk-*
# wget -P $ablestack_install_preparation_package_path/rpms https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/java-11-openjdk-11.0.18.0.10-3.el9.x86_64.rpm
# wget -P $ablestack_install_preparation_package_path/rpms https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/java-11-openjdk-headless-11.0.18.0.10-3.el9.x86_64.rpm

wget -P $ablestack_install_preparation_package_path https://images.ablecloud.io/mold/security_certification/bootstrap/preparation_bootstrap.sh

# rpm 다운로드 temp 폴더 삭제
rm -rf $ablestack_install_preparation_package_path/rpms_temp

#rpm-build 설치
cd /root
mkdir -p /root/rpmbuild/SOURCES
tar -zcf install_prepare-4.0.15.tar.gz install_prepare-4.0.15
mv install_prepare-4.0.15.tar.gz /root/rpmbuild/SOURCES/

#rpm build
rpmbuild -bb $ablestack_install_preparation_spec_file

########## ABLESTACK-V4.0-4.0.15 rpmbuild ##########
# 파일 위치
ablestack_spec_file="/root/ablestack.spec"
ablestack_package_path="/root/ABLESTACK-V4.0-4.0.15"

#필요 폴더 생성 및 삭제
rm -rf $ablestack_package_path
mkdir $ablestack_package_path
mkdir $ablestack_package_path/rpms
mkdir $ablestack_package_path/rpms_temps
mkdir $ablestack_package_path/openssl

# openssl 다운로드
wget -P $ablestack_package_path/openssl https://www.openssl.org/source/openssl-3.3.0.tar.gz

# jdk 다운로드
dnf -y --downloadonly --downloaddir=$ablestack_package_path/rpms_temp install java-11-openjdk
#dnf -y install java-11-openjdk

mv $ablestack_package_path/rpms_temp/* $ablestack_package_path/rpms/

#systemvm 템플릿 다운로드
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/systemvm/systemvmtemplate-4.19.0-kvm.qcow2.bz2                                 

#bootstrap.sh 다운로드
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/bootstrap/bootstrap.sh

#after_setting.sh 다운로드
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/after_setting.sh

#certificate_renewal.sh 다운로드
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/certificate_renewal.sh

#reset_integrity_verification.sh 다운로드
#wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/reset_integrity_verification.sh

#mold rpm 다운로드
wget -P $ablestack_package_path/mold_rpms https://images.ablecloud.io/mold/security_certification/mold_rpm/cloudstack-common-4.19.0.0-Mold.Diplo.202405151411.1.x86_64.rpm
wget -P $ablestack_package_path/mold_rpms https://images.ablecloud.io/mold/security_certification/mold_rpm/cloudstack-management-4.19.0.0-Mold.Diplo.202405151411.1.x86_64.rpm
wget -P $ablestack_package_path/mold_rpms https://images.ablecloud.io/mold/security_certification/mold_rpm/cloudstack-usage-4.19.0.0-Mold.Diplo.202405151411.1.x86_64.rpm

#jar 다운로드
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/jar/jetty-maven-plugin-9.4.54.v20240208.jar
wget -P $ablestack_package_path/ https://images.ablecloud.io/mold/security_certification/jar/tink-1.13.0.jar

# rpm 다운로드 temp 폴더 삭제
rm -rf $ablestack_package_path/rpms_temp

cd /root
rm -rf /root/rpmbuild/SOURCES/*
mkdir -p /root/rpmbuild/SOURCES
tar -zcf ABLESTACK-V4.0-4.0.15.tar.gz ABLESTACK-V4.0-4.0.15
mv ABLESTACK-V4.0-4.0.15.tar.gz /root/rpmbuild/SOURCES/

#rpm build
rpmbuild -bb $ablestack_spec_file

########## ABLESTACK-V4.0-4.0.15 hypervisor rpmbuild ##########
ablestack_install_hypervisor_preparation_spec_file="/root/ablestack_install_hypervisor_preparation.spec"
ablestack_install_hypervisor_preparation_package_path="/root/install_hypervisor_prepare-4.0.15"

#필요 폴더 생성 및 삭제
rm -rf $ablestack_install_hypervisor_preparation_package_path
mkdir $ablestack_install_hypervisor_preparation_package_path
mkdir $ablestack_install_hypervisor_preparation_package_path/rpms
mkdir $ablestack_install_hypervisor_preparation_package_path/rpms_temp
mkdir $ablestack_install_hypervisor_preparation_package_path/hypervisor_rpms


dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install python3-libvirt
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install qemu-img
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install java-11-openjdk
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install libvirt
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install perl
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install qemu-kvm
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install rng-tools
dnf -y --downloadonly --downloaddir=$ablestack_install_hypervisor_preparation_package_path/rpms_temp install tzdata-java

mv $ablestack_install_hypervisor_preparation_package_path/rpms_temp/* $ablestack_install_hypervisor_preparation_package_path/rpms/

# cloudstack 다운로드
wget -P $ablestack_install_hypervisor_preparation_package_path/hypervisor_rpms https://images.ablecloud.io/mold/security_certification/mold_rpm/cloudstack-agent-4.19.0.0-Mold.Diplo.202405151411.1.x86_64.rpm
wget -P $ablestack_install_hypervisor_preparation_package_path/hypervisor_rpms https://images.ablecloud.io/mold/security_certification/mold_rpm/cloudstack-common-4.19.0.0-Mold.Diplo.202405151411.1.x86_64.rpm

wget -P $ablestack_install_hypervisor_preparation_package_path https://images.ablecloud.io/mold/security_certification/bootstrap/preparation_hypervisor_bootstrap.sh

# rpm 다운로드 temp 폴더 삭제
rm -rf $ablestack_install_hypervisor_preparation_package_path/rpms_temp

#rpm-build 설치
cd /root
rm -rf /root/rpmbuild/SOURCES/*
mkdir -p /root/rpmbuild/SOURCES
tar -zcf install_hypervisor_prepare-4.0.15.tar.gz install_hypervisor_prepare-4.0.15
mv install_hypervisor_prepare-4.0.15.tar.gz /root/rpmbuild/SOURCES/

#rpm build
rpmbuild -bb $ablestack_install_hypervisor_preparation_spec_file
