Luciano Andrade 8 年之前
父节点
当前提交
1bcba2c2db
共有 1 个文件被更改,包括 389 次插入55 次删除
  1. 389 55
      src/Docker/Composer/FileFormat2.php

+ 389 - 55
src/Docker/Composer/FileFormat2.php

@@ -1,15 +1,67 @@
 <?php
 
-namespace Docker\Composer
+namespace Docker\Composer;
 use Extension;
 
+class NetworkService{
+	protected $name;
+	protected $config;
+	function __construct($name){
+		$this->name = $name;
+	}
+
+	/**
+        * aliases
+        */
+	function aliases($aliases){
+		$this->config["aliases"] = array_values((array)$aliases);
+		return $this;
+	}
+
+	function addAlias($alias){
+		if(!is_array($this->config["aliases"])) $this->config["aliases"] = array();
+		$this->config["aliases"][] = $alias;
+		return $this;
+	}
+
+        /**
+        * ipv4_address, 
+        */
+	function ipv4_address($ipv4_address){
+		$this->config["ipv4_address"] = $ipv4_address;
+	}
+
+	/**
+	* ipv6_address
+        */
+	function ipv6_address($ipv6_address){
+		$this->config["ipv6_address"] = $ipv6_address;
+	}
+
+        /**
+        * link_local_ips
+        */
+	function link_local_ips($link_local_ips){
+		$this->config["link_local_ips"] = array_values((array)$link_local_ips);
+		return $this;
+	}
+	function addLink_local_ips($link_local_ip){
+		if(!is_array($this->config["link_local_ips"])) $this->config["link_local_ips"] = array();
+		$this->config["link_local_ips"][] = $link_local_ip;
+		return $this;
+	}
+}
+
 class Service{
 
 	protected $name;
 	protected $config;
+	protected $networks;
 
-	function __construct($name, $){
+	function __construct($name){
 		$this->name = $name;
+		$this->config =  array();
+		$this->networks = array();
 	}
 
 	function getName($name){ return $this->name; }
@@ -47,7 +99,7 @@ class Service{
 
 	function command($cmd){
 		$this->config["command"] = $cmd; 
-		return $this:
+		return $this;
 	}
 
 	function getCommand(){ return $this->config["command"]; }
@@ -76,7 +128,7 @@ class Service{
 		return $this;
 	}
 
-	function addDevice($src $dest){
+	function addDevice($src, $dest){
 		$this->config["devices"][] =  $src . ":" . $dest;
 		return $this;
 	}
@@ -158,166 +210,450 @@ class Service{
         /**
         * env_file
         */
-	protected $env_file;
+	function env_file($env_file){
+		$this->config["env_file"] = array_values((array)$env_file);
+		return $this;
+	}
+
+	function addEnv_file($env_file){
+		if(!is_array($this->config["env_file"])) $this->config["env_file"] = array();
+		$this->config["env_file"][] = $env_file;
+		return $this;
+	}
 
         /**
         * environment
         */
-	protected $environment;
+	function environment($environment){
+		if(!is_int(key($environment))){
+			$new_env = array();
+			foreach($environment as $key => $value){
+				$new_env[] = $key . "=" . $value;
+			}
+		}
+		$this->config["environment"] = array_values((array)$environment);
+		return $this;
+	}
 
+	function addEnviroment($var, $value){
+		if(!is_array($this->config["environment"])) $this->config["environment"] = array();
+		$this->config["environment"][] = $var . "=" . $value;  
+		return $this;
+	}
+ 
         /**
         * expose
         */
-	protected $expose;
+	function expose($expose){
+		$this->config["expose"] = array_values((array)$expose);
+		return $this;
+	}
+
+	function addExpose($from, $to){
+		if(!is_array($this->config["expose"])) $this->config["expose"] = array();
+		$this->config["expose"][] = $from. ":" . $to;  
+		return $this;
+	}
 
         /**
         * extends
         */
-	protected $extends;
+	function extends($service, $file = null){
+		$this->config["extends"]["service"] = $service;
+		if($file){
+			$this->config["extends"]["file"] = $file;
+		}
+	}
 
         /**
         * external_links
         */
-	protected $external_links;
+	function external_links($external_links){
+		$this->config["external_links"] = array_values((array)$external_links);
+		return $this;
+	}
+
+	function addExternal_links($from, $to = null){
+		if(!is_array($this->config["external_links"])) $this->config["external_links"] = array();
+		if(!$to){
+			$this->config["external_links"][] = $from;  
+		}else{
+			$this->config["external_links"][] = $from. ":" . $to;  
+		}
+		return $this;
+	}
 
         /**
         * extra_hosts
         */
-	protected $extra_hosts;
+	function extra_hosts($extra_hosts){
+		$this->config["extra_hosts"] = array_values((array)$extra_hosts);
+		return $this;
+	}
+
+	function addExtra_hosts($from, $to){
+		if(!is_array($this->config["extra_hosts"])) $this->config["extra_hosts"] = array();
+		$this->config["extra_hosts"][] = $from. ":" . $to;  
+		return $this;
+	}
 
         /**
         * group_add
+        * TODO : addGroup_add ? and reset the config[group_add] entry with this method?
         */
-	protected $group_add;
+	function group_add($group_add){
+		if(!is_array($this->config["egroup_add"])) $this->config["group_add"] = array();
+		$this->config["group_add"] = $this->config["group_add"] + array_values((array)$group_add);
+		return $this;
+	}
 
         /**
         * healthcheck
         */
-	protected $healthcheck;
+	function healthcheck($test, $interval = null, $timeout=null, $retries=null){
+		if($test === false){
+			$this->config["healthcheck"]["disabled"] = true;
+		}else{
+			$this->config["healthcheck"] = array(
+				"test" => $test,
+				"interval" => $interval,
+				"timeout" => $timeout,
+				"retries" => $retries,
+				);
+		}
+
+		return $this;
+	}
 
         /**
         * image
         */
-	protected $image;
+	function image($image){
+		$this->config["image"] = $image;
+		return $this;
+	}
 
         /**
         * init
         */
-	protected $init
+	function init($init){
+		$this->config["init"] = $init;
+	}
+
         /**
         * isolation
         */
-	protected $isolation;
+	function isolation($isolation){
+		$this->config["isolation"] = $isolation;
+		return $this;
+	}
 
         /**
         * labels
         */
-	protected $labels;
+	function labels($labels){
+		$this->config["labels"] = $labels;
+		return $this;
+	}
+
+	function addLabels($label, $value){
+		if(!is_array($this->config["labels"])) $this->config["labels"] = array();
+		$this->config["labels"][$label] = $value;
+		return $this;
+	}
 
         /**
         * links
         */
-	protected $links;
+	function links($links){
+		$this->config["links"] = array_values((array)$links);
+		return $this;
+	}
+	function addLinks($source, $dest){
+		if(!is_array($this->config["links"])) $this->config["links"] = array();
+		$this->config["links"][] = $source . ":" . $dest;
+		return $this;
+	}
 
         /**
         * logging
         */
-	protected $logging;
+	function logging($driver, $options = false){
+		$this->config["logging"]["driver"] = $driver;
+		if($options){
+			$this->config["logging"]["options"] = $options;
+		}
+		return $this;	
+	}
 
         /**
         * network_mode
         */
-	protected $network_mode;
+	function network_mode($network_mode){
+		$this->config["network_mode"] = $network_mode;
+		return $this;
+	}
 
         /**
         * networks
         */
-	protected $networks;
-
-        /**
-        * aliases
-        */
-	protected $aliases;
-
-        /**
-        * ipv4_address, 
-        */
-	protected $ipv4_address;
-
-	/**
-	* ipv6_address
-        */
-	protected $ipv6_address;
+	function network($network){
+		$netObj = $this->networks[$network] = new NetworkService($network);
+		return $netObj;
+	}
 
-        /**
-        * link_local_ips
-        */
-	protected $link_local_ips;
+	function getNetworks($network){
+		return $this->networks[$networks];
+	}
 
+        
         /**
         * pid
         */
-	protected $pid;
+	function pid($pid){
+		$this->config["pid"] = $pid;
+		return $this;
+	}
 
         /**
         * pids_limit
         */
-	protected $pids_limit;
+	function pids_limit($pids_limit){
+		$this->config["pids_limit"] = $pids_limit;
+		return $this;
+	}
 
         /**
         * ports
         */
-	protected $ports;
+	function ports($ports){
+		$this->config["ports"] = array_values((array)$ports);
+		return $this;
+	}
+
+	function addPorts($from, $to = false){
+		if(!is_array($this->config["ports"])) $this->config["ports"] = array();
+		if(!$to){
+			$this->config["ports"][] = $from;
+		}else{
+			$this->config["ports"][] = $from . ":" . $to;
+		}
+		return $this;
+	}
 
         /**
         * security_opt
         */
-	protected $security_opt;
+	function security_opt($security_opt){
+		$this->config["security_opt"] = array_values((array)$security_opt);
+		return $this;
+	}
+
+	function addSecurity_opt($security_opt){
+		if(!is_array($this->config["security_opt"])) $this->config["security_opt"] = array();
+		$this->config["ports"][] = $security_opt;
+		return $this;
+	}
+
 
         /**
         * stop_grace_period
         */
-	protected $stop_grace_period;
+	function stop_grace_period($stop_grace_period){
+		$this->config["stop_grace_period"] = $stop_grace_period;
+		return $this;
+	}
 
         /**
         * stop_signal
         */
-	protected $stop_signal;
+	function stop_signal($stop_signal){
+		$this->config["stop_signal"] = $stop_signal;
+		return $stop_signal;
+	}
 
         /**
         * sysctls
         */
-	protected $sysctls;
+	function sysctls($sysctls){
+		$this->config["sysctls"] = array_values((array)$sysctls);
+		return $this;
+	}
+
+	function addSysctls($sysctl, $value){
+		if(!is_array( $this->config["sysctls"])) $this->config["sysctls"] = array();
+		$this->config["sysctls"][] = $sysctls . "=" . $value;
+		return $this;
+	}
 
         /**
         * ulimits
         */
-	protected $ulimits;
+	function ulimits($ulimit, $config){
+		if(!is_array( $this->config["ulimits"])) $this->config["ulimits"] = array();
+		$this->config["ulimits"][$ulimit] = $config;
+		return $this;
+	}
+
 
         /**
         * userns_mode
         */
-	protected $userns_mode;
+	function userns_mode($userns_mode){
+		$this->config["userns_mode"] = $userns_mode;
+		return $this;
+		
+	}
 
         /**
         * volumes
         */
-	protected $volumes;
+	function volumes($volumes){
+		$this->config["volumes"] = array_values((array)$volumes);
+		return $this;
+	}
+
+	function addVolumes($from, $to = false){
+		if(!is_array($this->config["volumes"])) $this->config["volumes"] = array();
+		if(!$to){
+			$this->config["volumes"][] = $from;  
+		}else{
+			$this->config["volumes"][] = $from . ":" . $to;  
+		}
+		return $this;
+	}
 
 	/**
-	* volume_driver
+	* volume_driver, not supported
 	*/
 	protected $volume_driver;
 
         /**
         * volumes_from
         */
-	protected $volumes_from;
+	function volumes_from($volumes_from){
+		$this->config["volumes_from"] = array_values((array)$volumes_from);
+		return $this;
+	}
+
+	function addVolumes_from($volumes_from){
+		if(!is_array($this->config["volumes_from"])) $this->config["volumes_from"] = array();
+		$this->config["volumes_from"][] = $volumes_from;
+		return $this;
+	}
+
 
         /**
         * restart
         */
-	protected $restart;
+	function restart($restart){
+		$this->config["restart"] = $restart;
+		return $this;
+	}
+	
+
+	function cpu_count($cpu_count){ 
+		$this->config["cpu_count"] = $cpu_count;
+		return $this;
+	}
+
+	function cpu_percent($cpu_percent){
+		$this->config["cpu_percent"] = $cpu_percent;
+		return $this;
+	}
+	function cpu_shares($cpu_shares){
+		$this->config["cpu_shares"] = $cpu_shares;
+		return $this;
+	}
+	function cpu_quota($cpu_quota){
+		$this->config["cpu_quota"] = $cpu_quota;
+		return $this;
+	}
+	function cpus($cpus){
+		$this->config["cpus"] = $cpus;
+		return $this;
+	}
+	function cpuset($cpuset){
+		$this->config["cpuset"] = $cpuset;
+		return $this;
+	}
+	function domainname($domainname){
+		$this->config["domainname"] = $domainname;
+		return $this;
+	}
+	function hostname($hostname){
+		$this->config["hostname"] = $hostname;
+		return $this;
+	}
+
+	function ipc($ipc){
+		$this->config["ipc"] = $ipc;
+		return $this;
+	}
+	function mac_address($mac_address){
+		$this->config["mac_address"] = $mac_address;
+		return $this;
+	}
+
+	function mem_limit($mem_limit){
+		$this->config["mem_limit"] = $mem_limit;
+		return $this;
+	}
+
+	function memswap_limit($memswap_limit){
+		$this->config["memswap_limit"] = $memswap_limit;
+		return $this;
+	}
+
+	function mem_swappiness($mem_swappiness){
+		$this->config["mem_swappiness"] = $mem_swappiness;
+		return $this;
+	}
+
+	function mem_reservation($mem_reservation){
+		$this->config["mem_reservation"] = $mem_reservation;
+		return $this;
+	}
+
+	function oom_score_adj($oom_score_adj){
+		$this->config["oom_score_adj"] = $oom_score_adj;
+		return $this;
+	}
+
+	function privileged($privileged){
+		$this->config["privileged"] = $privileged;
+		return $this;
+	}
+
+	function read_only($read_only){
+		$this->config["read_only"] = $read_only;
+		return $this;
+	}
+
+	function shm_size($shm_size){
+		$this->config["shm_size"] = $shm_size;
+		return $this;
+	}
+
+	function stdin_open($stdin_open){
+		$this->config["stdin_open"] = $stdin_open;
+		return $this;
+	}
+
+	function tty($tty){
+		$this->config["tty"] = $tty;
+		return $this;
+	}
+
+	function user($user){
+		$this->config["user"] = $user;
+		return $this;
+	}
+
+	function working_dir($working_dir){
+		$this->config["working_dir"] = $working_dir;
+		return $this;
+	}
 }
 
 class ServiceNotFoundException extends Extension
@@ -348,10 +684,8 @@ class FileFormat2
 
 	function service($name){
 		if(!$this->services[$name]) throw new ServiceNotFoundException($name);
-		return $this->services[$name]
+		return $this->services[$name];
         }
-
-	
 }