#!/bin/sh # 多版本函数库 CREATE_VERSION_SOFTLINK="create_version_softlink.sh" REMOVE_LATEST_SOFTLINK="remove_latest_softlink.sh" # 获取包安装类型 get_package_install_type() { local _outvar="$1" local _install_info="$2" local _package="$3" local _install_type_gpit="" eval "${_outvar}=\"\"" if [ ! -f "${_install_info}" ]; then return 1 fi _install_type_gpit="$(grep -i "^\(${_package}_\)\?install_type=" "${_install_info}" | cut -d"=" -f2-)" if [ "${_install_type_gpit}" = "" ]; then return 1 fi eval "${_outvar}=\"${_install_type_gpit}\"" } # 获取包特性参数 get_package_feature_type() { local _outvar="$1" local _install_info="$2" local _package="$3" local _operation="$4" local _feature_type_gpft="" eval "${_outvar}=\"\"" if [ ! -f "${_install_info}" ]; then return 1 fi if [ "${_package}" = "opp" ]; then _feature_type_gpft="$(grep -i "^Opp_Install_Feature=" "${_install_info}" | cut -d"=" -f2-)" else _feature_type_gpft="$(grep -i "^\(${_package}_\)\?feature_type=" "${_install_info}" | cut -d"=" -f2-)" fi if [ "${_feature_type_gpft}" = "" ]; then _feature_type_gpft="all" fi normalize_feature "_feature_type_gpft" "${_feature_type_gpft}" "${_operation}" eval "${_outvar}=\"${_feature_type_gpft}\"" } # 获取包芯片参数 get_package_chip_type() { local _outvar="$1" local _install_info="$2" local _package="$3" local _operation="$4" local _cihp_gpct="" eval "${_outvar}=\"\"" if [ ! -f "${_install_info}" ]; then return 1 fi if [ "${_package}" = "opp" ]; then _cihp_gpct="$(grep -i "^Opp_Install_Chip=" "${_install_info}" | cut -d"=" -f2-)" else _cihp_gpct="$(grep -i "^\(${_package}_\)\?chip_type=" "${_install_info}" | cut -d"=" -f2-)" fi if [ "${_cihp_gpct}" = "" ]; then _cihp_gpct="all" fi normalize_feature "_cihp_gpct" "${_cihp_gpct}" "${_operation}" eval "${_outvar}=\"${_cihp_gpct}\"" } # 创建latest软链接 create_latest_softlink() { local latest_dirpath="$1" local line="$2" local version_dirpath="$4" local target latest_filepath version_filepath softlink local ret __index_list "${line}" 1 "target" 4 "softlink" __set_abs_path "${latest_dirpath}" "${target}" "latest_filepath" __set_abs_path "${version_dirpath}" "${target}" "version_filepath" if [ -d "${latest_filepath}" ]; then remove_dir_icp "${latest_filepath}" ret="$?" && [ $ret -ne 0 ] && return $ret fi create_softlink_icp "-r" "${version_filepath}" "${latest_filepath}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${softlink}" != "NA" ]; then create_softlink_by_install_path "${latest_dirpath}" "${target}" "${softlink}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi return 0 } # 删除latest软链接 del_latest_softlink() { local latest_dirpath="$1" local line="$2" local target latest_filepath softlink local ret __index_list "${line}" 1 "target" 4 "softlink" __set_abs_path "${latest_dirpath}" "${target}" "latest_filepath" if [ "${softlink}" != "NA" ]; then remove_softlinks "${install_path}" "${softlink}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi remove_softlink_icp "${latest_filepath}" "NA" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # 迁移latest/conf实体目录下的文件到版本conf目录下 migrate_conf_files_from_latest_to_version() { local install_type="$1" local install_path="$2" local version_dir="$3" local latest_dir="$4" local filelist_path="$5" local feature_param="$6" local latest_conf_path="${install_path}/${latest_dir}/conf" local version_conf_path="${install_path}/${version_dir}/conf" local ret if [ -d "${latest_conf_path}" ] && [ ! -L "${latest_conf_path}" ] && [ -d "${version_conf_path}" ]; then foreach_filelist "filter_common_dirs" "reset_mod_dirs" "${install_type}" "${install_path}/${version_dir}" "mkdir" "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} is_dir_empty "${latest_conf_path}" # 如果目录不为空,则迁移用户数据 if [ $? -ne 0 ]; then mv -f "${latest_conf_path}"/* "${version_conf_path}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi remove_dir_icp "${latest_conf_path}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} foreach_filelist "filter_common_dirs" "change_mod_and_own_dirs" "${install_type}" "${install_path}/${version_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "reverse" "normal" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi return 0 } # 创建公共目录软链接到latest目录下 create_common_dirs_softlink_to_latest() { local install_type="$1" local install_path="$2" local version_dir="$3" local latest_dir="$4" local filelist_path="$5" local feature_param="$6" local ret foreach_filelist "filter_common_dirs" "reset_mod_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} migrate_conf_files_from_latest_to_version "${install_type}" "${install_path}" "${version_dir}" "${latest_dir}" "${filelist_path}" "${feature_param}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} foreach_filelist "filter_common_dirs" "create_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} foreach_filelist "filter_common_dirs" "create_latest_softlink" "${install_type}" "${install_path}/${latest_dir}" "copy copy_entity move" \ "${filelist_path}" "${feature_param}" "no" "normal" "${install_path}/${version_dir}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} foreach_filelist "filter_common_dirs" "change_mod_and_own_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "reverse" "normal" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 删除公共目录到latest目录下的软链接 del_common_dirs_softlink_from_latest() { local install_type="$1" local install_path="$2" local version_dir="$3" local latest_dir="$4" local filelist_path="$5" local feature_param="$6" local ret create_stash_mod "${install_path}/${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_filelist "filter_common_dirs" "reset_mod_dirs_with_stash_mod" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_filelist "filter_common_dirs" "del_latest_softlink" "${install_type}" "${install_path}/${latest_dir}" "copy copy_entity move" \ "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_filelist "filter_common_dirs" "remove_install_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "reverse" "normal" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_stashmod "restore_stash_mod" "${install_path}/${latest_dir}" "reverse" ret="$?" && [ $ret -ne 0 ] && return $ret remove_stash_mod "${install_path}/${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # latest目录下公共脚本添加条目 add_latest_common_script() { local latest_path="$1" local package="$2" local username="$3" local usergroup="$4" local docker_root="$5" local ret mod # 保存bin目录的权限 get_file_mod "mod" "-L" "${latest_path}/bin" # 给latest/bin目录提升权限到750 chmod "750" "${latest_path}/bin" ret="$?" && [ $ret -ne 0 ] && return $ret # 设置setenv add_setenv "${latest_path}" "${package}" "NA" "${username}" "${usergroup}" "true" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 给latest目录添加prereq_check条目 add_prereq_check "${latest_path}" "${package}" "${username}" "${usergroup}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 恢复bin目录的权限 chmod "${mod}" "${latest_path}/bin" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # latest目录下公共脚本删除条目 del_latest_common_script() { local latest_path="$1" local package="$2" local username="$3" local docker_root="$4" local ret # 保存bin目录的权限 get_file_mod "mod" "-L" "${latest_path}/bin" # 给latest/bin目录提升权限到750 chmod "750" "${latest_path}/bin" ret="$?" && [ $ret -ne 0 ] && return $ret # unsetenv del_setenv "${latest_path}" "${package}" "${username}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 给latest目录删除prereq_check条目 del_prereq_check "${latest_path}" "${package}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 恢复bin目录的权限 chmod "${mod}" "${latest_path}/bin" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # 设置版本软链接 do_create_version_softlink() { local curpath install_path local username="$(id -un)" local usergroup="$(id -gn)" local ret total_ret="0" version version_dir packages package version_pair version_pair_arr version_dir_del installer_path local install_for_all curpath="$(dirname $(readlink -f "${BASH_SOURCE:-$0}"))" install_path="$(readlink -f "${curpath}/..")" version_dir="$(basename "${curpath}")" get_version_by_version_dir "version" "${install_path}/${LATEST_DIR}" "${version_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret get_installed_packages_by_version_version_dir "packages" "${install_path}/${LATEST_DIR}" "${version}" "${version_dir}" if [ "${packages}" = "" ]; then return 1 fi get_install_for_all "install_for_all" for package in ${packages}; do get_running_package_version "version_pair" "${install_path}/${LATEST_DIR}" "${package}" ret="$?" && [ $ret -ne 0 ] && total_ret="$ret" && continue if [ "${version_pair}" != "" ]; then unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 1 "version_dir_del" del_package_softlink_in_latest "${install_path}" "${package}" "${version_dir_del}" "${LATEST_DIR}" "${username}" "" ret="$?" && [ $ret -ne 0 ] && total_ret="$ret" fi create_package_softlink_to_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${LATEST_DIR}" \ "${username}" "${usergroup}" "${install_for_all}" "" ret="$?" && [ $ret -ne 0 ] && total_ret="$ret" done return $total_ret } # 删除运行软链接 do_remove_latest_softlink() { local curpath install_path local ret total_ret="0" running_packages package version_pair version_pair_arr version version_dir local username="$(id -un)" curpath="$(dirname $(readlink -f "${BASH_SOURCE:-$0}"))" install_path="$(readlink -f "${curpath}/..")" get_running_packages "running_packages" "${install_path}/${LATEST_DIR}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${running_packages}" = "" ]; then return 0 fi for package in ${running_packages}; do get_running_package_version "version_pair" "${install_path}/${LATEST_DIR}" "${package}" ret="$?" && [ $ret -ne 0 ] && total_ret="$ret" && continue unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 0 "version" 1 "version_dir" del_package_softlink_in_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${LATEST_DIR}" "${username}" "" ret="$?" && [ $ret -ne 0 ] && total_ret="$ret" done return $total_ret } # 文件中添加一个空行 add_blank_line_to_file() { local filepath="$1" echo >> "${filepath}" } # 创建总设置软链脚本 create_total_create_softlink_script() { local install_path="$1" local version_dir="$2" local username="$3" local usergroup="$4" local script_filepath="${install_path}/${version_dir}/${CREATE_VERSION_SOFTLINK}" local ret if [ -f "${script_filepath}" ]; then return 0 fi cat "${curpath}/common_func_v2.inc" > "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} cat "${curpath}/version_cfg.inc" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} cat "${curpath}/multi_version.inc" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} echo "do_create_version_softlink" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_mod "${script_filepath}" "550" "" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_own "${script_filepath}" "${username}:${usergroup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 创建总删除软链脚本 create_total_remove_softlink_script() { local install_path="$1" local latest_dir="$2" local username="$3" local usergroup="$4" local script_filepath="${install_path}/${latest_dir}/${REMOVE_LATEST_SOFTLINK}" local ret if [ -f "${script_filepath}" ]; then return 0 fi cat "${curpath}/common_func_v2.inc" > "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} cat "${curpath}/version_cfg.inc" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} cat "${curpath}/multi_version.inc" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} add_blank_line_to_file "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} echo "do_remove_latest_softlink" >> "${script_filepath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_mod "${script_filepath}" "550" "" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_own "${script_filepath}" "${username}:${usergroup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 删除总创建软链脚本 del_total_create_softlink_script() { local install_path="$1" local latest_dir="$2" local version="$3" local version_dir="$4" local script_filepath="${install_path}/${version_dir}/${CREATE_VERSION_SOFTLINK}" local ret version_cfg_path packages get_version_cfg_path "version_cfg_path" "${install_path}/${latest_dir}" if [ ! -f "${version_cfg_path}" ]; then remove_file "${script_filepath}" "NA" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 fi get_installed_packages_by_version_version_dir "packages" "${install_path}/${latest_dir}" "${version}" "${version_dir}" if [ "${packages}" = "" ]; then remove_file "${script_filepath}" "NA" ret="$?" && [ $ret -ne 0 ] && return $ret fi return 0 } # 删除总删除软链脚本 del_total_remove_softlink_script() { local install_path="$1" local latest_dir="$2" local script_filepath="${install_path}/${latest_dir}/${REMOVE_LATEST_SOFTLINK}" local ret version_cfg_path get_version_cfg_path "version_cfg_path" "${install_path}/${latest_dir}" if [ ! -f "${version_cfg_path}" ]; then remove_file "${script_filepath}" "NA" ret="$?" && [ $ret -ne 0 ] && return $ret fi return 0 } # 创建platform.ini配置 create_platform_ini() { local install_path="$1" local latest_dir="$2" local username="$3" local usergroup="$4" local config_dirpath="${install_path}/${latest_dir}" local config_filepath="${config_dirpath}/platform.ini" local ret mod # 配置文件存在则跳过 if [ -f "${config_filepath}" ]; then return 0 fi # 保存当前目录的权限 get_file_mod "mod" -L "${config_dirpath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} # 恢复权限 cleanup="chmod ${mod} \"${config_dirpath}\"" # 添加目录写权限 chmod u+w "${config_dirpath}" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} touch "${config_filepath}" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_mod "${config_filepath}" "660" "" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_own "${config_filepath}" "${username}:${usergroup}" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} eval "${cleanup}" return 0 } # 删除platform.ini配置 del_platform_ini() { local install_path="$1" local latest_dir="$2" local config_dirpath="${install_path}/${latest_dir}" local config_filepath="${config_dirpath}/platform.ini" local ret mod if [ ! -f "${config_filepath}" ]; then return 0 fi # 保存当前目录的权限 get_file_mod "mod" -L "${config_dirpath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} # 恢复权限 cleanup="chmod ${mod} \"${config_dirpath}\"" # 添加目录写权限 chmod u+w "${config_dirpath}" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} remove_file "${config_filepath}" "NA" cleanup_if_error "$?" "${cleanup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} eval "${cleanup}" return 0 } # 创建目录并且设置权限 make_dir_with_set_permission() { local path="$1" local mod="$2" local username="$3" local usergroup="$4" local install_for_all="$5" local ret make_dir "${path}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_mod "${path}" "${mod}" "${install_for_all}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} change_own "${path}" "${username}:${usergroup}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 创建版本包目录软链接到latest目录下 create_package_dir_softlink_to_latest() { local install_path="$1" local version_dir="$2" local latest_dir="$3" local package="$4" local username="$5" local usergroup="$6" local install_for_all="$7" local ret package_dirpath package_prefix get_package_dirpath "package_dirpath" "${package}" package_prefix="$(dirname "${package_dirpath}")" if [ ! -d "${install_path}/${version_dir}/${package_dirpath}" ]; then return 0 fi if [ ! -d "${install_path}/${latest_dir}/${package_prefix}" ]; then make_dir_with_set_permission "${install_path}/${latest_dir}/${package_prefix}" "750" "${username}" "${usergroup}" "${install_for_all}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi create_softlink_icp "-r" "${install_path}/${version_dir}/${package_dirpath}" "${install_path}/${latest_dir}/${package_dirpath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 创建子包软链到latest目录下 do_create_package_softlink_to_latest() { local install_path="$1" local package="$2" local version="$3" local version_dir="$4" local latest_dir="$5" local username="$6" local usergroup="$7" local install_for_all="$8" local docker_root="$9" local ret total_ret="0" install_type feature_type feature_param filelist_path install_info_path local chip_type check_param_not_empty "package" "need set package parameter in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} _package_to_log_pkg_name "LOG_PKG_NAME" "${package}" check_ret_error "$?" "Set log package name failed in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} if [ ! -d "${install_path}/${latest_dir}" ]; then make_dir_with_set_permission "${install_path}/${latest_dir}" "750" "${username}" "${usergroup}" "${install_for_all}" ret="$?" && [ $ret -ne 0 ] && return $ret fi get_package_filelist "filelist_path" "${install_path}" "${version_dir}" "${package}" get_package_install_info "install_info_path" "${install_path}" "${version_dir}" "${package}" check_file_exists "${install_info_path}" "${install_info_path} doesn't exist in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} get_package_install_type "install_type" "${install_info_path}" "${package}" check_ret_error "$?" "Get install_type from ascend_install.info failed in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} get_package_feature_type "feature_type" "${install_info_path}" "${package}" "install" check_ret_error "$?" "Get feature_type from ascend_install.info failed in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} get_package_chip_type "chip_type" "${install_info_path}" "${package}" "install" check_ret_error "$?" "Get chip_type from ascend_install.info failed in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} pack_feature_param "feature_param" "${feature_type}" "n" "${chip_type}" # 创建公共目录到latest目录下的软链接 create_common_dirs_softlink_to_latest "${install_type}" "${install_path}" "${version_dir}" "${latest_dir}" "${filelist_path}" "${feature_param}" ret="$?" && [ $ret -ne 0 ] && return $ret # 创建版本包到latest目录下的软链接 create_package_dir_softlink_to_latest "${install_path}" "${version_dir}" "${latest_dir}" "${package}" \ "${username}" "${usergroup}" "${install_for_all}" ret="$?" && [ $ret -ne 0 ] && return $ret # 调用脚本创建自定义内容软链接到latest目录下 local custom_create_softlink="${install_path}/${version_dir}/${package}/script/${package}_custom_create_softlink.sh" if [ -f "${custom_create_softlink}" ]; then chmod u+x "${custom_create_softlink}" "${custom_create_softlink}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}" check_ret_error "$?" "Run ${package} custom create ${version_dir} softlink failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi # latest目录下公共脚本添加条目 add_latest_common_script "${install_path}/${latest_dir}" "${package}" "${username}" "${usergroup}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 更新version.cfg中的running配置 set_running_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}" check_ret_error "$?" "Set ${package} running version ${version} ${version_dir} in create package softlink to latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} # 更新version.cfg中的upgrade配置 set_upgrade_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}" check_ret_error "$?" "Set ${package} upgrade version ${version} ${version_dir} in create package softlink to latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} create_total_remove_softlink_script "${install_path}" "${latest_dir}" "${username}" "${usergroup}" check_ret_warning "$?" "Create total remove softlink script in ${latest_dir} in create package softlink to latest failed!" create_platform_ini "${install_path}" "${latest_dir}" "${username}" "${usergroup}" check_ret_warning "$?" "Create platform.ini in ${latest_dir} in create package softlink to latest failed!" return ${total_ret} } # 创建子包软链到latest目录下 create_package_softlink_to_latest() { local install_path="$1" local package="$2" local version="$3" local version_dir="$4" local latest_dir="$5" local username="$6" local usergroup="$7" local install_for_all="$8" local docker_root="$9" local ret installer_path get_package_install_common_parser "installer_path" "${install_path}" "${version_dir}" "${package}" check_file_exists "${installer_path}" "${installer_path} doesn't exist in create package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} install_options="--create-latest-softlink" install_options="${install_options} --install-path=\"${install_path}\" --package=\"${package}\" --version=\"${version}\" --version-dir=\"${version_dir}\"" install_options="${install_options} --latest-dir=\"${latest_dir}\" --username=\"${username}\" --usergroup=\"${usergroup}\"" if [ "${install_for_all}" = "y" ]; then install_options="${install_options} --install_for_all" fi if [ "${docker_root}" != "" ]; then install_options="${install_options} --docker-root=\"${docker_root}\"" fi eval sh "${installer_path}" "${install_options}" check_ret_error "$?" "Create ${version_dir} ${package} softlink to latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 删除latest目录下版本包目录的软链接 del_package_dir_softlink_in_latest() { local install_path="$1" local latest_dir="$2" local package="$3" local ret package_dirpath package_prefix get_package_dirpath "package_dirpath" "${package}" package_prefix="$(dirname "${package_dirpath}")" remove_softlink_icp "${install_path}/${latest_dir}/${package_dirpath}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} if [ "${package_prefix}" != "." ]; then remove_dir_if_empty "${install_path}/${latest_dir}/${package_prefix}" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi return 0 } # 删除latest目录下子包的软链接 do_del_package_softlink_in_latest() { local install_path="$1" local package="$2" local version_dir="$3" local latest_dir="$4" local username="$5" local docker_root="$6" local ret install_type feature_type feature_param filelist_path install_info_path local chip_type _package_to_log_pkg_name "LOG_PKG_NAME" "${package}" check_ret_error "$?" "Set log package name failed in del package softlink in latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} if [ ! -d "${install_path}/${latest_dir}" ]; then comm_log "ERROR" "${install_path}/${latest_dir} doesn't exist in del package softlink in latest!" return 1 fi filelist_path="${curpath}/filelist.csv" install_info_path="${curpath}/../ascend_install.info" check_file_exists "${install_info_path}" "${install_info_path} doesn't exist in del package softlink in latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} # 卸载时install_type强制为full install_type="full" get_package_feature_type "feature_type" "${install_info_path}" "${package}" "uninstall" check_ret_error "$?" "Get feature_type from ascend_install.info failed in del package softlink in latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} get_package_chip_type "chip_type" "${install_info_path}" "${package}" "uninstall" check_ret_error "$?" "Get chip_type from ascend_install.info failed in del package softlink to latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} pack_feature_param "feature_param" "${feature_type}" "n" "${chip_type}" # latest目录下公共脚本删除条目 del_latest_common_script "${install_path}/${latest_dir}" "${package}" "${username}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret # 调用脚本删除latest目录下自定义内容软链接 local custom_remove_softlink="${install_path}/${version_dir}/${package}/script/${package}_custom_remove_softlink.sh" if [ -f "${custom_remove_softlink}" ]; then chmod u+x "${custom_remove_softlink}" "${custom_remove_softlink}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}" check_ret_error "$?" "Run ${package} custom remove ${version_dir} softlink failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} fi # 删除公共目录到latest目录下的软链接 del_common_dirs_softlink_from_latest "${install_type}" "${install_path}" "${version_dir}" "${latest_dir}" "${filelist_path}" "${feature_param}" ret="$?" && [ $ret -ne 0 ] && return $ret # 删除版本包到latest目录下的软链接 del_package_dir_softlink_in_latest "${install_path}" "${latest_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret # 删除latest下tools空目录。toolkit包创建latest软链时,latest下存在软链tools/simulator -> ${arch}-linux/simulator # 通过filelist.csv文件,无法删除tools目录 # 解决toolkit包安装卸载latest目录下残留tools目录问题 remove_dir_if_empty "${install_path}/${latest_dir}/tools" # 移除version.cfg中的running配置 unset_running_package "${install_path}/${latest_dir}" "${package}" check_ret_error "$?" "Unset ${package} running version in del package softlink in latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 删除latest目录下子包的软链接 del_package_softlink_in_latest() { local install_path="$1" local package="$2" local version="$3" local version_dir="$4" local latest_dir="$5" local username="$6" local docker_root="$7" local ret installer_path get_package_install_common_parser "installer_path" "${install_path}" "${version_dir}" "${package}" check_file_exists "${installer_path}" "${installer_path} doesn't exist in del package softlink in latest!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} # 更新version.cfg中的upgrade配置 set_upgrade_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}" check_ret_error "$?" "Set ${package} upgrade version ${version} ${version_dir} in create package softlink to latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} install_options="--remove-latest-softlink" install_options="${install_options} --install-path=\"${install_path}\" --package=\"${package}\" --version-dir=\"${version_dir}\"" install_options="${install_options} --latest-dir=\"${latest_dir}\" --username=\"${username}\"" if [ "${docker_root}" != "" ]; then install_options="${install_options} --docker-root=\"${docker_root}\"" fi eval sh "${installer_path}" "${install_options}" check_ret_error "$?" "Remove ${version_dir} ${package} softlink in latest failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return 0 } # 创建版本目录 create_version_dir() { local install_path="$1" local version_dir="$2" local username="$3" local usergroup="$4" local install_for_all="$5" local ret if [ ! -d "${install_path}/${version_dir}" ]; then make_dir "${install_path}/${version_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret change_mod "${install_path}/${version_dir}" "750" "${install_for_all}" ret="$?" && [ $ret -ne 0 ] && return $ret change_own "${install_path}/${version_dir}" "${username}:${usergroup}" ret="$?" && [ $ret -ne 0 ] && return $ret fi return 0 } # 检查多版本兼容性 check_multi_version_compatiable() { local install_path="$1" local latest_dir="$2" local current_version="$3" local current_version_dir="$4" local current_package="$5" local exec_cmd="$6" local ret version_info_path required_packages package version_pair version_pair_arr version version_dir local pkg_version_info_path pkg_version required script_dirpath running_packages get_package_version_info "version_info_path" "${install_path}" "${current_version_dir}" "${current_package}" _get_required_packages "required_packages" "${version_info_path}" get_package_script_dirpath "script_dirpath" "${install_path}" "${current_version_dir}" "${current_package}" for package in ${required_packages}; do get_running_package_version "version_pair" "${install_path}/${latest_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret [ "${version_pair}" = "" ] && continue unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 0 "version" 1 "version_dir" get_package_version_info "pkg_version_info_path" "${install_path}" "${version_dir}" "${package}" [ ! -f "${pkg_version_info_path}" ] && continue get_package_version "pkg_version" "${pkg_version_info_path}" _get_required_package_info "required" "${version_info_path}" "${package}" _check_version_required "${pkg_version}" "${required}" "${script_dirpath}" if [ $? -ne 0 ]; then eval "${exec_cmd}" ret="$?" && [ $ret -ne 0 ] && return $ret fi done get_running_packages "running_packages" "${install_path}/${latest_dir}" for package in ${running_packages}; do [ "${current_package}" = "${package}" ] && continue get_running_package_version "version_pair" "${install_path}/${latest_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 0 "version" 1 "version_dir" get_package_version_info "pkg_version_info_path" "${install_path}" "${version_dir}" "${package}" [ ! -f "${pkg_version_info_path}" ] && continue _get_required_package_info "required" "${pkg_version_info_path}" "${current_package}" [ "${required}" = "" ] && continue get_package_script_dirpath "script_dirpath" "${install_path}" "${version_dir}" "${package}" _check_version_required "${current_version}" "${required}" "${script_dirpath}" if [ $? -ne 0 ]; then eval "${exec_cmd}" ret="$?" && [ $ret -ne 0 ] && return $ret fi done return 0 } # 多版本安装时检查版本兼容性 check_compatiable_in_multi_version_install() { local install_path="$1" local latest_dir="$2" local username="$3" local docker_root="$4" local current_version="$5" local current_version_dir="$6" local current_package="$7" local ret exec_cmd exec_cmd="del_package_softlink_in_latest \"${install_path}\" \"\${package}\" \"\${version}\" \"\${version_dir}\" \"${latest_dir}\" \"${username}\" \"${docker_root}\"" check_multi_version_compatiable "${install_path}" "${latest_dir}" "${current_version}" "${current_version_dir}" "${current_package}" "${exec_cmd}" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # 多版本安装流程 multi_version_install() { local install_type="$1" local install_path="$2" local filelist_path="$3" local package="$4" local feature_param="$5" local version="$6" local version_dir="$7" local username="$8" local usergroup="$9" local setenv="${10}" local is_upgrade="${11}" local docker_root="${12}" local custom_options="${13}" local install_for_all="${14}" local ret total_ret="0" pkg_running_version version_pair_arr last_version last_version_dir create_version_dir "${install_path}" "${version_dir}" "${username}" "${usergroup}" "${install_for_all}" ret="$?" && [ $ret -ne 0 ] && return $ret version_install "${install_type}" "${install_path}" "${filelist_path}" "${package}" "${feature_param}" \ "${version_dir}" "${username}" "${usergroup}" "${setenv}" "${is_upgrade}" "${docker_root}" "${custom_options}" ret="$?" && [ $ret -ne 0 ] && return $ret create_total_create_softlink_script "${install_path}" "${version_dir}" "${username}" "${usergroup}" check_ret_warning "$?" "Create total create softlink script in ${version_dir} in multi version install failed!" get_running_package_version "pkg_running_version" "${install_path}/${LATEST_DIR}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${pkg_running_version}" != "" ]; then unpack_version_pair "version_pair_arr" "${pkg_running_version}" __index_list "${version_pair_arr}" 0 "last_version" 1 "last_version_dir" del_package_softlink_in_latest "${install_path}" "${package}" "${last_version}" "${last_version_dir}" "${LATEST_DIR}" \ "${username}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret fi # 版本兼容性检查 check_compatiable_in_multi_version_install "${install_path}" "${LATEST_DIR}" "${username}" "${docker_root}" \ "${version}" "${version_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret create_package_softlink_to_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${LATEST_DIR}" \ "${username}" "${usergroup}" "${install_for_all}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret add_installed_package_version "${install_path}/${LATEST_DIR}" "${package}" "${version}" "${version_dir}" check_ret_error "$?" "Add ${package} installed version ${version} ${version_dir} in multi version install failed!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} return ${total_ret} } # 删除latest下空目录 del_empty_dirs_in_latest() { local install_type="$1" local install_path="$2" local latest_dir="$3" local filelist_path="$4" local feature_param="$5" local ret create_stash_mod "${install_path}/${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_filelist "filter_common_dirs" "reset_mod_dirs_with_stash_mod" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "no" "normal" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_filelist "filter_common_dirs" "remove_install_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \ "${filelist_path}" "${feature_param}" "reverse" "normal" ret="$?" && [ $ret -ne 0 ] && return $ret foreach_stashmod "restore_stash_mod" "${install_path}/${latest_dir}" "reverse" ret="$?" && [ $ret -ne 0 ] && return $ret remove_stash_mod "${install_path}/${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret return 0 } # 获取aicpu创建软链脚本路径 get_aicpu_custom_create_softlink_path() { local _outvar="$1" local _install_path="$2" local _version_dir="$3" local _chip_name="$4" eval "${_outvar}=\"${_install_path}/${_version_dir}/opp/${_chip_name}/aicpu/script/aicpu_custom_create_softlink.sh\"" } # 重建软链时处理aicpu软链 deal_with_aicpu_package() { local install_path="$1" local version_dir="$2" local latest_dir="$3" local chip_name for chip_name in "Ascend910" "Ascend310P" "Ascend310" "Ascend310RC" "Ascend"; do get_aicpu_custom_create_softlink_path "aicpu_script" "${install_path}" "${version_dir}" "${chip_name}" if [ -f "${aicpu_script}" ]; then # 创建软链时,支持强制覆盖老版本软链。 # 因为存在这样的场景:先卸载新版本的runtime包,再卸载新版本的aicpu_kernels包, # 卸载新版本的runtime包时触发该流程,此时老版本的aicpu_kernels包软链还在存在于环境上。 "${aicpu_script}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}" # 忽略aicpu创建软链报错 fi done return 0 } # 多版本卸载时检查版本兼容性 recreate_compatiable_softlink_in_multi_version_uninstall() { local install_path="$1" local latest_dir="$2" local package="$3" local username="$4" local usergroup="$5" local docker_root="$6" local is_final_running="$7" local ret installed_versions version_pair version_pair_arr version version_dir exec_cmd is_compatiable local install_for_all # 切换目录避免使用uninstall.sh脚本,重建子包软链接时,找到不当前路径问题 # sh: 0: getcwd() failed: No such file or directory cd "${install_path}" # 判断是否有本包的running版本包 get_running_package_version "version_pair" "${install_path}/${latest_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${version_pair}" != "" ]; then return 0 fi # 枚举version.cfg中本包installed版本 get_installed_package_versions "installed_versions" "${install_path}/${latest_dir}" "${package}" ret="$?" && [ $ret -ne 0 ] && return $ret get_install_for_all "install_for_all" for version_pair in ${installed_versions}; do unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 0 "version" 1 "version_dir" is_compatiable="true" exec_cmd="eval \"is_compatiable=false\"" check_multi_version_compatiable "${install_path}" "${latest_dir}" "${version}" "${version_dir}" "${package}" "${exec_cmd}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${is_compatiable}" = "true" ]; then create_package_softlink_to_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${latest_dir}" \ "${username}" "${usergroup}" "${install_for_all}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && return $ret if [ "${is_final_running}" = "true" ]; then # 产品线nnrt包,runtime与aicpu_kernels包并存的场景。 # 由于aicpu_kernels包并不是严格按照多版本设计实现,所以没有重建软链的能力, # 卸载时依赖其它包(也就是runtime包)协助创建aicpu_kernels包的兼容版本软链。 # 这里假设aicpu_kernels包的软链总是跟随runtime包。 deal_with_aicpu_package "${install_path}" "${version_dir}" "${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret fi break fi done return 0 } # 创建安装包软链接到latest create_installed_packages_softlink() { local install_path="$1" local latest_dir="$2" local current_package="$3" local username="$4" local usergroup="$5" local docker_root="$6" local ret total_ret="0" installed_packages package version_pair version_pair_arr version version_dir exec_cmd is_compatiable local install_for_all get_installed_packages "installed_packages" "${install_path}/${latest_dir}" ret="$?" && [ $ret -ne 0 ] && return $ret get_install_for_all "install_for_all" for package in ${installed_packages}; do [ "${current_package}" = "${package}" ] && continue get_package_last_installed_version "version_pair" "${install_path}" "${package}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" && continue unpack_version_pair "version_pair_arr" "${version_pair}" __index_list "${version_pair_arr}" 0 "version" 1 "version_dir" is_compatiable="true" exec_cmd="eval \"is_compatiable=false\"" check_multi_version_compatiable "${install_path}" "${latest_dir}" "${version}" "${version_dir}" "${package}" "${exec_cmd}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" && continue if [ "${is_compatiable}" = "true" ]; then create_package_softlink_to_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${latest_dir}" \ "${username}" "${usergroup}" "${install_for_all}" "${docker_root}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" fi done return ${total_ret} } # 多版本卸载流程 multi_version_uninstall() { local install_type="$1" local install_path="$2" local filelist_path="$3" local package="$4" local feature_param="$5" local version="$6" local version_dir="$7" local username="$8" local usergroup="$9" local docker_root="${10}" local custom_options="${11}" local is_recreate_softlink="${12}" local tmp_root tmp_filelist_path local ret total_ret=0 install_path_full="" is_running="" is_upgrade local running_packages is_final_running="false" local version_cfg_exist check_param_not_empty "usergroup" "need set usergroup parameter in multi version uninstall!" ret="$?" && [ ${ret} -ne 0 ] && return ${ret} get_tmp_root "tmp_root" tmp_filelist_path=$(mktemp "$tmp_root/filelist_XXXXXX" || exit 1) cp -f "${filelist_path}" "${tmp_filelist_path}" if [ $? -ne 0 ]; then log "ERROR" "cp -f ${filelist_path} ${tmp_filelist_path} failed!" exit 1 fi del_tmp_filelist="rm -f \"${tmp_filelist_path}\"" is_package_version_running "is_running" "${install_path}/${LATEST_DIR}" "${package}" "${version}" "${version_dir}" if [ $? -ne 0 ]; then eval "${del_tmp_filelist}" return 1 fi if [ "${is_running}" = "true" ]; then del_package_softlink_in_latest "${install_path}" "${package}" "${version}" "${version_dir}" "${LATEST_DIR}" \ "${username}" "${docker_root}" if [ $? -ne 0 ]; then eval "${del_tmp_filelist}" return 1 fi get_running_packages "running_packages" "${install_path}/${LATEST_DIR}" if [ "${running_packages}" = "" ]; then is_final_running="true" fi fi is_package_version_upgrade "is_upgrade" "${install_path}/${LATEST_DIR}" "${package}" "${version}" "${version_dir}" if [ $? -ne 0 ]; then eval "${del_tmp_filelist}" return 1 fi if [ "${is_upgrade}" = "true" ]; then # 移除version.cfg中的upgrade配置 unset_upgrade_package "${install_path}/${LATEST_DIR}" "${package}" if [ $? -ne 0 ]; then comm_log "ERROR" "Unset ${package} upgrade version in multi version uninstall failed!" eval "${del_tmp_filelist}" return 1 fi fi version_uninstall "${install_type}" "${install_path}" "${filelist_path}" "${package}" "${feature_param}" \ "${version_dir}" "${username}" "${docker_root}" "${custom_options}" if [ $? -ne 0 ]; then eval "${del_tmp_filelist}" return 1 fi del_installed_package_version "${install_path}/${LATEST_DIR}" "${package}" "${version}" "${version_dir}" if [ $? -ne 0 ]; then comm_log "ERROR" "Del ${package} installed version ${version} ${version_dir} in multi version uninstall failed!" eval "${del_tmp_filelist}" return 1 fi # 删除版本目录下创建软链总脚本 del_total_create_softlink_script "${install_path}" "${LATEST_DIR}" "${version}" "${version_dir}" check_ret_warning "$?" "Del total create softlink script in ${version_dir} in multi version uninstall failed!" does_version_cfg_exist "version_cfg_exist" "${install_path}/${LATEST_DIR}" if [ "${version_cfg_exist}" = "false" ]; then # 删除latest目录下删除软链总脚本 del_total_remove_softlink_script "${install_path}" "${LATEST_DIR}" check_ret_warning "$?" "Del total remove softlink script in ${LATEST_DIR} in multi version uninstall failed!" # 删除latest目录下platform.ini del_platform_ini "${install_path}" "${LATEST_DIR}" check_ret_warning "$?" "Del platform.ini in ${LATEST_DIR} in multi version uninstall failed!" fi if [ "${is_recreate_softlink}" = "y" ]; then # 多版本卸载时检查版本兼容性 recreate_compatiable_softlink_in_multi_version_uninstall "${install_path}" "${LATEST_DIR}" "${package}" \ "${username}" "${usergroup}" "${docker_root}" "${is_final_running}" if [ $? -ne 0 ]; then comm_log "ERROR" "Recreate ${package} compatiable softlink in multi version uninstall failed!" eval "${del_tmp_filelist}" return 1 fi if [ "${is_final_running}" = "true" ]; then comm_log "INFO" "Start create installed packages softlink." create_installed_packages_softlink "${install_path}" "${LATEST_DIR}" "${package}" "${username}" "${usergroup}" "${docker_root}" if [ $? -ne 0 ]; then comm_log "ERROR" "Create installed packages softlink in multi version uninstall failed!" eval "${del_tmp_filelist}" return 1 fi fi fi get_running_packages "running_packages" "${install_path}/${LATEST_DIR}" if [ "${running_packages}" = "" ]; then # 删除latest下空目录 del_empty_dirs_in_latest "${install_type}" "${install_path}" "${LATEST_DIR}" "${tmp_filelist_path}" "${feature_param}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" fi # 删除临时文件tmp_filelist_path eval "${del_tmp_filelist}" # 删除版本空目录 is_dir_empty "${install_path}/${version_dir}" if [ $? -eq 0 ]; then remove_dir_icp "${install_path}/${version_dir}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" fi # 删除latest空目录 is_dir_empty "${install_path}/${LATEST_DIR}" if [ $? -eq 0 ]; then remove_dir_icp "${install_path}/${LATEST_DIR}" ret="$?" && [ $ret -ne 0 ] && total_ret="1" fi return ${total_ret} }