-
Notifications
You must be signed in to change notification settings - Fork 48
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
NixOS synergy + hot swap #18
Draft
roberth
wants to merge
15
commits into
zimbatm:master
Choose a base branch
from
hercules-ci:nixos
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
61f9bef
go mod init; tidy
roberth 0c92bb3
Add Nix boilerplate
roberth ddbd228
Typo
roberth 0f92304
Support children that accept systemd socket activation
roberth 9b860ff
Add basic NixOS module and test
roberth 525cda6
nix/package.nix: Add source filtering
roberth 863e1ea
Basic config file loading
roberth 22e9a17
nixos: Use config file
roberth d75818b
-config-file help: No plan to watch it, but rely on signal
roberth 5c88757
Load config before each exec
roberth 3a6a382
Manipulate environment with map
roberth bbf94ba
Get environment from config file
roberth f5eb94d
Add config_test.go
roberth f38dfa4
Test command and environment update
roberth 21b0cac
WIP process state
roberth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/socketmaster | ||
/examples/childserver/childserver | ||
/.vagrant | ||
|
||
# Nix | ||
result | ||
result-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"io/ioutil" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
/* | ||
flag.StringVar(&command, "command", "", "Program to start") | ||
flag.StringVar(&addr, "listen", "tcp://:8080", "Port on which to bind") | ||
flag.IntVar(&startTime, "start", 3000, "How long the new process takes to boot in millis") | ||
flag.BoolVar(&useSyslog, "syslog", false, "Log to syslog") | ||
flag.StringVar(&username, "user", "", "run the command as this user") | ||
*/ | ||
|
||
// The mutable configuration items | ||
type Config struct { | ||
Command string `yaml:"command"` | ||
Environment map[string]string `yaml:"environment"` | ||
} | ||
|
||
func emptyConfig() Config { | ||
return Config{ | ||
Command: "", | ||
Environment: make(map[string]string), | ||
} | ||
} | ||
|
||
func (config *Config) LoadFile(path string) error { | ||
yamlFile, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
return config.LoadBytes(yamlFile) | ||
} | ||
|
||
func (config *Config) LoadBytes(yamlString []byte) error { | ||
return yaml.Unmarshal(yamlString, &config) | ||
} | ||
|
||
func (config *Config) LoadString(yamlString string) error { | ||
return config.LoadBytes([]byte(yamlString)) | ||
} | ||
|
||
// Some of the mutable flags can be set on the command line as well. | ||
func (config *Config) LinkToFlagSet(flags *flag.FlagSet) { | ||
flags.StringVar(&config.Command, "command", config.Command, "Program to start") | ||
} | ||
|
||
func (config *Config) Merge(other Config) error { | ||
empty := emptyConfig() | ||
|
||
if other.Command != empty.Command { | ||
if config.Command == empty.Command { | ||
config.Command = other.Command | ||
} else { | ||
return errors.New("command can only be set once.") | ||
} | ||
} | ||
|
||
if len(other.Environment) != 0 { | ||
if len(config.Environment) == 0 { | ||
config.Environment = other.Environment | ||
} else { | ||
// Can't be set via args, so impossible | ||
return errors.New("environment can only be set once.") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
func Test_Config_Empty(t *testing.T) { | ||
var config Config | ||
err := config.LoadBytes( | ||
[]byte(""), | ||
) | ||
if err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
if config.Command != "" { | ||
t.Fatalf("wrong Command value '%s'", config.Command) | ||
} | ||
} | ||
|
||
// func Test_Config_Marshal(t *testing.T) { | ||
// var config Config | ||
// config.Command = "hi" | ||
// config.Environment = make(map[string]string) | ||
// config.Environment["greeting"] = "hello" | ||
|
||
// out, err := yaml.Marshal(config) | ||
|
||
// if err != nil { | ||
// t.Fatal("unexpected error", err) | ||
// } | ||
|
||
// fmt.Println(string(out)) | ||
// t.Fatal("???") | ||
// } | ||
|
||
func Test_Config_Command(t *testing.T) { | ||
var config Config | ||
err := yaml.Unmarshal([]byte(`command: hi`), &config) | ||
// err := config.LoadString(`command: hi`) | ||
if err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
if config.Command != "hi" { | ||
t.Fatalf("wrong command value '%s'", config.Command) | ||
} | ||
} | ||
|
||
// environment | ||
func Test_Config_Environment(t *testing.T) { | ||
var config Config | ||
err := yaml.Unmarshal([]byte("command: hi\nenvironment:\n hello: world"), &config) | ||
// err := config.LoadString(`command: hi`) | ||
if err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
if config.Environment["hello"] != "world" { | ||
t.Fatalf("wrong environment.hello value '%s'", config.Environment["hello"]) | ||
} | ||
} | ||
|
||
func Test_Config_Merge_Environment(t *testing.T) { | ||
a := Config{Environment: map[string]string{}} | ||
b := Config{Environment: map[string]string{"foo": "bar"}} | ||
|
||
a.Merge(b) | ||
|
||
if a.Environment["foo"] != "bar" { | ||
t.Fatal("foo isn't bar") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import "strings" | ||
|
||
func EnvironmentToMap(envStrings []string) map[string]string { | ||
envMap := make(map[string]string) | ||
for _, envLine := range envStrings { | ||
|
||
// Get the variable name | ||
var name string | ||
|
||
var silly = strings.Split(envLine, "=") | ||
if len(silly) == 0 { | ||
// pathological, skip | ||
continue | ||
} | ||
|
||
name = silly[0] | ||
|
||
value := envLine[len(name)+1:] | ||
|
||
envMap[name] = value | ||
} | ||
return envMap | ||
} | ||
|
||
func MapToEnvironment(envMap map[string]string) []string { | ||
var envStrings []string | ||
for k, v := range envMap { | ||
envStrings = append(envStrings, k+"="+v) | ||
} | ||
return envStrings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func Test_EnvironmentToMap(t *testing.T) { | ||
m := EnvironmentToMap([]string{"a=b=c"}) | ||
if len(m) != 1 { | ||
t.Fatal("len") | ||
} | ||
if m["a"] != "b=c" { | ||
t.Fatal("a != b=c") | ||
} | ||
} | ||
|
||
func Test_MapToEnvironment(t *testing.T) { | ||
m := MapToEnvironment(map[string]string{"a": "b=c"}) | ||
if len(m) != 1 { | ||
t.Fatal("len") | ||
} | ||
if m[0] != "a=b=c" { | ||
t.Fatal("a=b=c") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
description = "A very basic flake"; | ||
|
||
inputs.nixpkgs.url = "github:NixOS/nixpkgs"; | ||
inputs.flake-utils.url = "github:numtide/flake-utils"; | ||
|
||
outputs = { self, nixpkgs, flake-utils }: | ||
let | ||
inherit (flake-utils.lib) eachDefaultSystem; | ||
inherit (nixpkgs) lib; | ||
|
||
flakeAttrs = { | ||
nixosModules.default = { lib, pkgs, ... }: | ||
let | ||
inherit (pkgs.stdenv.hostPlatform) system; | ||
in | ||
{ | ||
imports = [ ./nix/nixos/module.nix ]; | ||
config = { | ||
socketmaster.package = | ||
lib.mkDefault self.packages.${system}.default; | ||
}; | ||
}; | ||
}; | ||
|
||
perSystem = system: | ||
let | ||
pkgs = nixpkgs.legacyPackages.${system}; | ||
in | ||
{ | ||
devShell = self.devShells.${system}.default; | ||
devShells.default = pkgs.mkShell { | ||
nativeBuildInputs = [ | ||
pkgs.go | ||
pkgs.gopls | ||
pkgs.go-outline | ||
pkgs.nixpkgs-fmt | ||
]; | ||
}; | ||
|
||
defaultPackage = self.packages.${system}.default; | ||
packages.default = pkgs.callPackage ./nix/package.nix { }; | ||
checks = | ||
lib.optionalAttrs pkgs.stdenv.isLinux { | ||
nixos = pkgs.callPackage ./nix/nixos/test.nix { | ||
extraModules = [ self.nixosModules.default ]; | ||
}; | ||
}; | ||
}; | ||
|
||
systemAttrs = eachDefaultSystem perSystem; | ||
|
||
in | ||
systemAttrs // flakeAttrs; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/zimbatm/socketmaster | ||
|
||
go 1.17 | ||
|
||
require ( | ||
google.golang.org/grpc v1.45.0 | ||
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 | ||
) | ||
|
||
require ( | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect | ||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect | ||
golang.org/x/text v0.3.0 // indirect | ||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect | ||
google.golang.org/protobuf v1.26.0 // indirect | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reminds me of flake-parts :)