blob: a355413722fc345061129b3148ef19a6b68a9d51 [file] [log] [blame]
tom4d0c6632014-09-15 23:27:01 -07001#!/bin/bash
Pavlin Radoslavov91413792014-10-15 11:00:32 -07002# -----------------------------------------------------------------------------
Luca Pretee6183752015-07-29 01:55:50 -07003# Packages ONOS distributable into onos.tar.gz, onos.zip or a deb file
Pavlin Radoslavov91413792014-10-15 11:00:32 -07004# -----------------------------------------------------------------------------
tom4d0c6632014-09-15 23:27:01 -07005
Brian O'Connorf5d93632015-09-04 20:18:31 -07006set -e
7
Luca Pretee6183752015-07-29 01:55:50 -07008# Build the staging directory used to produce the packages
9function build_stage_dir() {
10 # Make sure we have the original apache karaf bits first
11 [ ! -d $M2_REPO ] && echo "M2 repository $M2_REPO not found" && exit 1
12 [ -d $ONOS_STAGE ] && echo "ONOS stage $ONOS_STAGE already exists" && exit 1
13
14 # Create the stage directory and warp into it
15 mkdir -p $ONOS_STAGE
16 cd $ONOS_STAGE
17
18 # Check if Apache Karaf bits are available and if not, fetch them.
19 if [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ]; then
20 echo "Downloading $KARAF_TAR..."
21 curl -sL http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz > $KARAF_TAR
22 fi
23 [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
24 echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
25
26 # Unroll the Apache Karaf bits, prune them and make ONOS top-level directories.
27 [ -f $KARAF_ZIP ] && unzip -q $KARAF_ZIP && rm -rf $ONOS_STAGE/$KARAF_DIST/demos
28 [ -f $KARAF_TAR ] && tar zxf $KARAF_TAR && rm -rf $ONOS_STAGE/$KARAF_DIST/demos
29 mkdir bin
30
31 # Stage the ONOS admin scripts and patch in Karaf service wrapper extras
32 cp -r $ONOS_ROOT/tools/package/bin .
Brian O'Connor0eed29e2015-09-16 11:38:33 -070033 cp -r $ONOS_ROOT/tools/package/init $ONOS_STAGE/init
Luca Pretee6183752015-07-29 01:55:50 -070034 cp -r $ONOS_ROOT/tools/package/etc/* $ONOS_STAGE/$KARAF_DIST/etc
35
36 # Stage all builtin ONOS apps for factory install
37 onos-stage-apps $ONOS_STAGE/apps $ONOS_STAGE/$KARAF_DIST/system
38
39 # Mark the org.onosproject.drivers app active by default
40 touch $ONOS_STAGE/apps/org.onosproject.drivers/active
41
42 # Patch-in proper Karaf version into the startup script
43 sed "s/\$KARAF_VERSION/$KARAF_VERSION/g" \
44 $ONOS_ROOT/tools/package/bin/onos-service > bin/onos-service
45 sed "s/\$KARAF_VERSION/$KARAF_VERSION/g" \
46 $ONOS_ROOT/tools/package/bin/onos-client > bin/onos
47 chmod a+x bin/onos-service bin/onos
48
49 # Stage the ONOS bundles, but only those that match the version
50 mkdir -p $ONOS_STAGE/$KARAF_DIST/system/org/onosproject
51 find $M2_REPO/org/onosproject -type f -path "*/$ONOS_POM_VERSION/*" \
52 -name '*.jar' -o -name '*.pom' -o -name '*-features.xml' \
53 | grep -v -Ee '-tests.jar|-[0-9]{8}.[0-9]{6}-' \
54 | while read src; do
55 dst=$ONOS_STAGE/$KARAF_DIST/system/${src#$M2_REPO/*}
56 mkdir -p $(dirname $dst)
57 cp $src $dst
58 done
59
60 # ONOS Patching ----------------------------------------------------------------
61
62 # Patch the Apache Karaf distribution file to add ONOS features repository
63 perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
64 $ONOS_STAGE/$KARAF_DIST/etc/org.apache.karaf.features.cfg
65
66 # Patch the Apache Karaf distribution file to load default ONOS boot features
67 export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
68 perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
69 $ONOS_STAGE/$KARAF_DIST/etc/org.apache.karaf.features.cfg
70
71 # Patch the Apache Karaf distribution with ONOS branding bundle
72 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-*.jar \
73 $ONOS_STAGE/$KARAF_DIST/lib
74
75 # Patch in the ONOS version file
76 echo $ONOS_VERSION > $ONOS_STAGE/VERSION
77}
78
79function build_compressed_package() {
80 # Package up the ONOS tar file
81 cd $ONOS_STAGE_ROOT
82 rm -f $ONOS_TAR $ONOS_ZIP
83 COPYFILE_DISABLE=1 tar zcf $ONOS_TAR $ONOS_BITS
84
85 # Figure out whether we should build ONOS zip file and if so, build it.
86 which zip >/dev/null && [ -z "$ONOS_TAR_ONLY" ] && buildZip=true || unset buildZip
87 [ -n "$buildZip" ] && zip -rq $ONOS_ZIP $ONOS_BITS
88
89 # Report on the archives that were built and clean-up
90 [ -n "$buildZip" ] && ls -lh $ONOS_TAR $ONOS_ZIP || ls -lh $ONOS_TAR
91 rm -r $ONOS_STAGE
92}
93
94# Build a DEB package
Luca Pretef2049d12015-08-01 14:48:01 -070095function build_deb() {
Brian O'Connor8a0c20e2015-09-16 22:54:24 -070096 echo "This command may ask for your password to run commands as sudo,"
97 echo " because you need to be root in order to generate a proper DEB package."
Luca Pretee6183752015-07-29 01:55:50 -070098
99 sudo rm -fr $ONOS_DEB_ROOT
100
Luca Pretef2049d12015-08-01 14:48:01 -0700101 mkdir -p $ONOS_DEB_ROOT/{DEBIAN,opt,etc/init}
Luca Pretee6183752015-07-29 01:55:50 -0700102
Brian O'Connor8a0c20e2015-09-16 22:54:24 -0700103 # Copy the debian directory and fill in version
104 cp -r $ONOS_ROOT/tools/package/deb/* $ONOS_DEB_ROOT/DEBIAN/
105 sed -i'' -E "s/@ONOS_POM_VERSION/$ONOS_POM_VERSION/" $ONOS_DEB_ROOT/DEBIAN/control
Luca Pretee6183752015-07-29 01:55:50 -0700106
107 cp -r $ONOS_STAGE $ONOS_DEB_ROOT/opt/onos
Brian O'Connor0eed29e2015-09-16 11:38:33 -0700108 cp $ONOS_ROOT/tools/package/init/onos.conf $ONOS_DEB_ROOT/etc/init/
Luca Pretee6183752015-07-29 01:55:50 -0700109
110 mkdir -p $ONOS_DEB_ROOT/opt/onos/var/
111
112 sudo chown -R root:root $ONOS_DEB_ROOT
Luca Pretef2049d12015-08-01 14:48:01 -0700113
Luca Pretee6183752015-07-29 01:55:50 -0700114 sudo dpkg-deb --build $ONOS_DEB_ROOT > /dev/null &&
115 sudo mv $ONOS_STAGE_ROOT/deb.deb $ONOS_DEB && ls -l $ONOS_DEB
Brian O'Connor8a0c20e2015-09-16 22:54:24 -0700116
117 #TODO run lintian on .deb
Luca Pretee6183752015-07-29 01:55:50 -0700118}
119
Luca Pretef2049d12015-08-01 14:48:01 -0700120# Build an RPM package
121function build_rpm() {
122 read -r -p "WARN: rpm-build utility and root priviledges are need to build the package. Do you want to continue? [Y/n] " response
Brian O'Connorac322442015-09-17 23:49:17 -0700123 [[ "$response" =~ ^[Nn] ]] && exit 0
Luca Pretef2049d12015-08-01 14:48:01 -0700124
Brian O'Connorac322442015-09-17 23:49:17 -0700125 sudo rm -fr $ONOS_RPM_ROOT
Luca Pretef2049d12015-08-01 14:48:01 -0700126
Brian O'Connorac322442015-09-17 23:49:17 -0700127 sudo yum -y install rpm-build
Luca Pretef2049d12015-08-01 14:48:01 -0700128
Brian O'Connorac322442015-09-17 23:49:17 -0700129 mkdir -p $ONOS_RPM_ROOT/{BUILD,RPMS,SOURCES/onos-$ONOS_RPM_VERSION/{etc/init,opt},SPECS,SRPMS}
Luca Pretef2049d12015-08-01 14:48:01 -0700130
Brian O'Connorac322442015-09-17 23:49:17 -0700131 cp -r $ONOS_STAGE $ONOS_RPM_ROOT/SOURCES/onos-$ONOS_RPM_VERSION/opt/onos
132 cp $ONOS_ROOT/tools/package/init/onos.conf $ONOS_RPM_ROOT/SOURCES/onos-$ONOS_RPM_VERSION/etc/init/
Luca Pretef2049d12015-08-01 14:48:01 -0700133
Brian O'Connorac322442015-09-17 23:49:17 -0700134 cd $ONOS_RPM_ROOT/SOURCES
135 COPYFILE_DISABLE=1 tar zcf onos-$ONOS_RPM_VERSION.tar.gz onos-$ONOS_RPM_VERSION
Luca Pretef2049d12015-08-01 14:48:01 -0700136
Brian O'Connorac322442015-09-17 23:49:17 -0700137 cp $ONOS_ROOT/tools/package/rpm/onos.spec $ONOS_RPM_ROOT/SPECS/
138 sed -i'' -E "s/@ONOS_RPM_VERSION/$ONOS_RPM_VERSION/g" $ONOS_RPM_ROOT/SPECS/onos.spec
Luca Pretef2049d12015-08-01 14:48:01 -0700139
Brian O'Connorac322442015-09-17 23:49:17 -0700140 rpmbuild --define "_topdir $ONOS_RPM_ROOT" -bb $ONOS_RPM_ROOT/SPECS/onos.spec
141
142 cp $ONOS_RPM_ROOT/RPMS/noarch/onos-$ONOS_RPM_VERSION-1.noarch.rpm $ONOS_STAGE_ROOT && ls -l $ONOS_STAGE_ROOT/onos-$ONOS_RPM_VERSION-1.noarch.rpm
Luca Pretef2049d12015-08-01 14:48:01 -0700143}
144
Luca Pretee6183752015-07-29 01:55:50 -0700145# Script entry point
tom5c255702014-09-18 06:57:39 -0700146[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
147. $ONOS_ROOT/tools/build/envDefaults
tom4d0c6632014-09-15 23:27:01 -0700148
149# Bail on any errors
150set -e
151
Luca Pretee6183752015-07-29 01:55:50 -0700152# Before starting make sure the environment is clan - delete onos staging folder
153rm -fr $ONOS_STAGE
tom4d0c6632014-09-15 23:27:01 -0700154
Luca Pretef2049d12015-08-01 14:48:01 -0700155# If there are parameters check if we want to build a deb - otherwise build tar.gz
Luca Pretee6183752015-07-29 01:55:50 -0700156case ${1:---tar} in
Luca Pretef2049d12015-08-01 14:48:01 -0700157 "--tar" | "-T") build_stage_dir
158 build_compressed_package
Luca Pretee6183752015-07-29 01:55:50 -0700159 ;;
Luca Pretef2049d12015-08-01 14:48:01 -0700160 "--deb" | "-D") build_stage_dir
161 build_deb
Luca Pretee6183752015-07-29 01:55:50 -0700162 ;;
Luca Pretef2049d12015-08-01 14:48:01 -0700163 "--rpm" | "-R") build_stage_dir
164 build_rpm
165 ;;
166 *) echo "usage: $(basename $0) [--tar|--deb|--rpm]" >&2 && exit 1
Luca Pretee6183752015-07-29 01:55:50 -0700167 ;;
168esac