31c821c28f098e5ee833fe0e41c54fe7800e1252
[ric-plt/lib/rmr.git] / ci / ci_build.ksh
1 #!/usr/bin/env ksh
2 # :vim ts=4 sw=4 noet:
3 #==================================================================================
4 #    Copyright (c) 2019 Nokia
5 #    Copyright (c) 2018-2019 AT&T Intellectual Property.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #==================================================================================
19 #
20
21 #       Mnemonic:       ci_build.ksh
22 #       Abstract:       Script builds RMr, exececutes the unit and application based
23 #                               tests, then generates packages. The packages are left in /tmp
24 #                               unless -t target-dir is given on the command line. A YAML file
25 #                               is created which can be used to find all of the packages which
26 #                               were deposited in the target directory. In an environment which
27 #                               is also capable of generating RPM ppackages, there should be
28 #                               four packages: a run-time and development package for both
29 #                               debian (.deb) and rh (rpm) based systems.
30 #
31 #                               The intent of this script is for it to be executed as a part
32 #                               of a docker image build process such that the resulting image
33 #                               contains the RMr packages, and a YAML file which provides the
34 #                               paths to the package(s) in the image.  Docker tools can then
35 #                               be used to extract the packages and push them to some external
36 #                               repository.
37 #
38 #                               Assumptions:
39 #                                       We assume that this scirpt is executed at the 'root' of the
40 #                               RMr repo (i.e. the directory which has a subdirectory ci).
41 #
42 #       Date:           14 June 2019
43 # --------------------------------------------------------------------------------
44
45 # stash a set of packages for a particular flavour ($1)
46 #
47 function stash_pkgs {
48         echo "  - $1:" >>$yaml_file      # add package flavour (dev, runtime, etc)
49
50         for pkg in deb rpm
51         do
52                 ls .build/*.$pkg 2>/dev/null | while read f
53                 do
54                         cp $f $target_dir/${f##*/}
55                         echo "    $pkg: $target_dir/${f##*/}" >>$yaml_file
56                 done
57
58         done
59 }
60
61 # ---------------------------------------------------------------------------
62
63 target_dir="/tmp"
64 verbose=0
65
66 while [[ $1 == -* ]]
67 do
68         case $1 in
69                 -t)     target_dir=$2; shift;;
70                 -v)     verbose=1;;
71
72                 *)      echo "$1 is not recognised"
73                         echo ""
74                         echo "usage: $0 [-t target-dir]"
75                         exit 1
76                         ;;
77         esac
78
79         shift
80 done
81
82 if [[ ! -d $target_dir ]]
83 then
84         echo "[FAIL] cannot find directory: $target_dir"
85         exit 1
86 fi
87
88 if [[ ! -d ./ci ]]                                                      # verify we are in the root of the RMr repo filesystem, abort if not
89 then
90         echo "[FAIL] current working directory does not seem right; should be RMr repo root: $PWD"
91         exit 1
92 fi
93
94 set -e                                                                          # fail unconditionally on first issue
95 yaml_file=$target_dir/build_packages.yml
96 rm -f $yaml_file
97
98 mkdir -p .build
99 (
100         cd .build
101         rm -f *.deb *.rpm                                               # these shouldn't be there, but take no chances
102         cmake .. -DBUILD_DOC=1 -DDEV_PKG=1              # set up dev config for unit test (requires dev stuff)
103         make package
104 )
105 (
106         cd test                                 # execute tests
107         ksh unit_test.ksh                               # unit tests first
108         cd app_test
109         ksh run_all.ksh                                 # application based tests if units pass
110 )
111
112 # initialise the yaml file
113 cat <<-endKat >$yaml_file
114 ---
115 # package types which might be listed below
116 pkg_types:
117   - deb
118   - rpm
119
120 packages:
121 endKat
122
123 stash_pkgs development      # testing good, stash dev packages built above
124
125 (
126         cd .build                               # now build the run-time packages
127         rm -f *.deb *.rpm               # reconfig should delete these, but take no chances
128         cmake ..                # configure run-time build
129         make package
130 )
131 stash_pkgs runtime                      # move packages to target and record in yaml file
132
133 echo "..." >>$yaml_file         # terminate yaml file
134
135 set +e
136 if (( verbose ))
137 then
138         echo "generated yaml file:"
139         cat $yaml_file
140         echo ""
141         echo "------"
142         ls -al $target_dir/*.deb $target_dir/*.rpm
143 fi
144
145 exit 0