github.com/r3labs/libcompose@v0.4.1-0.20171123133234-495fe0619cc3/config/types.go (about)

     1  package config
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/r3labs/libcompose/yaml"
     7  )
     8  
     9  // EnvironmentLookup defines methods to provides environment variable loading.
    10  type EnvironmentLookup interface {
    11  	Lookup(key string, config *ServiceConfig) []string
    12  }
    13  
    14  // ResourceLookup defines methods to provides file loading.
    15  type ResourceLookup interface {
    16  	Lookup(file, relativeTo string) ([]byte, string, error)
    17  	ResolvePath(path, inFile string) string
    18  }
    19  
    20  // ServiceConfigV1 holds version 1 of libcompose service configuration
    21  type ServiceConfigV1 struct {
    22  	Build          string               `yaml:"build,omitempty"`
    23  	CapAdd         []string             `yaml:"cap_add,omitempty"`
    24  	CapDrop        []string             `yaml:"cap_drop,omitempty"`
    25  	CgroupParent   string               `yaml:"cgroup_parent,omitempty"`
    26  	CPUQuota       yaml.StringorInt     `yaml:"cpu_quota,omitempty"`
    27  	CPUSet         string               `yaml:"cpuset,omitempty"`
    28  	CPUShares      yaml.StringorInt     `yaml:"cpu_shares,omitempty"`
    29  	Command        yaml.Command         `yaml:"command,flow,omitempty"`
    30  	ContainerName  string               `yaml:"container_name,omitempty"`
    31  	Devices        []string             `yaml:"devices,omitempty"`
    32  	DNS            yaml.Stringorslice   `yaml:"dns,omitempty"`
    33  	DNSOpts        []string             `yaml:"dns_opt,omitempty"`
    34  	DNSSearch      yaml.Stringorslice   `yaml:"dns_search,omitempty"`
    35  	Dockerfile     string               `yaml:"dockerfile,omitempty"`
    36  	DomainName     string               `yaml:"domainname,omitempty"`
    37  	Entrypoint     yaml.Command         `yaml:"entrypoint,flow,omitempty"`
    38  	EnvFile        yaml.Stringorslice   `yaml:"env_file,omitempty"`
    39  	Environment    yaml.MaporEqualSlice `yaml:"environment,omitempty"`
    40  	GroupAdd       []string             `yaml:"group_add,omitempty"`
    41  	Hostname       string               `yaml:"hostname,omitempty"`
    42  	Image          string               `yaml:"image,omitempty"`
    43  	Isolation      string               `yaml:"isolation,omitempty"`
    44  	Labels         yaml.SliceorMap      `yaml:"labels,omitempty"`
    45  	Links          yaml.MaporColonSlice `yaml:"links,omitempty"`
    46  	LogDriver      string               `yaml:"log_driver,omitempty"`
    47  	MacAddress     string               `yaml:"mac_address,omitempty"`
    48  	MemLimit       yaml.MemStringorInt  `yaml:"mem_limit,omitempty"`
    49  	MemSwapLimit   yaml.MemStringorInt  `yaml:"memswap_limit,omitempty"`
    50  	MemSwappiness  yaml.MemStringorInt  `yaml:"mem_swappiness,omitempty"`
    51  	Name           string               `yaml:"name,omitempty"`
    52  	Net            string               `yaml:"net,omitempty"`
    53  	OomKillDisable bool                 `yaml:"oom_kill_disable,omitempty"`
    54  	OomScoreAdj    yaml.StringorInt     `yaml:"oom_score_adj,omitempty"`
    55  	Pid            string               `yaml:"pid,omitempty"`
    56  	Uts            string               `yaml:"uts,omitempty"`
    57  	Ipc            string               `yaml:"ipc,omitempty"`
    58  	Ports          []string             `yaml:"ports,omitempty"`
    59  	Privileged     bool                 `yaml:"privileged,omitempty"`
    60  	Restart        string               `yaml:"restart,omitempty"`
    61  	ReadOnly       bool                 `yaml:"read_only,omitempty"`
    62  	ShmSize        yaml.MemStringorInt  `yaml:"shm_size,omitempty"`
    63  	StdinOpen      bool                 `yaml:"stdin_open,omitempty"`
    64  	SecurityOpt    []string             `yaml:"security_opt,omitempty"`
    65  	StopSignal     string               `yaml:"stop_signal,omitempty"`
    66  	Tmpfs          yaml.Stringorslice   `yaml:"tmpfs,omitempty"`
    67  	Tty            bool                 `yaml:"tty,omitempty"`
    68  	User           string               `yaml:"user,omitempty"`
    69  	VolumeDriver   string               `yaml:"volume_driver,omitempty"`
    70  	Volumes        []string             `yaml:"volumes,omitempty"`
    71  	VolumesFrom    []string             `yaml:"volumes_from,omitempty"`
    72  	WorkingDir     string               `yaml:"working_dir,omitempty"`
    73  	Expose         []string             `yaml:"expose,omitempty"`
    74  	ExternalLinks  []string             `yaml:"external_links,omitempty"`
    75  	LogOpt         map[string]string    `yaml:"log_opt,omitempty"`
    76  	ExtraHosts     []string             `yaml:"extra_hosts,omitempty"`
    77  	Ulimits        yaml.Ulimits         `yaml:"ulimits,omitempty"`
    78  }
    79  
    80  // Log holds v2 logging information
    81  type Log struct {
    82  	Driver  string            `yaml:"driver,omitempty"`
    83  	Options map[string]string `yaml:"options,omitempty"`
    84  }
    85  
    86  // ServiceConfig holds version 2 of libcompose service configuration
    87  type ServiceConfig struct {
    88  	Build           yaml.Build           `yaml:"build,omitempty"`
    89  	CapAdd          []string             `yaml:"cap_add,omitempty"`
    90  	CapDrop         []string             `yaml:"cap_drop,omitempty"`
    91  	CPUSet          string               `yaml:"cpuset,omitempty"`
    92  	CPUShares       yaml.StringorInt     `yaml:"cpu_shares,omitempty"`
    93  	CPUQuota        yaml.StringorInt     `yaml:"cpu_quota,omitempty"`
    94  	Command         yaml.Command         `yaml:"command,flow,omitempty"`
    95  	CgroupParent    string               `yaml:"cgroup_parent,omitempty"`
    96  	ContainerName   string               `yaml:"container_name,omitempty"`
    97  	Devices         []string             `yaml:"devices,omitempty"`
    98  	DependsOn       []string             `yaml:"depends_on,omitempty"`
    99  	DNS             yaml.Stringorslice   `yaml:"dns,omitempty"`
   100  	DNSOpts         []string             `yaml:"dns_opt,omitempty"`
   101  	DNSSearch       yaml.Stringorslice   `yaml:"dns_search,omitempty"`
   102  	DomainName      string               `yaml:"domainname,omitempty"`
   103  	Entrypoint      yaml.Command         `yaml:"entrypoint,flow,omitempty"`
   104  	EnvFile         yaml.Stringorslice   `yaml:"env_file,omitempty"`
   105  	Environment     yaml.MaporEqualSlice `yaml:"environment,omitempty"`
   106  	Expose          []string             `yaml:"expose,omitempty"`
   107  	Extends         yaml.MaporEqualSlice `yaml:"extends,omitempty"`
   108  	ExternalLinks   []string             `yaml:"external_links,omitempty"`
   109  	ExtraHosts      []string             `yaml:"extra_hosts,omitempty"`
   110  	GroupAdd        []string             `yaml:"group_add,omitempty"`
   111  	Image           string               `yaml:"image,omitempty"`
   112  	Isolation       string               `yaml:"isolation,omitempty"`
   113  	Hostname        string               `yaml:"hostname,omitempty"`
   114  	Ipc             string               `yaml:"ipc,omitempty"`
   115  	Labels          yaml.SliceorMap      `yaml:"labels,omitempty"`
   116  	Links           yaml.MaporColonSlice `yaml:"links,omitempty"`
   117  	Logging         Log                  `yaml:"logging,omitempty"`
   118  	MacAddress      string               `yaml:"mac_address,omitempty"`
   119  	MemLimit        yaml.MemStringorInt  `yaml:"mem_limit,omitempty"`
   120  	MemReservation  yaml.MemStringorInt  `yaml:"mem_reservation,omitempty"`
   121  	MemSwapLimit    yaml.MemStringorInt  `yaml:"memswap_limit,omitempty"`
   122  	MemSwappiness   yaml.MemStringorInt  `yaml:"mem_swappiness,omitempty"`
   123  	NetworkMode     string               `yaml:"network_mode,omitempty"`
   124  	Networks        *yaml.Networks       `yaml:"networks,omitempty"`
   125  	OomKillDisable  bool                 `yaml:"oom_kill_disable,omitempty"`
   126  	OomScoreAdj     yaml.StringorInt     `yaml:"oom_score_adj,omitempty"`
   127  	Pid             string               `yaml:"pid,omitempty"`
   128  	Ports           []string             `yaml:"ports,omitempty"`
   129  	Privileged      bool                 `yaml:"privileged,omitempty"`
   130  	SecurityOpt     []string             `yaml:"security_opt,omitempty"`
   131  	ShmSize         yaml.MemStringorInt  `yaml:"shm_size,omitempty"`
   132  	StopGracePeriod string               `yaml:"stop_grace_period,omitempty"`
   133  	StopSignal      string               `yaml:"stop_signal,omitempty"`
   134  	Tmpfs           yaml.Stringorslice   `yaml:"tmpfs,omitempty"`
   135  	VolumeDriver    string               `yaml:"volume_driver,omitempty"`
   136  	Volumes         *yaml.Volumes        `yaml:"volumes,omitempty"`
   137  	VolumesFrom     []string             `yaml:"volumes_from,omitempty"`
   138  	Uts             string               `yaml:"uts,omitempty"`
   139  	Restart         string               `yaml:"restart,omitempty"`
   140  	ReadOnly        bool                 `yaml:"read_only,omitempty"`
   141  	StdinOpen       bool                 `yaml:"stdin_open,omitempty"`
   142  	Tty             bool                 `yaml:"tty,omitempty"`
   143  	User            string               `yaml:"user,omitempty"`
   144  	WorkingDir      string               `yaml:"working_dir,omitempty"`
   145  	Ulimits         yaml.Ulimits         `yaml:"ulimits,omitempty"`
   146  }
   147  
   148  // VolumeConfig holds v2 volume configuration
   149  type VolumeConfig struct {
   150  	Driver     string            `yaml:"driver,omitempty"`
   151  	DriverOpts map[string]string `yaml:"driver_opts,omitempty"`
   152  	External   yaml.External     `yaml:"external,omitempty"`
   153  }
   154  
   155  // Ipam holds v2 network IPAM information
   156  type Ipam struct {
   157  	Driver string       `yaml:"driver,omitempty"`
   158  	Config []IpamConfig `yaml:"config,omitempty"`
   159  }
   160  
   161  // IpamConfig holds v2 network IPAM configuration information
   162  type IpamConfig struct {
   163  	Subnet     string            `yaml:"subnet,omitempty"`
   164  	IPRange    string            `yaml:"ip_range,omitempty"`
   165  	Gateway    string            `yaml:"gateway,omitempty"`
   166  	AuxAddress map[string]string `yaml:"aux_addresses,omitempty"`
   167  }
   168  
   169  // NetworkConfig holds v2 network configuration
   170  type NetworkConfig struct {
   171  	Driver     string            `yaml:"driver,omitempty"`
   172  	DriverOpts map[string]string `yaml:"driver_opts,omitempty"`
   173  	External   yaml.External     `yaml:"external,omitempty"`
   174  	Ipam       Ipam              `yaml:"ipam,omitempty"`
   175  }
   176  
   177  // Config holds libcompose top level configuration
   178  type Config struct {
   179  	Version  string                 `yaml:"version,omitempty"`
   180  	Services RawServiceMap          `yaml:"services,omitempty"`
   181  	Volumes  map[string]interface{} `yaml:"volumes,omitempty"`
   182  	Networks map[string]interface{} `yaml:"networks,omitempty"`
   183  }
   184  
   185  // NewServiceConfigs initializes a new Configs struct
   186  func NewServiceConfigs() *ServiceConfigs {
   187  	return &ServiceConfigs{
   188  		m: make(map[string]*ServiceConfig),
   189  	}
   190  }
   191  
   192  // ServiceConfigs holds a concurrent safe map of ServiceConfig
   193  type ServiceConfigs struct {
   194  	m  map[string]*ServiceConfig
   195  	mu sync.RWMutex
   196  }
   197  
   198  // Has checks if the config map has the specified name
   199  func (c *ServiceConfigs) Has(name string) bool {
   200  	c.mu.RLock()
   201  	defer c.mu.RUnlock()
   202  	_, ok := c.m[name]
   203  	return ok
   204  }
   205  
   206  // Get returns the config and the presence of the specified name
   207  func (c *ServiceConfigs) Get(name string) (*ServiceConfig, bool) {
   208  	c.mu.RLock()
   209  	defer c.mu.RUnlock()
   210  	service, ok := c.m[name]
   211  	return service, ok
   212  }
   213  
   214  // Add add the specifed config with the specified name
   215  func (c *ServiceConfigs) Add(name string, service *ServiceConfig) {
   216  	c.mu.Lock()
   217  	c.m[name] = service
   218  	c.mu.Unlock()
   219  }
   220  
   221  // Remove removes the config with the specified name
   222  func (c *ServiceConfigs) Remove(name string) {
   223  	c.mu.Lock()
   224  	delete(c.m, name)
   225  	c.mu.Unlock()
   226  }
   227  
   228  // Len returns the len of the configs
   229  func (c *ServiceConfigs) Len() int {
   230  	c.mu.RLock()
   231  	defer c.mu.RUnlock()
   232  	return len(c.m)
   233  }
   234  
   235  // Keys returns the names of the config
   236  func (c *ServiceConfigs) Keys() []string {
   237  	keys := []string{}
   238  	c.mu.RLock()
   239  	defer c.mu.RUnlock()
   240  	for name := range c.m {
   241  		keys = append(keys, name)
   242  	}
   243  	return keys
   244  }
   245  
   246  // All returns all the config at once
   247  func (c *ServiceConfigs) All() map[string]*ServiceConfig {
   248  	c.mu.RLock()
   249  	defer c.mu.RUnlock()
   250  	return c.m
   251  }
   252  
   253  // RawService is represent a Service in map form unparsed
   254  type RawService map[string]interface{}
   255  
   256  // RawServiceMap is a collection of RawServices
   257  type RawServiceMap map[string]RawService
   258  
   259  // ParseOptions are a set of options to customize the parsing process
   260  type ParseOptions struct {
   261  	Interpolate bool
   262  	Validate    bool
   263  	Preprocess  func(RawServiceMap) (RawServiceMap, error)
   264  	Postprocess func(map[string]*ServiceConfig) (map[string]*ServiceConfig, error)
   265  }