-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
124 lines (92 loc) · 3.63 KB
/
Dockerfile
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
FROM fedora:latest as builder
LABEL authors="regis"
# Install necessary tools
RUN dnf install -y wget tar rpm-build make rpmlint
# Install Go 1.23
RUN wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz \
&& tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz \
&& rm go1.23.0.linux-amd64.tar.gz
RUN dnf install -y gccgo
# Set up Go environment
ENV PATH="/usr/local/go/bin:${PATH}"
# Verify Go installation
RUN go version
# Set the working directory inside the container
WORKDIR /app
# Copy go.mod and go.sum files first to leverage Docker caching
COPY go.mod go.sum ./
# Download dependencies only when go.mod or go.sum change
RUN go mod tidy && go mod download
# Copy the project files into the container
COPY . .
# Create necessary directories for RPM build
RUN mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} \
&& mkdir -p rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/usr/local/bin \
&& mkdir -p rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/etc/systemd/system \
&& mkdir -p rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/var/lib/dsync
RUN tar -cvzf rpmbuild/SOURCES/drive-sync-1.0.tar.gz .
# Build the CLI binary
RUN cd cli/dsync \
&& go build -o /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/usr/local/bin/dsync
# Build the daemon binary
RUN cd /app/daemon \
&& go build -o /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/usr/local/bin/dsync-daemon
# Initialize the database file
RUN touch /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/var/lib/dsync/database.sqlite \
&& chmod 0660 /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/var/lib/dsync/database.sqlite
# Copy the systemd service file into the RPM structure
RUN cp /app/service/dsync-daemon.service /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/etc/systemd/system/
# Create the RPM spec file
RUN cat <<EOL > /app/rpmbuild/SPECS/drive-sync.spec
Name: drive-sync
Version: 1.0
Release: 1%{?dist}
Summary: Drive Sync CLI and Daemon
License: Your License
Source0: %{name}-%{version}.tar.gz
%description
This package installs the Drive Sync CLI and a daemon for background synchronization with Google Drive.
%prep
%build
%install
echo "Directory tree of the build root:"
find %{buildroot} -type d -print
# Make sure this path exists in the build directory
mkdir -p %{buildroot}/usr/local/bin
mkdir -p %{buildroot}/etc/systemd/system
mkdir -p %{buildroot}/var/lib/dsync
touch %{buildroot}/var/lib/dsync/database.sqlite
chmod 0660 %{buildroot}/var/lib/dsync/database.sqlite
# Copy binaries to the build root
cp -a /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/usr/local/bin/* %{buildroot}/usr/local/bin/
cp /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/etc/systemd/system/dsync-daemon.service %{buildroot}/etc/systemd/system/
cp /app/rpmbuild/BUILDROOT/drive-sync-1.0-1.x86_64/var/lib/dsync/database.sqlite %{buildroot}/var/lib/dsync/
%files
/usr/local/bin/dsync
/usr/local/bin/dsync-daemon
/etc/systemd/system/dsync-daemon.service
/var/lib/dsync/database.sqlite
%post
systemctl daemon-reload
systemctl enable dsync-daemon.service
systemctl start dsync-daemon.service
%preun
if [ $1 -eq 0 ]; then
systemctl stop dsync-daemon.service
systemctl disable dsync-daemon.service
rm -f /etc/systemd/system/dsync-daemon.service
rm -rf %{buildroot}/var/lib/dsync
fi
%postun
systemctl daemon-reload
pkill dsync-daemon
%changelog
* Fri Aug 30 2024 Inshal Khan <khanmf@rknec.edu> 1.0-1
- Initial package
EOL
# Build the RPM package
RUN rpmbuild -ba /app/rpmbuild/SPECS/drive-sync.spec --define "_topdir /app/rpmbuild"
# Final stage to extract the RPM from the build image
FROM alpine
WORKDIR /app
COPY --from=builder /app/rpmbuild/RPMS/x86_64/ .