---
title: "通过shell脚本自动初始化python环境"
author: "student_lp"
date: "2014-08-18T10:02:37Z"
tags: []
categories: ["互联网"]
slug: "shell-python"
draft: false
lastmod: "2014-08-17T16:19:14Z"
id: 2105224
url: "https://student-lp.iteye.com/blog/2105224"
---

在项目周期内需要经历三个过程，分别是开发、测试、部署。在这三个过程中，一般需要有三个环境分别与之对应，分别是：开发环境、测试环境和生产环境。为了维护这三个环境的一致性（至少是环境所提供的依赖库和功能一致）和环境的快速搭建，因此在项目的推进过程（环境搭建和代码部署）中，都强调实施的自动化。这时通过编写shell脚本可以满足这一需求。

    像python这种脚本语言，有着强大的第三方开发插件库，能够提供丰富的功能。因此在项目的开发过程中，就避免不了引入非常多的第三方插件，丰富项目的功能并节省项目的开发周期。但是大量第三方插件的引入，也为项目环境的搭建部署增加了难度，如果通过人工完成环境的克隆是一个很困难的过程。如果我们在开发过程中，环境的搭建都是通过shell脚本初始化完成的，并且在需要引入新的插件过程中，及时调整shell脚本，那么即使在一个全新的环境中，再次进行环境的初始化，这也是一件非常容易的事情，这也就实现了环境的自动化部署。

    结合前面讲解的《python虚拟环境virtualenv的搭建使用》中，python的virtualenv虚拟环境和插件自动安装指令，就可以通过编写shell脚本，轻松的完成环境的搭建。并且由于不同虚拟环境的隔离，新搭建的环境不会对原有的环境产生任何影响。

**一、项目实战：搭建一个python环境**

```
#!/bin/bash
# 生成virtualenv脚本

set -e
#创建virtualenv
/usr/local/python27/bin/virtualenv /www/venv

#安装依赖模块
unset http_proxy https_proxy
/www/venv/bin/pip install psutil

#设置代理
#export https_proxy=http://*.*.*.*:8124
#export http_proxy=http://*.*.*.*:8124

#安装依赖模块
/www/venv/bin/pip install lz4 protobuf msgpack-python pyzmq setproctitle mysql-connector-python python-memcached pymongo

#安装Dpark
/www/venv/bin/pip install https://github.com/douban/dpark/archive/master.zip

VENV_PYTHON=/www/venv/bin/python

#解决virtualenv环境下dpark无法在mesos集群中运行的问题
sed -i "1s|^.*$|#!$VENV_PYTHON|" /www/venv/lib/python2.7/site-packages/dpark/bin/executor27.py
```

   部分扩展说明：

- psutil：psutil is a module providing an interface for retrieving information on all running processes and system utilization (CPU, memory, disks, network, users) in a portable way by using Python, implementing many functionalities offered by command line tools such as: **ps, top, df, kill, free, lsof, free, netstat, ifconfig, nice, ionice, iostat, iotop, uptime, pidof, tty, who, taskset, pmap**.It currently supports **Linux**, **Windows**, **OSX**, **FreeBSD**, **Sun Solaris** both **32-bit** and **64-bit** with Python versions from **2.4** to **3.3** by using a single code base.  使用案例：[https://pypi.python.org/pypi/psutil](https://pypi.python.org/pypi/psutil)
- lz4:This package provides bindings for the [lz4 compression library](http://code.google.com/p/lz4/) by Yann Collet.   使用案例： [https://pypi.python.org/pypi/lz4](https://pypi.python.org/pypi/lz4%C2%A0)

**二、项目实战：运行所需执行的python脚本**

```
#!/bin/bash
set -e
cd `dirname $0`
source /www/venv/bin/activate

DATE=$1
#日志清理分析
python clean_day.py -m mesos --date $DATE
python kpi_day_duration.py -m mesos --date $DATE
```

**三、项目实战：环境的初始化**

```
#!/bin/bash
#Program:
#	Program for init warehouse env
#History:
#   2014/03/20 First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#init data store path
hadoop fs -test -d /data/project
if [ $? = 0 ]
then
    hadoop fs -rm -r /data/project
fi
hadoop fs -mkdir /data/project/eventlog
hadoop fs -mkdir /data/project/userlog
hadoop fs -mkdir /data/project/definelog

#init oozie workspace for load data into hive
hadoop fs -test -d  /user/oozie/workspaces/adminspaces/init_partition
if [ $? = 0 ]
then
    hadoop fs -rm -r  /user/oozie/workspaces/adminspaces/init_partition
fi
hadoop fs -mkdir /user/oozie/workspaces/yidongspaces/init_partition
hadoop fs -put ./Oozie/script.q /user/oozie/workspaces/adminspaces/init_partition/script.q
hadoop fs -put ./Oozie/workflow.xml /user/oozie/workspaces/adminspaces/init_partition/workflow.xml

#init hive database and tables
hive -f ./hive/hive.q
```

**四、项目实战：数据录入**

```
#!/bin/bash
datestart=$1
dateend=$2

beg_s=`date -d "$datestart" +%s`
end_s=`date -d "$dateend" +%s`

while [[ "$beg_s" -le "$end_s" ]]
do
        /www/venv/bin/python CleanData.py -d $(date -d @$beg_s +"%Y%m%d%H")
        beg_s=$((beg_s+3600))
done
```