def upload_rabbitmq_conf(template_name=None, context=None, restart=True): """Upload RabbitMQ configuration from a template.""" template_name = template_name or u'rabbitmq/rabbitmq.config' destination = u'/etc/rabbitmq/rabbitmq.config' upload_template(template_name, destination, context=context, use_sudo=True) if restart: restart_service(u'rabbitmq')
def upload_supervisor_app_conf(app_name, template_name=None, context=None): """Upload Supervisor app configuration from a template.""" context = context or {'app_name': app_name} template_name = template_name or [u'supervisor/%s.conf' % app_name, u'supervisor/base.conf'] destination = u'/etc/supervisor/conf.d/%s.conf' % app_name upload_template(template_name, destination, context=context, use_sudo=True) supervisor_command(u'update')
def upload_nginx_site_conf(site_name, template_name=None, context=None, enable=True): """Upload Nginx site configuration from a template.""" template_name = template_name or [u'nginx/%s.conf' % site_name, u'nginx/site.conf'] site_available = u'/etc/nginx/sites-available/%s' % site_name upload_template(template_name, site_available, context=context, use_sudo=True) if enable: enable_site(site_name)
def upload_local_settings(): """Upload local.py template to server.""" require('environment') dest = os.path.join(env.project_root, 'settings', 'local.py') _load_passwords(env.password_names, generate=True) upload_template('django/local.py', dest, use_sudo=True) with settings(warn_only=True): sudo('chown %s:%s %s' % (env.project_user, env.project_user, dest))
def test_destination(self): "Destination should be used for putting template on the server." with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/') put = self.mocks['put'] self.assertTrue(put.called) args, kwargs = put.call_args self.assertEqual(kwargs['remote_path'], '/tmp/foo.txt')
def test_use_sudo(self): "Use sudo should be used for putting template on the server." with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/', use_sudo=True) put = self.mocks['put'] sudo = self.mocks['sudo'] self.assertTrue(put.called) self.assertTrue(sudo.called) args, kwargs = put.call_args self.assertEqual(kwargs['use_sudo'], True)
def test_default_jinja_loader(self): "The default template are loaded from the argyle package." with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/') self.assertTrue(mock_environment.called) args, kwargs = mock_environment.call_args loader = kwargs['loader'] self.assertTrue(isinstance(loader, jinja2.ChoiceLoader)) self.assertEqual(len(loader.loaders), 1) self.assertTrue(isinstance(loader.loaders[0], jinja2.PackageLoader))
def test_additional_template_directories(self): "Additional template directories should come before defaults." with settings(ARGYLE_TEMPLATE_DIRS=('tests/templates', )): with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/') self.assertTrue(mock_environment.called) args, kwargs = mock_environment.call_args loader = kwargs['loader'] self.assertTrue(isinstance(loader, jinja2.ChoiceLoader)) self.assertEqual(len(loader.loaders), 2) self.assertTrue(isinstance(loader.loaders[0], jinja2.FileSystemLoader))
def upload_supervisor_app_conf(app_name, template_name=None, context=None): """Upload Supervisor app configuration from a template.""" default = {'app_name': app_name} context = context or {} default.update(context) template_name = template_name or [ u'supervisor/%s.conf' % app_name, u'supervisor/base.conf' ] destination = u'/etc/supervisor/conf.d/%s.conf' % app_name upload_template(template_name, destination, context=default, use_sudo=True) supervisor_command(u'update')
def test_default_template_context(self): "The fabric env should be the default template context." with patch('argyle.base.Environment') as mock_environment: mock_env = Mock() mock_template = Mock() mock_env.get_or_select_template.return_value = mock_template mock_environment.return_value = mock_env upload_template('foo.txt', '/tmp/') self.assertTrue(mock_template.render.called) args, kwargs = mock_template.render.call_args context = args[0] self.assertEqual(context, env)
def test_destination_filename(self): "If destination is a direction use file name from template." run = self.mocks['run'] result = Mock() result.succeeded = False # Not a directory run.return_value = result with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/bar.txt') put = self.mocks['put'] self.assertTrue(put.called) args, kwargs = put.call_args self.assertEqual(kwargs['remote_path'], '/tmp/bar.txt')
def upload_pg_hba_conf(template_name=None, pg_version=None, pg_cluster="main", restart=True): """ Upload configuration for pg_hba.conf If the version is not given it will be guessed. """ template_name = template_name or u"postgres/pg_hba.conf" version = pg_version or detect_version() config = {"version": version, "cluster": pg_cluster} destination = u"/etc/postgresql/%(version)s/%(cluster)s/pg_hba.conf" % config upload_template(template_name, destination, use_sudo=True) if restart: restart_service(u"postgresql")
def upload_pg_hba_conf(template_name=None, pg_version=None, pg_cluster='main', restart=True): """ Upload configuration for pg_hba.conf If the version is not given it will be guessed. """ template_name = template_name or u'postgres/pg_hba.conf' version = pg_version or detect_version() config = {'version': version, 'cluster': pg_cluster} destination = u'/etc/postgresql/%(version)s/%(cluster)s/pg_hba.conf' % config upload_template(template_name, destination, use_sudo=True) if restart: restart_service(u'postgresql')
def test_upload_tempalte_list(self): "Filename can be a list of filename for get_or_select_template." with patch('argyle.base.Environment') as mock_environment: mock_env = Mock() mock_template = Mock() mock_template.filename = 'bar.txt' # Second template is used mock_env.get_or_select_template.return_value = mock_template mock_environment.return_value = mock_env upload_template(['foo.txt', 'bar.txt'] , '/tmp/') put = self.mocks['put'] self.assertTrue(put.called) args, kwargs = put.call_args self.assertEqual(kwargs['remote_path'], '/tmp/bar.txt')
def test_additional_template_context(self): "Additional template context should be passed to the template render." with patch('argyle.base.Environment') as mock_environment: mock_env = Mock() mock_template = Mock() mock_env.get_or_select_template.return_value = mock_template mock_environment.return_value = mock_env extra = {'foo': 'bar'} upload_template('foo.txt', '/tmp/', context=extra) self.assertTrue(mock_template.render.called) args, kwargs = mock_template.render.call_args context = args[0] expected = env.copy() expected.update(extra) self.assertEqual(context, expected)
def test_destination_test(self): "Check if destination is a directory for constucting destination filename." with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/') self.assertRunCommand('test -d /tmp/')
def test_destination_test_sudo(self): "Destination check will use sudo if the upload requires sudo." with patch('argyle.base.Environment') as mock_environment: upload_template('foo.txt', '/tmp/', use_sudo=True) self.assertSudoCommand('test -d /tmp/')