Skip to content

Commit

Permalink
Merge pull request #45 from nlewo/various
Browse files Browse the repository at this point in the history
Various
  • Loading branch information
nlewo authored Jun 22, 2024
2 parents 385a998 + 7650246 commit e67ac84
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.22'

- name: Build
run: go build -v ./...
Expand Down
20 changes: 13 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ var runCmd = &cobra.Command{
}
gitConfig := config.MkGitConfig(cfg)

repositoryStatus := repository.RepositoryStatus{}
repository, err := repository.New(gitConfig, repositoryStatus)
if err != nil {
logrus.Errorf("Failed to initialize the repository: %s", err)
os.Exit(1)
}

machineId, err := utils.ReadMachineId()
if err != nil {
logrus.Error(err)
Expand All @@ -49,6 +42,19 @@ var runCmd = &cobra.Command{
logrus.Errorf("Ignoring the state file %s because of the loading error: %s", storeFilename, err)
}
metrics.SetBuildInfo(cmd.Version)

// 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
}
repository, err := repository.New(gitConfig, mainCommitId)
if err != nil {
logrus.Errorf("Failed to initialize the repository: %s", err)
os.Exit(1)
}

manager := manager.New(repository, store, metrics, gitConfig.Path, cfg.Hostname, machineId)
go poller.Poller(manager, cfg.Remotes)
http.Serve(manager,
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nlewo/comin

go 1.17
go 1.22

require (
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df
Expand Down
8 changes: 6 additions & 2 deletions internal/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func StatusFromString(status string) Status {
return Init
}

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

type Deployment struct {
UUID string `json:"uuid"`
Expand All @@ -57,6 +57,7 @@ type Deployment struct {
Err error `json:"-"`
ErrorMsg string `json:"error_msg"`
RestartComin bool `json:"restart_comin"`
ProfilePath string `json:"profile_path"`
Status Status `json:"status"`
Operation string `json:"operation"`

Expand All @@ -68,6 +69,7 @@ type DeploymentResult struct {
Err error
EndAt time.Time
RestartComin bool
ProfilePath string
}

func New(g generation.Generation, deployerFunc DeployFunc, deploymentCh chan DeploymentResult) Deployment {
Expand All @@ -93,6 +95,7 @@ func (d Deployment) Update(dr DeploymentResult) Deployment {
d.ErrorMsg = dr.Err.Error()
}
d.RestartComin = dr.RestartComin
d.ProfilePath = dr.ProfilePath
if dr.Err == nil {
d.Status = Done
} else {
Expand All @@ -111,7 +114,7 @@ func (d Deployment) IsTesting() bool {
func (d Deployment) Deploy(ctx context.Context) Deployment {
go func() {
// FIXME: propagate context
cominNeedRestart, err := d.deployerFunc(
cominNeedRestart, profilePath, err := d.deployerFunc(
ctx,
d.Generation.EvalMachineId,
d.Generation.OutPath,
Expand All @@ -127,6 +130,7 @@ func (d Deployment) Deploy(ctx context.Context) Deployment {

deploymentResult.EndAt = time.Now().UTC()
deploymentResult.RestartComin = cominNeedRestart
deploymentResult.ProfilePath = profilePath
d.deploymentCh <- deploymentResult
}()
d.Status = Running
Expand Down
31 changes: 24 additions & 7 deletions internal/generation/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ type Generation struct {

Status Status `json:"status"`

SelectedRemoteUrl string `json:"remote-url"`
SelectedRemoteName string `json:"remote-name"`
SelectedBranchName string `json:"branch-name"`
SelectedCommitId string `json:"commit-id"`
SelectedCommitMsg string `json:"commit-msg"`
SelectedBranchIsTesting bool `json:"branch-is-testing"`

MainCommitId string `json:"main-commit-id"`
MainRemoteName string `json:"main-remote-name"`
MainBranchName string `json:"main-branch-name"`

EvalStartedAt time.Time `json:"eval-started-at"`
evalTimeout time.Duration
evalFunc EvalFunc
Expand Down Expand Up @@ -113,20 +118,32 @@ type EvalResult struct {
}

func New(repositoryStatus repository.RepositoryStatus, flakeUrl, hostname, machineId string, evalFunc EvalFunc, buildFunc BuildFunc) Generation {
remoteUrl := ""
for _, r := range repositoryStatus.Remotes {
if r.Name == repositoryStatus.SelectedRemoteName {
remoteUrl = r.Url
}
}
return Generation{
UUID: uuid.NewString(),
SelectedRemoteUrl: remoteUrl,
SelectedRemoteName: repositoryStatus.SelectedRemoteName,
SelectedBranchName: repositoryStatus.SelectedBranchName,
SelectedCommitId: repositoryStatus.SelectedCommitId,
SelectedCommitMsg: repositoryStatus.SelectedCommitMsg,
SelectedBranchIsTesting: repositoryStatus.SelectedBranchIsTesting,
evalTimeout: 6 * time.Second,
evalFunc: evalFunc,
buildFunc: buildFunc,
FlakeUrl: flakeUrl,
Hostname: hostname,
MachineId: machineId,
Status: Init,

MainRemoteName: repositoryStatus.MainRemoteName,
MainBranchName: repositoryStatus.MainBranchName,
MainCommitId: repositoryStatus.MainCommitId,

evalTimeout: 6 * time.Second,
evalFunc: evalFunc,
buildFunc: buildFunc,
FlakeUrl: flakeUrl,
Hostname: hostname,
MachineId: machineId,
Status: Init,
}
}

Expand Down
19 changes: 10 additions & 9 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Manager struct {
hostname string
// The machine id of the current host
machineId string
triggerRepository chan string
triggerRepository chan []string
generationFactory func(repository.RepositoryStatus, string, string) generation.Generation
stateRequestCh chan struct{}
stateResultCh chan State
Expand Down Expand Up @@ -68,7 +68,7 @@ func New(r repository.Repository, s store.Store, p prometheus.Prometheus, path,
evalFunc: nix.Eval,
buildFunc: nix.Build,
deployerFunc: nix.Deploy,
triggerRepository: make(chan string),
triggerRepository: make(chan []string),
stateRequestCh: make(chan struct{}),
stateResultCh: make(chan State),
cominServiceRestartFunc: utils.CominServiceRestart,
Expand All @@ -92,8 +92,8 @@ func (m Manager) GetState() State {
return <-m.stateResultCh
}

func (m Manager) Fetch(remote string) {
m.triggerRepository <- remote
func (m Manager) Fetch(remotes []string) {
m.triggerRepository <- remotes
}

func (m Manager) toState() State {
Expand Down Expand Up @@ -180,7 +180,7 @@ func (m Manager) onRepositoryStatus(ctx context.Context, rs repository.Repositor
return m
}

func (m Manager) onTriggerRepository(ctx context.Context, remoteName string) Manager {
func (m Manager) onTriggerRepository(ctx context.Context, remoteNames []string) Manager {
if m.isFetching {
logrus.Debugf("The manager is already fetching the repository")
return m
Expand All @@ -190,10 +190,10 @@ func (m Manager) onTriggerRepository(ctx context.Context, remoteName string) Man
logrus.Debugf("The manager is already running: it is currently not able to run tasks in parallel")
return m
}
logrus.Debugf("Trigger fetch and update remote %s", remoteName)
logrus.Debugf("Trigger fetch and update remotes %s", remoteNames)
m.isRunning = true
m.isFetching = true
m.repositoryStatusCh = m.repository.FetchAndUpdate(ctx, remoteName)
m.repositoryStatusCh = m.repository.FetchAndUpdate(ctx, remoteNames)
return m
}

Expand All @@ -204,12 +204,13 @@ func (m Manager) Run() {
logrus.Infof(" hostname = %s", m.hostname)
logrus.Infof(" machineId = %s", m.machineId)
logrus.Infof(" repositoryPath = %s", m.repositoryPath)

for {
select {
case <-m.stateRequestCh:
m.stateResultCh <- m.toState()
case remoteName := <-m.triggerRepository:
m = m.onTriggerRepository(ctx, remoteName)
case remoteNames := <-m.triggerRepository:
m = m.onTriggerRepository(ctx, remoteNames)
case rs := <-m.repositoryStatusCh:
m = m.onRepositoryStatus(ctx, rs)
case evalResult := <-m.generation.EvalCh():
Expand Down
16 changes: 8 additions & 8 deletions internal/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRepositoryMock() (r *repositoryMock) {
rsCh: rsCh,
}
}
func (r *repositoryMock) FetchAndUpdate(ctx context.Context, remoteName string) (rsCh chan repository.RepositoryStatus) {
func (r *repositoryMock) FetchAndUpdate(ctx context.Context, remoteNames []string) (rsCh chan repository.RepositoryStatus) {
return r.rsCh
}

Expand All @@ -49,8 +49,8 @@ func TestRun(t *testing.T) {
m.evalFunc = nixEvalMock
m.buildFunc = nixBuildMock

deployFunc := func(context.Context, string, string, string) (bool, error) {
return false, nil
deployFunc := func(context.Context, string, string, string) (bool, string, error) {
return false, "", nil
}
m.deployerFunc = deployFunc

Expand All @@ -60,7 +60,7 @@ func TestRun(t *testing.T) {
assert.Equal(t, State{}, m.GetState())

// the repository is fetched
m.Fetch("origin")
m.Fetch([]string{"origin"})
assert.Equal(t, repository.RepositoryStatus{}, m.GetState().RepositoryStatus)

// we inject a repositoryStatus
Expand Down Expand Up @@ -100,10 +100,10 @@ func TestFetchBusy(t *testing.T) {

assert.Equal(t, State{}, m.GetState())

m.Fetch("origin")
m.Fetch([]string{"origin"})
assert.Equal(t, repository.RepositoryStatus{}, m.GetState().RepositoryStatus)

m.Fetch("origin")
m.Fetch([]string{"origin"})
assert.Equal(t, repository.RepositoryStatus{}, m.GetState().RepositoryStatus)
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func TestOptionnalMachineId(t *testing.T) {
m.buildFunc = nixBuildMock

go m.Run()
m.Fetch("origin")
m.Fetch([]string{"origin"})
r.rsCh <- repository.RepositoryStatus{SelectedCommitId: "foo"}

// we simulate the end of the evaluation
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestIncorrectMachineId(t *testing.T) {
assert.Equal(t, State{}, m.GetState())

// the repository is fetched
m.Fetch("origin")
m.Fetch([]string{"origin"})
r.rsCh <- repository.RepositoryStatus{SelectedCommitId: "foo"}

assert.True(t, m.GetState().IsRunning)
Expand Down
Loading

0 comments on commit e67ac84

Please sign in to comment.