protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $filters = $input->getArgument('filter');
     $config = $this->getContainer()->getParameter('upstart');
     $dir = "{$config['configDir']}/{$config['project']}";
     $deletions = [false => 0, true => 0];
     if ($filters) {
         $jobs = $this->filter($filters);
         foreach ($jobs as $job) {
             $file = "{$dir}/{$job['name']}.conf";
             if (file_exists($file)) {
                 $deletions[$this->delete($file)]++;
             }
             $files = glob("{$dir}/{$job['name']}-*.conf");
             foreach ($files as $file) {
                 $deletions[$this->delete($file)]++;
             }
         }
     } else {
         $files = glob("{$dir}/*.conf");
         foreach ($files as $file) {
             $deletions[$this->delete($file)]++;
         }
     }
     $tag = $deletions[false] == 0 ? 'info' : 'errors';
     $output->writeln("<{$tag}>Done with {$deletions[true]} deletions, {$deletions[false]} errors.</{$tag}>");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $config = $this->getContainer()->getParameter('upstart');
     $filters = $input->getArgument('filter');
     if ($filters) {
         $jobs = $this->filter($filters);
     } else {
         $jobs = $config['job'];
     }
     $args = [];
     $hasExpands = false;
     foreach ($jobs as $job) {
         if ($job['quantity'] > 1) {
             $hasExpands = true;
             $log = explode('.', $job['log']);
             $ext = array_pop($log);
             $log = implode('.', $log);
             $args[] = escapeshellarg($log) . '*.' . escapeshellarg($ext);
         } else {
             $args[] = escapeshellarg($job['log']);
         }
     }
     if (count($args) == 1 && !$hasExpands) {
         array_unshift($args, '-n ' . escapeshellarg($input->getOption('tail')));
     }
     if ($input->getOption('watch')) {
         $this->passthru("tail -F " . implode(' ', $args), []);
     } else {
         $return = $this->exec("tail " . implode(' ', $args), []);
         $output->writeln($return);
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $filters = $input->getArgument('filter');
     $config = $this->getContainer()->getParameter('upstart');
     if ($filters) {
         $grepArgs = [];
         $jobs = $this->filter($filters);
         foreach ($jobs as $job) {
             $grepArgs[] = '-e ' . escapeshellarg("{$config['project']}/{$job['name']}");
         }
         $grepArgs = implode(' ', $grepArgs);
     } else {
         $grepArgs = '-e ' . escapeshellarg("{$config['project']}/");
     }
     if ($input->getOption('watch')) {
         $interval = $input->getOption('interval');
         $this->passthru('watch -d -n %s %s', [$interval, "initctl list | grep {$grepArgs} | sort"]);
     } else {
         $return = $this->exec("initctl list | grep {$grepArgs} | sort", []);
         foreach (explode("\n", $return) as $line) {
             if (!trim($line)) {
                 continue;
             }
             if (strpos($line, ' stop/')) {
                 $output->writeln("<fg=white;bg=red>{$line}</>");
             } else {
                 $output->writeln("<fg=white;bg=green>{$line}</>");
             }
         }
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $config = $this->getContainer()->getParameter('upstart');
     $filters = $input->getArgument('filter');
     if ($filters) {
         $this->checkFilters($filters);
         foreach ($filters as $filter) {
             if (in_array($filter, $config['tagNames'])) {
                 $this->exec('initctl emit %s', ["{$config['project']}.{$filter}.stop"]);
             } elseif (in_array($filter, $config['jobNames'])) {
                 $this->exec('initctl stop %s', ["{$config['project']}/{$filter}"]);
             }
         }
     } else {
         $config = $this->getContainer()->getParameter('upstart');
         $this->exec('initctl emit %s', ["{$config['project']}.stop"]);
     }
 }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        parent::execute($input, $output);
        $config = $this->getContainer()->getParameter('upstart');
        if (!$input->getOption('no-bash-completion')) {
            $this->installBashCompletion($output, $config);
        }
        $filters = $input->getArgument('filter');
        if ($filters) {
            $jobs = $this->filter($filters);
        } else {
            $jobs = $config['job'];
            $deleteCommand = $this->getApplication()->has('upstart:delete') ? $this->getApplication()->get('upstart:delete') : $this->getApplication()->get('delete');
            $deleteCommand->run(new ArrayInput([]), $output);
        }
        $dir = "{$config['configDir']}/{$config['project']}";
        if (!file_exists($dir)) {
            if (mkdir($dir)) {
                $output->writeln("<info>Dir '{$dir}' is created.</info>");
            } else {
                $output->writeln("<error>Can not mkdir '{$dir}'.</error>");
                throw new \Exception("Can not mkdir '{$dir}'.");
            }
        }
        $stanzaNameMap = ['preStart' => 'pre-start', 'postStart' => 'post-start', 'preStop' => 'pre-stop', 'postStop' => 'post-stop', 'startOn' => 'start on', 'stopOn' => 'stop on', 'normalExit' => 'normal exit', 'respawnLimit' => 'respawn limit', 'apparmorLoad' => 'apparmor load', 'apparmorSwitch' => 'apparmor switch', 'killSignal' => 'kill signal', 'killTimeout' => 'kill timeout', 'reloadSignal' => 'reload signal'];
        foreach ($jobs as $options) {
            $controllerContent = [];
            $instanceContent = [];
            if ($options['quantity'] > 1) {
                $instanceContent[] = 'instance $instance';
            }
            foreach ($options['native'] as $stanza => $value) {
                if (isset($stanzaNameMap[$stanza])) {
                    $stanza = $stanzaNameMap[$stanza];
                }
                switch ($stanza) {
                    case 'start on':
                    case 'stop on':
                        if ($options['quantity'] > 1) {
                            $controllerContent[] = "{$stanza} {$value}";
                        } else {
                            $instanceContent[] = "{$stanza} {$value}";
                        }
                        break;
                    case 'respawn':
                    case 'manual':
                    case 'task':
                        if ($value) {
                            $instanceContent[] = $stanza;
                        }
                        break;
                    case 'script':
                        $value = strtr($value, ["\n" => "\n\t"]);
                        $instanceContent[] = "script\n\t{$value}\nend script";
                        break;
                    case 'emits':
                    case 'export':
                        foreach ($value as $item) {
                            $instanceContent[] = "{$stanza} {$item}";
                        }
                        break;
                    case 'env':
                        foreach ($value as $itemName => $item) {
                            if (!is_null($item)) {
                                $instanceContent[] = "{$stanza} {$itemName}={$item}";
                            } else {
                                $instanceContent[] = "{$stanza} {$itemName}";
                            }
                        }
                        break;
                    case 'respawn limit':
                    case 'normal exit':
                        $instanceContent[] = "{$stanza} " . implode(' ', $value);
                        break;
                    case 'cgroup':
                    case 'limit':
                        foreach ($value as $item) {
                            $instanceContent[] = "{$stanza} " . implode(' ', $item);
                        }
                        break;
                    default:
                        $instanceContent[] = "{$stanza} {$value}";
                        break;
                }
            }
            if ($options['quantity'] > 1) {
                $instances = implode(' ', range(1, $options['quantity'], 1));
                $controllerContent = implode("\n", $controllerContent);
                $controllerContent = <<<BODY
{$controllerContent}
pre-start script
    for instance in {$instances}
    do
        start {$config['project']}/{$options['name']}.instance instance=\$instance || :
    done
end script

post-stop script
    for instance in {$instances}
    do
        stop {$config['project']}/{$options['name']}.instance instance=\$instance || :
    done
end script

BODY;
                $controllerFile = "{$dir}/{$options['name']}.conf";
                if (file_put_contents($controllerFile, $controllerContent)) {
                    $output->writeln("<info>Created '{$controllerFile}'.</info>");
                } else {
                    throw new \Exception("Can not write '{$controllerFile}'.");
                }
                $instanceFile = "{$dir}/{$options['name']}.instance.conf";
            } else {
                $instanceFile = "{$dir}/{$options['name']}.conf";
            }
            $instanceContent = implode("\n", $instanceContent) . "\n";
            if (file_put_contents($instanceFile, $instanceContent)) {
                $output->writeln("<info>Created '{$instanceFile}'.</info>");
            } else {
                throw new \Exception("Can not write '{$instanceFile}'.");
            }
        }
    }