Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
CRAP
94.12% covered (success)
94.12%
32 / 34
DockerLauncher
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
10.02
94.12% covered (success)
94.12%
32 / 34
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 handle
0.00% covered (danger)
0.00%
0 / 1
9.02
93.33% covered (success)
93.33%
28 / 30
1<?php
2
3namespace Qmp\Laravel\DockerLauncher\Console;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\Log;
7use Qmp\Laravel\DockerLauncher\Docker;
8
9class DockerLauncher extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'docker:launcher 
17                            {--command=test : Launch command}
18                            {--type=artisan : Type of command to launch. If not artisan starting java or other container type to execute task}
19                            {--image=\'\' : Docker Image to use }
20                            {--cmd=\'\' : Docker CMD}
21                            {--output-file=\'\' : Docker logs output}
22                            {--container-name=\'\' : Container Name}
23                            {--service-name= : Php Service Name}
24                            {--user= : User}
25                            {--host-uid= : Host uid}
26                            {--host-gid= : Host gid}
27                            {--restart-policy=on-failure : Restart Policy}';        
28    /**
29     * The console command description.
30     *
31     * @var string
32     */
33    protected $description = 'Create docker container and run docker command. Possibility to run artisan command when type = artisan';
34
35    protected $docker;
36
37    protected $config;
38
39    /**
40     * Create a new command instance.
41     *
42     * @param Docker $docker
43     * @param array $config
44     */
45    public function __construct(Docker $docker, array $config)
46    {
47        parent::__construct();
48
49        $this->docker = $docker;
50        $this->config = $config;
51    }
52
53    /**
54     * Execute the console command.
55     *
56     * @return mixed
57     */
58    public function handle()
59    {
60        $config = resolve('qmp.docker.config');
61
62        $envs = collect([
63            'SERVICE_NAME=' => $this->option('service-name'),
64            'USER=' => $this->option('user'),
65            'HOST_UID=' => $this->option('host-uid'),
66            'HOST_GID=' => $this->option('host-gid'),
67            'CONTAINER_NAME=' => $this->option('container-name'),
68            'IS_RUNNER=' => 1
69        ]);
70
71        Log::debug('envs:' . var_export($envs, true));
72
73        $config->setEnvs($envs->filter()->map(function($value, $key) { return $key . $value; })->values()->toArray());
74        $config->setRestartPolicy($this->option('restart-policy'));
75
76        $command = $this->option('command');
77        $type = $this->option('type');
78        $containerName = $this->option('container-name') === 'false' ? $type . '_' . $command : $this->option('container-name');
79
80        if ($type === 'artisan') {
81            $artisanCommand = 'docker:'  . $command;
82            $this->docker->setDockerInstance($containerName, $artisanCommand);
83        } else {
84            $image = $this->option('image');
85
86            $output = $this->option('output-file');
87            $output = empty($output) || $output === "''" ? $this->config['output'] : $output;
88
89            createFile777($output);
90            
91            $launcherCmd = $this->option('cmd');
92            $mount = [];
93            $this->docker->setDockerInstance(
94                $containerName,
95                '',
96                empty($image) || $image === "''" ? $this->config['image'] : $image,
97                $output,
98                empty($launcherCmd) || $launcherCmd === "''" ? $this->config['command'] : $launcherCmd,
99                $mount
100            );
101        }
102
103        $this->docker->run();
104        //$this->docker->removeOldService();
105    }
106}