Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
kcq committed Aug 8, 2017
1 parent 6df60f9 commit 8c2da78
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 108 deletions.
21 changes: 11 additions & 10 deletions cmd/docker-slim-sensor/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
"syscall"

"github.com/docker-slim/docker-slim/messages"
"github.com/docker-slim/docker-slim/pkg/utils/errutils"
"github.com/docker-slim/docker-slim/pkg/utils/fsutils"
"github.com/docker-slim/docker-slim/report"
"github.com/docker-slim/docker-slim/utils"

log "github.com/Sirupsen/logrus"
)
Expand Down Expand Up @@ -239,7 +240,7 @@ func (p *artifactStore) saveArtifacts() {
//TODO: use exludePaths to filter discovered links
for linkName, linkProps := range p.linkMap {
linkPath := fmt.Sprintf("%s/files%s", p.storeLocation, linkName)
linkDir := utils.FileDir(linkPath)
linkDir := fsutils.FileDir(linkPath)
err := os.MkdirAll(linkDir, 0777)
if err != nil {
log.Warn("saveArtifacts - dir error => ", err)
Expand All @@ -264,7 +265,7 @@ func (p *artifactStore) saveArtifacts() {
for inPath, isDir := range includePaths {
dstPath := fmt.Sprintf("%s/files%s", p.storeLocation, inPath)
if isDir {
err, errs := utils.CopyDir(inPath, dstPath, true, true, nil, nil, nil)
err, errs := fsutils.CopyDir(inPath, dstPath, true, true, nil, nil, nil)
if err != nil {
log.Warnf("CopyDir(%v,%v) error: %v", inPath, dstPath, err)
}
Expand All @@ -273,7 +274,7 @@ func (p *artifactStore) saveArtifacts() {
log.Warnf("CopyDir(%v,%v) copy errors: %+v", inPath, dstPath, errs)
}
} else {
if err := utils.CopyFile(inPath, dstPath, true); err != nil {
if err := fsutils.CopyFile(inPath, dstPath, true); err != nil {
log.Warnf("CopyFile(%v,%v) error: %v", inPath, dstPath, err)
}
}
Expand Down Expand Up @@ -301,17 +302,17 @@ func (p *artifactStore) saveReport() {
if os.IsNotExist(err) {
os.MkdirAll(artifactDirName, 0777)
_, err = os.Stat(artifactDirName)
utils.FailOn(err)
errutils.FailOn(err)
}

reportFilePath := filepath.Join(artifactDirName, reportName)
log.Debug("sensor: monitor - saving report to ", reportFilePath)

reportData, err := json.MarshalIndent(creport, "", " ")
utils.FailOn(err)
errutils.FailOn(err)

err = ioutil.WriteFile(reportFilePath, reportData, 0644)
utils.FailOn(err)
errutils.FailOn(err)
}

func getFileHash(artifactFileName string) (string, error) {
Expand Down Expand Up @@ -357,7 +358,7 @@ func cpFile(src, dst string) error {
}
defer s.Close()

dstDir := utils.FileDir(dst)
dstDir := fsutils.FileDir(dst)
err = os.MkdirAll(dstDir, 0777)
if err != nil {
log.Warnln("sensor: monitor - dir error =>", err)
Expand Down Expand Up @@ -400,7 +401,7 @@ func cpFile(src, dst string) error {
}

//note: need to do the same for symlinks too
if err := utils.UpdateFileTimes(dst, sysStat.Atim, sysStat.Mtim); err != nil {
if err := fsutils.UpdateFileTimes(dst, sysStat.Atim, sysStat.Mtim); err != nil {
log.Warnln("sensor: cpFile - UpdateFileTimes error =>", dst)
return err
}
Expand Down Expand Up @@ -476,7 +477,7 @@ func createDummyFile(src, dst string) error {
}

//note: need to do the same for symlinks too
if err := utils.UpdateFileTimes(dst, sysStat.Mtim, sysStat.Atim); err != nil {
if err := fsutils.UpdateFileTimes(dst, sysStat.Mtim, sysStat.Atim); err != nil {
log.Warnln("sensor: createDummyFile - UpdateFileTimes error =>", dst)
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/docker-slim-sensor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"time"

"github.com/docker-slim/docker-slim/messages"
"github.com/docker-slim/docker-slim/pkg/utils/errutils"
"github.com/docker-slim/docker-slim/report"
"github.com/docker-slim/docker-slim/sensor/ipc"
"github.com/docker-slim/docker-slim/sensor/monitors/fanotify"
"github.com/docker-slim/docker-slim/sensor/monitors/pevent"
"github.com/docker-slim/docker-slim/sensor/monitors/ptrace"
"github.com/docker-slim/docker-slim/utils"

log "github.com/Sirupsen/logrus"
"github.com/cloudimmunity/system"
Expand Down Expand Up @@ -88,7 +88,7 @@ func main() {
log.Infof("sensor: args => %#v", os.Args)

dirName, err := os.Getwd()
utils.WarnOn(err)
errutils.WarnOn(err)
log.Debugf("sensor: cwd => %#v", dirName)

initSignalHandlers()
Expand All @@ -101,10 +101,10 @@ func main() {
doneChan = make(chan struct{})

err = ipc.InitChannels()
utils.FailOn(err)
errutils.FailOn(err)

cmdChan, err := ipc.RunCmdServer(doneChan)
utils.FailOn(err)
errutils.FailOn(err)

monDoneChan := make(chan bool, 1)
monDoneAckChan := make(chan bool)
Expand Down
4 changes: 2 additions & 2 deletions cmd/docker-slim/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/docker-slim/docker-slim/master/commands"
"github.com/docker-slim/docker-slim/master/config"
"github.com/docker-slim/docker-slim/utils"
"github.com/docker-slim/docker-slim/pkg/version"

log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
Expand All @@ -24,7 +24,7 @@ var app *cli.App

func init() {
app = cli.NewApp()
app.Version = utils.CurrentVersion()
app.Version = version.Current()
app.Name = AppName
app.Usage = AppUsage
app.CommandNotFound = func(ctx *cli.Context, command string) {
Expand Down
4 changes: 2 additions & 2 deletions master/builder/image_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/docker-slim/docker-slim/master/config"
"github.com/docker-slim/docker-slim/master/docker/dockerfile"
"github.com/docker-slim/docker-slim/utils"
"github.com/docker-slim/docker-slim/pkg/utils/fsutils"

//log "github.com/Sirupsen/logrus"
"github.com/cloudimmunity/go-dockerclientx"
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewImageBuilder(client *docker.Client,
}

dataDir := filepath.Join(artifactLocation, "files")
builder.HasData = utils.IsDir(dataDir)
builder.HasData = fsutils.IsDir(dataDir)

return builder, nil
}
Expand Down
34 changes: 18 additions & 16 deletions master/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/docker-slim/docker-slim/master/inspectors/container"
"github.com/docker-slim/docker-slim/master/inspectors/container/probes/http"
"github.com/docker-slim/docker-slim/master/inspectors/image"
"github.com/docker-slim/docker-slim/utils"
"github.com/docker-slim/docker-slim/pkg/utils/errutils"
"github.com/docker-slim/docker-slim/pkg/utils/fsutils"
"github.com/docker-slim/docker-slim/pkg/version"

log "github.com/Sirupsen/logrus"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -44,7 +46,7 @@ func OnBuild(doDebug bool,
client := dockerclient.New(clientConfig)

imageInspector, err := image.NewInspector(client, imageRef)
utils.FailOn(err)
errutils.FailOn(err)

if imageInspector.NoImage() {
fmt.Println("docker-slim: [build] target image not found -", imageRef)
Expand All @@ -53,9 +55,9 @@ func OnBuild(doDebug bool,

log.Info("docker-slim: inspecting 'fat' image metadata...")
err = imageInspector.Inspect()
utils.FailOn(err)
errutils.FailOn(err)

localVolumePath, artifactLocation := utils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID)
localVolumePath, artifactLocation := fsutils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID)
imageInspector.ArtifactLocation = artifactLocation

log.Infof("docker-slim: [%v] 'fat' image size => %v (%v)",
Expand All @@ -65,7 +67,7 @@ func OnBuild(doDebug bool,

log.Info("docker-slim: processing 'fat' image info...")
err = imageInspector.ProcessCollectedData()
utils.FailOn(err)
errutils.FailOn(err)

containerInspector, err := container.NewInspector(client,
imageInspector,
Expand All @@ -76,11 +78,11 @@ func OnBuild(doDebug bool,
excludePaths,
includePaths,
doDebug)
utils.FailOn(err)
errutils.FailOn(err)

log.Info("docker-slim: starting instrumented 'fat' container...")
err = containerInspector.RunContainer()
utils.FailOn(err)
errutils.FailOn(err)

log.Info("docker-slim: watching container monitor...")

Expand All @@ -90,7 +92,7 @@ func OnBuild(doDebug bool,

if doHTTPProbe {
probe, err := http.NewCustomProbe(containerInspector, httpProbeCmds)
utils.FailOn(err)
errutils.FailOn(err)
probe.Start()
continueAfter.ContinueChan = probe.DoneChan()
}
Expand All @@ -113,24 +115,24 @@ func OnBuild(doDebug bool,
<-continueAfter.ContinueChan
fmt.Println("docker-slim: HTTP probe is done...")
default:
utils.Fail("unknown continue-after mode")
errutils.Fail("unknown continue-after mode")
}

containerInspector.FinishMonitoring()

log.Info("docker-slim: shutting down 'fat' container...")
err = containerInspector.ShutdownContainer()
utils.WarnOn(err)
errutils.WarnOn(err)

if !containerInspector.HasCollectedData() {
imageInspector.ShowFatImageDockerInstructions()
fmt.Printf("docker-slim: [build] no data collected (no minified image generated) - done. (version: %v)\n", utils.CurrentVersion())
fmt.Printf("docker-slim: [build] no data collected (no minified image generated) - done. (version: %v)\n", version.Current())
return
}

log.Info("docker-slim: processing instrumented 'fat' container info...")
err = containerInspector.ProcessCollectedData()
utils.FailOn(err)
errutils.FailOn(err)

if customImageTag == "" {
customImageTag = imageInspector.SlimImageRepo
Expand All @@ -143,21 +145,21 @@ func OnBuild(doDebug bool,
artifactLocation,
imageOverrides,
overrides)
utils.FailOn(err)
errutils.FailOn(err)

if !builder.HasData {
log.Info("docker-slim: WARNING - no data artifacts")
}

err = builder.Build()
utils.FailOn(err)
errutils.FailOn(err)

log.Infoln("docker-slim: created new image:", builder.RepoName, "( has data artifacts:", builder.HasData, ")")

if doRmFileArtifacts {
log.Info("docker-slim: removing temporary artifacts...")
err = utils.RemoveArtifacts(artifactLocation) //TODO: remove only the "files" subdirectory
utils.WarnOn(err)
err = fsutils.Remove(artifactLocation) //TODO: remove only the "files" subdirectory
errutils.WarnOn(err)
}

fmt.Println("docker-slim: [build] done.")
Expand Down
11 changes: 6 additions & 5 deletions master/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/docker-slim/docker-slim/master/config"
"github.com/docker-slim/docker-slim/master/docker/dockerclient"
"github.com/docker-slim/docker-slim/master/inspectors/image"
"github.com/docker-slim/docker-slim/utils"
"github.com/docker-slim/docker-slim/pkg/utils/errutils"
"github.com/docker-slim/docker-slim/pkg/utils/fsutils"

log "github.com/Sirupsen/logrus"
"github.com/dustin/go-humanize"
Expand All @@ -20,7 +21,7 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string
client := dockerclient.New(clientConfig)

imageInspector, err := image.NewInspector(client, imageRef)
utils.FailOn(err)
errutils.FailOn(err)

if imageInspector.NoImage() {
fmt.Println("docker-slim: [info] target image not found -", imageRef)
Expand All @@ -29,9 +30,9 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string

log.Info("docker-slim: inspecting 'fat' image metadata...")
err = imageInspector.Inspect()
utils.FailOn(err)
errutils.FailOn(err)

_, artifactLocation := utils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID)
_, artifactLocation := fsutils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID)
imageInspector.ArtifactLocation = artifactLocation

log.Infof("docker-slim: [%v] 'fat' image size => %v (%v)",
Expand All @@ -41,7 +42,7 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string

log.Info("docker-slim: processing 'fat' image info...")
err = imageInspector.ProcessCollectedData()
utils.FailOn(err)
errutils.FailOn(err)

fmt.Println("docker-slim: [info] done.")
}
Loading

0 comments on commit 8c2da78

Please sign in to comment.