Add a library function for listing all builders

Add OpenAllBuilders(), for listing all open builders.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai
2017-03-15 16:44:52 -04:00
parent 5a6a88b5ed
commit f51e520e99

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/containers/storage/pkg/ioutils"
@@ -216,6 +217,32 @@ func OpenBuilderByPath(store storage.Store, path string) (*Builder, error) {
return nil, storage.ErrContainerUnknown
}
// OpenAllBuilders loads all containers which have a state file that we use in
// their data directory, typically so that they can be listed.
func OpenAllBuilders(store storage.Store) (builders []*Builder, err error) {
containers, err := store.Containers()
if err != nil {
return nil, err
}
for _, container := range containers {
cdir, err := store.GetContainerDirectory(container.ID)
if err != nil {
return nil, err
}
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
if err != nil && os.IsNotExist(err) {
continue
}
b := &Builder{}
err = json.Unmarshal([]byte(buildstate), &b)
if err == nil && b.Type == containerType {
b.store = store
builders = append(builders, b)
}
}
return builders, nil
}
// Save saves the builder's current state to the build container's metadata.
// This should not need to be called directly, as other methods of the Builder
// object take care of saving their state.