Commit 0ad51d3d authored by Marco Mariani's avatar Marco Mariani

import cleanup

parent ead116af
...@@ -30,24 +30,25 @@ ...@@ -30,24 +30,25 @@
import logging import logging
import os import os
import shutil
import subprocess
import pkg_resources import pkg_resources
import pwd import pwd
import shutil
import stat import stat
import subprocess
import tarfile import tarfile
import tempfile import tempfile
import textwrap import textwrap
import utils
import xmlrpclib import xmlrpclib
from supervisor import xmlrpc from supervisor import xmlrpc
from slapos.grid.utils import (getSoftwareUrlHash, getCleanEnvironment, bootstrapBuildout,
launchBuildout, SlapPopen, dropPrivileges, updateFile)
from slapos.slap.slap import NotFoundError from slapos.slap.slap import NotFoundError
from svcbackend import getSupervisorRPC from slapos.grid.svcbackend import getSupervisorRPC
from exception import BuildoutFailedError, WrongPermissionError, \ from slapos.grid.exception import (BuildoutFailedError, WrongPermissionError,
PathDoesNotExistError PathDoesNotExistError)
from networkcache import download_network_cached, upload_network_cached from slapos.grid.networkcache import download_network_cached, upload_network_cached
from watchdog import getWatchdogID from slapos.grid.watchdog import getWatchdogID
REQUIRED_COMPUTER_PARTITION_PERMISSION = '0750' REQUIRED_COMPUTER_PARTITION_PERMISSION = '0750'
...@@ -67,7 +68,7 @@ class Software(object): ...@@ -67,7 +68,7 @@ class Software(object):
""" """
self.url = url self.url = url
self.software_root = software_root self.software_root = software_root
self.software_url_hash = utils.getSoftwareUrlHash(self.url) self.software_url_hash = getSoftwareUrlHash(self.url)
self.software_path = os.path.join(self.software_root, self.software_path = os.path.join(self.software_root,
self.software_url_hash) self.software_url_hash)
self.buildout = buildout self.buildout = buildout
...@@ -136,7 +137,7 @@ class Software(object): ...@@ -136,7 +137,7 @@ class Software(object):
it. If it fails, we notify the server. it. If it fails, we notify the server.
""" """
root_stat_info = os.stat(self.software_root) root_stat_info = os.stat(self.software_root)
os.environ = utils.getCleanEnvironment(pwd.getpwuid(root_stat_info.st_uid os.environ = getCleanEnvironment(pwd.getpwuid(root_stat_info.st_uid
).pw_dir) ).pw_dir)
if not os.path.isdir(self.software_path): if not os.path.isdir(self.software_path):
os.mkdir(self.software_path) os.mkdir(self.software_path)
...@@ -175,9 +176,9 @@ class Software(object): ...@@ -175,9 +176,9 @@ class Software(object):
self.createProfileIfMissing(buildout_cfg, self.url) self.createProfileIfMissing(buildout_cfg, self.url)
buildout_parameter_list.extend(['-c', buildout_cfg]) buildout_parameter_list.extend(['-c', buildout_cfg])
utils.bootstrapBuildout(self.software_path, self.buildout, bootstrapBuildout(self.software_path, self.buildout,
additional_buildout_parametr_list=buildout_parameter_list) additional_buildout_parametr_list=buildout_parameter_list)
utils.launchBuildout(self.software_path, launchBuildout(self.software_path,
os.path.join(self.software_path, 'bin', 'buildout'), os.path.join(self.software_path, 'bin', 'buildout'),
additional_buildout_parametr_list=buildout_parameter_list) additional_buildout_parametr_list=buildout_parameter_list)
finally: finally:
...@@ -358,7 +359,7 @@ class Partition(object): ...@@ -358,7 +359,7 @@ class Partition(object):
'are %s' % 'are %s' %
(self.instance_path, permission, (self.instance_path, permission,
REQUIRED_COMPUTER_PARTITION_PERMISSION)) REQUIRED_COMPUTER_PARTITION_PERMISSION))
os.environ = utils.getCleanEnvironment(pwd.getpwuid( os.environ = getCleanEnvironment(pwd.getpwuid(
instance_stat_info.st_uid).pw_dir) instance_stat_info.st_uid).pw_dir)
# Generates buildout part from template # Generates buildout part from template
template_location = os.path.join(self.software_path, 'instance.cfg') template_location = os.path.join(self.software_path, 'instance.cfg')
...@@ -418,12 +419,12 @@ class Partition(object): ...@@ -418,12 +419,12 @@ class Partition(object):
self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list), self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list),
self.instance_path)) self.instance_path))
process_handler = utils.SlapPopen(invocation_list, process_handler = SlapPopen(invocation_list,
preexec_fn=lambda: utils.dropPrivileges(uid, gid), preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=self.instance_path, cwd=self.instance_path,
env=utils.getCleanEnvironment(pwd.getpwuid(uid).pw_dir), env=getCleanEnvironment(pwd.getpwuid(uid).pw_dir),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
if process_handler.returncode is None or process_handler.returncode != 0: if process_handler.returncode is None or process_handler.returncode != 0:
message = 'Failed to bootstrap buildout in %r.' % (self.instance_path) message = 'Failed to bootstrap buildout in %r.' % (self.instance_path)
self.logger.error(message) self.logger.error(message)
...@@ -432,12 +433,12 @@ class Partition(object): ...@@ -432,12 +433,12 @@ class Partition(object):
if not os.path.exists(buildout_binary): if not os.path.exists(buildout_binary):
# use own buildout generation # use own buildout generation
utils.bootstrapBuildout(self.instance_path, self.buildout, bootstrapBuildout(self.instance_path, self.buildout,
['buildout:bin-directory=%s'% os.path.join(self.instance_path, ['buildout:bin-directory=%s'% os.path.join(self.instance_path,
'sbin')]) 'sbin')])
buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout') buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')
# Launches buildout # Launches buildout
utils.launchBuildout(self.instance_path, buildout_binary) launchBuildout(self.instance_path, buildout_binary)
# Generates supervisord configuration file from template # Generates supervisord configuration file from template
self.logger.info("Generating supervisord config file from template...") self.logger.info("Generating supervisord config file from template...")
# check if CP/etc/run exists and it is a directory # check if CP/etc/run exists and it is a directory
...@@ -469,7 +470,7 @@ class Partition(object): ...@@ -469,7 +470,7 @@ class Partition(object):
self.addServiceToGroup(partition_id, runner_list,self.run_path) self.addServiceToGroup(partition_id, runner_list,self.run_path)
self.addServiceToGroup(partition_id, service_list,self.service_path, self.addServiceToGroup(partition_id, service_list,self.service_path,
extension=getWatchdogID()) extension=getWatchdogID())
utils.updateFile(self.supervisord_partition_configuration_path, updateFile(self.supervisord_partition_configuration_path,
self.partition_supervisor_configuration) self.partition_supervisor_configuration)
self.updateSupervisor() self.updateSupervisor()
...@@ -511,12 +512,12 @@ class Partition(object): ...@@ -511,12 +512,12 @@ class Partition(object):
if os.path.exists(destroy_executable_location): if os.path.exists(destroy_executable_location):
uid, gid = self.getUserGroupId() uid, gid = self.getUserGroupId()
self.logger.debug('Invoking %r' % destroy_executable_location) self.logger.debug('Invoking %r' % destroy_executable_location)
process_handler = utils.SlapPopen([destroy_executable_location], process_handler = SlapPopen([destroy_executable_location],
preexec_fn=lambda: utils.dropPrivileges(uid, gid), preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=self.instance_path, cwd=self.instance_path,
env=utils.getCleanEnvironment(pwd.getpwuid(uid).pw_dir), env=getCleanEnvironment(pwd.getpwuid(uid).pw_dir),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
if process_handler.returncode is None or process_handler.returncode != 0: if process_handler.returncode is None or process_handler.returncode != 0:
message = 'Failed to destroy Computer Partition in %r.' % \ message = 'Failed to destroy Computer Partition in %r.' % \
self.instance_path self.instance_path
......
...@@ -30,9 +30,7 @@ ...@@ -30,9 +30,7 @@
import argparse import argparse
import ConfigParser import ConfigParser
from exception import BuildoutFailedError
from hashlib import md5 from hashlib import md5
from lxml import etree
import logging import logging
import os import os
import pkg_resources import pkg_resources
...@@ -49,19 +47,17 @@ if sys.version_info < (2, 6): ...@@ -49,19 +47,17 @@ if sys.version_info < (2, 6):
warnings.warn('Used python version (%s) is old and have problems with' warnings.warn('Used python version (%s) is old and have problems with'
' IPv6 connections' % sys.version.split('\n')[0]) ' IPv6 connections' % sys.version.split('\n')[0])
from lxml import etree
from slapos.slap.slap import NotFoundError from slapos.slap.slap import NotFoundError
from slapos.slap.slap import ServerError from slapos.slap.slap import ServerError
from SlapObject import Software, Partition, WrongPermissionError, \ from slapos.grid.exception import BuildoutFailedError
from slapos.grid.SlapObject import Software, Partition, WrongPermissionError, \
PathDoesNotExistError PathDoesNotExistError
from svcbackend import launchSupervisord from slapos.grid.svcbackend import launchSupervisord
from utils import createPrivateDirectory from slapos.grid.utils import (getSoftwareUrlHash, createPrivateDirectory, dropPrivileges,
from utils import dropPrivileges setRunning, setFinished, SlapPopen, updateFile)
from utils import getSoftwareUrlHash import slapos.slap
from utils import setRunning
from utils import setFinished
from utils import SlapPopen
from utils import updateFile
from slapos import slap
MANDATORY_PARAMETER_LIST = [ MANDATORY_PARAMETER_LIST = [
'computer_id', 'computer_id',
...@@ -468,7 +464,7 @@ class Slapgrid(object): ...@@ -468,7 +464,7 @@ class Slapgrid(object):
# Configures logger # Configures logger
self.logger = logging.getLogger('Slapgrid') self.logger = logging.getLogger('Slapgrid')
# Creates objects from slap module # Creates objects from slap module
self.slap = slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.master_url, key_file=self.key_file, self.slap.initializeConnection(self.master_url, key_file=self.key_file,
cert_file=self.cert_file, master_ca_file=self.master_ca_file) cert_file=self.cert_file, master_ca_file=self.master_ca_file)
self.computer = self.slap.registerComputer(self.computer_id) self.computer = self.slap.registerComputer(self.computer_id)
...@@ -1290,7 +1286,7 @@ class Slapgrid(object): ...@@ -1290,7 +1286,7 @@ class Slapgrid(object):
logger.error(exception) logger.error(exception)
try: try:
computer_partition.destroyed() computer_partition.destroyed()
except slap.NotFoundError: except NotFoundError:
logger.debug('Ignored slap error while trying to inform about ' logger.debug('Ignored slap error while trying to inform about '
'destroying not fully configured Computer Partition %r' % 'destroying not fully configured Computer Partition %r' %
computer_partition.getId()) computer_partition.getId())
......
Markdown is supported
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