#!/usr/bin/env bash EPKG_USER=$1 EPKG_TARS_PATH=/tmp/$EPKG_USER EPKG_USER_HOME=/home/$EPKG_USER URL=https://eulermaker.compass-ci.openeuler.openatom.cn/api/ems1/repositories/epkg/ EPKG_MANAGER_TAR=epkg_manager.tar.gz EPKG_ROOTFS_TAR=epkg_rootfs.tar.gz EPKG_INITIAL_SH=epkg_initial.sh create_epkg_user() { if [ $EPKG_USER != root ]; then useradd $EPKG_USER fi } mk_home() { echo "Attention: Two tars will be saved in $EPKG_TARS_PATH" echo "sure to continue? (y: continue, others: exit)" read choice if [ "$choice" != "y" ]; then return fi mkdir -p $EPKG_TARS_PATH } download_and_unpack() { echo "Attention: Need 150M space to download and unpack tars to $EPKG_TARS_PATH" echo "sure to continue? (y: continue, others: exit)" read choice if [ "$choice" != "y" ]; then return 1 fi if [ ! -f /tmp/$EPKG_INITIAL_SH ]; then curl -o /tmp/$EPKG_INITIAL_SH $URL/$EPKG_INITIAL_SH fi cp /tmp/$EPKG_INITIAL_SH $EPKG_USER_HOME if [ ! -f /tmp/$EPKG_MANAGER_TAR ]; then curl -o /tmp/$EPKG_MANAGER_TAR $URL/$EPKG_MANAGER_TAR fi tar -xvf /tmp/$EPKG_MANAGER_TAR -C $EPKG_TARS_PATH > /dev/null if [ ! -f /tmp/$EPKG_ROOTFS_TAR ]; then curl -o /tmp/$EPKG_ROOTFS_TAR $URL/$EPKG_ROOTFS_TAR fi tar -zxvf /tmp/$EPKG_ROOTFS_TAR -C $EPKG_TARS_PATH > /dev/null if [ $EPKG_USER != root ]; then chown -R $EPKG_USER:$EPKG_USER $EPKG_TARS_PATH chown $EPKG_USER:$EPKG_USER $EPKG_USER_HOME/$EPKG_INITIAL_SH fi return 0 } install_needed_tools() { local package_name="util-linux fakeroot" if rpm -q $package_name >/dev/null 2>&1; then return 0 fi local max_retries=3 local retry_interval=5 local download_timeout=60 local retry_count=0 echo "Attention: util-linux, fakeroot were needed for initialization" echo "sure to continue? (y: continue, others: exit)" read choice if [ "$choice" != "y" ]; then return 1 fi while ((retry_count < max_retries)); do if timeout ${download_timeout}s yum install -y $package_name; then echo "Package $package_name installed successfully on attempt $((retry_count+1))" return 0 else echo "Installation failed on attempt $((retry_count+1)). Retrying after $retry_interval seconds..." ((retry_count++)) sleep $retry_interval fi done echo "Failed to install package $package_name after $max_retries attempts." return 1 } echo "Execute user: $USER, initail epkg user: $EPKG_USER" # step 0. create nonroot user create_epkg_user # step 1. mk path to save tar files mk_home # step 2. install needed tools install_needed_tools # step 3. download epkg_manager.tar.gz and epkg_rootfs.tar.gz download_and_unpack if [ $? -ne 0 ]; then exit 1 fi