import uuid
from distutils.spawn import find_executable
+from functools import wraps
from glob import glob
- try:
- from tempfile import TemporaryDirectory # py3
- except ImportError:
- # define a minimal (but sufficient) equivalent for <= py 3.2
- class TemporaryDirectory(object): # type: ignore
- def __init__(self):
- self.name = tempfile.mkdtemp()
-
- def __enter__(self):
- if not self.name:
- self.name = tempfile.mkdtemp()
- return self.name
-
- def cleanup(self):
- shutil.rmtree(self.name)
-
- def __exit__(self, exc_type, exc_value, traceback):
- self.cleanup()
-
container_path = None
logger.info('Adding key to root@localhost\'s authorized_keys...')
if not os.path.exists('/root/.ssh'):
os.mkdir('/root/.ssh', 0o700)
- with open('/root/.ssh/authorized_keys', 'a') as f:
+ auth_keys_file = '/root/.ssh/authorized_keys'
+ add_newline = False
+ if os.path.exists(auth_keys_file):
+ with open(auth_keys_file, 'r') as f:
+ f.seek(0, 2)
+ if f.tell() > 0:
+ f.seek(-1, 2) # go to last character
+ if f.read() != '\n':
+ add_newline = True
+ with open(auth_keys_file, 'a') as f:
os.fchmod(f.fileno(), 0o600) # just in case we created it
+ if add_newline:
+ f.write('\n')
f.write(ssh_pub.strip() + '\n')
- logger.info('Enabling ssh module...')
- cli(['mgr', 'module', 'enable', 'ssh'])
- logger.info('Setting orchestrator backend to ssh...')
- cli(['orchestrator', 'set', 'backend', 'ssh'])
host = get_hostname()
logger.info('Adding host %s...' % host)
cli(['orchestrator', 'host', 'add', host])