~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/container/directory.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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