#!/usr/bin/python
#-*- coding: UTF-8 -*-
import commands
import urllib
import os
import sys
import subprocess
import time
print "-----------安装gitlab程序--------------"
def exec_cmd(cmd,timeout=-1):
    _p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    if timeout >0:
        timer=Timer(timeout,_p.kill)
        timer.start()
        _stdout,_stderr=p.communicate()
        if time.is_alive:
            timer.cancel()
            ret={'return_code': _p.returncode,'stdout': _stdout,'stderr': _stderr}
        else:
            ret={'return_code': _p.returncode,'stdout': '','stderr': _stderr}
    else:
        _stdout, _stderr = _p.communicate()
        ret={'return_code': _p.returncode,'stdout': _stdout,'stderr': _stderr}
    return ret
#安装gitalb安装前的依赖软件    
def install():
    pakages=['openssh-server','epel-release','postfix','cronie','vim']
    print "Installing pakages"
    for each in pakages:
        command="yum -y install %s" % each
        ret=exec_cmd(command)
        if ret['return_code']!=0:
            print 'pakage({0}) install failed,reason:{1}'.format(each,ret['stderr'])
            sys.exit(3)
        else:
            print 'pakage({0}) install success,'.format(each)
#做防火墙的
def command():
    command='sudo lokkit -s http -s ssh'
    ret=exec_cmd(command)
    if ret['return_code']!=0:
        print "the %s excute failed" % command
    return True
#显示下载进度
def report(count,blockSize,totalSize):
    percent=int(count*blockSize*100/totalSize)
    if percent <=100:
        sys.stdout.write(str(percent)+'% ||'+ "\r")
        sys.stdout.flush()
    return True   
#下载gitlab软件包
def down_load():
    print '------------------downloading gitlab pakage------------------'
    getfile='http://192.168.100.127/gitlab-7.9.2_omnibus-1.el6.x86_64.rpm'
    savefile='/root/gitlab-7.9.2_omnibus-1.el6.x86_64.rpm' 
    urllib.urlretrieve(getfile,savefile,reporthook=report)
    sys.stdout.write('\r downloaing complete!!!')
    sys.stdout.flush()
    return True
#编译gitlab软件包
def complie():
    print "编译gitlab,此过程需要一段时间,请耐心等待"
    command1='sudo rpm -i gitlab-7.9.2_omnibus-1.el6.x86_64.rpm'
    ret1=exec_cmd(command1)
    if ret1['return_code']!=0:
        print "the %s excute failed" % command1
    else:
        print "the %s excute success" % command1
    command2='gitlab-ctl reconfigure'
    ret2=exec_cmd(command2)
    if ret2['return_code']!=0:
        print "the %s excute failed" % command2
    else:
        print "the %s excute success" % command2
#停掉服务,为了修改域名
    command3='gitlab-ctl stop'
    ret3=exec_cmd(command3)
    if ret3['return_code']!=0:
        print "the %s excute failed" % command3
#调用alter_host函数    
    alter_host()
    command4='gitlab-ctl start'
    ret4=exec_cmd(command4)
    if ret3['return_code'] !=0:
        print "the %s excute failed" % command4
    return True
#修改邮箱的域名
def alter_host():
    host=commands.getoutput("ifconfig eth0|grep Bcast|awk '{print $2}'|cut -d ':' -f 2")
    config_file='/var/opt/gitlab/gitlab-rails/etc/gitlab.yml'
    command1='sed -i "s/gitlab.example.com/{0}/g" {1} '.format(host,config_file)
    ret1=exec_cmd(command1)
    if ret1['return_code']!=0:
        print "the % excute failed" % command1
    else:
        print "the command excute success"
install()
command()
down_load()
complie()
print "--------------安装完成--------------"