Skip to content

Commit

Permalink
add "Run your own ziglang.org/download mirror" docs
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Jun 8, 2024
1 parent 8a57d2a commit 89959f0
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 16 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,40 @@ This will give you a URL you can visit to add the bot to your server.
2. Download release binary, rename to `.exe`
3. Place at `C:/wrench.exe` or some other location. Put on PATH if desired.
4. In admin terminal run `wrench.exe setup`

## Run your own ziglang.org/download mirror

If you want to mirror https://ziglang.org/download on-demand, similar to what https://pkg.machengine.org does,
you may do so using e.g. this `config.toml` with `Mode = "zig"` which disables all other Wrench functionality so it only mirrors Zig downloads:

```toml
# Note: data will be written in a directory relative to this config file.
Mode = "zig"

# HTTP configuration
ExternalURL = "http://foobar.com"
Address = ":80"

# HTTPS configuration (optional, uses LetsEncrypt)
#ExternalURL = "https://foobar.com"
#Address = ":443"
#LetsEncryptEmail = "[email protected]"
```

Wrench will save data relative to that config file, so generally you should put that `config.toml` into e.g. a `wrench/` directory somewhere.

Running `wrench svc run` will start the server. Then you can fetch e.g.:

* http://localhost/
* http://localhost/zig/zig-linux-x86_64-0.13.0.tar.xz
* http://localhost/zig/index.json - a strict superset of https://ziglang.org/download/index.json

Downloads like http://localhost/zig/zig-linux-x86_64-0.13.0.tar.xz will be fetched on-demand from ziglang.org and then cached on the local filesystem forever after that.

http://localhost/zig/index.json is like https://ziglang.org/download/index.json with some small differences:

* It is fetched from ziglang.org once every 15 minutes and cached in-memory.
* Entries from https://machengine.org/zig/index.json are added so the index.json _additionally_ contains Mach [nominated Zig versions](https://machengine.org/about/nominated-zig/)
* `tarball` fields are rewritten to point to the configured `ExternalURL`

If you want to run Wrench as a system service, have it auto-start after reboot, etc. then you can e.g. put the config file in `/root/wrench/config.toml`, run `wrench svc install` as root to install the systemd service, use `wrench svc start` to start the service, and `wrench svc status` to see the status and log file locations.
7 changes: 5 additions & 2 deletions internal/wrench/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (b *Bot) loadConfig() error {
return errors.New("expected Config or ConfigFile to be specified")
}
b.Config = &Config{}
if absPath, err := filepath.Abs(b.ConfigFile); err == nil {
b.logf("loading config file: %s", absPath)
}
return LoadConfig(b.ConfigFile, b.Config)
}
return nil
Expand Down Expand Up @@ -120,7 +123,7 @@ func (b *Bot) run(s service.Service) error {
}

if b.Config.Runner == "" {
if !b.Config.PkgProxy {
if b.Config.ModeType() == ModeWrench {
b.store, err = OpenStore(filepath.Join(b.Config.WrenchDir, "wrench.db") + "?_pragma=busy_timeout%3d10000")
if err != nil {
return errors.Wrap(err, "OpenStore")
Expand All @@ -135,7 +138,7 @@ func (b *Bot) run(s service.Service) error {
if err := b.httpStart(); err != nil {
return errors.Wrap(err, "http")
}
if !b.Config.PkgProxy {
if b.Config.ModeType() == ModeWrench {
if err := b.schedulerStart(); err != nil {
return errors.Wrap(err, "scheduler")
}
Expand Down
62 changes: 53 additions & 9 deletions internal/wrench/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"github.com/hexops/wrench/internal/wrench/api"
)

type ModeType string

const (
ModeWrench ModeType = "wrench"
ModePkg ModeType = "pkg"
ModeZig ModeType = "zig"
)

type Config struct {
// ExternalURL where Wrench is hosted, if any.
ExternalURL string
Expand All @@ -21,61 +29,97 @@ type Config struct {
// Act as a Zig package proxy like pkg.machengine.org, instead of as a regular wrench server.
PkgProxy bool `toml:"PkgProxy,omitempty"`

// Mode to operate in, one of:
//
// * "wrench" -> https://wrench.machengine.org - custom CI system, etc.
// * "pkg" -> https://pkg.machengine.org - package mirror and Zig download mirror
// * "zig" -> Zig download mirror only (subset of pkg.machengine.org)
Mode string `toml:"Mode,omitempty"`

// (optional) Directory for caching LetsEncrypt certificates
LetsEncryptCacheDir string `toml:"LetsEncryptCacheDir,omitempty"`

// (optional) Email to use for LetsEncrypt notifications
LetsEncryptEmail string `toml:"LetsEncryptEmail,omitempty"`

// Where Wrench should store its data, cofiguration, etc. Defaults to the directory containing
// this config file.
WrenchDir string `toml:"WrenchDir,omitempty"`

// All options below here are only used in "wrench" mode.

// (optional) Discord bot token. See README.md for details on how to create this.
//
// Disabled if an empty string.
//
// Only used in "wrench" mode.
DiscordBotToken string `toml:"DiscordBotToken,omitempty"`

// (required if DiscordBotToken is set) Discord guild/server ID to operate in.
//
// Find this via User Settings -> Advanced -> Enabled developer mode, then right-click on any
// server and Copy ID)
//
// Only used in "wrench" mode.
DiscordGuildID string `toml:"DiscordGuildID,omitempty"`

// (optional) Discord channel name for Wrench to send messages in. Defaults to "wrench"
//
// Only used in "wrench" mode.
DiscordChannel string `toml:"DiscordChannel,omitempty"`

// (optional) Discord channel name for Wrench to relay all Discord messages to. Defaults to "disabled"
//
// Only used in "wrench" mode.
ActivityChannel string `toml:"ActivityChannel,omitempty"`

// (optional) Directory for caching LetsEncrypt certificates
LetsEncryptCacheDir string `toml:"LetsEncryptCacheDir,omitempty"`

// (optional) Email to use for LetsEncrypt notifications
LetsEncryptEmail string `toml:"LetsEncryptEmail,omitempty"`

// (optional) When specified, this is an arbitrary secret of your choosing which can be used to
// send GitHub webhook events from the github.com/hexops/wrench repository itself to Wrench. It
// will respond to these by recompiling and launching itself:
//
// The webhook URL should be: /webhook/github/self
//
// Only used in "wrench" mode.
GitHubWebHookSecret string `toml:"GitHubWebHookSecret,omitempty"`

// (optional) When specified Wrench can send PRs and assist with GitHub.
//
// Only applicable if running as the Wrench server.
//
// Only used in "wrench" mode.
GitHubAccessToken string `toml:"GitHubAccessToken,omitempty"`

// (optional) When specified wrench runners can push to Git using this configuration.
// It is suggested to use a limited push access token for the password since it will
// be distributed to all runners.
//
// Only applicable if running as the Wrench server.
//
// Only used in "wrench" mode.
GitPushUsername string `toml:"GitPushUsername,omitempty"`
GitPushPassword string `toml:"GitPushPassword,omitempty"`
GitConfigUserName string `toml:"GitConfigUserName,omitempty"`
GitConfigUserEmail string `toml:"GitConfigUserEmail,omitempty"`

// (optional) Generic secret used to authenticate with this server. Any arbitrary string.
//
// Only used in "wrench" mode.
Secret string `toml:"Secret,omitempty"`

// (optional) Act as a runner, connecting to the root Wrench server specified in ExternalURL.
//
// Only used in "wrench" mode.
Runner string `toml:"Runner,omitempty"`
}

// Where Wrench should store its data, cofiguration, etc. Defaults to the directory containing
// this config file.
WrenchDir string `toml:"WrenchDir,omitempty"`
func (c *Config) ModeType() ModeType {
if c.Mode != "" {
return ModeType(c.Mode)
}
if c.PkgProxy {
return ModePkg
}
return ModeWrench
}

func (c *Config) LogFilePath() string {
Expand Down
12 changes: 9 additions & 3 deletions internal/wrench/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ func (b *Bot) httpStart() error {
}

var mux http.Handler
if b.Config.PkgProxy {
b.logf("http: PkgProxy mode enabled")
if b.Config.ModeType() == ModePkg {
b.logf("http: pkg mirror mode enabled")
mux = b.httpMuxPkgProxy(handler)
} else {
} else if b.Config.ModeType() == ModeZig {
b.logf("http: zig mirror mode enabled")
mux = b.httpMuxPkgProxy(handler)
} else if b.Config.ModeType() == ModeWrench {
b.logf("http: wrench mode enabled")
mux = b.httpMuxDefault(handler)
} else {
b.logf("invalid config mode=%q", b.Config.ModeType())
}

b.logf("http: listening on %v - %v", b.Config.Address, b.Config.ExternalURL)
Expand Down
Loading

0 comments on commit 89959f0

Please sign in to comment.