updateJupyterImage_script 5.26 KB
Newer Older
chenpangpang's avatar
chenpangpang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash

# WORKSPACE="/opt/jupyter-ext/install"
WORKSPACE=$(pwd)
 
#循环读取images.txt,并存入list中
n=0
fail_image_array=()
for line in $(cat images.txt | grep ^[^#])
do
        list[$n]=$line
        ((n+=1))
done
 
echo "需推送的镜像地址如下:"
for variable in ${list[@]}
do
        echo ${variable}
done
 
for variable in ${list[@]}
do
        #下载镜像
        echo "准备拉取镜像: $variable"
        docker pull $variable
        
        # #获取拉取的镜像ID
        imageId=`docker images -q $variable`
        echo "[$variable]拉取完成后的镜像ID: $imageId"
        

        #启动容器
        CONTAINER_ID=$(docker run  -v $WORKSPACE:/jupyter-ext -itd $variable)

        echo "容器ID:${CONTAINER_ID},启动jupyter 检查文件中..."
        content=$(docker exec ${CONTAINER_ID} bash -c 'nohup jupyter lab --allow-root --ip=0.0.0.0 --port=8888 > /tmp/start.log 2>&1 & sleep 20s; cat /tmp/start.log| grep "JupyterLab application directory"')
        echo $content
        if [ -z "$content" ]; then
                echo "image jupyter sourcecode  path check failed!please retry! imageId:$imageId,containerId:${CONTAINER_ID}"
                docker stop ${CONTAINER_ID}
                fail_image_array+=("${variable}")
                continue
        else
              jupyter_file_path=$(echo "$content" | grep -oP '(?<=is\s)(\/[^ ]+)')
        fi

        echo "jupyter sourcecode path is :${jupyter_file_path}/static"

        file_check=$(docker exec ${CONTAINER_ID} bash -c 'if [ -d "'${jupyter_file_path}'/static" ]; then echo "success"; else echo "failed"; fi')
        echo $file_check
        if [ "failed" == "$file_check" ]; then
                echo "image jupyter sourcecode  path check failed!please retry! image:$variable"
                docker stop ${CONTAINER_ID}
                fail_image_array+=("${variable}")
                continue
        fi

        # 获取index的内容 那去js名 替换
        js_content=$(docker exec ${CONTAINER_ID} bash -c "cat '${jupyter_file_path}'/static/index.html ")

        echo $js_content
        js_path=$(echo "$js_content" | grep -oP '{{page_config\.fullStaticUrl}}/main\.[^"]+\.js\?v=[^"]+' )

        js_path2=$(echo "$js_content" | grep -oP '{{page_config\.fullStaticUrl}}/vendors~main\.[^"]+\.js\?v=[^"]+' )

        js_path3=$(echo "$js_content" | grep -oP '{{page_config\.fullStaticUrl}}/vendors~main\.[^"]+\.js' )

        echo "jupyter js source path is: $js_path"

        if [ -z "$js_path" ];then
                if [ -z "$js_path2" ];then
                        if [ -z "$js_path3" ];then
                                echo "image jupyter sourcecode main js path check failed!please retry! imageId:$imageId,containerId:${CONTAINER_ID}"
                                docker stop ${CONTAINER_ID}
                                fail_image_array+=("${variable}")
                                continue
                        else
                                js_path=$js_path3
                        fi
                else
                     js_path=$js_path2
                fi   
        fi


        cat $WORKSPACE/static/index-base.html > $WORKSPACE/static/index.html

        sed -i "s#mainJsParam#${js_path}#g" $WORKSPACE/static/index.html


        docker cp -a $WORKSPACE/static/index.html ${CONTAINER_ID}:${jupyter_file_path}/static/index.html
        docker cp -a $WORKSPACE/static/scnet-loading.gif ${CONTAINER_ID}:${jupyter_file_path}/static/scnet-loading.gif
        jupyter_dir=$(docker exec ${CONTAINER_ID} bash -c "whereis jupyter | cat|awk -F':' '{print \$2}'")

        echo "image env jupyter cmd path :${jupyter_dir}"
        # docker exec  ${CONTAINER_ID} bash -c "mkdir -p '/opt/conda/bin/';ln -s ${jupyter_dir} /opt/conda/bin/"
        # 卸载之前jupyterlab插件
        docker exec ${CONTAINER_ID} bash -c "pip uninstall -r /jupyter-ext/uninstall.txt -y"

        # 安装jupyterlab插件
       # 安装 jupyterlab 插件
docker exec ${CONTAINER_ID} bash -c "
    pip install --no-index --find-links=/jupyter-ext -r /jupyter-ext/requirements.txt
    jupyter_version=$(jupyter lab --version | cut -d. -f1)
    if pip list | grep -E 'jupyter_ext_model' && \
       pip list | grep -E 'jupyter_ext_platform' && \
       pip list | grep -E 'jupyterlab-language-pack-zh-CN'; then
        echo '安装成功'
    else
        echo '安装失败!! $variable' | tee /jupyter-ext/errorImages.txt
    fi
" 
        # 禁用splash插件
        docker exec ${CONTAINER_ID} bash -c "jupyter labextension disable @jupyterlab/apputils-extension:splash"

        # 打镜像
        docker commit ${CONTAINER_ID} $variable
        #重新打镜像标签
        # docker tag $imageId $registryAddr$repository:$imageNewTag
        
        # #推送镜像
        # docker push $registryAddr$repository:$imageNewTag     

        # 删除容器及镜像
        docker stop ${CONTAINER_ID}
        docker rm ${CONTAINER_ID}
        # docker rmi ${imageId}

        # 验证命令
        # docker run -it -p8080:8080 --env JUPYTERLAB_WORKSPACES_DIR=/tmp/111 --entrypoint='' f7fa3a053830 /bin/bash
        # mkidr -p /opt/conda/bin/;ln -n /usr/bin/jupyter /opt/conda/bin/jupyter ;/opt/conda/bin/jupyter lab --allow-root --ip=0.0.0.0 --port=8080
done
echo "jupyter source code update failed:"
echo $fail_image_array