vi pushJar2docker.sh
#!/bin/sh
# Linux拷贝文件到容器中进行替换的shell脚本
argc=$#
newline=$'\n'
push_file=app-core-1.0-SNAPSHOT.jar
target_dir=/opt/app/lib
docker_id=0
# eg: test_module01-v1.jar
push_file_check_regex='^[a-zA-Z0-9._-]+\.jar$'
# eg: /opt/app/lib
target_dir_check_regex='^[a-zA-Z0-9_/-]+$'
# functions define
function get_docker_id()
{
# return docker_id
docker_id=$(docker ps -a |grep -E ".*hub.center.tech/app/core.*user01.*" |awk '{print $1}')
#return docker_id # return will return result: 0|1
echo $docker_id # echo for return docker_id
}
function copy_file_to_docker(){
# copy default jar file to default target_dir
docker_id=$(get_docker_id)
echo "docker id: $docker_id"
# $1=$push_file, $2=$target_dir
path_src=$1
path_dst=$2
cmd_cp="docker cp $path_src $docker_id:$path_dst"
echo -e "\n$cmd_cp"
#copy file to docker
ret=`$cmd_cp`
echo "jar file pushed to docker finished!"
#ls -l
cmd_ls="docker exec $docker_id ls -l $path_dst/$path_src"
echo -e "\n$cmd_ls"
echo "target file properies in docker is: "
ret=`$cmd_ls`
echo -e "\n$ret\n"
echo -e "Please exec by manual:\ndocker exec -it $docker_id /bin/sh \n./restart.sh\n"
echo -e "\n --------END-----------\n"
return 0
}
# body
echo ----
echo $argc
if [ $argc -eq 1 ] && [ $1 == "-h" -o $1 == "/?" ]
then
echo "----------------------------------------------------------------------------------"
echo "$newline"
echo "1.show help info:$newline Use: $0 {-h | /?} $newline"
echo "2.direct copy default jar file $push_file to default target lib dir $target_dir:$newline Use: $0 $newline"
echo "3.copy jar file to target lib dir:$newline Use: $0 {file} {target_dir} $newline Tips: must end with "/"$newline eg: $0 app-core-1.0-SNAPSHOT.jar /opt/app/lib/ $newline"
exit
elif [ $argc -eq 0 ]
then
# for 2.direct copy default
echo "direct copy default"
copy_file_to_docker $push_file $target_dir
elif [ $argc -eq 2 ] && [[ $1 =~ $push_file_check_regex ]] && [[ $2 =~ $target_dir_check_regex ]]
then
# for 3.copy jar file to target lib dir
echo "copy jar file to target lib dir"
push_file=$1
target_dir=$2
copy_file_to_docker $push_file $target_dir
else
echo -e "Input error!\nUse: $0 -h for help!"
if [ $argc -ne 2 ]
then
echo "input argc number invalid!"
elif [[ ! $1 =~ $push_file_check_regex ]]
then
echo "input push_file invalid: \"$1\" not match: \"$push_file_check_regex\""
elif [[ ! $2 =~ $target_dir_check_regex ]]
then
echo "input target_dir invalid: \"$2\" not match: \"$target_dir_check_regex\""
else
echo "unknow error!"
fi
exit
fi