-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathContainers.php
250 lines (210 loc) · 6.97 KB
/
Containers.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace EE\Migration;
use EE\RevertableStepProcessor;
use EE;
/**
* Upgrade existing containers to new docker-image
*/
class Containers {
/** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */
private static $rsp;
/**
* Main entry point for the script. Migrates all containers
*
* @throws \Exception
*/
public static function start_container_migration() {
EE\Utils\delem_log( 'Starting container migration' );
self::$rsp = new RevertableStepProcessor();
$img_versions = EE\Utils\get_image_versions();
$current_versions = self::get_current_docker_images_versions();
$updated_images = [];
foreach ( $img_versions as $img => $version ) {
if ( 'easyengine/php5.6' === $img ) {
continue;
}
if ( $current_versions[ $img ] !== $version ) {
$updated_images[] = $img;
self::pull_or_error( $img, $version );
}
}
if ( empty( $updated_images ) ) {
return;
}
self::migrate_global_containers( $updated_images );
self::migrate_site_containers( $updated_images );
self::save_upgraded_image_versions( $current_versions, $img_versions, $updated_images );
if ( ! self::$rsp->execute() ) {
throw new \Exception( 'Unable to migrate sites to newer version' );
}
EE\Utils\delem_log( 'Container migration completed' );
}
/**
* Save updated image version in database.
*
* @param $current_versions array of current image versions.
* @param $new_versions array of new image version.
* @param $updated_images array of updated images.
*/
public static function save_upgraded_image_versions( $current_versions, $new_versions, $updated_images ) {
self::$rsp->add_step(
'save-image-verions-in-database',
'EE\Migration\Containers::create_database_entry',
'EE\Migration\Containers::revert_database_entry',
[ $new_versions, $updated_images ],
[ $current_versions, $updated_images ]
);
}
/**
* Update database entry of images
*
* @param $new_versions array of new image versions.
* @param $updated_images array of updated images.
*
* @throws \Exception
*/
public static function create_database_entry( $new_versions, $updated_images ) {
foreach ( $updated_images as $image ) {
EE\Model\Option::update( [ [ 'key', $image ] ], [ 'value' => $new_versions[ $image ] ] );
}
}
/**
* Revert database entry in case of exception.
*
* @param $old_version array of old image versions.
* @param $updated_images array of updated images.
*
* @throws \Exception
*/
public static function revert_database_entry( $old_version, $updated_images ) {
foreach ( $updated_images as $image ) {
EE\Model\Option::update( [ [ 'key', $image ] ], [ 'value' => $old_version[ $image ] ] );
}
}
/**
* Helper method to ensure exception is thrown if image isn't pulled
*
* @param $image docker image to pull
* @param $version version of image to pull
*
* @throws \Exception
*/
public static function pull_or_error( $image, $version ) {
if ( ! \EE::exec( "docker pull $image:$version" ) ) {
throw new \Exception( "Unable to pull $image. Please check logs for more details." );
}
}
/**
* Get current docker images versions.
*
* @return array
* @throws \Exception
*/
public static function get_current_docker_images_versions() {
$images = EE::db()
->table( 'options' )
->where( 'key', 'like', 'easyengine/%' )
->all();
$images = array_map( function ( $image ) {
return [ $image['key'] => $image['value'] ];
}, $images );
return array_merge( ...$images );
}
/**
* Migrates global containers. These are container which are not created per site (i.e. ee-cron-scheduler).
*
* @param $updated_images array of updated images.
*/
private static function migrate_global_containers( $updated_images ) {
$updated_global_images = GlobalContainers::get_updated_global_images( $updated_images );
if ( empty( $updated_global_images ) ) {
return;
}
$global_compose_file_path = EE_ROOT_DIR . '/services/docker-compose.yml';
$global_compose_file_backup_path = EE_BACKUP_DIR . '/services/docker-compose.yml.backup';
self::$rsp->add_step(
'backup-global-docker-compose-file',
'EE\Migration\SiteContainers::backup_restore',
'EE\Migration\GlobalContainers::revert_global_containers',
[ $global_compose_file_path, $global_compose_file_backup_path ],
[ $global_compose_file_backup_path, $global_compose_file_path, $updated_global_images ]
);
self::$rsp->add_step(
'stop-global-containers',
'EE\Migration\GlobalContainers::down_global_containers',
null,
[ $updated_global_images ],
null
);
self::$rsp->add_step(
'generate-global-docker-compose-file',
'EE\Service\Utils\generate_global_docker_compose_yml',
null,
[ new \Symfony\Component\Filesystem\Filesystem() ],
null
);
$all_global_images = GlobalContainers::get_all_global_images_with_service_name();
foreach ( $updated_global_images as $image_name ) {
$global_container_name = $all_global_images[ $image_name ];
$global_service_name = ltrim( $global_container_name, 'ee-' );
self::$rsp->add_step(
"upgrade-$global_container_name-container",
"EE\Migration\GlobalContainers::global_service_up",
"EE\Migration\GlobalContainers::global_service_down",
[ $global_service_name ],
[ $global_service_name ]
);
}
}
/**
* Migrate site specific container.
*
* @param $updated_images array of updated images
*
* @throws \Exception
*/
public static function migrate_site_containers( $updated_images ) {
$db = new \EE_DB();
$sites = ( $db->table( 'sites' )->all() );
foreach ( $sites as $site ) {
$docker_yml = $site['site_fs_path'] . '/docker-compose.yml';
$docker_yml_backup = EE_BACKUP_DIR . '/' . $site['site_url'] . '/docker-compose.yml.backup';
if ( ! SiteContainers::is_site_service_image_changed( $updated_images, $site ) ) {
continue;
}
$ee_site_object = SiteContainers::get_site_object( $site['site_type'] );
if ( $site['site_enabled'] ) {
self::$rsp->add_step(
"disable-${site['site_url']}-containers",
'EE\Migration\SiteContainers::disable_site',
'EE\Migration\SiteContainers::enable_site',
[ $site, $ee_site_object ],
[ $site, $ee_site_object ]
);
}
self::$rsp->add_step(
"take-${site['site_url']}-docker-compose-backup",
'EE\Migration\SiteContainers::backup_restore',
'EE\Migration\SiteContainers::backup_restore',
[ $docker_yml, $docker_yml_backup ],
[ $docker_yml_backup, $docker_yml ]
);
self::$rsp->add_step(
"generate-${site['site_url']}-docker-compose",
'EE\Migration\SiteContainers::generate_site_docker_compose_file',
null,
[ $site, $ee_site_object ],
null
);
if ( $site['site_enabled'] ) {
self::$rsp->add_step(
"upgrade-${site['site_url']}-containers",
'EE\Migration\SiteContainers::enable_site',
'EE\Migration\SiteContainers::enable_site',
[ $site, $ee_site_object ],
[ $site, $ee_site_object ]
);
}
}
}
}