Compress image using xz
[ta/caas-cpupooler.git] / rpmbuild.spec
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 %define COMPONENT cpupooler
16 %define COMPONENT_PART process-starter
17 %define RPM_NAME caas-%{COMPONENT}
18 %define RPM_MAJOR_VERSION 0.2.0
19 %define RPM_MINOR_VERSION 4
20 %define CPUPOOLER_VERSION 808b717165c10b0752bbafd4e2898d4e20c2fae8
21 %define DEPENDENCY_MANAGER_VERSION 0.5.0
22 %define IMAGE_TAG %{RPM_MAJOR_VERSION}-%{RPM_MINOR_VERSION}
23 %define PROCESS_STARTER_INSTALL_PATH /opt/bin/
24
25 Name:           %{RPM_NAME}
26 Version:        %{RPM_MAJOR_VERSION}
27 Release:        %{RPM_MINOR_VERSION}%{?dist}
28 Summary:        Containers as a Service cpu-pooler component
29 License:        %{_platform_license} and BSD 3-Clause License
30 URL:            https://github.com/nokia/CPU-Pooler
31 BuildArch:      x86_64
32 Vendor:         %{_platform_vendor} and Nokia
33 Source0:        %{name}-%{version}.tar.gz
34
35 Requires: docker-ce >= 18.09.2, rsync
36 BuildRequires: docker-ce-cli >= 18.09.2, xz
37
38 # I was able to pack an executable via this.
39 # more info at https://fedoraproject.org/wiki/Packaging:Debuginfo
40 %global debug_package %{nil}
41
42 %description
43 This RPM contains the cpu-pooler container image, process-starter binary and related deployment artifacts for the CaaS subsystem.
44
45 %prep
46 %autosetup
47
48 %build
49 # build the process-starter binary inside a builder conatiner
50 docker build \
51   --network=host \
52   --no-cache \
53   --force-rm \
54   --build-arg HTTP_PROXY="${http_proxy}" \
55   --build-arg HTTPS_PROXY="${https_proxy}" \
56   --build-arg NO_PROXY="${no_proxy}" \
57   --build-arg http_proxy="${http_proxy}" \
58   --build-arg https_proxy="${https_proxy}" \
59   --build-arg no_proxy="${no_proxy}" \
60   --build-arg DEPENDENCY_MANAGER="%{DEPENDENCY_MANAGER_VERSION}" \
61   --build-arg CPUPOOLER="%{CPUPOOLER_VERSION}" \
62   --tag %{COMPONENT_PART}:builder \
63   %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/docker-build/%{COMPONENT_PART}/
64
65 # create a directory for process-starter binary
66 mkdir -p %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/results
67
68 # run the builder conatiner for process-starter binary
69 docker run \
70   -id \
71   --rm \
72   --network=host \
73   --privileged \
74   -e HTTP_PROXY="${http_proxy}" \
75   -e HTTPS_PROXY="${https_proxy}" \
76   -e NO_PROXY="${no_proxy}" \
77   -e http_proxy="${http_proxy}" \
78   -e https_proxy="${https_proxy}" \
79   -e no_proxy="${no_proxy}" \
80   --entrypoint=/bin/sh \
81   %{COMPONENT_PART}:builder
82
83 # get the process-starter binary
84 docker cp $(docker ps | grep "%{COMPONENT_PART}:builder" | awk -F' ' '{ print $1 }'):/process-starter %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/results/
85
86 # rm container
87 for container_ran in $(docker ps -a | grep "%{COMPONENT_PART}:builder" | awk -F' ' '{ print $1 }')
88 do
89     docker rm -f $container_ran
90 done
91
92 # remove docker image
93 docker rmi -f %{COMPONENT_PART}:builder
94
95 # build the cpu pooler
96 docker build \
97   --network=host \
98   --no-cache \
99   --force-rm \
100   --build-arg HTTP_PROXY="${http_proxy}" \
101   --build-arg HTTPS_PROXY="${https_proxy}" \
102   --build-arg NO_PROXY="${no_proxy}" \
103   --build-arg http_proxy="${http_proxy}" \
104   --build-arg https_proxy="${https_proxy}" \
105   --build-arg no_proxy="${no_proxy}" \
106   --build-arg DEPENDENCY_MANAGER="%{DEPENDENCY_MANAGER_VERSION}" \
107   --build-arg CPUPOOLER="%{CPUPOOLER_VERSION}" \
108   --tag %{COMPONENT}:%{IMAGE_TAG} \
109   %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/docker-build/%{COMPONENT}/
110
111 # create a save folder
112 mkdir -p %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/docker-save/
113
114 # save the cpu poooler container
115 docker save %{COMPONENT}:%{IMAGE_TAG} | xz -z -T2 > %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/docker-save/%{COMPONENT}:%{IMAGE_TAG}.tar
116
117 # remove docker image
118 docker rmi -f %{COMPONENT}:%{IMAGE_TAG}
119
120 %install
121 mkdir -p %{buildroot}/%{_caas_container_tar_path}/
122 rsync -av %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/docker-save/%{COMPONENT}:%{IMAGE_TAG}.tar %{buildroot}/%{_caas_container_tar_path}/
123
124 mkdir -p %{buildroot}%{PROCESS_STARTER_INSTALL_PATH}
125 rsync -av %{_builddir}/%{RPM_NAME}-%{RPM_MAJOR_VERSION}/results/process-starter %{buildroot}/%{PROCESS_STARTER_INSTALL_PATH}/
126
127 mkdir -p %{buildroot}/%{_playbooks_path}/
128 rsync -av ansible/playbooks/cpupooler.yaml %{buildroot}/%{_playbooks_path}/
129
130 mkdir -p %{buildroot}/%{_roles_path}/
131 rsync -av ansible/roles/cpupooler %{buildroot}/%{_roles_path}/
132
133 %files
134 %{_caas_container_tar_path}/%{COMPONENT}:%{IMAGE_TAG}.tar
135 %{PROCESS_STARTER_INSTALL_PATH}/process-starter
136 %{_playbooks_path}/cpupooler.yaml
137 %{_roles_path}/cpupooler
138
139 %preun
140
141 %post
142 mkdir -p %{_postconfig_path}/
143 ln -sf %{_playbooks_path}/cpupooler.yaml %{_postconfig_path}/
144
145 %postun
146 if [ $1 -eq 0 ]; then
147     rm -f %{_postconfig_path}/cpupooler.yaml
148 fi
149
150 %clean
151 rm -rf ${buildroot}