diff --git a/Dockerfile b/Dockerfile index 243e637..d905842 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM alpine -ADD aptly.sh /bin/ -RUN chmod +x /bin/aptly.sh -RUN apk -Uuv add curl ca-certificates bash jo -ENTRYPOINT /bin/aptly.sh +ADD aptly-publisher /bin/ +RUN chmod +x /bin/aptly-publisher +RUN apk -Uuv add ca-certificates libc6-compat +ENTRYPOINT /bin/aptly-publisher diff --git a/aptly-publisher b/aptly-publisher new file mode 100755 index 0000000..d3a4b3f Binary files /dev/null and b/aptly-publisher differ diff --git a/aptly.sh b/aptly.sh deleted file mode 100644 index 349426a..0000000 --- a/aptly.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -APT_SERVICE_API="https://apt.lumeweb.com" - -USERNAME="${PLUGIN_APT_USERNAME}" -PASSWORD="${PLUGIN_APT_PASSWORD}" - -SNAPSHOT_NAME=$(date -u -%s) - -PACKAGE_NAME="$(basename ${PLUGIN_PACKAGE} .deb)" - -curl -u "${USERNAME}:${PASSWORD}" -X POST -F file=@${PLUGIN_PACKAGE} "http://${APT_SERVICE_API}/api/files/${PACKAGE_NAME}" -curl -u "${USERNAME}:${PASSWORD}" -X POST -F file=@${PLUGIN_PACKAGE} "http://${APT_SERVICE_API}/api/repos/${PLUGIN_REPO}/file/${PACKAGE_NAME}" - -JSON=$(jo Name="${SNAPSHOT_NAME}" Passphrase="${PLUGIN_GPG_PASSWORD}") -curl -u "${USERNAME}:${PASSWORD}" ${APT_SERVICE_API}/api/repos/${PLUGIN_REPO}/snapshots -d "${JSON}" - -JSON=$(jo Storage="s3" Prefix="." Distribution="ubuntu" Architectures="amd64" Passphrase="${PLUGIN_GPG_PASSWORD}") -curl -u "${USERNAME}:${PASSWORD}" ${APT_SERVICE_API}/api/publish -d "${JSON}" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..191f333 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module aptly-publisher + +go 1.18 + +require github.com/go-resty/resty/v2 v2.7.0 + +require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect diff --git a/main.go b/main.go new file mode 100644 index 0000000..b3450d1 --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "github.com/go-resty/resty/v2" + "os" + "path/filepath" + "strings" +) + +const APT_SERVICE_API = "https://apt.lumeweb.com" + +func main() { + err := run() + + if err != nil { + fmt.Printf("%s", err) + } +} + +func run() error { + client := resty.New() + files, _ := filepath.Glob("*.deb") + pkg := files[0] + pkg_name := strings.TrimSuffix(pkg, filepath.Ext(pkg)) + pkg_parts := strings.Split(pkg_name, "_") + pkg_parts = pkg_parts[:len(pkg_parts)-1] + pkg_name = strings.Join(pkg_parts, "-") + + repo := os.Getenv("PLUGIN_REPO") + gpg_pass := os.Getenv("PLUGIN_GPG_PASSWORD") + distro := os.Getenv("PLUGIN_DISTRO") + folder := os.Getenv("PLUGIN_FOLDER") + + client.SetBasicAuth(os.Getenv("PLUGIN_APT_USERNAME"), os.Getenv("PLUGIN_APT_PASSWORD")).SetBaseURL(APT_SERVICE_API) + + resp, err := client.R(). + SetFile("file", pkg). + Post("/api/files/" + pkg_name) + if err != nil { + return err + } + + resp, err = client.R(). + Post("/api/repos/" + repo + "/file/" + pkg_name) + + if err != nil { + return err + } + resp, err = client.R(). + SetBody(map[string]interface{}{ + "Architectures": []string{"amd64"}, + "Passphrase": gpg_pass, + "SourceKind": "local", + "Signing": map[string]interface{}{ + "Batch": true, + "Passphrase": gpg_pass, + }, + }). + Put(fmt.Sprintf("/api/publish/s3:%s:%s/%s", repo, folder, distro)) + + if err != nil { + return err + } + + _ = resp + + return nil +}