This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrude.psm1
488 lines (434 loc) · 15.3 KB
/
Drude.psm1
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<# Copyright (C) Alex Danilenko. All rights reserved. #>
requires -version 3.0
Function Check-ForDockerComposeFile(){
$file_found = Test-Path ".\docker-compose.yml";
Write-Verbose "docker-compose.yml file found: $file_found"
return $file_found
}
Function Check-DockerIsRunning() {
$docker_for_windows_process_name = "Docker for Windows";
$docker_is_running = Get-Process "$docker_for_windows_process_name" -ErrorAction Ignore;
Write-Verbose "Docker is running: $docker_is_running"
if($docker_is_running -eq $null) {
return $false;
} else {
return $true;
}
}
Function Check-DockerContainerIsRunning() {
param(
[Parameter(Position=0,Mandatory=$true)][string]$container
)
$container_status = ($(docker ps --filter id=$(docker-compose ps -q $container)) -match "Up ");
Write-Verbose "$container container status output: $container_status"
if($container_status -match "Up ") {
return $true
} else {
Write-Verbose "$container container is not running."
return $false
}
}
Function Check-Drude() {
param(
[string]$container = ""
)
$docker_compose_found = Check-ForDockerComposeFile
$docker_is_running = Check-DockerIsRunning
if($docker_compose_found -eq $false) {
Write-Host -ForegroundColor Red -Object "docker-compose.yml file is not found in current directory."
break
}
if ($docker_is_running -eq $false) {
Write-Host -ForegroundColor Red -Object "Docker for Windows is not running. Start Docker for Windows first."
break
}
if (($container -ne "") -and ($(Check-DockerContainerIsRunning($container)) -eq $false)) {
Write-Host -ForegroundColor Red -Object "Container '$container' is not running."
break
}
return $true
}
# ================================================================================================== #
<#
.Synopsis
Start container from docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
Executes "docker-compose -d"
.EXAMPLE
Start-Drude
.EXAMPLE
dsh-up
#>
Function Start-Drude(){
[cmdletbinding()]
[Alias("dsh-up")]
param (
[Parameter(Position=0)][string]$cliContainer = "cli"
)
if(Check-Drude -eq $true){
Write-Host -ForegroundColor Green -Object "Starting all containers for this project ..."
docker-compose up -d --remove-orphans
#Write-Verbose "Resetting user id in $cliContainer container ..."
#$host_uid = $(id -u)
#$container_uid = Invoke-DrudeBashCommand "id -u"
#
#if($host_uid -ne $container_uid) {
# Write-Verbose "Host UID ($host_uid) do not matches container UID ($container_uid)."
# Write-Host -ForegroundColor Yellow "Changing User ID in $cliContainer container $container_uid to $host_uid for matching host user id. It's one time operation so please be patient, it may take a while..."
# Invoke-DrudeBashCommand "usermod -u $host_uid -o docker" -container $cliContainer -user "root"
# Invoke-DrudeBashCommand "chown -R docker:users /var/www" -container $cliContainer -user "root"
#} else {
# Write-Verbose "Host UID already ($host_uid) matches container UID ($container_uid)."
#}
#Write-Verbose "Create ~/.phpstorm_helpers/phpcs_temp.tmp for being able to use PhpSniffer in PHPStorm."
#Invoke-DrudeBashCommand "mkdir /home/docker/.phpstorm_helpers/phpcs_temp.tmp -p -m 777" -container $cliContainer -user "root"
}
}
# ================================================================================================== #
<#
.Synopsis
Stops containers described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
Executes "docker-compose stop"
.EXAMPLE
Stop-Drude
#>
Function Stop-Drude(){
[cmdletbinding()]
[Alias("dsh-down","dsh-stop")]
param (
[switch]$allWorking=$false
)
if(Check-Drude -eq $true){
if($allWorking -eq $false) {
Write-Host -ForegroundColor Green -Object "Stopping all containers for this project ..."
docker-compose stop
}
else {
Write-Host -ForegroundColor Yellow -Object "Stopping all working containers for ALL projects ..."
docker stop $(docker ps -q)
}
}
}
# ================================================================================================== #
<#
.Synopsis
Restarts containers described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
Executes "docker-compose stop; docker-compose up -d"
.EXAMPLE
Restart-Drude
#>
Function Restart-Drude(){
[cmdletbinding()]
[Alias("dsh-restart")]
param (
[switch]$stopAllWorkingContainers=$false
)
if($stopAllWorkingContainers -eq $false) {
Stop-Drude
} else {
Stop-Drude -allWorking
}
Start-Drude
}
<#
.Synopsis
Prints status of containers described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
Executes "docker-compose ps"
.EXAMPLE
Get-DrudeStatus
.EXAMPLE
Get-DrudeStatus cli
#>
Function Get-DrudeStatus(){
[cmdletbinding()]
[Alias("dsh-status", "dsh-ps")]
param (
[Parameter(Position=0)][string]$container = ""
)
if($(Check-Drude) -eq $true) {
docker-compose ps $container
}
}
<#
.Synopsis
Initiates interactive bash shell session with cli container described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
.EXAMPLE
Invoke-DrudeBash
.EXAMPLE
dsh-bash
.EXAMPLE
Invoke-DrudeBash web
.EXAMPLE
Invoke-DrudeBash web root
.EXAMPLE
dsh-bash web
.EXAMPLE
dsh-bash web root
#>
Function Invoke-DrudeBash(){
[cmdletbinding()]
[Alias("dsh-bash")]
param (
[Parameter(Position=0)][string]$container = "cli",
[Parameter(Position=1)][string]$user = "docker"
)
if($(Check-Drude -container $container) -eq $true){
Write-Verbose "docker exec -u $user -it $(docker-compose ps -q $container) bash"
docker exec -u $user -it $(docker-compose ps -q $container) bash
}
[Console]::ResetColor()
}
# ================================================================================================== #
<#
.Synopsis
Executes command in needed container's interactive bash shell.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
.EXAMPLE
Invoke-DrudeBashCommand "cat /etc/hosts"
.EXAMPLE
dsh-exec "cat /etc/hosts"
.EXAMPLE
Invoke-DrudeBashCommand "cat /etc/hosts" cli
.EXAMPLE
dsh-exec "cat /etc/hosts" cli
#>
Function Invoke-DrudeBashCommand(){
[cmdletbinding()]
[Alias("dsh-exec")]
param (
[Parameter(Position=0,Mandatory=$true)][string]$command = "ls -la",
[Parameter(Position=1)][string]$container = "cli",
[string]$user = "docker"
)
if($(Check-Drude -container $container) -eq $true){
Write-Verbose "docker exec -u $user -it $(docker-compose ps -q $container) bash -c `"$command`""
docker exec -u $user -it $(docker-compose ps -q $container) bash -c "$command"
}
[Console]::ResetColor()
}
<#
.Synopsis
Executes drush command for needed site in needed docroot folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
.EXAMPLE
Invoke-DrudeDrushCommand "cc all" default
.EXAMPLE
dsh-drush "cc all" default
#>
Function Invoke-DrudeDrushCommand(){
[cmdletbinding()]
[Alias("dsh-drush")]
param (
[Parameter(Position=0)][string]$command = "status",
[Parameter(Position=1)][string]$site = "default",
[Parameter(Position=2)][string]$docroot = "/var/www/docroot",
[Parameter(Position=3)][string]$cliContainer = "cli"
)
if($(Check-Drude -container $cliContainer) -eq $true){
Write-Verbose "docker exec -it $(docker-compose ps -q $cliContainer) bash -c `"cd $docroot && cd sites/$site && drush $command`""
docker exec -it $(docker-compose ps -q $cliContainer) bash -c "cd $docroot && cd sites/$site && drush $command"
[Console]::ResetColor()
}
[Console]::ResetColor()
}
<#
.Synopsis
Prints logs for all or needed container described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
.EXAMPLE
Get-DrudeLogs
.EXAMPLE
dsh-logs
.EXAMPLE
Get-DrudeLogs cli
.EXAMPLE
dsh-logs cli
#>
Function Get-DrudeLogs(){
[cmdletbinding()]
[Alias("dsh-logs")]
param (
[Parameter(Position=0)][string]$container = ""
)
if($(Check-Drude -container $container) -eq $true){
docker-compose logs -f $container
}
[Console]::ResetColor()
}
<#
.Synopsis
Drops all containers described in docker-compose.yml in current folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
WARNING! This action may result to lose your data like databases.
.EXAMPLE
Clear-Drude
.EXAMPLE
dsh-destroy
#>
Function Clear-Drude(){
[cmdletbinding()]
[Alias("dsh-destroy")]
param (
[string]$arguments = "--remove-orphans"
)
if($(Check-Drude) -eq $true){
Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all containers and their contents."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Keeps all as it is."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0)
switch ($result){
0 {
docker-compose down $arguments
Write-Host -ForegroundColor Cyan -Object "Do you want to remove all downloaded docker images?"
Write-Host -ForegroundColor Red -Object "WARNING! This operation cannot be undone and will result to lost of data of all projects in your system!"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Yes, Delete all docker images."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Keeps all as it is."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result2 = $host.ui.PromptForChoice($title, "", $options, 1)
switch($result2) {
0 {
docker rmi $(docker images -q)
}
}
}
}
}
[Console]::ResetColor()
}
<#
.Synopsis
Drops all containers described in docker-compose.yml in current folder and starts containers from scratch.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
WARNING! This action may result to lose your data like databases.
.EXAMPLE
Reset-Drude
.EXAMPLE
dsh-reset
#>
Function Reset-Drude(){
[cmdletbinding()]
[Alias("dsh-reset")]
param (
[string]$arguments = "--remove-orphans"
)
if($(Check-Drude) -eq $true){
Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all containers and their contents."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Keeps all as it is."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0)
switch ($result){
0 {
docker-compose down $arguments
Start-Drude
[Console]::ResetColor()
}
}
}
[Console]::ResetColor()
}
<#
.Synopsis
Run behat tests in needed folder.
.DESCRIPTION
Should be executed in folder with docker-compose.yml.
Next files should exist:
- .\tests\behat\composer.json
- .\tests\behat\behat.yml
.EXAMPLE
Invoke-DrudeBehat
.EXAMPLE
dsh-behat
.EXAMPLE
dsh-behat "--tags=@drush"
#>
Function Invoke-DrudeBehat(){
[cmdletbinding()]
[Alias("dsh-behat")]
param (
[Parameter(Position=0)][string]$behatParams = '',
[string]$folder = "tests/behat"
)
if($(Check-Drude) -eq $true){
$behat_folder_found = Test-Path $folder
$behat_yml_found = Test-Path "$folder/behat.yml"
$behat_binary_bound = Test-Path "$folder/bin/behat"
$error = '';
if($behat_folder_found -eq $false) {
$error = "$folder folder is not found."
}
if ($behat_yml_found -eq $false) {
$error = "File $folder/behat.yml is not found. Usually it means that you need to copy behat.yml.dist to behat.yml"
}
if($error -ne ''){
Write-Host -ForegroundColor Red -Object $error
} else {
if($behat_binary_bound -eq $false){
Write-Host -ForegroundColor Yellow -Object "Behat is not installed. Installing..."
}
Invoke-DrudeBashCommand -container cli -command "cd $folder && composer install --prefer-source --no-interaction && ./bin/behat -p docker $behatParams"
}
}
[Console]::ResetColor()
}
<#
.Synopsis
Initialize new project based on DWND.
.DESCRIPTION
Downloads and unpacks zip archive from GitHub to needed folder.
.EXAMPLE
Initialize-DrudeDwnd
.EXAMPLE
Initialize-DrudeDwnd folderName
.EXAMPLE
dsh-init
.EXAMPLE
dsh-init folderName
#>
Function Initialize-DrudeDwnd(){
[cmdletbinding()]
[Alias("dsh-init")]
param ()
$currentFolder = Resolve-Path ".\"
$zip_file_url = "https://github.com/fat763/dwnd/archive/master.zip"
$zip_tmp_filename = "$env:temp\dwnd-temp.zip"
Write-Host -ForegroundColor Yellow -Object "You are going to downlowd DWND template project to $currentFolder"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Yes, download to $folder."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Nope, nope nope."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0)
switch ($result){
0 {
Invoke-WebRequest -Uri $zip_file_url -OutFile $zip_tmp_filename
Expand-Archive -Path $zip_tmp_filename -DestinationPath $env:temp -Force
Move-Item -Path "$env:temp\dwnd-master\**" -Destination "$currentFolder\" -Force
Write-Host -ForegroundColor Green -Object "DWND template project was downloaded to: $currentFolder\"
}
}
[Console]::ResetColor()
}