-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrushsitedeploy_steps.inc
208 lines (173 loc) · 5.86 KB
/
drushsitedeploy_steps.inc
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* @file
* Site Deployment plan for all the steps that need to happen as part of a
* deployment.
*/
class SiteDeploySteps {
private $configFile;
/**
* Constructor for SiteDeploySteps.
*
* @param array $config_file
* The config_file contains settings loaded from yaml config file.
*/
public function __construct($config_file) {
$this->configFile = $config_file;
}
/**
* Kicks off the whole process.
*/
public function run() {
$this->setup();
$this->disableModules();
$this->codeDeploy();
$this->postCodeDeploy();
$this->enableModules();
$this->enableThemes();
$this->setVariables();
$this->cleanUp();
}
/**
* Prep the site for the deployment.
*
* The goal of setup is to lock-down/prep the site and get it to a safe
* point where we feel comfortable deploying. Only minimally invasive steps
* should be included here.
*/
private function setup() {
DeployPrinter::printSection("Kicking off the deployment:");
$has_hook_to_execute = count(drush_command_implements('sitedeploy_setup')) > 0;
if ($has_hook_to_execute) {
drush_command_invoke_all_ref('sitedeploy_setup', $this->configFile);
}
if (isset ($this->configFile[maintenance_mode])) {
variable_set('maintenance_mode', $this->configFile[maintenance_mode]);
DeployPrinter::printMessage("Putting Site into maintenance mode");
// @todo make this a reusable function for setting variables.
$maintenenance_mode = variable_get('maintenance_mode');
if ($maintenenance_mode) {
DeployPrinter::printMessage("Confirmed that site is in maintenance mode");
}
}
if (isset ($this->configFile[maintenance_mode_message])) {
variable_set('maintenance_mode_message', $this->configFile[maintenance_mode_message]);
DeployPrinter::printMessage("Setting the maintenance mode message to: \"" . $this->configFile[maintenance_mode_message] . "\"");
}
}
/**
* The actual code deployment step.
*
* The goal of setup is to lock-down/prep the site and get it to a safe
* point where we feel comfortable deploying. Only minimally invasive steps
* should be included here.
*/
private function codeDeploy() {
DeployPrinter::printSection("Deploying Code");
$has_hook_to_execute = count(drush_command_implements('sitedeploy_codedeploy')) > 0;
if ($has_hook_to_execute) {
drush_command_invoke_all_ref('sitedeploy_codedeploy', $this->configFile);
}
}
/**
* Any steps that need to be completed post code-deployment.
*/
private function postCodeDeploy() {
DeployPrinter::printSection("Post Code Deployment Tasks");
$has_hook_to_execute = count(drush_command_implements('sitedeploy_postcodedeploy')) > 0;
if ($has_hook_to_execute) {
drush_command_invoke_all_ref('sitedeploy_postcodedeploy', $this->configFile);
}
}
/**
* Invokes a post-deploy hook once all sitedeploy code is complete.
*/
private function cleanUp() {
DeployPrinter::printSection("Clean up tasks");
DeployPrinter::printMessage("Taking site out of maintenance mode");
variable_set('maintenance_mode', 0);
$has_hook_to_execute = count(drush_command_implements('sitedeploy_cleanup')) > 0;
if ($has_hook_to_execute) {
drush_command_invoke_all_ref('sitedeploy_cleanup', $this->configFile);
}
}
/**
* Enables modules listed in the config array.
*/
private function enableModules() {
// Enable Modules.
$installed_module_list = module_list();
if (!isset($this->configFile[module_enable])) {
return;
}
DeployPrinter::printMessage("Enabling Modules");
foreach ($this->configFile[module_enable] as $key => $value) {
$have_modules_to_enable = count($value) > 0;
if ($have_modules_to_enable) {
DeployPrinter::printMessage("The following: $key modules will be enabled:");
// Only going to enable modules which aren't currently enabled.
$modules_to_enable = array_diff($value, $installed_module_list);
// @todo need to figure out how to hook into watchdog from drush. Would provide meaningful feedback here.
module_enable($modules_to_enable);
}
}
}
/**
* Disables modules listed in the config array.
*/
private function disableModules() {
$module_disable = $this->configFile[module_enable];
foreach ($module_disable as $key => $value) {
$have_modules_to_disable = count($value) > 0;
if ($have_modules_to_disable) {
DeployPrinter::printMessage("The following: $key modules will be disabled:");
module_disable($value);
}
}
}
/**
* Enables themes listed in the config array.
*/
private function enableThemes() {
$has_themes = count($this->configFile[themes]) > 0;
if ($has_themes) {
DeployPrinter::printMessage("Enabling Themes: ");
theme_enable($this->configFile[themes]);
}
}
/**
* Sets variable with confirmation to user.
*
* Does variable_get for confirmation.
*
* @param string $key
* The variable to set.
*
* @param string $value
* The value to set.
*/
private function deploySetVariable($key, $value) {
DeployPrinter::printMessage("Setting variable:$key to $value. ");
variable_set($key, $value);
$stored_value = variable_get($key);
if ($stored_value == $value) {
DeployPrinter::printMessage("confirmed");
}
else {
throw new Exception("Unable to set the variable: " . $key);
}
}
/**
* Grabs variables from the config object and sets them in the Drupal db.
*/
private function setVariables() {
$variables = $this->configFile[variables];
$has_variables = count($variables) > 0;
if ($has_variables) {
DeployPrinter::printMessage("Setting Variables: ");
foreach ($variables as $key => $value) {
self::deploySetVariable($key, $value);
}
}
}
}