Skip to content

Commit 68c9481

Browse files
committed
CFY-7569: Use temporary directory instead of current directory for temp files
1 parent dd624fe commit 68c9481

File tree

7 files changed

+33
-89
lines changed

7 files changed

+33
-89
lines changed

agent_packager/cli.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
"""Script to run Cloudify's Agent Packager via command line
44
55
Usage:
6-
cfy-ap [--config=<path> --force --dryrun --no-validation -v --env=<env_path>]
6+
cfy-ap [--config=<path> --force --dryrun --no-validation -v]
77
cfy-ap --version
88
99
Options:
1010
-h --help Show this screen
1111
-c --config=<path> Path to config yaml (defaults to config.yaml)
12-
-f --force Forces deletion and creation of venv and tar file.
12+
-f --force Forces deletion and creation of tar file.
1313
-d --dryrun Prints out the modules to be installed without actually installing them.
1414
-n --no-validation Does not validate that all modules were installed correctly.
15-
-e --env=<env_path> Virtualenv to use (defaults to a temporary directory)
1615
-v --verbose verbose level logging
1716
--version Display current version
1817
"""
@@ -48,8 +47,7 @@ def _run(test_options=None):
4847
force=options.get('--force'),
4948
dryrun=options.get('--dryrun'),
5049
no_validate=options.get('--no-validation'),
51-
verbose=options.get('--verbose'),
52-
virtualenv=options.get('--env')
50+
verbose=options.get('--verbose')
5351
)
5452

5553
def main():

agent_packager/packager.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
]
3939

4040
DEFAULT_CLOUDIFY_AGENT_URL = 'https://github.com/cloudify-cosmo/cloudify-agent/archive/{0}.tar.gz' # NOQA
41+
VENV_ROOT = ['cloudify', 'env']
4142

4243
lgr = logger.init()
4344
verbose_output = False
@@ -77,30 +78,6 @@ def _import_config(config_file=DEFAULT_CONFIG_FILE):
7778
sys.exit(codes.errors['invalid_yaml_file'])
7879

7980

80-
def _make_venv(venv, python, force):
81-
"""Handles the virtualenv.
82-
83-
removes the virtualenv if required, else, notifies
84-
that it already exists. If it doesn't exist, it will be
85-
created.
86-
:param string venv: path of virtualenv to install in.
87-
:param string python: python binary path to use.
88-
:param bool force: whether to force creation or not if it
89-
already exists.
90-
"""
91-
if utils.is_virtualenv(venv):
92-
if force:
93-
lgr.info('Installing within existing virtualenv: {0}'.format(venv))
94-
else:
95-
lgr.error('Virtualenv already exists at {0}. '
96-
'You can use the -f flag to install within the '
97-
'existing virtualenv.'.format(venv))
98-
sys.exit(codes.errors['virtualenv_already_exists'])
99-
else:
100-
lgr.debug('Creating virtualenv: {0}'.format(venv))
101-
utils.make_virtualenv(venv, python)
102-
103-
10481
def _handle_output_file(destination_tar, force):
10582
"""Handles the output tar.
10683
@@ -333,7 +310,7 @@ def _name_archive(distro, release, version, milestone, build):
333310

334311

335312
def create(config=None, config_file=None, force=False, dryrun=False,
336-
no_validate=False, verbose=True, virtualenv=None):
313+
no_validate=False, verbose=True):
337314
"""Creates an agent package (tar.gz)
338315
339316
This will try to identify the distribution of the host you're running on.
@@ -384,8 +361,8 @@ def create(config=None, config_file=None, force=False, dryrun=False,
384361
'({0})'.format(ex.message))
385362
sys.exit(codes.errors['could_not_identify_distribution'])
386363
python = config.get('python_path', '/usr/bin/python')
387-
venv = virtualenv or tempfile.mkdtemp(prefix='agent-packager')
388-
venv_already_exists = utils.is_virtualenv(venv)
364+
work_root = tempfile.mkdtemp(prefix='agent-packager')
365+
venv = os.path.join(work_root, *VENV_ROOT)
389366
destination_tar = config.get('output_tar', _name_archive(**name_params))
390367

391368
lgr.debug('Distibution is: {0}'.format(name_params['distro']))
@@ -394,7 +371,8 @@ def create(config=None, config_file=None, force=False, dryrun=False,
394371
lgr.debug('Destination tarfile is: {0}'.format(destination_tar))
395372

396373
if not dryrun:
397-
_make_venv(venv, python, force)
374+
lgr.debug('Creating virtualenv: {0}'.format(venv))
375+
utils.make_virtualenv(venv, python)
398376

399377
_handle_output_file(destination_tar, force)
400378

@@ -413,19 +391,18 @@ def create(config=None, config_file=None, force=False, dryrun=False,
413391
_uninstall_excluded(modules, venv)
414392
if not no_validate:
415393
_validate(final_set, venv)
416-
utils.tar(venv, destination_tar)
394+
utils.tar(work_root, os.path.join(*VENV_ROOT), destination_tar)
417395

418396
lgr.info('The following modules and plugins were installed '
419397
'in the agent:\n{0}'.format(utils.get_installed(venv)))
420398

421399
# if keep_virtualenv is explicitly specified to be false, the virtualenv
422400
# will not be deleted.
423-
# if keep_virtualenv is not in the config but the virtualenv already
424-
# existed, it will not be deleted.
425-
if ('keep_virtualenv' in config and not config['keep_virtualenv']) \
426-
or ('keep_virtualenv' not in config and not venv_already_exists):
427-
lgr.info('Removing origin virtualenv...')
428-
shutil.rmtree(venv)
401+
if not config.get('keep_virtualenv', False):
402+
lgr.info('Removing virtualenv...')
403+
shutil.rmtree(work_root)
404+
else:
405+
lgr.info('Virtualenv kept: {0}'.format(work_root))
429406

430407
# duh!
431408
lgr.info('Process complete!')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
distribution: Ubuntu
22
version: 3.0
3-
venv: /home/nir0s/Ubuntu-agent/env
3+
venv: /home/centos/Ubuntu-agent/env

agent_packager/tests/resources/config_file.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ release: trusty
33
python_path: '/usr/bin/python'
44
requirements_file: 'agent_packager/tests/resources/requirements.txt'
55
# cloudify_agent_version: 3.1
6-
cloudify_agent_module: https://github.com/nir0s/cloudify-agent/archive/master.tar.gz
6+
cloudify_agent_module: https://github.com/cloudify-cosmo/cloudify-agent/archive/master.tar.gz
77
core_modules:
8-
cloudify_plugins_common: https://github.com/cloudify-cosmo/cloudify-plugins-common/archive/3.1.tar.gz
8+
cloudify_plugins_common: https://github.com/cloudify-cosmo/cloudify-plugins-common/archive/master.tar.gz
99
# cloudify_rest_client: https://github.com/cloudify-cosmo/cloudify-rest-client/archive/3.1.tar.gz
1010
core_plugins:
1111
cloudify_script_plugin: exclude
@@ -17,6 +17,6 @@ core_plugins:
1717
additional_modules:
1818
- pyyaml==3.10
1919
additional_plugins:
20-
cloudify-fabric-plugin: https://github.com/cloudify-cosmo/cloudify-fabric-plugin/archive/1.1.tar.gz
20+
cloudify-fabric-plugin: https://github.com/cloudify-cosmo/cloudify-fabric-plugin/archive/1.5.1.tar.gz
2121
output_tar: Ubuntu-trusty-agent.tar.gz
2222
keep_virtualenv: true

agent_packager/tests/test_agent_packager.py

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_tar(self):
166166
os.makedirs('dir')
167167
with open('dir/content.file', 'w') as f:
168168
f.write('CONTENT')
169-
utils.tar('dir', 'tar.file')
169+
utils.tar(os.getcwd(), 'dir', 'tar.file')
170170
shutil.rmtree('dir')
171171
self.assertTrue(tarfile.is_tarfile('tar.file'))
172172
with closing(tarfile.open('tar.file', 'r:gz')) as tar:
@@ -176,12 +176,14 @@ def test_tar(self):
176176

177177
@venv
178178
def test_tar_no_permissions(self):
179-
e = self.assertRaises(SystemExit, utils.tar, TEST_VENV, '/file')
179+
e = self.assertRaises(SystemExit, utils.tar, os.getcwd(), TEST_VENV,
180+
'/file')
180181
self.assertEqual(e.message, codes.errors['failed_to_create_tar'])
181182

182183
@venv
183184
def test_tar_missing_source(self):
184-
e = self.assertRaises(SystemExit, utils.tar, 'missing', 'file')
185+
e = self.assertRaises(SystemExit, utils.tar, os.getcwd(), 'missing',
186+
'file')
185187
self.assertEqual(e.message, codes.errors['failed_to_create_tar'])
186188
os.remove('file')
187189

@@ -225,36 +227,6 @@ def test_create_agent_package(self):
225227
self.assertNotIn(excluded_module, pip_freeze_output)
226228
shutil.rmtree(TEST_VENV)
227229

228-
def test_create_agent_package_in_existing_venv_force(self):
229-
cli_options = {
230-
'--config': CONFIG_FILE,
231-
'--force': True,
232-
'--dryrun': False,
233-
'--no-validation': False,
234-
'--verbose': True
235-
}
236-
utils.make_virtualenv(TEST_VENV)
237-
try:
238-
cli._run(cli_options)
239-
finally:
240-
shutil.rmtree(TEST_VENV)
241-
242-
def test_create_agent_package_in_existing_venv_no_force(self):
243-
cli_options = {
244-
'--config': CONFIG_FILE,
245-
'--force': False,
246-
'--dryrun': False,
247-
'--no-validation': False,
248-
'--verbose': True
249-
}
250-
utils.make_virtualenv(TEST_VENV)
251-
try:
252-
e = self.assertRaises(SystemExit, cli._run, cli_options)
253-
self.assertEqual(
254-
e.message, codes.errors['virtualenv_already_exists'])
255-
finally:
256-
shutil.rmtree(TEST_VENV)
257-
258230
def test_dryrun(self):
259231
cli_options = {
260232
'--config': CONFIG_FILE,
@@ -278,12 +250,6 @@ def test_create_agent_package_no_cloudify_agent_configured(self):
278250
self.assertEqual(
279251
e.message, codes.errors['missing_cloudify_agent_config'])
280252

281-
@venv
282-
def test_create_agent_package_existing_venv_no_force(self):
283-
e = self.assertRaises(
284-
SystemExit, ap.create, None, CONFIG_FILE, verbose=True)
285-
self.assertEqual(e.message, codes.errors['virtualenv_already_exists'])
286-
287253
@venv
288254
def test_create_agent_package_tar_already_exists(self):
289255
config = ap._import_config(CONFIG_FILE)
@@ -313,7 +279,8 @@ def test_create_agent_package_with_version_info(self):
313279
ap.create(config, force=True, verbose=True)
314280
self.assertTrue(os.path.isfile(archive))
315281
finally:
316-
os.remove(archive)
282+
if os.path.exists(archive):
283+
os.remove(archive)
317284
os.environ.pop('VERSION')
318285
os.environ.pop('PRERELEASE')
319286
os.environ.pop('BUILD')

agent_packager/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def run(cmd, no_print=False):
1515
1616
:param string cmd: command to execute
1717
"""
18+
lgr.info('Running: {0}'.format(cmd))
1819
p = subprocess.Popen(
1920
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2021
(stdout, stderr) = p.communicate()
@@ -120,15 +121,16 @@ def download_file(url, destination):
120121
f.flush()
121122

122123

123-
def tar(source, destination):
124+
def tar(working_dir, source, destination):
124125
# TODO: solve or depracate..
125126
# TODO: apparently, it will tar the first child dir of
126127
# TODO: source, and not the given parent.
127128
# with closing(tarfile.open(destination, "w:gz")) as tar:
128129
# tar.add(source, arcname=os.path.basename(source))
129130
# WORKAROUND IMPLEMENTATION
130131
lgr.info('Creating tar file: {0}'.format(destination))
131-
r = run('tar czvf {0} {1}'.format(destination, source), no_print=True)
132+
r = run('tar czvf {0} -C {1} {2}'.format(destination, working_dir, source),
133+
no_print=False)
132134
if not r.returncode == 0:
133135
lgr.error('Failed to create tar file.')
134136
sys.exit(codes.errors['failed_to_create_tar'])

config/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ release: trusty
33
python_path: '/usr/bin/python'
44
requirements_file: 'agent_packager/tests/resources/requirements.txt'
55
# cloudify_agent_version: 3.1
6-
cloudify_agent_module: https://github.com/nir0s/cloudify-agent/archive/master.tar.gz
6+
cloudify_agent_module: https://github.com/cloudify-cosmo/cloudify-agent/archive/master.tar.gz
77
core_modules:
8-
cloudify_plugins_common: https://github.com/cloudify-cosmo/cloudify-plugins-common/archive/3.1.tar.gz
8+
cloudify_plugins_common: https://github.com/cloudify-cosmo/cloudify-plugins-common/archive/master.tar.gz
99
# cloudify_rest_client: https://github.com/cloudify-cosmo/cloudify-rest-client/archive/3.1.tar.gz
1010
core_plugins:
1111
cloudify_script_plugin: exclude
@@ -17,6 +17,6 @@ core_plugins:
1717
additional_modules:
1818
- pyyaml==3.10
1919
additional_plugins:
20-
cloudify-fabric-plugin: https://github.com/cloudify-cosmo/cloudify-fabric-plugin/archive/1.1.tar.gz
20+
cloudify-fabric-plugin: https://github.com/cloudify-cosmo/cloudify-fabric-plugin/archive/1.5.1.tar.gz
2121
output_tar: Ubuntu-trusty-agent.tar.gz
2222
keep_virtualenv: true

0 commit comments

Comments
 (0)