Skip to content
Snippets Groups Projects
Verified Commit dc9c2eda authored by Karel Koci's avatar Karel Koci :metal:
Browse files

nsfarm/lxd._lxd: make global variables lowercase

Upper case are constants and we do not consider these as constants so we
should not name them that way.
parent 7e7980ab
Branches
Tags
1 merge request!2Initial development
"""Internal global LXD handle """Internal global LXD handle.
""" """
import logging import logging
import pylxd import pylxd
IMAGES_SOURCE = "https://images.linuxcontainers.org" IMAGES_SOURCE = "https://images.linuxcontainers.org"
ROOT_PROFILE = "nsfarm-root"
INTERNET_PROFILE = "nsfarm-internet"
REQUIRED_PROFILES = (ROOT_PROFILE, INTERNET_PROFILE)
# Global LXD handles # Global LXD handles
IMAGES = None images = None
LOCAL = None local = None
def _profile_device(profile, check_func): def _profile_device(profile, check_func):
...@@ -21,20 +25,20 @@ def connect(): ...@@ -21,20 +25,20 @@ def connect():
logging.getLogger('ws4py').setLevel(logging.ERROR) logging.getLogger('ws4py').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.ERROR) logging.getLogger('urllib3').setLevel(logging.ERROR)
# Initialize LXD connection to linuximages.org # Initialize LXD connection to linuximages.org
global IMAGES global images
if IMAGES is None: if images is None:
IMAGES = pylxd.Client(IMAGES_SOURCE) images = pylxd.Client(IMAGES_SOURCE)
# Initialize LXD connection to local server # Initialize LXD connection to local server
global LOCAL global local
if LOCAL is None: if local is None:
LOCAL = pylxd.Client() local = pylxd.Client()
# Verify profiles # Verify profiles
for name in ("nsfarm-root", "nsfarm-internet"): for name in REQUIRED_PROFILES:
if not LOCAL.profiles.exists(name): if not local.profiles.exists(name):
# TODO better exception # TODO better exception
raise Exception("Missing required LXD profile: {}".format(name)) raise Exception("Missing required LXD profile: {}".format(name))
root = LOCAL.profiles.get("nsfarm-root") root = local.profiles.get(ROOT_PROFILE)
internet = LOCAL.profiles.get("nsfarm-internet") internet = local.profiles.get(INTERNET_PROFILE)
if _profile_device(root, lambda dev: dev["type"] == "disk"): if _profile_device(root, lambda dev: dev["type"] == "disk"):
# TODO better exception # TODO better exception
raise Exception("nsfarm-root does not provide disk device") raise Exception("nsfarm-root does not provide disk device")
......
...@@ -6,6 +6,7 @@ import itertools ...@@ -6,6 +6,7 @@ import itertools
import hashlib import hashlib
import logging import logging
import pexpect import pexpect
import pylxd
from .. import cli from .. import cli
from . import _lxd from . import _lxd
...@@ -43,7 +44,7 @@ class Container: ...@@ -43,7 +44,7 @@ class Container:
if parent.startswith("nsfarm:"): if parent.startswith("nsfarm:"):
self._parent = Container(parent[7:]) self._parent = Container(parent[7:])
elif parent.startswith("images:"): elif parent.startswith("images:"):
self._parent = _lxd.IMAGES.images.get_by_alias(parent[7:]) self._parent = _lxd.images.images.get_by_alias(parent[7:])
else: else:
raise Exception("The file has parent from unknown source: {}: {}".format(parent, self.fpath)) raise Exception("The file has parent from unknown source: {}: {}".format(parent, self.fpath))
# Calculate identity hash and generate image name # Calculate identity hash and generate image name
...@@ -89,8 +90,8 @@ class Container: ...@@ -89,8 +90,8 @@ class Container:
""" """
if self._lxd_image: if self._lxd_image:
return return
if _lxd.LOCAL.images.exists(self._image_alias, alias=True): if _lxd.local.images.exists(self._image_alias, alias=True):
self._lxd_image = _lxd.LOCAL.images.get_by_alias(self._image_alias) self._lxd_image = _lxd.local.images.get_by_alias(self._image_alias)
return return
# We do not have appropriate image so prepare it # We do not have appropriate image so prepare it
logger.warning("Bootstrapping image: %s", self._image_alias) logger.warning("Bootstrapping image: %s", self._image_alias)
...@@ -108,7 +109,7 @@ class Container: ...@@ -108,7 +109,7 @@ class Container:
image_source["alias"] = self._parent.fingerprint image_source["alias"] = self._parent.fingerprint
container_name = "nsfarm-bootstrap-{}-{}".format(self._name, self._hash) container_name = "nsfarm-bootstrap-{}-{}".format(self._name, self._hash)
try: try:
container = _lxd.LOCAL.containers.create({ container = _lxd.local.containers.create({
'name': container_name, 'name': container_name,
'profiles': ['nsfarm-root', 'nsfarm-internet'], 'profiles': ['nsfarm-root', 'nsfarm-internet'],
'source': image_source 'source': image_source
...@@ -119,7 +120,7 @@ class Container: ...@@ -119,7 +120,7 @@ class Container:
raise raise
logger.warning("Other instance is already bootsrapping image probably. " logger.warning("Other instance is already bootsrapping image probably. "
"Waiting for following container to go away: %s", container_name) "Waiting for following container to go away: %s", container_name)
while _lxd.LOCAL.containers.exists(container_name): while _lxd.local.containers.exists(container_name):
time.sleep(1) time.sleep(1)
self.prepare_image() # possibly get created image or try again self.prepare_image() # possibly get created image or try again
return return
...@@ -161,7 +162,7 @@ class Container: ...@@ -161,7 +162,7 @@ class Container:
for device in self._devices: for device in self._devices:
devices.update(device.acquire(self)) devices.update(device.acquire(self))
# Create and start container # Create and start container
self._lxd_container = _lxd.LOCAL.containers.create({ self._lxd_container = _lxd.local.containers.create({
'name': self._container_name(), 'name': self._container_name(),
'ephemeral': True, 'ephemeral': True,
'profiles': profiles, 'profiles': profiles,
...@@ -178,9 +179,9 @@ class Container: ...@@ -178,9 +179,9 @@ class Container:
def _container_name(self, prefix="nsfarm"): def _container_name(self, prefix="nsfarm"):
name = "{}-{}-{}".format(prefix, self._name, os.getpid()) name = "{}-{}-{}".format(prefix, self._name, os.getpid())
if _lxd.LOCAL.containers.exists(name): if _lxd.local.containers.exists(name):
i = 1 i = 1
while _lxd.LOCAL.containers.exists("{}-{}".format(name, i)): while _lxd.local.containers.exists("{}-{}".format(name, i)):
i += 1 i += 1
name = "{}-{}".format(name, i) name = "{}-{}".format(name, i)
return name return name
...@@ -208,7 +209,7 @@ class Container: ...@@ -208,7 +209,7 @@ class Container:
""" """
assert self._lxd_container is not None assert self._lxd_container is not None
self._logger.debug("Running command: %s", command) self._logger.debug("Running command: %s", command)
pexp = pexpect.spawn('lxc', ["exec", self._lxd_container.name] + command) pexp = pexpect.spawn('lxc', ["exec", self._lxd_container.name] + list(command))
pexp.logfile_read = cli.PexpectLogging(logging.getLogger(self._logger.name + str(command))) pexp.logfile_read = cli.PexpectLogging(logging.getLogger(self._logger.name + str(command)))
return pexp return pexp
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment