feat: allow enabling of each service individually

Signed-off-by: jolheiser <git@jolheiser.com>
tui
jolheiser 2024-07-26 11:44:28 -05:00
parent 9a15c8a3dd
commit 519db4565f
No known key found for this signature in database
3 changed files with 52 additions and 25 deletions

View File

@ -21,6 +21,7 @@ type cliArgs struct {
} }
type sshArgs struct { type sshArgs struct {
Enable bool
AuthorizedKeys string AuthorizedKeys string
CloneURL string CloneURL string
Port int Port int
@ -28,6 +29,7 @@ type sshArgs struct {
} }
type httpArgs struct { type httpArgs struct {
Enable bool
CloneURL string CloneURL string
Port int Port int
} }
@ -54,6 +56,7 @@ type logArgs struct {
} }
type tailscaleArgs struct { type tailscaleArgs struct {
Enable bool
Hostname string Hostname string
DataDir string DataDir string
} }
@ -65,12 +68,14 @@ func parseArgs(args []string) (c cliArgs, e error) {
c = cliArgs{ c = cliArgs{
RepoDir: ".ugit", RepoDir: ".ugit",
SSH: sshArgs{ SSH: sshArgs{
Enable: true,
AuthorizedKeys: ".ssh/authorized_keys", AuthorizedKeys: ".ssh/authorized_keys",
CloneURL: "ssh://localhost:8448", CloneURL: "ssh://localhost:8448",
Port: 8448, Port: 8448,
HostKey: ".ssh/ugit_ed25519", HostKey: ".ssh/ugit_ed25519",
}, },
HTTP: httpArgs{ HTTP: httpArgs{
Enable: true,
CloneURL: "http://localhost:8449", CloneURL: "http://localhost:8449",
Port: 8449, Port: 8449,
}, },
@ -82,6 +87,7 @@ func parseArgs(args []string) (c cliArgs, e error) {
Level: log.InfoLevel, Level: log.InfoLevel,
}, },
Tailscale: tailscaleArgs{ Tailscale: tailscaleArgs{
Enable: false,
Hostname: "ugit", Hostname: "ugit",
DataDir: ".tsnet", DataDir: ".tsnet",
}, },
@ -97,10 +103,12 @@ func parseArgs(args []string) (c cliArgs, e error) {
}) })
fs.BoolVar(&c.Log.JSON, "log.json", c.Log.JSON, "Print logs in JSON(L) format") fs.BoolVar(&c.Log.JSON, "log.json", c.Log.JSON, "Print logs in JSON(L) format")
fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories") fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories")
fs.BoolVar(&c.SSH.Enable, "ssh.enable", c.SSH.Enable, "Enable SSH server")
fs.StringVar(&c.SSH.AuthorizedKeys, "ssh.authorized-keys", c.SSH.AuthorizedKeys, "Path to authorized_keys") fs.StringVar(&c.SSH.AuthorizedKeys, "ssh.authorized-keys", c.SSH.AuthorizedKeys, "Path to authorized_keys")
fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base") fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base")
fs.IntVar(&c.SSH.Port, "ssh.port", c.SSH.Port, "SSH port") fs.IntVar(&c.SSH.Port, "ssh.port", c.SSH.Port, "SSH port")
fs.StringVar(&c.SSH.HostKey, "ssh.host-key", c.SSH.HostKey, "SSH host key (created if it doesn't exist)") fs.StringVar(&c.SSH.HostKey, "ssh.host-key", c.SSH.HostKey, "SSH host key (created if it doesn't exist)")
fs.BoolVar(&c.HTTP.Enable, "http.enable", c.HTTP.Enable, "Enable HTTP server")
fs.StringVar(&c.HTTP.CloneURL, "http.clone-url", c.HTTP.CloneURL, "HTTP clone URL base") fs.StringVar(&c.HTTP.CloneURL, "http.clone-url", c.HTTP.CloneURL, "HTTP clone URL base")
fs.IntVar(&c.HTTP.Port, "http.port", c.HTTP.Port, "HTTP port") fs.IntVar(&c.HTTP.Port, "http.port", c.HTTP.Port, "HTTP port")
fs.StringVar(&c.Meta.Title, "meta.title", c.Meta.Title, "App title") fs.StringVar(&c.Meta.Title, "meta.title", c.Meta.Title, "App title")
@ -118,6 +126,7 @@ func parseArgs(args []string) (c cliArgs, e error) {
}) })
return nil return nil
}) })
fs.BoolVar(&c.Tailscale.Enable, "tailscale.enable", c.Tailscale.Enable, "Enable Tailscale")
fs.StringVar(&c.Tailscale.Hostname, "tailscale.hostname", c.Tailscale.Hostname, "Tailscale host to show private repos on") fs.StringVar(&c.Tailscale.Hostname, "tailscale.hostname", c.Tailscale.Hostname, "Tailscale host to show private repos on")
fs.StringVar(&c.Tailscale.DataDir, "tailscale.data-dir", c.Tailscale.DataDir, "Tailscale data/state directory") fs.StringVar(&c.Tailscale.DataDir, "tailscale.data-dir", c.Tailscale.DataDir, "Tailscale data/state directory")

View File

@ -62,23 +62,25 @@ func main() {
panic(err) panic(err)
} }
sshSettings := ssh.Settings{ if args.SSH.Enable {
AuthorizedKeys: args.SSH.AuthorizedKeys, sshSettings := ssh.Settings{
CloneURL: args.SSH.CloneURL, AuthorizedKeys: args.SSH.AuthorizedKeys,
Port: args.SSH.Port, CloneURL: args.SSH.CloneURL,
HostKey: args.SSH.HostKey, Port: args.SSH.Port,
RepoDir: args.RepoDir, HostKey: args.SSH.HostKey,
} RepoDir: args.RepoDir,
sshSrv, err := ssh.New(sshSettings) }
if err != nil { sshSrv, err := ssh.New(sshSettings)
panic(err) if err != nil {
}
go func() {
log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
if err := sshSrv.ListenAndServe(); err != nil {
panic(err) panic(err)
} }
}() go func() {
log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
if err := sshSrv.ListenAndServe(); err != nil {
panic(err)
}
}()
}
httpSettings := http.Settings{ httpSettings := http.Settings{
Title: args.Meta.Title, Title: args.Meta.Title,
@ -98,15 +100,17 @@ func main() {
URL: link.URL, URL: link.URL,
}) })
} }
httpSrv := http.New(httpSettings) if args.HTTP.Enable {
go func() { httpSrv := http.New(httpSettings)
log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port) go func() {
if err := httpSrv.ListenAndServe(); err != nil { log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
panic(err) if err := httpSrv.ListenAndServe(); err != nil {
} panic(err)
}() }
}()
}
if _, ok := os.LookupEnv("TS_AUTHKEY"); ok { if args.Tailscale.Enable {
tailnetSettings := httpSettings tailnetSettings := httpSettings
tailnetSettings.ShowPrivate = true tailnetSettings.ShowPrivate = true
tailnetSrv := http.New(tailnetSettings) tailnetSrv := http.New(tailnetSettings)

View File

@ -76,7 +76,9 @@
configFile = pkgs.writeText "ugit.yaml" (builtins.readFile (yamlFormat.generate "ugit-yaml" cfg.config)); configFile = pkgs.writeText "ugit.yaml" (builtins.readFile (yamlFormat.generate "ugit-yaml" cfg.config));
authorizedKeysFile = pkgs.writeText "ugit_keys" (builtins.concatStringsSep "\n" cfg.authorizedKeys); authorizedKeysFile = pkgs.writeText "ugit_keys" (builtins.concatStringsSep "\n" cfg.authorizedKeys);
in { in {
options = with lib; { options = let
inherit (lib) mkEnableOption mkOption types;
in {
services.ugit = { services.ugit = {
enable = mkEnableOption "Enable ugit"; enable = mkEnableOption "Enable ugit";
@ -86,6 +88,12 @@
default = ugit; default = ugit;
}; };
tsAuthKey = mkOption {
type = types.str;
description = "Tailscale one-time auth-key";
default = "";
};
repoDir = mkOption { repoDir = mkOption {
type = types.str; type = types.str;
description = "where ugit stores repositories"; description = "where ugit stores repositories";
@ -155,7 +163,12 @@
if (builtins.length cfg.authorizedKeys) > 0 if (builtins.length cfg.authorizedKeys) > 0
then authorizedKeysFile then authorizedKeysFile
else cfg.authorizedKeysFile; else cfg.authorizedKeysFile;
args = ["--config=${configFile}" "--repo-dir=${cfg.repoDir}" "--ssh.authorized-keys=${authorizedKeysPath}" "--ssh.host-key=${cfg.hostKeyFile}"]; args = [
"--config=${configFile}"
"--repo-dir=${cfg.repoDir}"
"--ssh.authorized-keys=${authorizedKeysPath}"
"--ssh.host-key=${cfg.hostKeyFile}"
];
in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}"; in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
after = ["network.target"]; after = ["network.target"];
@ -166,6 +179,7 @@
Restart = "always"; Restart = "always";
RestartSec = "15"; RestartSec = "15";
WorkingDirectory = "/var/lib/ugit"; WorkingDirectory = "/var/lib/ugit";
Environment = ["TS_AUTHKEY=${cfg.tsAuthKey}"];
}; };
}; };
}; };