Use file-locking also for config.
This commit is contained in:
parent
fa23e4828f
commit
ace50a79e7
@ -1,22 +1,25 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import fcntl
|
||||||
import json
|
import json
|
||||||
from threading import Lock
|
|
||||||
|
|
||||||
CONF_FILE = '/etc/vmmgr/config.json'
|
CONF_FILE = '/etc/vmmgr/config.json'
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.lock = Lock()
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
with self.lock:
|
# Load configuration from file. Uses file lock as interprocess mutex
|
||||||
|
with open('/var/lock/vmmgr-hosts.lock', 'w') as lock:
|
||||||
|
fcntl.lockf(lock, fcntl.LOCK_EX)
|
||||||
with open(CONF_FILE, 'r') as f:
|
with open(CONF_FILE, 'r') as f:
|
||||||
self.data = json.load(f)
|
self.data = json.load(f)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with self.lock:
|
# Save configuration to a file. Uses file lock as interprocess mutex
|
||||||
|
with open('/var/lock/vmmgr-hosts.lock', 'w') as lock:
|
||||||
|
fcntl.lockf(lock, fcntl.LOCK_EX)
|
||||||
with open(CONF_FILE, 'w') as f:
|
with open(CONF_FILE, 'w') as f:
|
||||||
json.dump(self.data, f, sort_keys=True, indent=4)
|
json.dump(self.data, f, sort_keys=True, indent=4)
|
||||||
|
|
||||||
|
@ -113,8 +113,9 @@ def reboot_vm():
|
|||||||
def update_hosts_lease(app, is_request):
|
def update_hosts_lease(app, is_request):
|
||||||
# This is a poor man's DHCP server which uses /etc/hosts as lease database
|
# This is a poor man's DHCP server which uses /etc/hosts as lease database
|
||||||
# Leases the first unused IP from range 172.17.0.0/16
|
# Leases the first unused IP from range 172.17.0.0/16
|
||||||
|
# Uses file lock as interprocess mutex
|
||||||
ip = None
|
ip = None
|
||||||
with open('/var/lock/hosts.lock', 'w') as lock:
|
with open('/var/lock/vmmgr-hosts.lock', 'w') as lock:
|
||||||
fcntl.lockf(lock, fcntl.LOCK_EX)
|
fcntl.lockf(lock, fcntl.LOCK_EX)
|
||||||
with open('/etc/hosts', 'r') as f:
|
with open('/etc/hosts', 'r') as f:
|
||||||
leases = [l.strip().split(' ', 1) for l in f]
|
leases = [l.strip().split(' ', 1) for l in f]
|
||||||
@ -138,6 +139,8 @@ def set_container_ip(pid, ip):
|
|||||||
subprocess.run(['nsenter', '-a', '-t', pid, '--', '/bin/sh', '-c', cmd])
|
subprocess.run(['nsenter', '-a', '-t', pid, '--', '/bin/sh', '-c', cmd])
|
||||||
|
|
||||||
def clean_ephemeral_layer(app):
|
def clean_ephemeral_layer(app):
|
||||||
|
# Cleans containers ephemeral layer.
|
||||||
|
# This is done early in the container start process, so the inode of the delta0 directory must remain unchanged
|
||||||
layer = os.path.join('/var/lib/lxc', app, 'delta0')
|
layer = os.path.join('/var/lib/lxc', app, 'delta0')
|
||||||
if os.path.exists(layer):
|
if os.path.exists(layer):
|
||||||
for item in os.scandir(layer):
|
for item in os.scandir(layer):
|
||||||
|
Loading…
Reference in New Issue
Block a user