-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.ps1
181 lines (156 loc) · 6.06 KB
/
start.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
# Receive first positional argument
$FunctionName=$ARGS[0]
$arguments=@()
if ($ARGS.Length -gt 1) {
$arguments = $ARGS[1..($ARGS.Length - 1)]
}
$poetry_verbosity="-vv"
$current_dir = Get-Location
$repo_root_rel = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$repo_root = (Get-Item $repo_root_rel).FullName
$TOOL_VERSION = Invoke-Expression -Command "python -c ""import os;import sys;content={};f=open(r'$($current_dir)/version.py');exec(f.read(),content);f.close();print(content['__version__'])"""
function Default-Func {
Write-Host ""
Write-Host "AYON dependency package tool $TOOL_VERSION"
Write-Host ""
Write-Host "Usage: ./start.ps1 [target]"
Write-Host ""
Write-Host "Runtime targets:"
Write-Host " install Install Poetry and update venv by lock file."
Write-Host " set-env Set all env vars in .env file."
Write-Host " listen Start listener on a server."
Write-Host " create Create dependency package for single bundle."
Write-Host " list-bundles List bundles available on server."
Write-Host " docker-build [bundle] [variant] Build dependency package using docker. Variant can be 'centos7', 'ubuntu', 'debian' or 'rocky9'"
Write-Host ""
}
function Exit-WithCode($exitcode) {
# Only exit this host process if it's a child of another PowerShell parent process...
$parentPID = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId=$PID" | Select-Object -Property ParentProcessId).ParentProcessId
$parentProcName = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId=$parentPID" | Select-Object -Property Name).Name
if ('powershell.exe' -eq $parentProcName) { $host.SetShouldExit($exitcode) }
Restore-Cwd
exit $exitcode
}
function Install-Poetry() {
Write-Host ">>> Installing Poetry ... "
$python = "python"
if (Get-Command "pyenv" -ErrorAction SilentlyContinue) {
if (-not (Test-Path -PathType Leaf -Path "$($repo_root)\.python-version")) {
$result = & pyenv global
if ($result -eq "no global version configured") {
Write-Host "!!! ", "Using pyenv but having no local or global version of Python set."
Exit-WithCode 1
}
}
$python = & pyenv which python
}
(Invoke-WebRequest -Uri https://install.python-poetry.org/ -UseBasicParsing).Content | & $($python) -
}
function CreatePackageWithDocker {
$startTime = [int][double]::Parse((Get-Date -UFormat %s))
Write-Host ">>> Building AYON using Docker ..."
$bundleName = $args[0]
$variant = $args[1]
$imageIdPath = "$($repo_root)/docker-image.id"
if ($null -eq $variant) {
$variant = "ubuntu"
}
if ($variant -eq "ubuntu") {
$dockerfile = "$($repo_root)\Dockerfile"
} else {
$dockerfile = "$($repo_root)\Dockerfile.$variant"
}
if (-not (Test-Path -PathType Leaf -Path $dockerfile)) {
Write-Host "!!! Dockerfile for specifed platform [$variant] doesn't exist."
Restore-Cwd
Exit-WithCode 1
}
Write-Host ">>> Running Docker build ..."
if (Test-Path $imageIdPath) {
Remove-Item $imageIdPath
}
docker build --pull --iidfile $imageIdPath --build-arg BUILD_DATE=$(Get-Date -UFormat %Y-%m-%dT%H:%M:%SZ) --build-arg VERSION=$TOOL_VERSION --build-arg BUNDLE_NAME=$bundleName -t ynput/ayon-dependencies:$TOOL_VERSION-$variant -f $dockerfile .
if (Test-Path $imageIdPath) {
$cid = Get-Content $imageIdPath
Remove-Item $imageIdPath
docker image rm $cid
}
if ($LASTEXITCODE -ne 0) {
Write-Host "!!! Docker command failed. $LASTEXITCODE"
Restore-Cwd
Exit-WithCode 1
}
$endTime = [int][double]::Parse((Get-Date -UFormat %s))
Write-Host "*** All done in $($endTime - $startTime) secs."
}
function Change-Cwd() {
Set-Location -Path $repo_root
}
function Restore-Cwd() {
Set-Location -Path $current_dir
}
function install {
# install dependencies for tool
if (-not (Test-Path -PathType Container -Path "$($env:POETRY_HOME)\bin")) {
Install-Poetry
}
Change-Cwd
Write-Host ">>> ", "Poetry config ... "
& "$env:POETRY_HOME\bin\poetry" install --no-interaction --no-root --ansi $poetry_verbosity
}
function set_env {
# set all env vars in .env file
if (-not (Test-Path "$($repo_root)\.env")) {
Write-Host "!!! ", ".env file must be prepared!" -ForegroundColor red
Exit-WithCode 1
} else {
$content = Get-Content -Path "$($repo_root)\.env" -Encoding UTF8 -Raw
foreach($line in $content.split()) {
if ($line){
$parts = $line.split("=")
$varName = $parts[0]
$value = $parts[1]
Write-Host "Setting $varName with $value"
Set-Item "env:$varName" $value
}
}
}
}
function main {
if ($null -eq $FunctionName) {
Default-Func
return
}
$FunctionName = $FunctionName.ToLower() -replace "\W"
if ($FunctionName -eq "install") {
Change-Cwd
install
} elseif ($FunctionName -eq "listen") {
Change-Cwd
set_env
& "$env:POETRY_HOME\bin\poetry" run python "$($repo_root)\service" @arguments
} elseif ($FunctionName -eq "setenv") {
Change-Cwd
set_env
} elseif ($FunctionName -eq "create") {
Change-Cwd
set_env
& "$env:POETRY_HOME\bin\poetry" run python "$($repo_root)\dependencies" create @arguments
} elseif ($FunctionName -eq "listbundles") {
Change-Cwd
set_env
& "$env:POETRY_HOME\bin\poetry" run python "$($repo_root)\dependencies" list-bundles @arguments
} elseif ($FunctionName -eq "dockerbuild") {
Change-Cwd
CreatePackageWithDocker @arguments
} else {
Write-Host "Unknown function \"$FunctionName\""
Default-Func
}
Restore-Cwd
}
# Force POETRY_HOME to this directory
$env:POETRY_HOME="$repo_root\.poetry"
$env:POETRY_VERSION="1.8.1"
main