Skip to content

Commit

Permalink
Merge pull request #63 from nlewo/stage
Browse files Browse the repository at this point in the history
Huge refactoring
  • Loading branch information
nlewo authored Jan 22, 2025
2 parents 9afca85 + 7e1427f commit 773f614
Show file tree
Hide file tree
Showing 31 changed files with 1,607 additions and 1,121 deletions.
29 changes: 29 additions & 0 deletions cmd/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"net/http"
"time"

"github.com/spf13/cobra"
)

var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Trigger a fetch of all Git remotes",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
url := "http://localhost:4242/api/fetcher/fetch"
client := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return
}
client.Do(req)
},
}

func init() {
rootCmd.AddCommand(fetchCmd)
}
27 changes: 21 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package cmd
import (
"os"
"path"
"time"

"github.com/nlewo/comin/internal/builder"
"github.com/nlewo/comin/internal/config"
"github.com/nlewo/comin/internal/deployer"
"github.com/nlewo/comin/internal/fetcher"
"github.com/nlewo/comin/internal/http"
"github.com/nlewo/comin/internal/manager"
"github.com/nlewo/comin/internal/poller"
"github.com/nlewo/comin/internal/nix"
"github.com/nlewo/comin/internal/prometheus"
"github.com/nlewo/comin/internal/repository"
"github.com/nlewo/comin/internal/scheduler"
store "github.com/nlewo/comin/internal/store"
"github.com/nlewo/comin/internal/utils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -46,17 +51,27 @@ var runCmd = &cobra.Command{
// We get the last mainCommitId to avoid useless
// redeployment as well as non fast forward checkouts
var mainCommitId string
if ok, lastDeployment := store.LastDeployment(); ok {
mainCommitId = lastDeployment.Generation.MainCommitId
var lastDeployment *deployer.Deployment
if ok, ld := store.LastDeployment(); ok {
mainCommitId = ld.Generation.MainCommitId
lastDeployment = &ld
}
repository, err := repository.New(gitConfig, mainCommitId)
repository, err := repository.New(gitConfig, mainCommitId, metrics)
if err != nil {
logrus.Errorf("Failed to initialize the repository: %s", err)
os.Exit(1)
}

manager := manager.New(repository, store, metrics, gitConfig.Path, gitConfig.Dir, cfg.Hostname, machineId)
go poller.Poller(manager, cfg.Remotes)
fetcher := fetcher.NewFetcher(repository)
fetcher.Start()
sched := scheduler.New()
sched.FetchRemotes(fetcher, cfg.Remotes)

builder := builder.New(gitConfig.Path, gitConfig.Dir, cfg.Hostname, 5*time.Minute, nix.Eval, 30*time.Minute, nix.Build)
deployer := deployer.New(nix.Deploy, lastDeployment)

manager := manager.New(store, metrics, sched, fetcher, builder, deployer, machineId)

http.Serve(manager,
metrics,
cfg.ApiServer.ListenAddress, cfg.ApiServer.Port,
Expand Down
68 changes: 10 additions & 58 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,14 @@ import (

"github.com/dustin/go-humanize"

"github.com/nlewo/comin/internal/deployment"
"github.com/nlewo/comin/internal/generation"
"github.com/nlewo/comin/internal/builder"
"github.com/nlewo/comin/internal/manager"
"github.com/nlewo/comin/internal/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func generationStatus(g generation.Generation) {
fmt.Printf(" Current Generation\n")
switch g.Status {
case generation.Init:
fmt.Printf(" Status: initializated\n")
case generation.Evaluating:
fmt.Printf(" Status: evaluating (since %s)\n", humanize.Time(g.EvalStartedAt))
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.BuildSucceeded:
fmt.Printf(" Status: built (%s)\n", humanize.Time(g.BuildEndedAt))
case generation.EvaluationFailed:
fmt.Printf(" Status: evaluation failed (%s)\n", humanize.Time(g.EvalEndedAt))
case generation.BuildFailed:
fmt.Printf(" Status: build failed (%s)\n", humanize.Time(g.BuildEndedAt))
}
printCommit(g.SelectedRemoteName, g.SelectedBranchName, g.SelectedCommitId, g.SelectedCommitMsg)
}

func deploymentStatus(d deployment.Deployment) {
fmt.Printf(" Current Deployment\n")
fmt.Printf(" Operation: %s\n", d.Operation)
switch d.Status {
case deployment.Init:
fmt.Printf(" Status: initializated\n")
case deployment.Running:
fmt.Printf(" Status: running (since %s)\n", humanize.Time(d.StartAt))
case deployment.Done:
fmt.Printf(" Status: succeeded (%s)\n", humanize.Time(d.EndAt))
case deployment.Failed:
fmt.Printf(" Status: failed (%s)\n", humanize.Time(d.EndAt))
}
printCommit(d.Generation.SelectedRemoteName, d.Generation.SelectedBranchName, d.Generation.SelectedCommitId, d.Generation.SelectedCommitMsg)
}

func printCommit(selectedRemoteName, selectedBranchName, selectedCommitId, selectedCommitMsg string) {
fmt.Printf(" Commit %s from '%s/%s'\n",
selectedCommitId,
selectedRemoteName,
selectedBranchName,
)
fmt.Printf(" %s\n",
utils.FormatCommitMsg(selectedCommitMsg),
)
}

func getStatus() (status manager.State, err error) {
url := "http://localhost:4242/status"
url := "http://localhost:4242/api/status"
client := http.Client{
Timeout: time.Second * 2,
}
Expand Down Expand Up @@ -101,19 +51,21 @@ var statusCmd = &cobra.Command{
if err != nil {
logrus.Fatal(err)
}
fmt.Printf("Status of the machine %s\n", status.Hostname)
fmt.Printf("Status of the machine %s\n", status.Builder.Hostname)
needToReboot := "no"
if status.NeedToReboot {
needToReboot = "yes"
}
fmt.Printf(" Need to reboot: %s\n", needToReboot)
for _, r := range status.RepositoryStatus.Remotes {
fmt.Printf(" Remote %s fetched %s\n",
r.Url, humanize.Time(r.FetchedAt),
fmt.Printf(" Fetcher\n")
for _, r := range status.Fetcher.RepositoryStatus.Remotes {
fmt.Printf(" Remote %s %s fetched %s\n",
r.Name, r.Url, humanize.Time(r.FetchedAt),
)
}
deploymentStatus(status.Deployment)
generationStatus(status.Generation)
fmt.Printf(" Builder\n")
builder.GenerationShow(*status.Builder.Generation)
status.Deployer.Show(" ")
},
}

Expand Down
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
in {
comin = final.buildGoModule rec {
pname = "comin";
version = "0.2.0";
version = "0.6.0";
nativeCheckInputs = [ final.git ];
src = final.lib.fileset.toSource {
root = ./.;
Expand All @@ -42,7 +42,7 @@
./main.go
];
};
vendorHash = "sha256-9qObgfXvMkwE+1BVZNQXVhKhL6LqMqyIUhGnXf8q9SI=";
vendorHash = "sha256-VP8y/iSBIXZFfSmhHsXkp6RxP+2DovX3PbEDtMUMyYE=";
ldflags = [
"-X github.com/nlewo/comin/cmd.version=${version}"
];
Expand Down
22 changes: 13 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ go 1.22
require (
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df
github.com/dustin/go-humanize v1.0.1
github.com/go-co-op/gocron/v2 v2.11.0
github.com/go-git/go-git/v5 v5.11.0
github.com/mattn/go-sqlite3 v1.14.19
github.com/google/uuid v1.6.0
github.com/prometheus/client_golang v1.19.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -26,25 +28,27 @@ 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/jonboulle/clockwork v0.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 773f614

Please sign in to comment.