forked from microrealestate/microrealestate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmre.ps1
149 lines (136 loc) · 5.23 KB
/
mre.ps1
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
# PROD_COMPOSE_FILES=@("monitoring", "microservices.base", "microservices.prod")
$PROD_COMPOSE_FILES = @("microservices.base", "microservices.prod")
$DEV_COMPOSE_FILES = @("microservices.base", "microservices.dev")
$prod_compose_files_argument = $PROD_COMPOSE_FILES | ForEach-Object {" -f ./docker-compose.$_.yml"}
$dev_compose_files_argument = $DEV_COMPOSE_FILES | ForEach-Object {" -f ./docker-compose.$_.yml"}
$prod_compose_files_argument = -join $prod_compose_files_argument
$dev_compose_files_argument = -join $dev_compose_files_argument
# environment variables are in *.env files
function Initialize-Environment($envFile) {
if (Test-Path $envFile -PathType Leaf) {
foreach ($line in Get-Content $envFile) {
# If line is a comment or null/whitespace/newline skip.
if ($line.StartsWith("#") -or [String]::IsNullOrWhiteSpace($line)) {
continue
} else {
# Split the variable name and variable.
# (matching only first '=' aka return exactly 2 sub strings)
$lineSplit = $line -split '=',2
# Add it as environment variable
[System.Environment]::SetEnvironmentVariable($lineSplit[0], [System.Environment]::ExpandEnvironmentVariables($lineSplit[1].Replace("$`{", "%").Replace("}", "%")))
}
}
}
}
function Confirm-Development() {
Write-Output "checking dev config..."
Invoke-Expression -Command "docker-compose $dev_compose_files_argument config"
}
function Confirm-Production() {
Write-Output "checking prod config..."
Invoke-Expression -Command "docker-compose $prod_compose_files_argument config"
}
function Start-Development() {
Write-Output "Starting microrealestate..."
Invoke-Expression -Command "docker-compose $dev_compose_files_argument rm --stop --force"
Invoke-Expression -Command "docker-compose $dev_compose_files_argument up --build --remove-orphans"
}
function Read-Environment() {
Invoke-Expression -Command "docker-compose $prod_compose_files_argument ps"
}
function Build-Environment($1) {
Write-Output "Building microrealestate $1..."
Invoke-Expression -Command "docker-compose $prod_compose_files_argument rm --stop --force $1"
Invoke-Expression -Command "docker-compose $prod_compose_files_argument build --no-cache $1"
}
function Start-Environment($1) {
Write-Output "Starting microrealestate $1..."
# if (-not (Test-Path './data/elasticsearch' -PathType Container)) {
# New-Item -Path './data/' -Name "elasticsearch" -ItemType "directory"
# }
# Verify ./data is present, if not, create it.
if (-not (Test-Path './data' -PathType Container)) {
New-Item -Path './' -Name "data" -ItemType "directory"
}
if (-not (Test-Path './data/mongodb' -PathType Container)) {
New-Item -Path './data/' -Name "mongodb" -ItemType "directory"
}
if ( -not ([String]::IsNullOrWhiteSpace($1))) {
Invoke-Expression -Command "docker-compose $prod_compose_files_argument up -d $1"
} else {
Invoke-Expression -Command "docker-compose $prod_compose_files_argument up -d --force-recreate --remove-orphans"
}
if ($LASTEXITCODE -ne 0) {
Stop-Environment
Write-Output "ERROR: Fail to start microrealestate $1"
} else {
if (([String]::IsNullOrWhiteSpace($1))) {
Read-Environment
Write-Output ""
Write-Output "front-end http://localhost:$Env:NGINX_PORT/app"
Write-Output "Deprecated front-end http://localhost:$Env:NGINX_PORT"
# Write-Output "kibana http://localhost:$Env:KIBANA_PORT"
# Write-Output "cadvisor http://localhost:$Env:CADVISOR_PORT"
# Write-Output "rabbitmq Management http://localhost:$Env:RABBITMQ_MANAGEMENT_PORT"
}
}
return $LASTEXITCODE
}
function Stop-Environment {
Write-Output "Stopping microrealestate $1..."
Invoke-Expression -Command "docker-compose $prod_compose_files_argument rm -sf $1"
}
function Write-Help
{
Write-Output "Usage: $0 [option...] {dev|build|status|start|stop|restart|config-dev|config-prod}"
}
switch ($args[0]) {
config-dev {
Initialize-Environment dev.env
Initialize-Environment .env
Confirm-Development
}
config-prod {
Initialize-Environment prod.env
Initialize-Environment .env
Confirm-Production
}
dev {
Initialize-Environment dev.env
Initialize-Environment .env
Start-Development
}
status {
Initialize-Environment prod.env
Initialize-Environment .env
Read-Environment
}
build {
Initialize-Environment prod.env
Initialize-Environment .env
Build-Environment $args[1]
}
start {
Initialize-Environment prod.env
Initialize-Environment .env
Start-Environment $args[1]
}
stop {
Initialize-Environment prod.env
Initialize-Environment .env
Stop-Environment $args[1]
}
restart {
Initialize-Environment prod.env
Initialize-Environment .env
Start-Environment $args[1]
}
-h {
Write-Help
Exit 0
}
Default {
Write-Help
Exit 1
}
}