consistent exit codes
[pharext/pharext] / src / pharext / Installer.php
index c88b0d69f8d7b49a72ea4b0214d9bb79ab4f3140..83a40ad9769d4babb16dfd063c27d10f3dd6d5f0 100644 (file)
@@ -2,10 +2,12 @@
 
 namespace pharext;
 
-use Phar;
 use pharext\Cli\Args as CliArgs;
 use pharext\Cli\Command as CliCommand;
 
+use Phar;
+use SplObjectStorage;
+
 /**
  * The extension install command executed by the extension phar
  */
@@ -13,18 +15,6 @@ class Installer implements Command
 {
        use CliCommand;
        
-       /**
-        * The temporary directory we should operate in
-        * @var string
-        */
-       private $tmp;
-
-       /**
-        * The directory we came from
-        * @var string
-        */
-       private $cwd;
-
        /**
         * Create the command
         */
@@ -48,14 +38,37 @@ class Installer implements Command
                                "sudo -S %s"],
                        ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
+                       [null, "signature", "Show package signature",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "license", "Show package license",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "name", "Show package name",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "date", "Show package release date",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "release", "Show package release version",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "version", "Show pharext version",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
                ]);
        }
        
-       /**
-        * Cleanup temp directory
-        */
-       public function __destruct() {
-               $this->cleanup();
+       private function extract(Phar $phar) {
+               $this->debug("Extracting %s ...\n", basename($phar->getPath()));
+               return (new Task\Extract($phar))->run($this->args->verbose);
+       }
+
+       private function hooks(SplObjectStorage $phars) {
+               $hooks = [];
+               foreach ($phars as $phar) {
+                       if (isset($phar["pharext_install.php"])) {
+                               $callable = include $phar["pharext_install.php"];
+                               if (is_callable($callable)) {
+                                       $hooks[] = $callable($this);
+                               }
+                       }
+               }
+               return $hooks;
        }
 
        /**
@@ -63,212 +76,131 @@ class Installer implements Command
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
-               $this->cwd = getcwd();
-               $this->tmp = $this->tempname(basename(Phar::running(false)));
-
-               $phar = new Phar(Phar::running(false));
-               foreach ($phar as $entry) {
-                       if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
-                               $temp = $this->newtemp($entry->getBaseName());
-                               $phar->extractTo($temp, $entry->getFilename(), true);
-                               $phars[$temp] = new Phar($temp."/".$entry->getFilename());
-                       }
-               }
-               $phars[$this->tmp] = $phar;
-
-               foreach ($phars as $phar) {
-                       if (isset($phar["pharext_install.php"])) {
-                               $callable = include $phar["pharext_install.php"];
-                               if (is_callable($callable)) {
-                                       $recv[] = $callable($this);
+               try {
+                       $list = new SplObjectStorage();
+                       $phar = new Phar(Phar::running(false));
+                       $temp = $this->extract($phar);
+
+                       foreach ($phar as $entry) {
+                               $dep_file = $entry->getBaseName();
+                               if (fnmatch("*.ext.phar*", $dep_file)) {
+                                       $dep_phar = new Phar("$temp/$dep_file");
+                                       $list[$dep_phar] = $this->extract($dep_phar);
                                }
                        }
+                       /* the actual ext.phar at last */
+                       $list[$phar] = $temp;
+
+                       /* installer hooks */
+                       $hook = $this->hooks($list);
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EEXTRACT);
                }
-               
+
+               /* standard arg stuff */
                $errs = [];
                $prog = array_shift($argv);
                foreach ($this->args->parse(--$argc, $argv) as $error) {
                        $errs[] = $error;
                }
-               
+
                if ($this->args["help"]) {
                        $this->header();
                        $this->help($prog);
                        exit;
                }
-               
+               try {
+                       foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
+                               if ($this->args[$opt]) {
+                                       printf("%s\n", $this->metadata($opt));
+                                       exit;
+                               }
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EARGS);
+               }
+
                foreach ($this->args->validate() as $error) {
                        $errs[] = $error;
                }
-               
+
                if ($errs) {
                        if (!$this->args["quiet"]) {
                                $this->header();
                        }
                        foreach ($errs as $err) {
                                $this->error("%s\n", $err);
-                       } 
+                       }
                        if (!$this->args["quiet"]) {
                                $this->help($prog);
                        }
-                       exit(1);
-               }
-               
-               if (isset($recv)) {
-                       foreach ($recv as $r) {
-                               $r($this);
-                       }
+                       exit(self::EARGS);
                }
-               foreach ($phars as $temp => $phar) {
-                       $this->installPackage($phar, $temp);
-               }
-       }
 
-       /**
-        * Prepares, configures, builds and installs the extension
-        */
-       private function installPackage(Phar $phar, $temp) {
-               $this->info("Installing %s ... \n", basename($phar->getAlias()));
                try {
-                       $phar->extractTo($temp, null, true);
+                       /* post process hooks */
+                       foreach ($hook as $callback) {
+                               if (is_callable($callback)) {
+                                       $callback($this);
+                               }
+                       }
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(3);
+                       exit(self::EARGS);
                }
 
-               if (!chdir($temp)) {
-                       $this->error(null);
-                       exit(4);
+               /* install packages */
+               try {
+                       foreach ($list as $phar) {
+                               $this->info("Installing %s ...\n", basename($phar->getPath()));
+                               $this->install($list[$phar]);
+                               $this->activate($list[$phar]);
+                               $this->cleanup($list[$phar]);
+                               $this->info("Successfully installed %s!\n", basename($phar->getPath()));
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EINSTALL);
                }
-
+       }
+       
+       /**
+        * Phpize + trinity
+        */
+       private function install($temp) {
                // phpize
-               $this->exec("phpize", $this->php("ize"));
+               $this->info("Running phpize ...\n");
+               $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
+               $phpize->run($this->args->verbose);
 
                // configure
-               $args = ["--with-php-config=". $this->php("-config")];
-               if ($this->args->configure) {
-                       $args = array_merge($args, $this->args->configure);
-               }
-               $this->exec("configure", "./configure", $args);
+               $this->info("Running configure ...\n");
+               $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
+               $configure->run($this->args->verbose);
 
                // make
-               if ($this->args->verbose) {
-                       $this->exec("make", "make", ["-j3"]);
-               } else {
-                       $this->exec("make", "make", ["-j3", "-s"]);
-               }
+               $this->info("Running make ...\n");
+               $make = new Task\Make($temp);
+               $make->run($this->args->verbose);
 
                // install
-               if ($this->args->verbose) {
-                       $this->exec("install", "make", ["install"], true);
-               } else {
-                       $this->exec("install", "make", ["install", "-s"], true);
-               }
-
-               // activate
-               $this->activate();
-
-               // cleanup
-               $this->cleanup($temp);
+               $this->info("Running make install ...\n");
+               $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
+               $install = new Task\Make($temp, ["install"], $sudo);
+               $install->run($this->args->verbose);
        }
 
-       /**
-        * Perform any cleanups
-        */
-       private function cleanup($temp = null) {
-               if (!isset($temp)) {
-                       $temp = $this->tmp;
-               }
+       private function cleanup($temp) {
                if (is_dir($temp)) {
-                       chdir($this->cwd);
-                       $this->info("Cleaning up %s ...\n", $temp);
                        $this->rm($temp);
+               } elseif (file_exists($temp)) {
+                       unlink($temp);
                }
        }
 
-       /**
-        * Execute a program with escalated privileges handling interactive password prompt
-        * @param string $command
-        * @param string $output
-        * @return int
-        */
-       private function sudo($command, &$output) {
-               if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
-                       return -1;
-               }
-               $stdout = $pipes[1];
-               $passwd = 0;
-               while (!feof($stdout)) {
-                       $R = [$stdout]; $W = []; $E = [];
-                       if (!stream_select($R, $W, $E, null)) {
-                               continue;
-                       }
-                       $data = fread($stdout, 0x1000);
-                       /* only check a few times */
-                       if ($passwd++ < 10) {
-                               if (stristr($data, "password")) {
-                                       printf("\n%s", $data);
-                               }
-                       }
-                       $output .= $data;
-               }
-               return proc_close($proc);
-       }
-       /**
-        * Execute a system command
-        * @param string $name pretty name
-        * @param string $command command
-        * @param array $args command arguments
-        * @param bool $sudo whether the command may need escalated privileges
-        */
-       private function exec($name, $command, array $args = null, $sudo = false) {
-               $exec = escapeshellcmd($command);
-               if ($args) {
-                       $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
-               }
-
-               if ($this->args->verbose) {
-                       $this->info("Running %s ...\n", $exec);
-               } else {
-                       $this->info("Running %s ... ", $name);
-               }
-
-               if ($sudo && isset($this->args->sudo)) {
-                       $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output);
-               } elseif ($this->args->verbose) {
-                       passthru($exec ." 2>&1", $retval);
-               } else {
-                       exec($exec ." 2>&1", $output, $retval);
-                       $output = implode("\n", $output);
-               }
-
-               if ($retval) {
-                       $this->error("Command %s failed with (%s)\n", $command, $retval);
-                       if (isset($output) && !$this->args->quiet) {
-                               printf("%s\n", $output);
-                       }
-                       exit(2);
-               }
-               if (!$this->args->verbose) {
-                       // we already have a bunch of output
-                       $this->info("OK\n");
-               }
-       }
-       
-       /**
-        * Construct a command from prefix common-name and suffix
-        * @param type $suffix
-        * @return string
-        */
-       private function php($suffix) {
-               $cmd = $this->args["common-name"] . $suffix;
-               if (isset($this->args->prefix)) {
-                       $cmd = $this->args->prefix . "/bin/" . $cmd;
-               }
-               return $cmd;
-       }
-
-       private function activate() {
+       private function activate($temp) {
                if ($this->args->ini) {
                        $files = [realpath($this->args->ini)];
                } else {
@@ -276,36 +208,13 @@ class Installer implements Command
                        $files[] = php_ini_loaded_file();
                }
 
-               $extension = basename(current(glob("modules/*.so")));
-               $pattern = preg_quote($extension);
-
-               foreach ($files as $index => $file) {
-                       $temp = new Tempfile("phpini");
-                       foreach (file($file) as $line) {
-                               if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
-                                       // already there
-                                       $this->info("Extension already activated\n");
-                                       return;
-                               }
-                               fwrite($temp->getStream(), $line);
-                       }
-               }
-
-               // not found, add extension line to the last process file
-               if (isset($temp, $file)) {
-                       fprintf($temp->getStream(), "extension=%s\n", $extension);
-                       $temp->closeStream();
-
-                       $path = $temp->getPathname();
-                       $stat = stat($file);
-
-                       $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
-                       $this->exec("INI owner transfer", "chown", [$ugid, $path], true);
-                       
-                       $perm = decoct($stat["mode"] & 0777);
-                       $this->exec("INI permission transfer", "chmod", [$perm, $path], true);
-                       
-                       $this->exec("INI activation", "mv", [$path, $file], true);
+               $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
+               $type = $this->metadata("type") ?: "extension";
+               
+               $this->info("Running INI activation ...\n");
+               $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
+               if (!$activate->run($this->args->verbose)) {
+                       $this->info("Extension already activated ...\n");
                }
        }
 }