打包运行环境


这里就要用到主角了:Conda Pack。官网地址:https://conda.github.io/conda-pack/。
官网文档安装Conda Pack的方法有三种,conda安装、pip包安装、pip源码安装。这里使用conda命令安装:

  1. conda install -c conda-forge conda-pack


使用起来,也相对简单

  1. # 该命令会将指定的名为my_env_name运行环境打包成my_env_name.tar.gz
  2. conda pack -n my_env_name


我们也可以通过-o参数指定打包之后的名称

  1. conda pack -n my_env_name -o out_name.tar.gz


将我们上面的2.7的环境打包

  1. conda pack -n 2.7 -o 2.7.tar.gz


会在当前目录生成2.7.tar.gz。

创建一个名为2.7的目录

  1. mkdir 2.7


将2.7.tar.gz解压到2.7目录下

  1. tar -zxvf 2.7.tar.gz -C 2.7


执行如下命令激活环境

  1. source 2.7/bin/activate


查看版本号

  1. (2.7) [root@localhost flask-with-runtime-env]# python -V
  2. Python 2.7.18 :: Anaconda, Inc.


将解压后的2.7文件夹重新打包成2.7.tar.bz2,用于我们flask程序的运行环境

  1. tar -zcvf 2.7.tar.bz2 2.7/


移动到我们项目的env目录下

  1. mv 2.7.tar.bz2 resources/env/

打包项目并测试

将运行环境放到env下之后,我们可以直接执行build.sh进行打包。

  1. sh build.sh
  1. #!/usr/bin/env bash
  2. APP_HOME=$(cd $(dirname $0); pwd)
  3. TARGET_DIR="$APP_HOME/target"
  4. VERSION="1.0.0"
  5. TARGET_APP_HOME="$TARGET_DIR/flask-with-runtime-$VERSION"
  6. #TARGET_SCRIPTS
  7. echo "Working directory is: $APP_HOME"
  8. echo "Checkiing python version ..."
  9. python_version=$(python -V 2>&1)
  10. if [[ -z "$python_version" ]];then
  11. echo "No Python!"
  12. exit 1
  13. else :
  14. echo $python_version
  15. fi
  16. if [ -d "$TARGET_DIR" ];then
  17. rm -rf $TARGET_DIR
  18. fi
  19. mkdir -p "$TARGET_APP_HOME"
  20. cp -r $APP_HOME/application $TARGET_APP_HOME
  21. echo "Compiling ..."
  22. python -m compileall $TARGET_APP_HOME/application
  23. find $TARGET_APP_HOME/application -name '*.py' -type f -print -exec rm {} \;
  24. find $TARGET_APP_HOME/application -name '__pycache__' -exec rm -rf {} \;
  25. echo "Copy runtime resources to target."
  26. cp -r $APP_HOME/resources/* $TARGET_APP_HOME/
  27. echo "Packaging ..."
  28. COPYFILE_DISABLE=1 tar -zcf $TARGET_APP_HOME.tar.gz -C $TARGET_DIR flask-with-runtime-$VERSION
  29. rm -rf $TARGET_DIR/flask-with-runtime-$VERSION

执行完成后,查看target目录:

  1. [root@localhost flask-with-runtime-env]# ls target/
  2. flask-with-runtime-1.0.0.tar.gz


这个压缩包就是我们最终打包的程序,它里面带有独立的运行环境。只需要将它复制到要部署的机器上,解压,然后执行install脚本即可。

将压缩包复制到/opt/目录下

  1. cp target/flask-with-runtime-1.0.0.tar.gz /opt/


去/opt目录下解压

  1. [root@localhost flask-with-runtime-env]# cd /opt/
  2. [root@localhost opt]# tar -zxvf flask-with-runtime-1.0.0.tar.gz
  3. flask-with-runtime-1.0.0/
  4. flask-with-runtime-1.0.0/application/
  5. flask-with-runtime-1.0.0/application/main.pyc
  6. flask-with-runtime-1.0.0/bin/
  7. flask-with-runtime-1.0.0/bin/start
  8. flask-with-runtime-1.0.0/bin/stop
  9. flask-with-runtime-1.0.0/env/
  10. flask-with-runtime-1.0.0/env/2.7.tar.bz2
  11. flask-with-runtime-1.0.0/scripts/
  12. flask-with-runtime-1.0.0/scripts/flask.service.template
  13. flask-with-runtime-1.0.0/scripts/install.sh


进入flask-with-runtime-1.0.0目录,执行安装脚本

  1. cd flask-with-runtime-1.0.0
  2. [root@localhost flask-with-runtime-1.0.0]# sh scripts/install.sh
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/flask-application.service to /usr/lib/systemd/system/flask-application.service.
  4. Installed successfully.
  5. Please run command 'systemctl status flask-application' for more information about service.


查看运行状态

  1. systemctl status flask-application

访问目标主机:5000