github.com/polydawn/docket@v0.5.4-0.20140630233848-90b70fb433da/conf/format.go (about)

     1  package conf
     2  
     3  import (
     4  	"strings"
     5  	"path/filepath"
     6  	. "polydawn.net/hroot/util"
     7  )
     8  
     9  const DockFolder     = "dock"
    10  const GraphFolder    = "graph"
    11  
    12  //Image and parent image
    13  type Image struct {
    14  	//What image to use
    15  	Name        string     `toml:"name"`
    16  
    17  	//What image to build from
    18  	Upstream    string     `toml:"upstream"`
    19  
    20  	//What the upstream image is called in the docker index
    21  	Index       string     `toml:"index"`
    22  }
    23  
    24  //A container's settings
    25  type Container struct {
    26  	//What command to run
    27  	Command     []string   `toml:"command"`
    28  
    29  	//Which folder to start
    30  	Folder      string     `toml:"folder"`
    31  
    32  	//Run in privileged mode?
    33  	Privileged  bool       `toml:"privileged"`
    34  
    35  	//Array of mounts (each an array of strings: hostfolder, guestfolder, "ro"/"rw" permission)
    36  	Mounts      [][]string `toml:"mounts"`
    37  
    38  	//What ports do you want to forward? (each an array of ints: hostport, guestport)
    39  	Ports       [][]string `toml:"ports"`
    40  
    41  	//Do you want to use custom DNS servers?
    42  	DNS         []string   `toml:"dns"`
    43  
    44  	//Attach interactive terminal?
    45  	Attach      bool       `toml:"attach"`
    46  
    47  	//Delete when done?
    48  	Purge       bool       `toml:"purge"`
    49  
    50  	//Env variables (each an array of strings: variable, value)
    51  	Environment [][]string `toml:"environment"`
    52  }
    53  
    54  //Localize a container object to a given folder
    55  func (c *Container) Localize(dir string) {
    56  	//Get the absolute directory this config is relative to
    57  	cwd, err := filepath.Abs(dir)
    58  	if err != nil { ExitGently("Cannot determine absolute path: ", dir) }
    59  
    60  	//Handle mounts
    61  	for i := range c.Mounts {
    62  
    63  		//Check for triple-dot ... notation, which is relative to that config's directory, not the CWD
    64  		if strings.Index(c.Mounts[i][0], "...") == 0 {
    65  			c.Mounts[i][0] = strings.Replace(c.Mounts[i][0], "...", cwd, 1)
    66  		}
    67  
    68  		//Find the absolute path for each host mount
    69  		abs, err := filepath.Abs(c.Mounts[i][0])
    70  		if err != nil { ExitGently("Cannot determine absolute path:", c.Mounts[i][0]) }
    71  		c.Mounts[i][0] = abs
    72  	}
    73  }
    74  
    75  //Default container
    76  var DefaultContainer = Container {
    77  	Command:     []string{},
    78  	Folder:      "/",
    79  	Privileged:  false,
    80  	Mounts:      [][]string{},
    81  	Ports:       [][]string{},
    82  	DNS:         []string{},
    83  	Attach:      false,
    84  	Purge:       false,
    85  	Environment: [][]string{},
    86  }
    87  
    88  //Hroot configuration
    89  type Configuration struct {
    90  	//The image struct
    91  	Image    Image                `toml:"image"`
    92  
    93  	//The settings struct
    94  	Settings Container            `toml:"settings"`
    95  
    96  	//A map of named targets, each representing another set of container settings
    97  	Targets  map[string]Container `toml:"target"`
    98  }
    99  
   100  //Default configuration
   101  var DefaultConfiguration = Configuration {
   102  	Settings: DefaultContainer,
   103  }
   104  
   105  //Folder location
   106  type Folders struct {
   107  	//Where we've decided the graph folder is or should be
   108  	Graph string
   109  }
   110  
   111  //Default folders
   112  func DefaultFolders(dir string) *Folders {
   113  	return &Folders {
   114  		Graph: filepath.Join(dir, GraphFolder),
   115  	}
   116  }