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

Prepare persistent storage #13

Merged
merged 14 commits into from
Mar 27, 2024
19 changes: 9 additions & 10 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nlewo/comin/internal/deployment"
"github.com/nlewo/comin/internal/generation"
"github.com/nlewo/comin/internal/manager"
"github.com/nlewo/comin/internal/repository"
"github.com/nlewo/comin/internal/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -25,14 +24,14 @@ func generationStatus(g generation.Generation) {
fmt.Printf(" Status: initializated\n")
case generation.Evaluating:
fmt.Printf(" Status: evaluating (since %s)\n", humanize.Time(g.EvalStartedAt))
case generation.Evaluated:
case generation.EvaluationSucceeded:
fmt.Printf(" Status: evaluated (%s)\n", humanize.Time(g.EvalEndedAt))
case generation.Building:
fmt.Printf(" Status: building (since %s)\n", humanize.Time(g.BuildStartedAt))
case generation.Built:
case generation.BuildSucceeded:
fmt.Printf(" Status: built (%s)\n", humanize.Time(g.BuildEndedAt))
}
printCommit(g.RepositoryStatus)
printCommit(g.SelectedRemoteName, g.SelectedBranchName, g.SelectedCommitId, g.SelectedCommitMsg)
}

func deploymentStatus(d deployment.Deployment) {
Expand All @@ -48,17 +47,17 @@ func deploymentStatus(d deployment.Deployment) {
case deployment.Failed:
fmt.Printf(" Status: failed (%s)\n", humanize.Time(d.EndAt))
}
printCommit(d.Generation.RepositoryStatus)
printCommit(d.Generation.SelectedRemoteName, d.Generation.SelectedBranchName, d.Generation.SelectedCommitId, d.Generation.SelectedCommitMsg)
}

func printCommit(rs repository.RepositoryStatus) {
func printCommit(selectedRemoteName, selectedBranchName, selectedCommitId, selectedCommitMsg string) {
fmt.Printf(" Commit %s from '%s/%s'\n",
rs.SelectedCommitId,
rs.SelectedRemoteName,
rs.SelectedBranchName,
selectedCommitId,
selectedRemoteName,
selectedBranchName,
)
fmt.Printf(" %s\n",
utils.FormatCommitMsg(rs.SelectedCommitMsg),
utils.FormatCommitMsg(selectedCommitMsg),
)
}

Expand Down
24 changes: 7 additions & 17 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
overlay = final: prev: {
comin = final.buildGoModule rec {
pname = "comin";
version = "0.0.1";
version = "0.1.1";
nativeCheckInputs = [ final.git ];
src = final.lib.cleanSourceWith {
src = ./.;
Expand All @@ -26,7 +26,7 @@
p == "README.md"
);
};
vendorHash = "sha256-kyj0CbB3IfRvrNXsO9JEVYJ8Hr5e747i+ZKcbR6WfKM=";
vendorHash = "sha256-7rh1t3DkKfJvUOkPjdi2vqS8JTZpWtI61mTBKDHcPVk=";
buildInputs = [ final.makeWrapper ];
postInstall = ''
# This is because Nix needs Git at runtime by the go-git library
Expand All @@ -45,11 +45,7 @@
hostname = cfg.services.comin.hostname;
state_dir = "/var/lib/comin";
remotes = cfg.services.comin.remotes;
} // (
if cfg.services.comin.inotifyRepositoryPath != null
then { inotify.repository_path = cfg.services.comin.inotifyRepositoryPath; }
else { }
);
};
cominConfigYaml = yaml.generate "comin.yaml" cominConfig;
in {
options = with lib; with types; {
Expand All @@ -65,7 +61,10 @@
type = str;
default = config.networking.hostName;
description = ''
The hostname of the machine.
The name of the NixOS configuration to evaluate and
deploy. This value is used by comin to evaluate the
flake output
nixosConfigurations."<hostname>".config.system.build.toplevel
'';
};
remotes = mkOption {
Expand Down Expand Up @@ -170,15 +169,6 @@
Note it is only used by comin at evaluation.
'';
};
inotifyRepositoryPath = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The path of a local repository to watch. On each commit,
the worker is triggered to fetch new commits. This
allows to have fast switch when the repository is local.
'';
};
};
};
config = lib.mkIf cfg.services.comin.enable {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
33 changes: 32 additions & 1 deletion internal/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/google/uuid"
"github.com/nlewo/comin/internal/generation"
"github.com/sirupsen/logrus"
)
Expand All @@ -17,9 +18,38 @@ const (
Failed
)

func StatusToString(status Status) string {
switch status {
case Init:
return "init"
case Running:
return "running"
case Done:
return "done"
case Failed:
return "failed"
}
return ""
}

func StatusFromString(status string) Status {
switch status {
case "init":
return Init
case "running":
return Running
case "done":
return Done
case "failed":
return Failed
}
return Init
}

type DeployFunc func(context.Context, string, string, string) (bool, error)

type Deployment struct {
UUID string `json:"uuid"`
Generation generation.Generation `json:"generation"`
StartAt time.Time `json:"start_at"`
EndAt time.Time `json:"end_at"`
Expand All @@ -42,11 +72,12 @@ type DeploymentResult struct {

func New(g generation.Generation, deployerFunc DeployFunc, deploymentCh chan DeploymentResult) Deployment {
operation := "switch"
if g.RepositoryStatus.IsTesting() {
if g.SelectedBranchIsTesting {
operation = "test"
}

return Deployment{
UUID: uuid.NewString(),
Generation: g,
deployerFunc: deployerFunc,
deploymentCh: deploymentCh,
Expand Down
Loading
Loading