Add rally use command - part 2

This completes the behaviour of the 'rally use' command.
when the command is called, it creates a file called openrc-{deployid}
in ~/.rally/ which contains instructions to export deployment specific
environment variables

It also creates a symlink ~/.rally/openrc that refers to the latest
deployment called with 'rally use'

It becomes simple to just do 'source ~/.rally/openrc' and then
'nova list'

Implements blueprint: rally-use-command

Change-Id: I51ddfdbf7b8ab41b5b26685dd7b671468d306fe1
This commit is contained in:
Julien Vey 2014-02-04 08:22:25 +01:00
parent 3d420c286b
commit e61269ef33
2 changed files with 46 additions and 7 deletions

View File

@ -364,6 +364,24 @@ class TaskCommands(object):
class UseCommands(object):
def _update_openrc_deployment_file(self, deploy_id):
openrc_path = os.path.expanduser('~/.rally/openrc-%s' % deploy_id)
endpoint = db.deployment_get(deploy_id)['endpoint']
with open(openrc_path, 'w+') as env_file:
env_file.write('export OS_AUTH_URL=%(auth_url)s\n'
'export OS_USERNAME=%(username)s\n'
'export OS_PASSWORD=%(password)s\n'
'export OS_TENANT_NAME=%(tenant_name)s\n'
% endpoint)
expanded_path = os.path.expanduser('~/.rally/openrc')
if os.path.exists(expanded_path):
os.remove(expanded_path)
os.symlink(openrc_path, expanded_path)
def _update_rally_deployment_file(self, deploy_id):
expanded_path = os.path.expanduser('~/.rally/deployment')
fileutils.update_env_file(expanded_path, 'RALLY_DEPLOYMENT', deploy_id)
def deployment(self, deploy_id):
"""Set the RALLY_DEPLOYMENT env var to be used by all CLI commands
@ -372,8 +390,8 @@ class UseCommands(object):
print('Using deployment : %s' % deploy_id)
if not os.path.exists(os.path.expanduser('~/.rally/')):
os.makedirs(os.path.expanduser('~/.rally/'))
expanded_path = os.path.expanduser('~/.rally/deployment')
fileutils.update_env_file(expanded_path, 'RALLY_DEPLOYMENT', deploy_id)
self._update_rally_deployment_file(deploy_id)
self._update_openrc_deployment_file(deploy_id)
def deprecated():

View File

@ -223,12 +223,33 @@ class UseCommandsTestCase(test.BaseTestCase):
super(UseCommandsTestCase, self).setUp()
self.use = main.UseCommands()
@mock.patch('os.remove')
@mock.patch('os.symlink')
@mock.patch('rally.cmd.main.db.deployment_get')
@mock.patch('os.path.exists')
@mock.patch('rally.cmd.main.fileutils.update_env_file')
def test_deployment(self, mock_file, mock_path):
def test_deployment(self, mock_env, mock_path, mock_deployment,
mock_symlink, mock_remove):
deploy_id = str(uuid.uuid4())
endpoint = {'endpoint': {'auth_url': 'fake_auth_url',
'username': 'fake_username',
'password': 'fake_password',
'tenant_name': 'fake_tenant_name'}}
mock_deployment.return_value = endpoint
mock_path.return_value = True
self.use.deployment(deploy_id)
mock_path.assert_called_once_with(os.path.expanduser('~/.rally/'))
mock_file.assert_called_once_with(os.path.expanduser(
'~/.rally/deployment'), 'RALLY_DEPLOYMENT', deploy_id)
with mock.patch('rally.cmd.main.open', mock.mock_open(),
create=True) as mock_file:
self.use.deployment(deploy_id)
self.assertEqual(2, mock_path.call_count)
mock_env.assert_called_once_with(os.path.expanduser(
'~/.rally/deployment'), 'RALLY_DEPLOYMENT', deploy_id)
mock_file.return_value.write.assert_called_once_with(
'export OS_AUTH_URL=fake_auth_url\n'
'export OS_USERNAME=fake_username\n'
'export OS_PASSWORD=fake_password\n'
'export OS_TENANT_NAME=fake_tenant_name\n')
mock_symlink.assert_called_once_with(
os.path.expanduser('~/.rally/openrc-%s' % deploy_id),
os.path.expanduser('~/.rally/openrc'))
mock_remove.assert_called_once_with(os.path.expanduser(
'~/.rally/openrc'))