summaryrefslogtreecommitdiffstats
path: root/build/make_repo.sh
blob: 8ef3c44816ee02431541787f50d473ff60e14868 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
set -ex

BUILD_PATH=$(cd "$(dirname "$0")"; pwd)
WORK_PATH=$(cd "$(dirname "$0")"/..; pwd)

function process_env()
{
    mkdir -p ${WORK_PATH}/work/repo/ ${WORK_PATH}/work/repo/pip

    set +e
    sudo docker info
    if [[ $? != 0 ]]; then
        wget -qO- https://get.docker.com/ | sh
    else
        echo "docker is already installed!"
    fi
    set -e

cat <<EOF >${WORK_PATH}/work/repo/cp_repo.sh
#!/bin/bash
set -ex
cp /*.tar.gz /result
EOF

    sudo apt-get install python-yaml -y
    sudo apt-get install python-cheetah -y
}

function make_repo()
{
    rm -f ${WORK_PATH}/work/repo/install_packages.sh
    rm -f ${WORK_PATH}/work/repo/Dockerfile

    TEMP=`getopt -o h -l os-ver:,package-tag:,tmpl:,default-package:,special-package:,ansible-dir: -n 'make_repo.sh' -- "$@"`

    if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

    eval set -- "$TEMP"

    os_ver=""
    package_tag=""
    tmpl=""
    default_package=""
    special_package=""
    special_package_dir=""
    ansible_dir=""
    while :; do
        case "$1" in
            --os-ver) os_ver=$2; shift 2;;
            --package-tag) package_tag=$2; shift 2;;
            --tmpl) tmpl=$2; shift 2;;
            --default-package) default_package=$2; shift 2;;
            --special-package) special_package=$2; shift 2;;
            --special-package-dir) special_package_dir=$2; shift 2;;
            --ansible-dir) ansible_dir=$2; shift 2;;
            --) shift; break;;
            *) echo "Internal error!" ; exit 1 ;;
        esac
    done

    if [[ ! -z ${package_tag} && ${package_tag} == "pip" ]]; then
        make_pip_repo
        return
    fi

    if [[ -z ${os_ver} || -z ${tmpl} || -z ${package_tag} ]]; then
        echo "parameter is wrong"
        exit 1
    fi

    if [[ ${os_ver} == trusty ]]; then
        arch=Debian
        os_name=ubuntu
    fi

    if [[ ${os_ver} =~ rhel[0-9]*$ ]]; then
        arch=RedHat
        os_name=centos
    fi

    dockerfile=Dockerfile
    docker_tmpl=${BUILD_PATH}/os/${os_name}/${os_ver}/${package_tag}/${dockerfile}".tmpl"
    docker_tag="${os_ver}/${package_tag}"

    python ${BUILD_PATH}/gen_ins_pkg_script.py "${ansible_dir}" "${arch}" "${BUILD_PATH}/templates/${tmpl}" \
               "${docker_tmpl}" "${default_package}" "${special_package}" "${special_package_dir}"

    # copy make package script to work/repo dir
    if [[ -n $arch && -d ${WORK_PATH}/build/templates/$arch ]]; then
        rm -rf ${WORK_PATH}/work/repo/$arch
        cp -rf ${WORK_PATH}/build/templates/$arch ${WORK_PATH}/work/repo/
    fi

    if [[ -n $os_ver && -d ${WORK_PATH}/build/os/$os_name/$os_ver ]]; then
        rm -rf ${WORK_PATH}/work/repo/$os_ver
        cp -rf ${WORK_PATH}/build/os/$os_name/$os_ver ${WORK_PATH}/work/repo
    fi

    if [[ -f ${WORK_PATH}/build/os/$os_name/comps.xml ]]; then
        cp -rf ${WORK_PATH}/build/os/$os_name/comps.xml ${WORK_PATH}/work/repo
    fi

    sudo docker build -t ${docker_tag} -f ${WORK_PATH}/work/repo/${dockerfile} ${WORK_PATH}/work/repo/

    sudo docker run -t -v ${WORK_PATH}/work/repo:/result ${docker_tag}

    image_id=$(sudo docker images|grep ${docker_tag}|awk '{print $3}')

    sudo docker rmi -f ${image_id}
}

function make_pip_repo()
{
    source $WORK_PATH/build/build.conf

    if [[ $PIP_CONF == "" ]]; then
        return
    fi

    for i in $PIP_CONF; do
        curl --connect-timeout 10 -o $WORK_PATH/work/repo/pip/`basename $i` $i
    done

    cd $WORK_PATH/work/repo; tar -zcvf pip.tar.gz ./pip; cd -
}

function make_all_repo()
{
    make_pip_repo

    make_repo --os-ver rhel6 --package-tag compass \
              --tmpl compass_core.tmpl \
              --default-package "epel-release python-yaml python-jinja2 python-paramiko"

    make_repo --os-ver trusty --package-tag juno \
              --ansible-dir $WORK_PATH/deploy/adapters/ansible \
              --tmpl Debian_juno.tmpl \
              --default-package "openssh-server" \
              --special-package "openvswitch-datapath-dkms openvswitch-switch"

    make_repo --os-ver trusty --package-tag kilo \
              --ansible-dir $WORK_PATH/deploy/adapters/ansible \
              --tmpl Debian_kilo.tmpl \
              --default-package "openssh-server" \
              --special-package "openvswitch-datapath-dkms openvswitch-switch"

    make_repo --os-ver rhel7 --package-tag juno \
              --ansible-dir $WORK_PATH/deploy/adapters/ansible \
              --tmpl RedHat_juno.tmpl \
              --default-package "rsyslog strace net-tools wget vim openssh-server dracut-config-rescue dracut-network" \
              --special-package ""
}

function main()
{
    process_env

    if [[ $# -eq 0 ]]; then
        make_all_repo
    else
        make_repo $*
    fi
}

main $*