Skip to content
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
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/socketmaster
/examples/childserver/childserver
/.vagrant

# Nix
result
result-*
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
===================

* Prefix output with the [pid]
* Set EINHORN_FDS to be eninhorn-compatible
* Set EINHORN_FDS to be einhorn-compatible
* Adding license and changelog
* Add a note about other related projects

Expand Down
74 changes: 74 additions & 0 deletions config.go
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
}
72 changes: 72 additions & 0 deletions config_test.go
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")
}
}
33 changes: 33 additions & 0 deletions environment.go
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
}
23 changes: 23 additions & 0 deletions environment_test.go
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")
}
}
42 changes: 42 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions flake.nix
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;
Copy link
Owner

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 :)


}
17 changes: 17 additions & 0 deletions go.mod
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
)
Loading