Browse Source

Correct some errors

gabriel 6 years ago
parent
commit
f96b82bde1
3 changed files with 113 additions and 90 deletions
  1. 2 2
      src/Docker/Composer/FileFormat.php
  2. 86 70
      src/Docker/Composer/Service.php
  3. 25 18
      test/BaseTest.php

+ 2 - 2
src/Docker/Composer/FileFormat.php

@@ -36,7 +36,7 @@ class FileFormat
     function __construct($path = ".", $version = "")
     {
         $this->path = $path;
-        $this->version= $version;
+        $this->version = $version . "";
         $this->services = array();
         $this->volumes = new Volumes();
         $this->network = new Network();
@@ -68,7 +68,7 @@ class FileFormat
      */
     function render()
     {
-        $rtr = array("version" => "'" . $this->version . "'", "services" => array());
+        $rtr = array("version" => $this->version, "services" => array());
         foreach ($this->services as $name => $service) {
             $rtr["services"][$name] = $service->getConfig();
         }

+ 86 - 70
src/Docker/Composer/Service.php

@@ -766,26 +766,6 @@ class Service
         return $this;
     }
 
-    /**
-     * @param $cpu_shares
-     * @return $this
-     */
-    function cpu_shares($cpu_shares)
-    {
-        $this->config["cpu_shares"] = $cpu_shares;
-        return $this;
-    }
-
-    /**
-     * @param $cpu_quota
-     * @return $this
-     */
-    function cpu_quota($cpu_quota)
-    {
-        $this->config["cpu_quota"] = $cpu_quota;
-        return $this;
-    }
-
     /**
      * @param $cpus
      * @return $this
@@ -796,16 +776,6 @@ class Service
         return $this;
     }
 
-    /**
-     * @param $cpuset
-     * @return $this
-     */
-    function cpuset($cpuset)
-    {
-        $this->config["cpuset"] = $cpuset;
-        return $this;
-    }
-
     /**
      * @param $domainname
      * @return $this
@@ -846,46 +816,6 @@ class Service
         return $this;
     }
 
-    /**
-     * @param $mem_limit
-     * @return $this
-     */
-    function mem_limit($mem_limit)
-    {
-        $this->config["mem_limit"] = $mem_limit;
-        return $this;
-    }
-
-    /**
-     * @param $memswap_limit
-     * @return $this
-     */
-    function memswap_limit($memswap_limit)
-    {
-        $this->config["memswap_limit"] = $memswap_limit;
-        return $this;
-    }
-
-    /**
-     * @param $mem_swappiness
-     * @return $this
-     */
-    function mem_swappiness($mem_swappiness)
-    {
-        $this->config["mem_swappiness"] = $mem_swappiness;
-        return $this;
-    }
-
-    /**
-     * @param $mem_reservation
-     * @return $this
-     */
-    function mem_reservation($mem_reservation)
-    {
-        $this->config["mem_reservation"] = $mem_reservation;
-        return $this;
-    }
-
     /**
      * @param $oom_score_adj
      * @return $this
@@ -966,6 +896,92 @@ class Service
         return $this;
     }
 
+    /**
+     * @param string $cpu Example 0.5 or 1 or 1.3
+     */
+    public function setLimitCPU($cpu)
+    {
+        if (!isset($this->config['deploy']) ||
+            !is_array($this->config['deploy'])) {
+            $this->config['deploy'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']) ||
+            !is_array($this->config['deploy']['resources'])) {
+            $this->config['deploy']['resources'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']['limits']) ||
+            !is_array($this->config['deploy']['resources']['limits'])) {
+            $this->config['deploy']['resources']['limits'] = array();
+        }
+        $this->config['deploy']['resources']['limits']["cpus"] = $cpu;
+        return $this;
+    }
+
+    /**
+     * @param string $memory Example 10M or 1G
+     */
+    public function setLimitMemory($memory)
+    {
+        if (!isset($this->config['deploy']) ||
+            !is_array($this->config['deploy'])) {
+            $this->config['deploy'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']) ||
+            !is_array($this->config['deploy']['resources'])) {
+            $this->config['deploy']['resources'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']['limits']) ||
+            !is_array($this->config['deploy']['resources']['limits'])) {
+            $this->config['deploy']['resources']['limits'] = array();
+        }
+        $this->config['deploy']['resources']['limits']["memory"] = $memory;
+        return $this;
+    }
+
+    /**
+     * @param string $cpu Example 0.5 or 1 or 1.3
+     */
+    public function setReservationCPU($cpu)
+    {
+        if (!isset($this->config['deploy']) ||
+            !is_array($this->config['deploy'])) {
+            $this->config['deploy'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']) ||
+            !is_array($this->config['deploy']['resources'])) {
+            $this->config['deploy']['resources'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']['reservations']) ||
+            !is_array($this->config['deploy']['resources']['reservations'])) {
+            $this->config['deploy']['resources']['reservations'] = array();
+        }
+        $this->config['deploy']['resources']['reservations']["cpus"] = $cpu;
+        return $this;
+    }
+
+    /**
+     * @param string $memory Example 10M or 1G
+     */
+    public function setReservationMemory($memory)
+    {
+        if (!isset($this->config['deploy']) ||
+            !is_array($this->config['deploy'])) {
+            $this->config['deploy'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']) ||
+            !is_array($this->config['deploy']['resources'])) {
+            $this->config['deploy']['resources'] = array();
+        }
+        if (!isset($this->config['deploy']['resources']['reservations']) ||
+            !is_array($this->config['deploy']['resources']['reservations'])) {
+            $this->config['deploy']['resources']['reservations'] = array();
+        }
+        $this->config['deploy']['resources']['reservations']["memory"] = $memory;
+//        $this->config['deploy']['resources']['reservations'][] = "memory: $memory";
+//        $this->config["volumes"][] = $from . ":" . $to;
+        return $this;
+    }
+
     /**
      * @return array
      */

+ 25 - 18
test/BaseTest.php

@@ -10,7 +10,7 @@ class BaseTest extends TestCase
 {
     public function testSimpleUsage()
     {
-        $composer = new FileFormat2();
+        $composer = new \Docker\Composer\FileFormat("", "2.2");
         $composer->addService("Base")->image("ubuntu:16.10");
         $composer->addService("Ftth")->image("ubuntu:17.10");
         $composer->addService("ubuntu")->image("ubuntu");
@@ -19,6 +19,11 @@ class BaseTest extends TestCase
         $composer->addService("test2")->build("./custom2")->addVolumes('volumen_test1_test2', '/opt/test2')
             ->network('flowdat3')->ipv4_address("172.172.172.2");
 
+        $composer->addService("test3")->build("./custom3")
+            ->setReservationMemory("1G")
+            ->setReservationCPU("1")
+            ->setLimitCPU("0.3");
+
         $composer->getVolumes()->addVolumen('volumen_test1_test2', 'local');
 
         $nc = new \Docker\Composer\NetworkConfig();
@@ -27,22 +32,24 @@ class BaseTest extends TestCase
 
         $rtr = Yaml::parse($composer->render());
 
-        $this->assertEquals(
-            array(
-                "version" => "2.2",
-                "services" => array(
-                    "Base" => array("image" => "ubuntu:16.10"),
-                    "Ftth" => array("image" => "ubuntu:17.10"),
-                    "ubuntu" => array("image" => "ubuntu"),
-                    "custom" => array("build" => array("context" => "./custom")),
-                    "test1" => array("build" => array("context" => "./custom1"), "volumes" => array('volumen_test1_test2:/opt/test1')),
-                    "test2" => array("build" => array("context" => "./custom2"), "volumes" => array('volumen_test1_test2:/opt/test2'),
-                        "networks" => array("flowdat3" => array("ipv4_address" => "172.172.172.2"))),
-                ),
-                "volumes" => array('volumen_test1_test2' => array('driver' => 'local')),
-                "networks" => array('flowdat3' => array('driver' => 'bridge',
-                    'ipam' => array('driver' => 'default',
-                        'config' => array(array('subnet' => '172.172.172.0/16'), array('gateway' => '172.172.172.1')))))
-            ), $rtr);
+        $expected = array(
+            "version" => "2.2",
+            "services" => array(
+                "Base" => array("image" => "ubuntu:16.10"),
+                "Ftth" => array("image" => "ubuntu:17.10"),
+                "ubuntu" => array("image" => "ubuntu"),
+                "custom" => array("build" => array("context" => "./custom")),
+                "test1" => array("build" => array("context" => "./custom1"), "volumes" => array('volumen_test1_test2:/opt/test1')),
+                "test2" => array("build" => array("context" => "./custom2"), "volumes" => array('volumen_test1_test2:/opt/test2'),
+                    "networks" => array("flowdat3" => array("ipv4_address" => "172.172.172.2"))),
+                "test3" => array("build" => array("context" => "./custom3"),
+                    "deploy" => array('resources' => array('reservations' => array("memory" => "1G", "cpus" => "1"), 'limits' => array("cpus" => "0.3")))),
+            ),
+            "volumes" => array('volumen_test1_test2' => array('driver' => 'local')),
+            "networks" => array('flowdat3' => array('driver' => 'bridge',
+                'ipam' => array('driver' => 'default',
+                    'config' => array(array('subnet' => '172.172.172.0/24'))))));
+
+        $this->assertEquals($expected, $rtr);
     }
 }