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

[ONPREM-1829] [HACKWEEK] Add initial support for Windows containers #96

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Only copy binaries on Windows
Appears we don't have privileges to create symlinks
  • Loading branch information
christian-stephen committed Feb 21, 2025
commit 5fa0bd94f5592730526017a629c2e562699e6b04
10 changes: 7 additions & 3 deletions acceptance/init_test.go
Original file line number Diff line number Diff line change
@@ -43,9 +43,13 @@ func TestInit(t *testing.T) {
assertFileIsCopied(t, orchSrc, orchDest)
assertFileIsCopied(t, agentSrc, agentDest)

agentLink, err := os.Readlink(circleciDest)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(agentLink, agentDest))
if runtime.GOOS == "windows" {
assertFileIsCopied(t, agentSrc, circleciDest)
} else {
agentLink, err := os.Readlink(circleciDest)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(agentLink, agentDest))
}
})
}

9 changes: 0 additions & 9 deletions init/binaries_unix.go

This file was deleted.

7 changes: 0 additions & 7 deletions init/binaries_windows.go

This file was deleted.

10 changes: 7 additions & 3 deletions init/init_test.go
Original file line number Diff line number Diff line change
@@ -26,9 +26,13 @@ func TestRun(t *testing.T) {
assertFileIsCopied(t, orchSrc, orchDest)
assertFileIsCopied(t, agentSrc, agentDest)

agentLink, errLink := os.Readlink(circleciDest)
assert.NilError(t, errLink)
assert.Check(t, cmp.DeepEqual(agentLink, agentDest))
if runtime.GOOS == "windows" {
assertFileIsCopied(t, agentSrc, circleciDest)
} else {
agentLink, errLink := os.Readlink(circleciDest)
assert.NilError(t, errLink)
assert.Check(t, cmp.DeepEqual(agentLink, agentDest))
}
})

t.Run("Fail when source files not present", func(t *testing.T) {
8 changes: 8 additions & 0 deletions init/init.go → init/init_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package init

import (
@@ -7,6 +9,12 @@ import (
"path/filepath"
)

const (
binOrchestrator = "orchestrator"
binCircleciAgent = "circleci-agent"
binCircleci = "circleci"
)

// Run function performs the copying of specific files and symlink creation
func Run(srcDir, destDir string) error {
// Copy the orchestrator binary
66 changes: 66 additions & 0 deletions init/init_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package init

import (
"errors"
"io"
"os"
"path/filepath"
)

const (
binOrchestrator = "orchestrator.exe"
binCircleciAgent = "circleci-agent.exe"
binCircleci = "circleci.exe"
)

// Run function performs the copying of specific files and symlink creation
func Run(srcDir, destDir string) error {
// Copy the orchestrator binary
orchestratorSrc := filepath.Join(srcDir, binOrchestrator)
orchestratorDest := filepath.Join(destDir, binOrchestrator)
if err := copyFile(orchestratorSrc, orchestratorDest); err != nil {
return err
}

// Copy the task agent binaries
agentSrc := filepath.Join(srcDir, binCircleciAgent)
agentDests := []string{filepath.Join(destDir, binCircleciAgent), filepath.Join(destDir, binCircleci)}
for _, dest := range agentDests {
if err := copyFile(agentSrc, dest); err != nil {
return err
}
}

return nil
}

func copyFile(srcPath, destPath string) (err error) {
closeFile := func(f *os.File) {
err = errors.Join(err, f.Close())
}

srcFile, err := os.Open(srcPath) //#nosec:G304 // this is trusted input
if err != nil {
return err
}
defer closeFile(srcFile)

// Get the file info to preserve the permissions
info, err := srcFile.Stat()
if err != nil {
return err
}

//#nosec:G304 // this is trusted output
destFile, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
defer closeFile(destFile)

if _, err = io.Copy(destFile, srcFile); err != nil {
return err
}

return err
}