Skip to content

Commit 80e7571

Browse files
committed
reworked
1 parent 9909a16 commit 80e7571

File tree

8 files changed

+125
-122
lines changed

8 files changed

+125
-122
lines changed

ide/deploy/__main__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .deploy import set_dry_run, deploy
77
from .client import build
88

9-
109
def signal_handler(sig, frame):
1110
print('Termination requested.')
1211
os.remove(expanduser("~/.nuv/tmp/deploy.pid"))
@@ -22,13 +21,16 @@ def check_port():
2221
return
2322

2423
def main():
25-
# Register the signal handler for SIGTERM
24+
2625
os.setpgrp()
26+
pgrp = os.getpgrp()
27+
2728
signal.signal(signal.SIGTERM, signal_handler)
28-
29+
pidfile = expanduser("~/.nuv/tmp/deploy.pgrp")
30+
print("PID GROUP", pgrp)
31+
2932
os.makedirs(expanduser("~/.nuv/tmp"), exist_ok=True)
30-
with open(expanduser("~/.nuv/tmp/deploy.pid"), "w") as f:
31-
f.write(str(os.getpid())+"\n")
33+
Path(pidfile).write_text(str(pgrp))
3234

3335
parser = argparse.ArgumentParser(description='Deployer')
3436
parser.add_argument('directory', help='The mandatory first argument')

ide/deploy/client.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,48 @@
1616
# under the License.
1717

1818
from pathlib import Path
19+
from subprocess import Popen, PIPE
1920
import os, os.path, json
20-
from subprocess import Popen
21+
import threading
2122
import asyncio
2223

23-
def get_nuvolaris_config(key):
24+
def get_nuvolaris_config(key, default):
2425
try:
2526
dir = os.environ.get("NUV_PWD", "/do_not_exists")
2627
file = f"{dir}/package.json"
2728
info = json.loads(Path(file).read_text())
28-
return info.get("nuvolaris", {}).get(key)
29+
return info.get("nuvolaris", {}).get(key, default)
2930
except:
30-
return None
31-
31+
return default
32+
33+
def readlines(inp):
34+
for line in iter(inp.readline, ''):
35+
print(line, end='')
36+
3237
# serve web area
33-
async def serve():
34-
devel = get_nuvolaris_config("devel")
35-
if devel is None:
36-
devel = "nuv ide serve"
37-
print(devel)
38-
#Popen(devel, shell=True, cwd=os.environ.get("NUV_PWD"), env=os.environ)
39-
pwd = os.environ.get("NUV_PWD")
40-
cmd = f"cd '{pwd}' ; {devel}"
41-
proc = await asyncio.create_subprocess_shell(cmd,
42-
stdin=asyncio.subprocess.PIPE,
43-
stdout=asyncio.subprocess.PIPE,
44-
stderr=asyncio.subprocess.PIPE)
38+
def launch(key, default):
39+
cmd = get_nuvolaris_config(key, default)
40+
proc = Popen(
41+
cmd, shell=True,
42+
cwd=os.environ.get("NUV_PWD"), env=os.environ,
43+
stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True
44+
)
45+
threading.Thread(target=readlines, args=(proc.stdout,)).start()
46+
threading.Thread(target=readlines, args=(proc.stderr,)).start()
47+
48+
def serve():
49+
launch("devel", "nuv ide serve")
50+
51+
def logs():
52+
launch("logs", "nuv activation poll")
4553

4654
# build
4755
def build():
48-
deploy = get_nuvolaris_config("deploy")
49-
if deploy is not None:
50-
Popen(deploy, shell=True, cwd=os.environ.get("NUV_PWD"), env=os.environ)
56+
deploy = get_nuvolaris_config("deploy", "true")
57+
proc = Popen(
58+
deploy, shell=True,
59+
env=os.environ,
60+
cwd=os.environ.get("NUV_PWD"),
61+
stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True
62+
)
63+
proc.communicate()

ide/deploy/deploy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def deploy_action(artifact):
9494
file = "packages/deploy/multi/requirements.txt"
9595
"""
9696
def deploy(file):
97-
print(f"*** {file}")
97+
#print(f"*** {file}")
9898
if isdir(file):
9999
for start in MAINS:
100100
sub = f"{file}/{start}"

ide/deploy/scan.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def scan():
2323
deployments = set()
2424
packages = set()
2525

26-
print(">>> Scan:")
26+
print("> Scan:")
2727
reqs = glob("packages/*/*/requirements.txt") + glob("packages/*/*/package.json")
2828
# req = reqs[0]
2929
# from util.deploy.deploy import *
3030
for req in reqs:
31-
print(">", req)
31+
print(">> Requirements:", req)
3232
sp = req.split("/")
3333
act = build_zip(sp[1],sp[2])
3434
deployments.add(act)
@@ -37,7 +37,7 @@ def scan():
3737
mains = glob("packages/*/*/index.js") + glob("packages/*/*/__main__.py")
3838
# main = mains[2]
3939
for main in mains:
40-
print(">", main)
40+
print(">> Main:", main)
4141
sp = main.split("/")
4242
act = build_action(sp[1],sp[2])
4343
deployments.add(act)
@@ -46,17 +46,17 @@ def scan():
4646
singles = glob("packages/*/*.py") + glob("packages/*/*.js")
4747
# single = singles[0]
4848
for single in singles:
49-
print(">", single)
49+
print(">> Action:", single)
5050
sp = single.split("/")
5151
deployments.add(single)
5252
packages.add(sp[1])
5353

54-
print(">>> Deploying:")
54+
print("> Deploying:")
5555

5656
for package in packages:
57-
print("%", package)
57+
print(">> Package:", package)
5858
deploy_package(package)
5959

6060
for action in deployments:
61-
print("^", action)
61+
print(">>> Action:", action)
6262
deploy_action(action)

ide/deploy/watch.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import signal
77

88
from .deploy import deploy
9-
from .client import serve
9+
from .client import serve, logs
1010

1111
def check_and_deploy(change):
1212
cur_dir_len = len(os.getcwd())+1
@@ -40,19 +40,21 @@ async def redeploy():
4040
print("Exception")
4141

4242
def watch():
43+
# start web server
44+
serve()
45+
# show logs
46+
logs()
47+
4348
loop = asyncio.get_event_loop()
44-
task1 = loop.create_task(redeploy())
45-
#task = asyncio.ensure_future(redeploy())
46-
task2 = loop.create_task(serve())
49+
task = loop.create_task(redeploy())
4750
def end_loop():
4851
print("Ending task.")
49-
task1.cancel()
50-
task2.cancel()
52+
task.cancel()
5153
loop.add_signal_handler(signal.SIGTERM, end_loop)
5254

5355
try:
54-
loop.run_until_complete(task1)
55-
except asyncio.CancelledError:
56+
loop.run_until_complete(task)
57+
except:
5658
pass
5759
finally:
5860
loop.stop()

ide/http-serve.sh

Lines changed: 0 additions & 41 deletions
This file was deleted.

ide/nuvfile.yml

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ tasks:
7474
desc: local web server
7575
ignore_error: true
7676
cmds:
77-
- bash http-serve.sh "$NUV_PWD/web"
77+
- >
78+
http-server
79+
-a 127.0.0.1 "$NUV_PWD/web"
80+
-c-1 --mimetypes mime.types
81+
-P $NUVDEV_HOST
82+
83+
poll:
84+
silent: true
85+
desc: poll activation logs
86+
cmds:
87+
- nuv activation poll
7888

7989
_info:
8090
silent: true
@@ -83,34 +93,62 @@ tasks:
8393
- config -d | grep OPENAI_
8494
- config -d | grep NUVDEV_
8595

86-
_venv:
87-
cmds:
88-
- python3 -V || die "I need python3 in the path"
89-
- python3 -m venv venv
90-
- |
91-
source venv/bin/activate
92-
pip install watchdog
93-
status:
94-
- test -d venv
95-
96-
deploy:
96+
prereq:
9797
silent: true
98-
desc: deploy a project or a single action (with _action_=<dir-or-file>)
9998
cmds:
100-
- test -e "/.nuvolaris" || test -n "$NUVDEV_FORCE" || die "nuv ide commands must be run in the ghrc.io/nuvolaris/devcontainer with VSCode"
99+
- test "$(python3 -V | awk -F. '{print $2}')" -ge 10 || die "python 3.10 or greater not available"
100+
- test "$(node -v | awk -F. '{print substr($1,2) }')" -ge 18 || die "nodejs 18 or greater not available"
101101
- test -d "$NUV_PWD/packages" || die "no packages in current directory"
102102
- test -e ~/.wskprops || die "please run 'nuv ide login' first"
103103
- test -n "$NUVDEV_HOST" || die "please run 'nuv ide login' first"
104-
- |
105-
if test -e ~/.nuv/tmp/deploy.pid
106-
then kill "$(cat ~/.nuv/tmp/deploy.pid)" 2>/dev/null || true
107-
fi
108104
- |
109105
if test -e "$NUV_PWD/package.json"
110106
then if ! test -d "$NUV_PWD/node_modules"
111107
then cd $NUV_PWD ; npm install
112108
fi
113-
fi
109+
fi
110+
- |
111+
if ! python3 -m pip list | grep watchfiles >/dev/null 2>/dev/null
112+
then python3 -m pip install --user watchfiles asyncio
113+
fi
114+
- |
115+
if ! which http-server >/dev/null 2>/dev/null
116+
then npm install -g http-server
117+
fi
118+
- task: kill
119+
120+
kill:
121+
silent: false
122+
ignore: true
123+
cmds:
124+
- |
125+
if test -e ~/.nuv/tmp/deploy.pgrp
126+
then
127+
PGRP="$(cat ~/.nuv/tmp/deploy.pgrp)"
128+
#pstree $PID
129+
LOOP=true
130+
while $LOOP
131+
do
132+
LOOP=false
133+
ps -a -o pgid,pid | while read GRP PID
134+
do
135+
echo $GRP $PID
136+
if test "$GRP" = "$PGRP"
137+
then kill -9 "$PID"
138+
LOOP=true
139+
fi
140+
done
141+
sleep 2
142+
done
143+
rm ~/.nuv/tmp/deploy.pgrp
144+
fi
145+
146+
deploy:
147+
silent: true
148+
interactive: true
149+
desc: deploy a project or a single action (with _action_=<dir-or-file>)
150+
cmds:
151+
- task: prereq
114152
- |
115153
set -a
116154
if test -e $NUV_PWD/.env
@@ -132,25 +170,12 @@ tasks:
132170
python3 -m deploy "$NUV_PWD" -s "{{._action_}}" $DRY
133171
fi
134172
135-
136173
devel:
137-
silent: true
174+
interactive: true
175+
silent: false
138176
desc: start interactive development mode files
139177
cmds:
140-
- test -e "/.nuvolaris" || test -n "$NUVDEV_FORCE" || die "nuv ide commands must be run in the ghrc.io/nuvolaris/devcontainer with VSCode"
141-
- test -d "$NUV_PWD/packages" || die "no packages in current directory"
142-
- test -e ~/.wskprops || die "please run 'nuv ide login' first"
143-
- test -n "$NUVDEV_HOST" || die "please run 'nuv ide login' first"
144-
- |
145-
if test -e ~/.nuv/tmp/deploy.pid
146-
then kill "$(cat ~/.nuv/tmp/deploy.pid)" 2>/dev/null|| true
147-
fi
148-
- |
149-
if test -e "$NUV_PWD/package.json"
150-
then if ! test -d "$NUV_PWD/node_modules"
151-
then cd $NUV_PWD ; npm install
152-
fi
153-
fi
178+
- task: prereq
154179
- |
155180
set -a
156181
if test -e $NUV_PWD/.env
@@ -163,10 +188,8 @@ tasks:
163188
then DRY="--dry-run" ; ECHO='echo'
164189
else DRY="" ; ECHO=""
165190
fi
166-
if ! pip list | grep watchfiles >/dev/null
167-
then pip install watchfiles asyncio
168-
fi
169191
python3 -m deploy "$NUV_PWD" -w $DRY
192+
#npm-run-all --parallel deploy serve
170193
true
171194
172195
undeploy:

ide/nuvopts.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Usage:
88
ide clean
99
ide setup
1010
ide serve
11+
ide logs
1112
ide shell
13+
ide kill
1214
ide python
1315
ide nodejs
1416

@@ -19,7 +21,9 @@ Commands:
1921
ide undeploy undeploy everything or just one action
2022
ide clean clean the temporay files
2123
ide setup setup the ide
22-
ide serve web server
23-
ide shell start a shell with current env
24+
ide serve serve web area
25+
ide kill kill current devel or deploy job
26+
ide logs poll for logs
27+
ide shell start a shell with current env
2428
ide python python subcommands
2529
ide nodejs nodejs subcommands

0 commit comments

Comments
 (0)