Commit 4ba06039 authored by Alain Takoudjou's avatar Alain Takoudjou

slapos.grid: fixup! copy .netrc file to buildout home path

See merge request !727
parents ed92d54a 755a4688
Pipeline #38675 failed with stage
in 0 seconds
...@@ -273,6 +273,21 @@ class Software(object): ...@@ -273,6 +273,21 @@ class Software(object):
if value: if value:
yield 'networkcache:%s=%s' % (networkcache_option, value) yield 'networkcache:%s=%s' % (networkcache_option, value)
def _copy_netrc_file(self, dest_path):
home_netrc = os.path.join(
pwd.getpwuid(os.stat(self.software_root).st_uid).pw_dir,
'.netrc'
)
buildout_netrc = os.path.join(dest_path, '.netrc')
if os.path.abspath(home_netrc) == buildout_netrc:
return
if os.path.exists(buildout_netrc):
os.unlink(buildout_netrc)
if os.path.exists(home_netrc):
shutil.copyfile(home_netrc, buildout_netrc)
os.chmod(buildout_netrc, 0o600)
self._set_ownership(buildout_netrc)
def _install_from_buildout(self): def _install_from_buildout(self):
""" Fetches buildout configuration from the server, run buildout with """ Fetches buildout configuration from the server, run buildout with
it. If it fails, we notify the server. it. If it fails, we notify the server.
...@@ -285,6 +300,7 @@ class Software(object): ...@@ -285,6 +300,7 @@ class Software(object):
else: else:
os.chmod(self.software_path, 0o755) os.chmod(self.software_path, 0o755)
self._set_ownership(self.software_path) self._set_ownership(self.software_path)
self._copy_netrc_file(self.software_path)
f = None f = None
extends_cache = tempfile.mkdtemp() extends_cache = tempfile.mkdtemp()
......
...@@ -253,21 +253,6 @@ def getCleanEnvironment(logger, home_path='/tmp'): ...@@ -253,21 +253,6 @@ def getCleanEnvironment(logger, home_path='/tmp'):
logger.debug('Removed from environment: %s', ', '.join(sorted(removed_env))) logger.debug('Removed from environment: %s', ', '.join(sorted(removed_env)))
return env return env
def copyNetrcFile(dest_path):
user_home = os.environ['HOME']
netrc_file = os.path.join(user_home, '.netrc')
buildout_netrc = os.path.join(dest_path, '.netrc')
if os.path.abspath(netrc_file) == buildout_netrc:
return
if os.path.exists(netrc_file):
shutil.copyfile(netrc_file, buildout_netrc)
os.chmod(buildout_netrc, 0o600)
def cleanupNetrcFile(dest_path):
buildout_netrc = os.path.join(dest_path, '.netrc')
if os.path.exists(buildout_netrc):
os.unlink(buildout_netrc)
def setRunning(logger, pidfile): def setRunning(logger, pidfile):
"""Creates a pidfile. If a pidfile already exists, we exit""" """Creates a pidfile. If a pidfile already exists, we exit"""
if os.path.exists(pidfile): if os.path.exists(pidfile):
...@@ -455,7 +440,6 @@ def launchBuildout(path, buildout_binary, logger, ...@@ -455,7 +440,6 @@ def launchBuildout(path, buildout_binary, logger,
path)) path))
if timeout is not None: if timeout is not None:
logger.debug('Launching buildout with %ss timeout', timeout) logger.debug('Launching buildout with %ss timeout', timeout)
copyNetrcFile(path)
process_handler = SlapPopen(invocation_list, process_handler = SlapPopen(invocation_list,
preexec_fn=lambda: dropPrivileges(uid, gid, logger=logger), preexec_fn=lambda: dropPrivileges(uid, gid, logger=logger),
cwd=path, cwd=path,
...@@ -472,7 +456,6 @@ def launchBuildout(path, buildout_binary, logger, ...@@ -472,7 +456,6 @@ def launchBuildout(path, buildout_binary, logger,
logger.exception(exc) logger.exception(exc)
raise BuildoutFailedError(exc) raise BuildoutFailedError(exc)
finally: finally:
cleanupNetrcFile(path)
old_umask = os.umask(umask) old_umask = os.umask(umask)
logger.debug('Restore umask from %03o to %03o' % (old_umask, umask)) logger.debug('Restore umask from %03o to %03o' % (old_umask, umask))
......
...@@ -40,6 +40,7 @@ import textwrap ...@@ -40,6 +40,7 @@ import textwrap
import time import time
import unittest import unittest
import six import six
import pwd
from six.moves.urllib import parse from six.moves.urllib import parse
import json import json
import re import re
...@@ -2226,11 +2227,16 @@ class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase): ...@@ -2226,11 +2227,16 @@ class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase):
def setUp(self): def setUp(self):
MasterMixin.setUp(self) MasterMixin.setUp(self)
self.orginal_home = os.environ['HOME'] self.pwuid_patch = patch.object(pwd, 'getpwuid', new=self.fake_getpwuid)
os.environ['HOME'] = self._tempdir self.pwuid_patch.start()
def tearDown(self): def tearDown(self):
os.environ['HOME'] = self.orginal_home self.pwuid_patch.stop()
def fake_getpwuid(self, uid):
return pwd.struct_passwd(
("fake", "x", uid, uid, "", self.software_root, "/bin/bash")
)
fake_waiting_time = 0.05 fake_waiting_time = 0.05
def test_one_software_buildout_fail_is_correctly_logged(self): def test_one_software_buildout_fail_is_correctly_logged(self):
...@@ -2301,39 +2307,31 @@ chmod a-rxw directory ...@@ -2301,39 +2307,31 @@ chmod a-rxw directory
def test_build_software_with_netrc(self): def test_build_software_with_netrc(self):
computer = self.getTestComputerClass()(self.software_root, self.instance_root, 1, 1) computer = self.getTestComputerClass()(self.software_root, self.instance_root, 1, 1)
home = os.environ['HOME'] netrc_file = os.path.join(self.software_root, '.netrc')
netrc_file = os.path.join(home, '.netrc')
with open(netrc_file, 'w') as f: with open(netrc_file, 'w') as f:
f.write('machine localhost login foo password bar') f.write('machine localhost login foo password bar')
with open(os.path.join(home, 'testing'), 'w') as f: with open(os.path.join(self.software_root, 'testing'), 'w') as f:
f.write('this is not buildout home') f.write('this is not buildout home')
os.chmod(netrc_file, 0o600) os.chmod(netrc_file, 0o600)
with httmock.HTTMock(computer.request_handler): with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0] software = computer.software_list[0]
software_path = os.path.join(self.software_root, software.software_hash) software_path = os.path.join(self.software_root, software.software_hash)
buildout_netrc = os.path.join(software_path, '.netrc') buildout_netrc = os.path.join(software_path, '.netrc')
test_file = os.path.join(software_path, 'd/file')
command = """#!/bin/sh command = """#!/bin/sh
mkdir -p d # $HOME is different from buildout netrc home location
if [ -s "$HOME/testing" ]; then if [ -s "$HOME/testing" ]; then
echo "testing file exists" echo "testing file exists"
exit 1 exit 1
fi fi
if [ -s "$HOME/.netrc" ]; then
cp $HOME/.netrc d/file
else
echo ".netrc file not found"
rm -f d/file
fi
""" """
software.setBuildout(command) software.setBuildout(command)
self.launchSlapgridSoftware() self.launchSlapgridSoftware()
self.assertTrue(os.path.exists(netrc_file)) self.assertTrue(os.path.exists(netrc_file))
self.assertFalse(os.path.exists(buildout_netrc)) self.assertTrue(os.path.exists(buildout_netrc))
self.assertTrue(os.path.exists(test_file))
with open(test_file) as f: with open(buildout_netrc) as f:
content = f.read() content = f.read()
self.assertEqual(content, 'machine localhost login foo password bar') self.assertEqual(content, 'machine localhost login foo password bar')
...@@ -2343,7 +2341,6 @@ fi ...@@ -2343,7 +2341,6 @@ fi
os.remove(completed) os.remove(completed)
self.launchSlapgridSoftware() self.launchSlapgridSoftware()
self.assertFalse(os.path.exists(buildout_netrc)) self.assertFalse(os.path.exists(buildout_netrc))
self.assertFalse(os.path.exists(test_file))
class SlapgridInitialization(unittest.TestCase): class SlapgridInitialization(unittest.TestCase):
""" """
......
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