Skip to content

Commit

Permalink
Reorganized code into packages
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Le Berrigaud <[email protected]>
  • Loading branch information
Jeffrey Peckham authored and Damien Le Berrigaud committed Oct 30, 2013
1 parent 5a25a1a commit 70f7a5e
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 78 deletions.
2 changes: 1 addition & 1 deletion bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

$(dirname $0)/go build -o out/agent main
$(dirname $0)/go build -o out/agent bosh/main
2 changes: 1 addition & 1 deletion bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

$(dirname $0)/go run src/main/agent.go $*
$(dirname $0)/go run src/bosh/main/agent.go $*
8 changes: 4 additions & 4 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
result=0

echo -e "\n Formatting packages..."
$(dirname $0)/go fmt agent/...
$(dirname $0)/go fmt bosh/...
let "result+=$?"

echo -e "\n Installing package dependencies..."
$(dirname $0)/go test -i agent/...
$(dirname $0)/go test -i bosh/...
let "result+=$?"

echo -e "\n Testing packages:"
$(dirname $0)/go test agent/... -parallel 4$@
$(dirname $0)/go test bosh/... -parallel 4$@
let "result+=$?"

echo -e "\n Vetting packages for potential issues..."
$(dirname $0)/go vet agent/...
$(dirname $0)/go vet bosh/...
let "result+=$?"

echo -e "\n Running build script to confirm everything compiles..."
Expand Down
44 changes: 0 additions & 44 deletions src/agent/app.go

This file was deleted.

57 changes: 57 additions & 0 deletions src/bosh/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package bootstrap

import (
"bosh/filesystem"
"bosh/infrastructure"
"fmt"
"os"
"path/filepath"
)

const (
VCAP_USERNAME = "vcap"
)

type boostrap struct {
fs filesystem.FileSystem
infrastructure infrastructure.Infrastructure
}

func New(fs filesystem.FileSystem, infrastructure infrastructure.Infrastructure) (b boostrap) {
b.fs = fs
b.infrastructure = infrastructure
return
}

func (boot boostrap) Run() {
publicKey, err := boot.infrastructure.GetPublicKey()
if err != nil {
failWithError("Error getting public key", err)
return
}

homeDir, err := boot.fs.HomeDir(VCAP_USERNAME)
if err != nil {
failWithError("Error finding home dir for user", err)
return
}

sshPath := filepath.Join(homeDir, ".ssh")
boot.fs.MkdirAll(sshPath, os.FileMode(0700))
boot.fs.Chown(sshPath, VCAP_USERNAME)

authKeysPath := filepath.Join(sshPath, "authorized_keys")
err = boot.fs.WriteToFile(authKeysPath, publicKey)
if err != nil {
failWithError("Error creating authorized_keys file", err)
return
}

boot.fs.Chown(authKeysPath, VCAP_USERNAME)
boot.fs.Chmod(authKeysPath, os.FileMode(0600))
}

func failWithError(message string, err error) {
fmt.Fprintf(os.Stderr, "%s: %s", message, err.Error())
os.Exit(1)
}
12 changes: 7 additions & 5 deletions src/agent/app_test.go → src/bosh/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package agent
package bootstrap

import (
testfs "bosh/testhelpers/filesystem"
testinf "bosh/testhelpers/infrastructure"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
testagent "testhelpers/agent"
"testing"
)

func TestRunSetsUpSsh(t *testing.T) {
fakeFs := &testagent.FakeFileSystem{
fakeFs := &testfs.FakeFileSystem{
HomeDirHomeDir: "/some/home/dir",
}

fakeInfrastructure := &testagent.FakeInfrastructure{
fakeInfrastructure := &testinf.FakeInfrastructure{
PublicKey: "some public key",
}

Run(fakeFs, fakeInfrastructure)
boot := New(fakeFs, fakeInfrastructure)
boot.Run()

sshDirPath := "/some/home/dir/.ssh"
sshDirStat := fakeFs.GetFileTestStat(sshDirPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package filesystem

import "os"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package filesystem

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package filesystem

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package infrastructure

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package infrastructure

import (
"github.com/stretchr/testify/assert"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package infrastructure

type Infrastructure interface {
GetPublicKey() (publicKey string, err error)
Expand Down
15 changes: 15 additions & 0 deletions src/bosh/main/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"bosh/bootstrap"
"bosh/filesystem"
"bosh/infrastructure"
)

func main() {
fs := filesystem.OsFileSystem{}
infrastructure := infrastructure.NewAwsInfrastructure("http://169.254.169.254")

b := bootstrap.New(fs, infrastructure)
b.Run()
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package agent
package filesystem

import "os"

type FakeFileSystem struct {
Files map[string]*FakeFileStats

HomeDirUsername string
HomeDirHomeDir string
HomeDirHomeDir string
}

type FakeFileStats struct {
FileMode os.FileMode
Username string
Username string
CreatedWith string
Content string
Content string
}

func (fs *FakeFileSystem) GetFileTestStat(path string) (stats *FakeFileStats) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package infrastructure

type FakeInfrastructure struct {
PublicKey string
Expand Down
12 changes: 0 additions & 12 deletions src/main/agent.go

This file was deleted.

0 comments on commit 70f7a5e

Please sign in to comment.