Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update config format + increment revision #9

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .deb.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package: deb-builder
source: deb-builder
version: 0.0.1
version: '1:0.1.0'
architecture:
- 386
- amd64
- arm
- arm64
maintainer: DewepOnline <deb@dewep.online>
homepage: https://dewep.online/deb-builder.html
description:
Expand All @@ -13,12 +16,13 @@ control:
depends:
- systemd
- ca-certificates
build: scripts/build.sh %s
build: scripts/build.sh
conffiles:
- /etc/fdns/config.yaml
preinst: scripts/preinst.sh
postinst: scripts/postinst.sh
prerm: scripts/prerm.sh
postrm: scripts/postrm.sh
data:
build/bin/deb-builder_amd64: usr/bin/deb-builder
usr/bin/deb-builder: build/bin/deb-builder_%arch%
usr/log/deb-builder.log: "+Empty empty log file\n"
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ new-conf:

.PHONY: build
build:
bash scripts/build.sh back
bash scripts/build.sh amd64

.PHONY: linter
linter:
Expand All @@ -20,8 +20,8 @@ tests:
ci:
bash scripts/ci.sh

deb: build
deb:
deb-builder build

install: build
@GO111MODULE=on GOOS=linux GOARCH=amd64 go build -o $(GOPATH)/bin/deb-builder ./cmd/deb-builder/
cp ./build/bin/deb-builder_amd64 $(GOPATH)/bin/deb-builder
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ example:
```yaml
package: demo-app # The name of the binary package.
source: demo # This field identifies the source package name.
version: 0.0.1 # The version number of a package. The format is: [epoch:]upstream_version[-revision].
architecture: # OS Architecture: any, all, i386, amd64, etc...
- i386
- amd64
version: 1:0.0.1 # The version number of a package. The format is: [epoch:]upstream_version.
architecture: # OS Architecture: all, 386, amd64, arm, arm64
- 386
- amd64
- arm
- arm64
maintainer: User Name <user.name@example.com> # The package maintainer’s name and email address. The name must come first, then the email address inside angle brackets <> (in RFC822 format).
homepage: http://example.com/ #The URL of the web site for this package, preferably (when applicable) the site from which the original source can be obtained and any additional upstream documentation or information may be found.
description: # This field contains a description of the binary package, consisting of two parts, the synopsis or the short description, and the long description.
- This is a demo utility
- It performs some actions. Don't forget to update this text.
- This is a demo utility
- It performs some actions. Don't forget to update this text.
section: utils # This field specifies an application area into which the package has been classified: admin, cli-mono, comm, database, debug, devel, doc, editors, education, electronics, embedded, fonts, games, gnome, gnu-r, gnustep, graphics, hamradio, haskell, httpd, interpreters, introspection, java, javascript, kde, kernel, libdevel, libs, lisp, localization, mail, math, metapackages, misc, net, news, ocaml, oldlibs, otherosfs, perl, php, python, ruby, rust, science, shells, sound, tasks, tex, text, utils, vcs, video, web, x11, xfce, zope.
priority: optional # This field represents how important it is that the user have the package installed: required, important, standard, optional, extra.
control:
depends: # This declares an absolute dependency. A package will not be configured unless all of the packages listed in its Depends field have been correctly configured (unless there is a circular dependency as described above).
- systemd | supervisor
- ca-certificates
build: scripts/build.sh %s # This field defines the script for building the application from the source code. During the build, the name of the architecture is passed to the script. Example: sh scripts/build.sh amd64
- systemd | supervisor
- ca-certificates
build: scripts/build.sh # This field defines the script for building the application from the source code. During the build, the name of the architecture is passed to the script. Example: sh scripts/build.sh amd64
conffiles: # The list of package files that are configuration files, when updating, files from this list are not overwritten with new ones, unless this is specified separately;
- /etc/demo/config.yaml
- /etc/demo/config.yaml
preinst: scripts/preinst.sh # The script executed before installation.
postinst: scripts/postinst.sh # The script executed after installation.
prerm: scripts/prerm.sh # The script executed before removal.
postrm: scripts/postrm.sh # The script executed after removal.
data: # A list of files that will be packaged in a package during assembly, where the source file is preceded by a colon, and after it is the name and location of the file in the package.
build/bin/demo: bin/demo
configs/config.yaml: etc/demo/config.yaml
data: # A list of files that will be packaged during the build, where the file in the destination package is preceded by a colon, and the source file is indicated after it. A placeholder %arch% is available indicating the architecture.
bin/demo: build/bin/demo_%arch%
etc/demo/config.yaml: configs/config.yaml
```

# build deb package
Expand Down
44 changes: 21 additions & 23 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"os"
"path/filepath"
"time"
"strings"

"github.com/dewep-online/deb-builder/pkg/archive"
"github.com/dewep-online/deb-builder/pkg/config"
"github.com/dewep-online/deb-builder/pkg/control"
"github.com/dewep-online/deb-builder/pkg/exec"
"github.com/dewep-online/deb-builder/pkg/packages"
"github.com/dewep-online/deb-builder/pkg/utils"
"github.com/deweppro/go-app/console"
"github.com/deweppro/go-archives/ar"
Expand All @@ -20,11 +21,12 @@ func Build() console.CommandGetter {
setter.Setup("build", "build deb package")
setter.Example("build")
setter.Flag(func(fs console.FlagsSetter) {
fs.StringVar("config", config.ConfigFileName, "Config file")
fs.StringVar("base-dir", utils.GetEnv("DEB_STORAGE_BASE_DIR", "/tmp/deb-storage"), "Deb package base storage")
fs.StringVar("tmp-dir", utils.GetEnv("DEB_BUILD_DIR", "/tmp/deb-build"), "Deb package build dir")
})
setter.ExecFunc(func(_ []string, baseDir, tmpDir string) {
conf, err := config.Detect()
setter.ExecFunc(func(_ []string, debConf, baseDir, tmpDir string) {
conf, err := config.Detect(debConf)
console.FatalIfErr(err, "deb config not found")

buildDir := fmt.Sprintf("%s/%s_%s", tmpDir, conf.Package, conf.Version)
Expand All @@ -38,22 +40,7 @@ func Build() console.CommandGetter {

// check file version

subVersion := ""
debFileNameBuild := func() string {
return fmt.Sprintf("%s/%s_%s%s_%s.deb", storeDir, conf.Package, conf.Version, subVersion, arch)
}
debFile := debFileNameBuild()
for {
utils.FileStat(debFile, func(fi os.FileInfo) {
subVersion = fmt.Sprintf("-%d", time.Now().Unix()-fi.ModTime().Unix())
debFile = debFileNameBuild()
})

if !utils.FileExist(debFile) {
break
}
<-time.After(time.Second)
}
debFile, revision, carch := packages.BuildName(storeDir, conf.Package, conf.Version, arch)

// package

Expand All @@ -65,8 +52,18 @@ func Build() console.CommandGetter {
dataFile := buildDir + "/data.tar.gz"
tg, err := archive.NewWriter(dataFile)
console.FatalIfErr(err, "create data.tar.gz")
for src, dst := range conf.Data {
if f, h, err1 := tg.WriteFile(src, dst); err1 != nil {
for dst, src := range conf.Data {
src = strings.ReplaceAll(src, "%arch%", arch)
var (
f, h string
err1 error
)
if src[0] == '+' {
f, h, err1 = tg.WriteData(dst, []byte(src)[1:])
} else {
f, h, err1 = tg.WriteFile(src, dst)
}
if err1 != nil {
console.FatalIfErr(err1, "write %s to data.tar.gz", src)
} else {
md5sum.Add(f, h)
Expand All @@ -81,8 +78,8 @@ func Build() console.CommandGetter {

ctrl := control.NewControl(conf)
ctrl.DataSize(tg.Size())
ctrl.Arch(arch)
ctrlFile, err := ctrl.Save(buildDir, subVersion)
ctrl.Arch(carch)
ctrlFile, err := ctrl.Save(buildDir, revision)
console.FatalIfErr(err, "create control")
cpkg.AddFile(ctrlFile)

Expand Down Expand Up @@ -113,6 +110,7 @@ func Build() console.CommandGetter {
console.FatalIfErr(deb.Import(dataFile, 0644), "write %s to %s", dataFile, debFile)
console.FatalIfErr(deb.Close(), "close file %s", debFile)

console.Infof("Result: %s", debFile)
})

})
Expand Down
82 changes: 34 additions & 48 deletions internal/commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/dewep-online/deb-builder/pkg/archive"
"github.com/dewep-online/deb-builder/pkg/buffer"
"github.com/dewep-online/deb-builder/pkg/hash"
"github.com/dewep-online/deb-builder/pkg/packages"
"github.com/dewep-online/deb-builder/pkg/utils"
Expand All @@ -21,13 +22,14 @@ import (
)

const (
PathMainPool = "/pool/main/"
PathDistStable = "/dists/stable/"
PathDistMain = "/dists/stable/main/"
PathDistBinAmd64 = "/dists/stable/main/binary-amd64/"
PathDistBinArm64 = "/dists/stable/main/binary-arm64/"
PathMainPool = "/pool/main/"
PathDistStable = "/dists/stable/"
PathDistMain = "/dists/stable/main/"
PathDistBin = "/dists/stable/main/binary-%s/"
)

var archs = []string{"i386", "amd64", "arm", "arm64"}

func GenerateRelease() console.CommandGetter {
return console.NewCommand(func(setter console.CommandSetter) {
setter.Setup("release", "Generate deb repository release")
Expand Down Expand Up @@ -56,8 +58,8 @@ func GenerateRelease() console.CommandGetter {
Validate dirs
*/

dirs := []string{PathMainPool, PathDistStable, PathDistMain, PathDistBinAmd64, PathDistBinArm64}
for _, dir := range dirs {
for _, arch := range archs {
dir := fmt.Sprintf(PathDistBin, arch)
console.FatalIfErr(os.MkdirAll(path+dir, 0755), "validate dirs")
}

Expand Down Expand Up @@ -126,39 +128,40 @@ func GenerateRelease() console.CommandGetter {
Release
*/

amd64pkg := &bytes.Buffer{}
arm64pkg := &bytes.Buffer{}
pkgBuffer := make(map[string]*buffer.Buffer)
for _, v := range archs {
pkgBuffer[v] = buffer.New(v)
}

for _, pkg := range pkgs {
pkgInfo, err0 := pkg.Encode()
console.FatalIfErr(err0, "encode package")
pkgInfo = append(pkgInfo, []byte("\n\n")...)

switch pkg.Architecture {
case "amd64":
_, err = amd64pkg.Write(pkgInfo)
console.FatalIfErr(err, "write amd64 package")
case "arm64":
_, err = arm64pkg.Write(pkgInfo)
console.FatalIfErr(err, "write arm64 package")
case "all":
_, err = arm64pkg.Write(pkgInfo)
console.FatalIfErr(err, "write arm64 package")
_, err = amd64pkg.Write(pkgInfo)
console.FatalIfErr(err, "write amd64 package")
if pkg.Architecture == "all" {
for _, arch := range archs {
pkgBuffer[arch].Write(pkgInfo)
}
} else {
if pb, ok := pkgBuffer[pkg.Architecture]; ok {
pb.Write(pkgInfo)
}
}
}

err = os.WriteFile(path+PathDistBinAmd64+"Packages", amd64pkg.Bytes(), 0755)
console.FatalIfErr(err, "write amd64 Packages")
err = archive.GZWriteFile(path+PathDistBinAmd64+"Packages.gz", amd64pkg.Bytes(), 0755)
console.FatalIfErr(err, "write amd64 Packages.gz")
err = os.WriteFile(path+PathDistBinArm64+"Packages", arm64pkg.Bytes(), 0755)
console.FatalIfErr(err, "write arm64 Packages")
err = archive.GZWriteFile(path+PathDistBinArm64+"Packages.gz", arm64pkg.Bytes(), 0755)
console.FatalIfErr(err, "write arm64 Packages.gz")
inRelease := []string{}
for _, arch := range archs {
dir := fmt.Sprintf(PathDistBin, arch)
inRelease = append(inRelease, path+dir+"Packages", path+dir+"Packages.gz")

err = os.WriteFile(path+dir+"Packages", pkgBuffer[arch].Bytes(), 0755)
console.FatalIfErr(err, "write amd64 Packages")
err = archive.GZWriteFile(path+dir+"Packages.gz", pkgBuffer[arch].Bytes(), 0755)
console.FatalIfErr(err, "write amd64 Packages.gz")
}

for osArch, osArchPath := range map[string]string{"amd64": PathDistBinAmd64, "arm64": PathDistBinArm64} {
for _, osArch := range archs {
osArchPath := fmt.Sprintf(PathDistBin, osArch)
releasePkg := packages.ReleaseModel{
Component: "main",
Origin: origin,
Expand All @@ -177,22 +180,13 @@ func GenerateRelease() console.CommandGetter {
InRelease
*/

inRelease := []string{
path + PathDistBinAmd64 + "Packages",
path + PathDistBinAmd64 + "Packages.gz",
path + PathDistBinAmd64 + "Release",
path + PathDistBinArm64 + "Packages",
path + PathDistBinArm64 + "Packages.gz",
path + PathDistBinArm64 + "Release",
}

inReleaseModel := &packages.InReleaseModel{
Origin: origin,
Label: label,
Component: "main",
Codename: "stable",
Date: time.Now().UTC().Format(time.RFC1123),
Architectures: "amd64 arm64",
Architectures: "i386 amd64 arm arm64",
Description: "Packages for Ubuntu and Debian",
MD5Sum: "",
SHA1: "",
Expand Down Expand Up @@ -245,14 +239,6 @@ deb [arch=amd64] https://yourdomain/ stable main
EOF
$ sudo apt-get update

=========================== arm64 ===========================

$ wget -qO - https://yourdomain/key.gpg | sudo apt-key add -
$ sudo tee /etc/apt/sources.list.d/yourdomain.list <<'EOF'
deb [arch=arm64] https://yourdomain/ stable main
EOF
$ sudo apt-get update

`

console.Infof(info)
Expand Down
28 changes: 28 additions & 0 deletions pkg/buffer/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package buffer

import (
"bytes"

"github.com/deweppro/go-app/console"
)

type Buffer struct {
a string
b *bytes.Buffer
}

func New(arch string) *Buffer {
return &Buffer{
a: arch,
b: &bytes.Buffer{},
}
}

func (v *Buffer) Write(b []byte) {
_, err := v.b.Write(b)
console.FatalIfErr(err, "write %s package", v.a)
}

func (v *Buffer) Bytes() []byte {
return v.b.Bytes()
}
Loading