-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMailhog_Command.php
More file actions
159 lines (135 loc) · 4.4 KB
/
Mailhog_Command.php
File metadata and controls
159 lines (135 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
use EE\Model\Site;
use EE\Site\Utils as Site_Utils;
use EE\Auth\Utils as Auth_Utils;
use EE\Utils as EE_Utils;
/**
* Manages mailhog on a site.
*
* @package ee-cli
*/
class Mailhog_Command extends EE_Command {
/**
* @var array $db Object containing essential site related information.
*/
private $site_data;
/**
* Enables mailhog on given site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to enable mailhog on.
*
* [--force]
* : Force enabling of mailhog for a site.
*
* ## EXAMPLES
*
* # Enable mailhog for site
* $ ee mailhog enable example.com
*
*/
public function enable( $args, $assoc_args ) {
Auth_Utils\init_global_admin_tools_auth();
EE_Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = Site_Utils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$force = EE_Utils\get_flag_value( $assoc_args, 'force' );
$this->site_data = Site::find( EE_Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}
if ( ! $force && $this->mailhog_enabled() ) {
EE::error( 'Mailhog is already up.' );
}
EE_DOCKER::docker_compose_up( $this->site_data->site_fs_path, [ 'mailhog' ] );
EE::exec( "docker-compose exec postfix postconf -e 'relayhost = mailhog:1025'" );
EE::exec( 'docker-compose restart postfix' );
$this->site_data->mailhog_enabled = 1;
$this->site_data->save();
EE::success( sprintf( 'Mailhog enabled for %s site', $this->site_data->site_url ) );
}
/**
* Disables mailhog on given site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to disable mailhog on.
*
* ## EXAMPLES
*
* # Disable mailhog for site
* $ ee mailhog disable example.com
*
*/
public function disable( $args, $assoc_args ) {
EE_Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = Site_Utils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$this->site_data = Site::find( EE_Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}
if ( ! $this->mailhog_enabled() ) {
EE::error( 'Mailhog is already down.' );
}
EE::exec( 'docker-compose stop mailhog' );
EE::exec( 'docker-compose exec postfix postconf -e \'relayhost =\'' );
EE::exec( 'docker-compose restart postfix' );
$this->site_data->mailhog_enabled = 0;
$this->site_data->save();
EE::success( sprintf( 'Mailhog disabled for %s site', $this->site_data->site_url ) );
}
/**
* Outputs status of mailhog for a site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to know mailhog status for.
*
* ## EXAMPLES
*
* # Check mailhog status on site
* $ ee mailhog status example.com
*
*/
public function status( $args, $assoc_args ) {
EE_Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = Site_Utils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$this->site_data = Site::find( EE_Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}
if ( $this->mailhog_enabled() ) {
EE::log( sprintf( 'Mailhog is UP for %s site.', $this->site_data->site_url ) );
} else {
EE::log( sprintf( 'Mailhog is DOWN for %s site.', $this->site_data->site_url ) );
}
}
/**
* Function to check if mailhog is present in docker-compose.yml or not.
*/
private function check_mailhog_available() {
EE::debug( 'Site type: ' . $this->site_data->site_type );
EE::debug( 'Site command: ' . $this->site_data->app_sub_type );
if ( EE_DOCKER::service_exists( 'mailhog', $this->site_data->site_fs_path ) ) {
return;
}
EE::error( sprintf( '%s site does not have support to enable/disable mailhog.', $this->site_data->site_url ) );
}
/**
* Function to check if mailhog is possible for the site-type and is enabled or not.
*
* @return bool Status of mailhog enabled or not.
*/
private function mailhog_enabled() {
$this->check_mailhog_available();
$launch = EE::launch( 'docker-compose ps -q mailhog' );
$id = trim( $launch->stdout );
if ( empty( $id ) ) {
return false;
}
return 'running' === EE_DOCKER::container_status( $id );
}
}