forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.go
56 lines (48 loc) · 1.66 KB
/
directory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package container
import (
"os"
"path/filepath"
"github.com/juju/loggo"
"github.com/juju/utils"
)
var (
logger = loggo.GetLogger("juju.container")
ContainerDir = "/var/lib/juju/containers"
RemovedContainerDir = "/var/lib/juju/removed-containers"
)
// NewDirectory creates a new directory for the container name in the
// directory identified by `ContainerDir`.
func NewDirectory(containerName string) (directory string, err error) {
directory = dirForName(containerName)
logger.Tracef("create directory: %s", directory)
if err = os.MkdirAll(directory, 0755); err != nil {
logger.Errorf("failed to create container directory: %v", err)
return "", err
}
return directory, nil
}
// RemoveDirectory moves the container directory from `ContainerDir`
// to `RemovedContainerDir` and makes sure that the names don't clash.
func RemoveDirectory(containerName string) error {
// Move the directory.
logger.Tracef("create old container dir: %s", RemovedContainerDir)
if err := os.MkdirAll(RemovedContainerDir, 0755); err != nil {
logger.Errorf("failed to create removed container directory: %v", err)
return err
}
removedDir, err := utils.UniqueDirectory(RemovedContainerDir, containerName)
if err != nil {
logger.Errorf("was not able to generate a unique directory: %v", err)
return err
}
if err := os.Rename(dirForName(containerName), removedDir); err != nil {
logger.Errorf("failed to rename container directory: %v", err)
return err
}
return nil
}
func dirForName(containerName string) string {
return filepath.Join(ContainerDir, containerName)
}