2023-02-24 22:19:13 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2023-02-24 21:22:21 +00:00
|
|
|
// Package unixpkgs contains dist Targets for building unix Tailscale packages.
|
|
|
|
package unixpkgs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-03 20:00:45 +00:00
|
|
|
"github.com/goreleaser/nfpm/v2"
|
|
|
|
"github.com/goreleaser/nfpm/v2/files"
|
2023-02-24 21:22:21 +00:00
|
|
|
"tailscale.com/release/dist"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tgzTarget struct {
|
2023-08-03 22:27:06 +00:00
|
|
|
filenameArch string // arch to use in filename instead of deriving from goEnv["GOARCH"]
|
|
|
|
goEnv map[string]string
|
2023-08-24 21:36:47 +00:00
|
|
|
signer dist.Signer
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tgzTarget) arch() string {
|
|
|
|
if t.filenameArch != "" {
|
|
|
|
return t.filenameArch
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOARCH"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tgzTarget) os() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOOS"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tgzTarget) String() string {
|
|
|
|
return fmt.Sprintf("%s/%s/tgz", t.os(), t.arch())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tgzTarget) Build(b *dist.Build) ([]string, error) {
|
|
|
|
var filename string
|
2023-08-03 22:27:06 +00:00
|
|
|
if t.goEnv["GOOS"] == "linux" {
|
2023-02-24 21:22:21 +00:00
|
|
|
// Linux used to be the only tgz architecture, so we didn't put the OS
|
|
|
|
// name in the filename.
|
|
|
|
filename = fmt.Sprintf("tailscale_%s_%s.tgz", b.Version.Short, t.arch())
|
|
|
|
} else {
|
|
|
|
filename = fmt.Sprintf("tailscale_%s_%s_%s.tgz", b.Version.Short, t.os(), t.arch())
|
|
|
|
}
|
2023-08-21 23:37:54 +00:00
|
|
|
if err := b.BuildWebClientAssets(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
ts, err := b.BuildGoBinary("tailscale.com/cmd/tailscale", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
tsd, err := b.BuildGoBinary("tailscale.com/cmd/tailscaled", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Building %s", filename)
|
|
|
|
|
|
|
|
out := filepath.Join(b.Out, filename)
|
|
|
|
f, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2023-08-24 21:36:47 +00:00
|
|
|
gw := gzip.NewWriter(f)
|
2023-02-24 21:22:21 +00:00
|
|
|
defer gw.Close()
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
addFile := func(src, dst string, mode int64) error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: dst,
|
|
|
|
Size: fi.Size(),
|
|
|
|
Mode: mode,
|
2023-05-26 19:42:05 +00:00
|
|
|
ModTime: b.Time,
|
2023-02-24 21:22:21 +00:00
|
|
|
Uid: 0,
|
|
|
|
Gid: 0,
|
|
|
|
Uname: "root",
|
|
|
|
Gname: "root",
|
|
|
|
}
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(tw, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
addDir := func(name string) error {
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: name + "/",
|
|
|
|
Mode: 0755,
|
2023-05-26 19:42:05 +00:00
|
|
|
ModTime: b.Time,
|
2023-02-24 21:22:21 +00:00
|
|
|
Uid: 0,
|
|
|
|
Gid: 0,
|
|
|
|
Uname: "root",
|
|
|
|
Gname: "root",
|
|
|
|
}
|
|
|
|
return tw.WriteHeader(hdr)
|
|
|
|
}
|
|
|
|
dir := strings.TrimSuffix(filename, ".tgz")
|
|
|
|
if err := addDir(dir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := addFile(tsd, filepath.Join(dir, "tailscaled"), 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := addFile(ts, filepath.Join(dir, "tailscale"), 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.os() == "linux" {
|
|
|
|
dir = filepath.Join(dir, "systemd")
|
|
|
|
if err := addDir(dir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tailscaledDir, err := b.GoPkg("tailscale.com/cmd/tailscaled")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := addFile(filepath.Join(tailscaledDir, "tailscaled.service"), filepath.Join(dir, "tailscaled.service"), 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := addFile(filepath.Join(tailscaledDir, "tailscaled.defaults"), filepath.Join(dir, "tailscaled.defaults"), 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := gw.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-31 22:47:00 +00:00
|
|
|
files := []string{filename}
|
|
|
|
|
|
|
|
if t.signer != nil {
|
2023-08-24 21:36:47 +00:00
|
|
|
outSig := out + ".sig"
|
|
|
|
if err := t.signer.SignFile(out, outSig); err != nil {
|
2023-07-31 22:47:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 21:36:47 +00:00
|
|
|
files = append(files, filepath.Base(outSig))
|
2023-07-31 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type debTarget struct {
|
2023-08-03 22:27:06 +00:00
|
|
|
goEnv map[string]string
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *debTarget) os() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOOS"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *debTarget) arch() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOARCH"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *debTarget) String() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return fmt.Sprintf("linux/%s/deb", t.goEnv["GOARCH"])
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *debTarget) Build(b *dist.Build) ([]string, error) {
|
|
|
|
if t.os() != "linux" {
|
|
|
|
return nil, errors.New("deb only supported on linux")
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:37:54 +00:00
|
|
|
if err := b.BuildWebClientAssets(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
ts, err := b.BuildGoBinary("tailscale.com/cmd/tailscale", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
tsd, err := b.BuildGoBinary("tailscale.com/cmd/tailscaled", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tailscaledDir, err := b.GoPkg("tailscale.com/cmd/tailscaled")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoDir, err := b.GoPkg("tailscale.com")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
arch := debArch(t.arch())
|
2023-08-03 20:00:45 +00:00
|
|
|
contents, err := files.PrepareForPackager(files.Contents{
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: ts,
|
|
|
|
Destination: "/usr/bin/tailscale",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: tsd,
|
|
|
|
Destination: "/usr/sbin/tailscaled",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: filepath.Join(tailscaledDir, "tailscaled.service"),
|
|
|
|
Destination: "/lib/systemd/system/tailscaled.service",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeConfigNoReplace,
|
|
|
|
Source: filepath.Join(tailscaledDir, "tailscaled.defaults"),
|
|
|
|
Destination: "/etc/default/tailscaled",
|
|
|
|
},
|
|
|
|
}, 0, "deb", false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-24 21:22:21 +00:00
|
|
|
info := nfpm.WithDefaults(&nfpm.Info{
|
|
|
|
Name: "tailscale",
|
|
|
|
Arch: arch,
|
|
|
|
Platform: "linux",
|
|
|
|
Version: b.Version.Short,
|
|
|
|
Maintainer: "Tailscale Inc <info@tailscale.com>",
|
|
|
|
Description: "The easiest, most secure, cross platform way to use WireGuard + oauth2 + 2FA/SSO",
|
|
|
|
Homepage: "https://www.tailscale.com",
|
|
|
|
License: "MIT",
|
|
|
|
Section: "net",
|
|
|
|
Priority: "extra",
|
|
|
|
Overridables: nfpm.Overridables{
|
2023-08-03 20:00:45 +00:00
|
|
|
Contents: contents,
|
2023-02-24 21:22:21 +00:00
|
|
|
Scripts: nfpm.Scripts{
|
|
|
|
PostInstall: filepath.Join(repoDir, "release/deb/debian.postinst.sh"),
|
|
|
|
PreRemove: filepath.Join(repoDir, "release/deb/debian.prerm.sh"),
|
|
|
|
PostRemove: filepath.Join(repoDir, "release/deb/debian.postrm.sh"),
|
|
|
|
},
|
|
|
|
Depends: []string{"iptables", "iproute2"},
|
|
|
|
Recommends: []string{"tailscale-archive-keyring (>= 1.35.181)"},
|
|
|
|
Replaces: []string{"tailscale-relay"},
|
|
|
|
Conflicts: []string{"tailscale-relay"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
pkg, err := nfpm.Get("deb")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := fmt.Sprintf("tailscale_%s_%s.deb", b.Version.Short, arch)
|
|
|
|
log.Printf("Building %s", filename)
|
|
|
|
f, err := os.Create(filepath.Join(b.Out, filename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := pkg.Package(info, f); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{filename}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type rpmTarget struct {
|
2023-08-03 22:27:06 +00:00
|
|
|
goEnv map[string]string
|
2023-08-24 21:36:47 +00:00
|
|
|
signer dist.Signer
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rpmTarget) os() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOOS"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rpmTarget) arch() string {
|
2023-08-03 22:27:06 +00:00
|
|
|
return t.goEnv["GOARCH"]
|
2023-02-24 21:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rpmTarget) String() string {
|
|
|
|
return fmt.Sprintf("linux/%s/rpm", t.arch())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rpmTarget) Build(b *dist.Build) ([]string, error) {
|
|
|
|
if t.os() != "linux" {
|
|
|
|
return nil, errors.New("rpm only supported on linux")
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:37:54 +00:00
|
|
|
if err := b.BuildWebClientAssets(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
ts, err := b.BuildGoBinary("tailscale.com/cmd/tailscale", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 22:27:06 +00:00
|
|
|
tsd, err := b.BuildGoBinary("tailscale.com/cmd/tailscaled", t.goEnv)
|
2023-02-24 21:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tailscaledDir, err := b.GoPkg("tailscale.com/cmd/tailscaled")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoDir, err := b.GoPkg("tailscale.com")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
arch := rpmArch(t.arch())
|
2023-08-03 20:00:45 +00:00
|
|
|
contents, err := files.PrepareForPackager(files.Contents{
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: ts,
|
|
|
|
Destination: "/usr/bin/tailscale",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: tsd,
|
|
|
|
Destination: "/usr/sbin/tailscaled",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeFile,
|
|
|
|
Source: filepath.Join(tailscaledDir, "tailscaled.service"),
|
|
|
|
Destination: "/lib/systemd/system/tailscaled.service",
|
|
|
|
},
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeConfigNoReplace,
|
|
|
|
Source: filepath.Join(tailscaledDir, "tailscaled.defaults"),
|
|
|
|
Destination: "/etc/default/tailscaled",
|
|
|
|
},
|
|
|
|
// SELinux policy on e.g. CentOS 8 forbids writing to /var/cache.
|
|
|
|
// Creating an empty directory at install time resolves this issue.
|
|
|
|
&files.Content{
|
|
|
|
Type: files.TypeDir,
|
|
|
|
Destination: "/var/cache/tailscale",
|
|
|
|
},
|
|
|
|
}, 0, "rpm", false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-24 21:22:21 +00:00
|
|
|
info := nfpm.WithDefaults(&nfpm.Info{
|
|
|
|
Name: "tailscale",
|
|
|
|
Arch: arch,
|
|
|
|
Platform: "linux",
|
|
|
|
Version: b.Version.Short,
|
|
|
|
Maintainer: "Tailscale Inc <info@tailscale.com>",
|
|
|
|
Description: "The easiest, most secure, cross platform way to use WireGuard + oauth2 + 2FA/SSO",
|
|
|
|
Homepage: "https://www.tailscale.com",
|
|
|
|
License: "MIT",
|
|
|
|
Overridables: nfpm.Overridables{
|
2023-08-03 20:00:45 +00:00
|
|
|
Contents: contents,
|
2023-02-24 21:22:21 +00:00
|
|
|
Scripts: nfpm.Scripts{
|
|
|
|
PostInstall: filepath.Join(repoDir, "release/rpm/rpm.postinst.sh"),
|
|
|
|
PreRemove: filepath.Join(repoDir, "release/rpm/rpm.prerm.sh"),
|
|
|
|
PostRemove: filepath.Join(repoDir, "release/rpm/rpm.postrm.sh"),
|
|
|
|
},
|
|
|
|
Depends: []string{"iptables", "iproute"},
|
|
|
|
Replaces: []string{"tailscale-relay"},
|
|
|
|
Conflicts: []string{"tailscale-relay"},
|
|
|
|
RPM: nfpm.RPM{
|
|
|
|
Group: "Network",
|
2023-08-03 22:27:06 +00:00
|
|
|
Signature: nfpm.RPMSignature{
|
|
|
|
PackageSignature: nfpm.PackageSignature{
|
2023-08-24 21:36:47 +00:00
|
|
|
SignFn: t.signer,
|
2023-08-03 22:27:06 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-24 21:22:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
pkg, err := nfpm.Get("rpm")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := fmt.Sprintf("tailscale_%s_%s.rpm", b.Version.Short, arch)
|
|
|
|
log.Printf("Building %s", filename)
|
|
|
|
|
|
|
|
f, err := os.Create(filepath.Join(b.Out, filename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := pkg.Package(info, f); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{filename}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// debArch returns the debian arch name for the given Go arch name.
|
|
|
|
// nfpm also does this translation internally, but we need to do it outside nfpm
|
|
|
|
// because we also need the filename to be correct.
|
|
|
|
func debArch(arch string) string {
|
|
|
|
switch arch {
|
|
|
|
case "386":
|
|
|
|
return "i386"
|
|
|
|
case "arm":
|
|
|
|
// TODO: this is supposed to be "armel" for GOARM=5, and "armhf" for
|
|
|
|
// GOARM=6 and 7. But we have some tech debt to pay off here before we
|
|
|
|
// can ship more than 1 ARM deb, so for now match redo's behavior of
|
|
|
|
// shipping armv5 binaries in an armv7 trenchcoat.
|
|
|
|
return "armhf"
|
2023-05-09 09:09:55 +00:00
|
|
|
case "mipsle":
|
|
|
|
return "mipsel"
|
|
|
|
case "mips64le":
|
|
|
|
return "mips64el"
|
2023-02-24 21:22:21 +00:00
|
|
|
default:
|
|
|
|
return arch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rpmArch returns the RPM arch name for the given Go arch name.
|
|
|
|
// nfpm also does this translation internally, but we need to do it outside nfpm
|
|
|
|
// because we also need the filename to be correct.
|
|
|
|
func rpmArch(arch string) string {
|
|
|
|
switch arch {
|
|
|
|
case "amd64":
|
|
|
|
return "x86_64"
|
|
|
|
case "386":
|
|
|
|
return "i386"
|
|
|
|
case "arm":
|
|
|
|
return "armv7hl"
|
|
|
|
case "arm64":
|
|
|
|
return "aarch64"
|
2023-05-09 09:09:55 +00:00
|
|
|
case "mipsle":
|
|
|
|
return "mipsel"
|
|
|
|
case "mips64le":
|
|
|
|
return "mips64el"
|
2023-02-24 21:22:21 +00:00
|
|
|
default:
|
|
|
|
return arch
|
|
|
|
}
|
|
|
|
}
|