summaryrefslogtreecommitdiffstats
path: root/build.sh
blob: 7151317a438596bbacad1093e8717416ae681423 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
set -ex

SCRIPT_DIR=`cd ${BASH_SOURCE[0]%/*};pwd`
COMPASS_DIR=${SCRIPT_DIR}
WORK_DIR=$SCRIPT_DIR/work/building
PACKAGES="fuse fuseiso createrepo genisoimage curl"

source $SCRIPT_DIR/build/build.conf

mkdir -p $WORK_DIR

cd $WORK_DIR
function prepare_env()
{
    set +e
    for i in $PACKAGES; do
        if ! apt --installed list 2>/dev/null |grep "\<$i\>"
        then
            sudo apt-get install  -y --force-yes  $i
        fi
    done
    set -e

    if [[ ! -d $CACHE_DIR ]]; then
        mkdir -p $CACHE_DIR
    fi
}

function download_git()
{
    file_dir=$CACHE_DIR/${1%.*} 
    if [[ -d $file_dir/.git ]]; then
        cd $file_dir
        git pull origin master
        cd -
    else
        rm -rf $CACHE_DIR/$file_dir
        git clone $2 $file_dir
    fi
}

function download_url()
{
    rm -f $CACHE_DIR/$1.md5
    curl --connect-timeout 10 -o $CACHE_DIR/$1.md5 $2.md5 2>/dev/null
    if [[ -f $CACHE_DIR/$1 ]]; then
        local_md5=`md5sum $CACHE_DIR/$1 | cut -d ' ' -f 1`
        repo_md5=`cat $CACHE_DIR/$1.md5 | cut -d ' ' -f 1`
        if [[ $local_md5 == $repo_md5 ]]; then
            return
        fi
    fi

    curl --connect-timeout 10 -o $CACHE_DIR/$1 $2
}

function download_local()
{
    cp $2 $CACHE_DIR/ -rf
}

function download_packages()
{
     for i in $CENTOS_BASE $COMPASS_CORE $COMPASS_WEB $COMPASS_INSTALL $TRUSTY_JUNO_PPA $UBUNTU_ISO \
              $CENTOS_ISO $CENTOS7_JUNO_PPA $LOADERS $CIRROS $APP_PACKAGE $COMPASS_PKG $PIP_REPO $ANSIBLE_MODULE; do
         name=`basename $i`
         if [[ ${name##*.} == git ]]; then
             download_git  $name $i
         elif [[ "https?" =~ ${i%%:*} ]]; then
             download_url  $name $i
         else
             download_local $name $i
         fi
     done
}

function copy_file()
{
    new=$1

    # main process
    mkdir -p $new/compass $new/bootstrap $new/pip $new/guestimg $new/app_packages $new/ansible
    mkdir -p $new/repos/cobbler/{ubuntu,centos}/{iso,ppa}

    cp -rf $SCRIPT_DIR/util/ks.cfg $new/isolinux/ks.cfg

    rm -rf $new/.rr_moved

    cp $CACHE_DIR/`basename $UBUNTU_ISO` $new/repos/cobbler/ubuntu/iso/ -rf
    cp $CACHE_DIR/`basename $TRUSTY_JUNO_PPA` $new/repos/cobbler/ubuntu/ppa/ -rf
    cp $CACHE_DIR/`basename $CENTOS_ISO` $new/repos/cobbler/centos/iso/ -rf
    cp $CACHE_DIR/`basename $CENTOS7_JUNO_PPA` $new/repos/cobbler/centos/ppa/ -rf

    cp $CACHE_DIR/`basename $LOADERS` $new/ -rf || exit 1
    cp $CACHE_DIR/`basename $CIRROS` $new/guestimg/ -rf || exit 1
    cp $CACHE_DIR/`basename $APP_PACKAGE` $new/app_packages/ -rf || exit 1
    cp $CACHE_DIR/`basename $ANSIBLE_MODULE | sed 's/.git//g'`  $new/ansible/ -rf || exit 1

    for i in $COMPASS_CORE $COMPASS_INSTALL $COMPASS_WEB; do
        cp $CACHE_DIR/`basename $i | sed 's/.git//g'` $new/compass/ -rf
    done

    cp $COMPASS_DIR/deploy/adapters $new/compass/compass-adapters -rf

    tar -zxvf $CACHE_DIR/pip.tar.gz -C $new/

    find $new/compass -name ".git" |xargs rm -rf
}

function rebuild_ppa()
{
    name=`basename $COMPASS_PKG`
    rm -rf ${name%%.*} $name
    cp $CACHE_DIR/$name $WORK_DIR
    cp $SCRIPT_DIR/build/os/centos/comps.xml $WORK_DIR
    tar -zxvf $name
    cp ${name%%.*}/*.rpm $1/Packages -f
    rm -rf $1/repodata/*
    createrepo -g $WORK_DIR/comps.xml $1
}

function make_iso()
{
    download_packages
    name=`basename $CENTOS_BASE`
    cp  $CACHE_DIR/$name ./ -f
    # mount base iso
    mkdir -p base new
    fuseiso $name base
    cd base;find .|cpio -pd ../new ;cd -
    fusermount -u base
    chmod 755 ./new -R

    copy_file new
    rebuild_ppa new

    mkisofs -quiet -r -J -R -b isolinux/isolinux.bin \
            -no-emul-boot -boot-load-size 4 \
            -boot-info-table -hide-rr-moved \
            -x "lost+found:" \
            -o compass.iso new/

    md5sum compass.iso > compass.iso.md5

    # delete tmp file
    rm -rf new base $name
}

function process_param()
{
    TEMP=`getopt -o c:d:f: --long iso-dir:,iso-name:,cache-dir: -n 'build.sh' -- "$@"`

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

    eval set -- "$TEMP"

    while :; do
        case "$1" in
            -d|--iso-dir) export ISO_DIR=$2; shift 2;;
            -f|--iso-name) export ISO_NAME=$2; shift 2;;
            -c|--cache-dir) export CACHE_DIR=$2; shift 2;;
            --) shift; break;;
            *) echo "Internal error!" ; exit 1 ;;
        esac
    done

    export CACHE_DIR=${CACHE_DIR:-$WORK_DIR/cache}
    export ISO_DIR=${ISO_DIR:-$WORK_DIR}
    export ISO_NAME=${ISO_NAME:-"compass.iso"}
}

function copy_iso()
{
   if [[ $ISO_DIR/$ISO_NAME == $WORK_DIR/compass.iso ]]; then
      return
   fi

   cp $WORK_DIR/compass.iso $ISO_DIR/$ISO_NAME -f
}

process_param $*
prepare_env
make_iso
copy_iso