...
 
Commits (5)
......@@ -75,12 +75,16 @@ def progress(app, msg, state, log = True):
print(inflight[app])
#XXX Need to get a lot more paranoid about validating the .json.
#XXX Let json.dumps generate json, instead of building it as strings.
def start_container(appname, name, json):
d = docker.from_env()
try:
#XXX Renew certs if needed, also need to do that from a timer.
#XXX Need to do something so that apps can't steal each other's containers.
cont = d.containers.get(name)
except docker.errors.NotFound:
nets = []
#XXX Need to do something so that apps can't steal each other's private networks.
for netname in json.get('networks', []):
try:
net = d.networks.get(netname)
......@@ -93,14 +97,13 @@ def start_container(appname, name, json):
for hostp, guestp in json.get('expose', []):
args.setdefault('ports', {})[hostp] = guestp
if 'hostname' in json:
args['hostname'] = json.get('hostname')
args['hostname'] = json.get('hostname')
#XXX Watch out for ".." and friends in the appname/name/guestd name.
sds = {'/dev/log': {'bind':'/dev/log', 'mode':'rw'}}
for guestd in json.get('storagedirs', []):
#XXX hostd must incorporate guestd for >1 storagedirs, a hash?
hostd = '/mnt/vol00/appdata/%s/%s' % (appname,
name.strip('/').replace('/', '-'))
hostd = '/mnt/vol00/appdata/%s/%s-%s' % (appname, name,
guestd.strip('/').replace('/', '-'))
os.makedirs(hostd, mode=0o700, exist_ok=True)
sds[hostd] = { 'bind': guestd, 'mode': 'rw' }
if len(sds) > 0:
......@@ -114,7 +117,12 @@ def start_container(appname, name, json):
env[net.name.replace('-', '_') + "_SUBNET"] = net.attrs['IPAM']['Config'][0]['Subnet']
args['environment'] = env
image_name = json.get('reuse', name)
if 'reuse' in json:
#XXX Validate that reuse is one of the other images in this app.
image_name = "%s-%s" % (appname, json['reuse'])
else:
image_name = name
cont = d.containers.create(image_name, **args)
#Docker won't let you attach more than 1 container if attached to
......@@ -127,6 +135,7 @@ def start_container(appname, name, json):
cont.reload() #So we get the IP address(es).
#XXX Make sure different apps can't stomp on each other's names!
#XXX 'hostname' vs 'PRIVDNS'/'PUBDNS' is weird, unify?
for privdns in json.get('PRIVDNS', []):
#XXX These should be .int. subdomains.
priv_ip = cont.attrs['NetworkSettings']['Networks']['private']['IPAddress']
......@@ -142,6 +151,7 @@ def start_container(appname, name, json):
dnsd_cmd('["add","%s","A","%s"]' % (recs[1], pub_ip))
def start_app(app, json):
#XXX Thread this.
for cname, cj in json['containers'].items():
print("Starting container %s" % cname)
start_container(app, app + '-' + cname, cj)
......@@ -177,12 +187,13 @@ def install_app(app):
if 'reuse' in appj['containers'][img]:
print("Skipping %s-%s due to 'reuse'" % (app, img))
continue
#XXX Thread this?
print("Installing image %s" % app + '-' + img)
install_image(app, app + '-' + img, "%s of %s" % (i+1, len(appj['containers'])))
progress(app, "Application components installed successfully, starting", 1)
start_app(app, appj)
with open('/appsdir/' + app_cfgfile, "wb") as f:
with open('/apps/' + app_cfgfile, "wb") as f:
f.write(raw_json)
progress(app, "Application started successfully", 100)
except:
......@@ -203,7 +214,7 @@ class AppsTCPHandler(socketserver.StreamRequestHandler):
#XXX Exceptions.
if cmd == "install":
app = args[0]
if os.path.exists("/appsdir/" + app + ".json"):
if os.path.exists("/apps/" + app + ".json"):
self.wfile.write('[100,"App is installed"]\n'.encode())
if app in inflight:
del(inflight[app]) #General cleanliness.
......@@ -258,7 +269,8 @@ dyndns_thread.start()
init_complete.wait()
#Start up all installed apps.
for app in os.scandir('/appsdir'):
#XXX Parallelize this.
for app in os.scandir('/apps'):
try:
with open(app.path, 'r') as f:
appj = json.load(f)
......
......@@ -145,7 +145,7 @@ fi
echo Starting app store
mkdir -p /mnt/vol00/sysdata/appstore/data
docker container create -t -v /mnt/vol00/sysdata/appstore/data:/data \
-v /mnt/vol00/appsdir:/appsdir -v /var/run/docker.sock:/var/run/docker.sock \
-v /mnt/vol00/apps:/apps -v /var/run/docker.sock:/var/run/docker.sock \
--name appstore appstore python3 ./appserver.py "https://boundery.me" "$APIKEY" boundery.me
docker network connect --alias="apps-control" appscontrol appstore
docker network connect --alias="dnsd-control" dnsdcontrol appstore
......
......@@ -46,7 +46,7 @@ if [ -d /var/lib/docker/overlay2 ]; then
if [ "$dlayers" != "$layers" ]; then
echo "Cached docker images are no longer valid, will reload images"
(cd /var/lib/docker; ls -A | xargs -d'\n' rm -rf)
rm -f /mnt/vol00/appsdir/*
rm -f /mnt/vol00/apps/*
fi
fi
......