forked from ynput/ayon-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.ps1
223 lines (190 loc) · 6.89 KB
/
manage.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
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
$FunctionName=$ARGS[0]
$arguments=@()
if ($ARGS.Length -gt 1) {
$arguments = $ARGS[1..($ARGS.Length - 1)]
}
# Settings
$SCRIPT_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Set-Location "$($SCRIPT_DIR)"
$SETTINGS_FILE = "settings/template.json"
$IMAGE_NAME = "ynput/ayon"
$DEFAULT_IMAGE = "$($IMAGE_NAME):dev"
$SERVER_CONTAINER = "server"
# Variables
# TODO: tag needs to be set to the current version. TBD.
$TAG = (git describe --tags --always --dirty)
# Abstract the 'docker compose' / 'docker-compose' command
$COMPOSE = "docker-compose"
# By default, just show the usage message
function defaultfunc {
Write-Host ""
Write-Host "Ayon server $($TAG)"
Write-Host ""
Write-Host "Usage: ./manage.ps1 [target]"
Write-Host ""
Write-Host "Runtime targets:"
Write-Host " setup Apply settings temlpate form the settings/template.json"
Write-Host " dbshell Open a PostgreSQL shell"
Write-Host " reload Reload the running server"
Write-Host " demo Create demo projects based on settings in demo directory"
Write-Host ""
Write-Host "Development:"
Write-Host " backend Download / update backend"
Write-Host " frontend Download / update frontend"
Write-Host " build Build docker image"
Write-Host " relinfo Create RELEASE file with version info (debugging)"
Write-Host " dist Publish docker image to docker hub"
Write-Host " dump [PROJECT] Dump project database into file"
Write-Host " restore [PROJECT] Restore project database from file"
Write-Host ""
}
# Makefile syntax, oh so bad
# Errors abound, frustration high
# Gotta love makefiles
function setup {
Write-Host "Server container: $($SERVER_CONTAINER)"
if (!(Test-Path "$($SCRIPT_DIR)/settings/template.json")) {
& "$($COMPOSE)" exec -T "$($SERVER_CONTAINER)" python -m setup
} else {
Get-Content "$($SCRIPT_DIR)/settings/template.json" | & "$($COMPOSE)" exec -T "$($SERVER_CONTAINER)" python -m setup -
}
& "$($COMPOSE)" exec "$($SERVER_CONTAINER)" bash "/backend/reload.sh"
}
function dbshell {
& "$($COMPOSE)" exec postgres psql -U ayon ayon
}
function reload {
& "$($COMPOSE)" exec "$($SERVER_CONTAINER)" bash "/backend/reload.sh"
}
function demo {
foreach ($file in (Get-ChildItem "demo/*.json")) {
Get-Content "$($file.FullName)" | & "$($COMPOSE)" exec -T "$($SERVER_CONTAINER)" python -m demogen
}
}
function update {
docker pull $DEFAULT_IMAGE
& "$($COMPOSE)" up --detach --build "$($SERVER_CONTAINER)"
}
function relinfo {
$backend_dir = "$($SCRIPT_DIR)\backend"
$frontend_dir = "$($SCRIPT_DIR)\frontend"
$output_file = "$($SCRIPT_DIR)\RELEASE"
$cur_date = Get-Date
$backend_version = Invoke-Expression -Command "python -c ""import os;import sys;content={};f=open(r'$($backend_dir)\ayon_server\version.py');exec(f.read(),content);f.close();print(content['__version__'])"""
$build_date = Get-Date -Date $cur_date -Format "yyyyMMdd"
$build_time = Get-Date -Date $cur_date -Format "HHmm"
$cur_cwd = Get-Location
Set-Location $backend_dir
$backend_branch = Invoke-Expression -Command "git branch --show-current"
$backend_commit = Invoke-Expression -Command "git rev-parse --short HEAD"
Set-Location $frontend_dir
$frontend_branch = Invoke-Expression -Command "git branch --show-current"
$frontend_commit = Invoke-Expression -Command "git rev-parse --short HEAD"
Set-Location $cur_cwd
$output_content = @"
version=$($backend_version)
build_date=$($build_date)
build_time=$($build_time)
frontend_branch=$($backend_branch)
backend_branch=$($frontend_branch)
frontend_commit=$($backend_commit)
backend_commit=$($frontend_commit)
"@
$output_content | Out-File -FilePath $output_file -Encoding utf8
}
# The following targets are for development purposes only.
function build {
backend
frontend
relinfo
# Build the docker image
docker build -t "$($IMAGE_NAME):$($TAG)" -t "$($IMAGE_NAME):latest" .
}
function dist {
build
# Publish the docker image to the registry
docker push "$($IMAGE_NAME):$($TAG)"
docker push "$($IMAGE_NAME):latest"
}
function backend {
& git -C "$($SCRIPT_DIR)/backend" pull
if ($lastexitCode) {
& git clone https://github.com/pypeclub/ayon-backend "$($SCRIPT_DIR)/backend"
}
}
function frontend {
& git -C "$($SCRIPT_DIR)/frontend" pull
if ($lastexitCode) {
& git clone https://github.com/pypeclub/ayon-frontend "$($SCRIPT_DIR)/frontend"
}
}
function dump {
$projectname = $args[0]
if ($projectname -eq $null) {
Write-Error "Error: Project name is required. Usage: ./manage.ps1 dump [PROJECT]"
exit 1
}
Write-Host "Dumping project '$projectname'"
$dumpfile = "dump.$projectname.sql"
# Create the file if it doesn't exist.
if (Test-Path $dumpfile) {
Remove-Item $dumpfile
}
New-Item $dumpfile -Force
# Write the DROP SCHEMA statement to the file.
echo "DROP SCHEMA IF EXISTS project_$projectname CASCADE;" >> $dumpfile
# Write the DELETE statement to the file.
echo "DELETE FROM public.projects WHERE name = '$projectname';" >> $dumpfile
# Get the list of tables in the project schema.
docker compose exec -t postgres pg_dump --table=public.projects --column-inserts ayon -U ayon | Out-String -Stream | Select-String -Pattern "^INSERT INTO" -AllMatches | Select-String -Pattern "'$($projectname)'" -AllMatches >> $dumpfile
docker compose exec postgres psql -U ayon ayon -Atc "SELECT DISTINCT(product_type) from project_$($projectname).products;" | ForEach-Object { "INSERT INTO public.product_types (name) VALUES ('$($_)') ON CONFLICT DO NOTHING;" >> $dumpfile }
docker compose exec postgres pg_dump --schema=project_$($projectname) ayon -U ayon >> $dumpfile
}
function restore {
$projectname = $args[0]
if ($projectname -eq $null) {
Write-Error "Error: Project name is required. Usage: ./manage.ps1 restore [PROJECT]"
exit 1
}
$dumpfile = "dump.$projectname.sql"
# Check if the dump file exists.
if (-not (Test-Path $dumpfile)) {
Write-Error "Error: Dump file $file not found"
exit 1
}
# Restore the database from the dump file.
Get-Content $dumpfile | docker-compose exec -T postgres psql -U ayon ayon
}
function main {
if ($FunctionName -eq "setup") {
setup
} elseif ($FunctionName -eq "dbshell") {
dbshell
} elseif ($FunctionName -eq "reload") {
reload
} elseif ($FunctionName -eq "demo") {
demo
} elseif ($FunctionName -eq "update") {
update
} elseif ($FunctionName -eq "build") {
build
} elseif ($FunctionName -eq "relinfo") {
relinfo
} elseif ($FunctionName -eq "dist") {
dist
} elseif ($FunctionName -eq "backend") {
backend
} elseif ($FunctionName -eq "frontend") {
frontend
} elseif ($FunctionName -eq "dump") {
dump @arguments
} elseif ($FunctionName -eq "restore") {
restore @arguments
} elseif ($null -eq $FunctionName) {
defaultfunc
} else {
Write-Host "Unknown function ""$FunctionName"""
defaultfunc
}
}
main