本文整理汇总了Python中charmhelpers.core.host.write_file方法的典型用法代码示例。如果您正苦于以下问题:Python host.write_file方法的具体用法?Python host.write_file怎么用?Python host.write_file使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类charmhelpers.core.host
的用法示例。
在下文中一共展示了host.write_file方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: configure_admin
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def configure_admin(self):
"""Configure the admin user."""
hookenv.log("Configuring user for jenkins")
admin = self._admin_data()
api = Api()
api.update_password(admin.username, admin.password)
# Save the password to a file. It's not used directly by this charm
# but it's convenient for integration with third-party tools.
host.write_file(
paths.ADMIN_PASSWORD, admin.password.encode("utf-8"),
owner="root", group="root", perms=0o0600)
if not os.path.exists(paths.LAST_EXEC):
# This mean it's the very first time we configure the user,
# and we want to create this file in order to avoid Jenkins
# presenting the setup wizard.
host.write_file(
paths.LAST_EXEC, "{}\n".format(api.version()).encode("utf-8"),
owner="jenkins", group="nogroup", perms=0o0600)
示例2: write_key_file
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def write_key_file(self, unit_name, key):
"""Write rndc keyfile for given unit_name
@param unit_name: str Name of unit using key
@param key: str RNDC key
@returns None
"""
key_file = '/etc/designate/rndc_{}.key'.format(unit_name)
template = ('key "rndc-key" {{\n algorithm hmac-md5;\n '
'secret "{}";\n}};')
host.write_file(
key_file,
str.encode(template.format(key)),
owner='root',
group='designate',
perms=0o440)
示例3: test_writes_content_with_default
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def test_writes_content_with_default(self, os_, log):
uid = 0
gid = 0
path = '/some/path/{baz}'
fmtstr = b'what is {juju}'
perms = 0o444
fileno = 'some-fileno'
with patch_open() as (mock_open, mock_file):
mock_file.fileno.return_value = fileno
host.write_file(path, fmtstr)
mock_open.assert_called_with('/some/path/{baz}', 'wb')
os_.fchown.assert_called_with(fileno, uid, gid)
os_.fchmod.assert_called_with(fileno, perms)
mock_file.write.assert_called_with(b'what is {juju}')
示例4: _install_plugin
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def _install_plugin(self, plugins_site, plugin, wget_options):
"""Download and install a given plugin."""
plugin_filename = "%s.hpi" % plugin
url = os.path.join(plugins_site, plugin_filename)
plugin_path = os.path.join(paths.PLUGINS, plugin_filename)
if not os.path.isfile(plugin_path):
hookenv.log("Installing plugin %s" % plugin_filename)
command = ("wget",) + wget_options + ("-q", "-O", "-", url)
plugin_data = subprocess.check_output(command)
host.write_file(
plugin_path, plugin_data, owner="jenkins", group="jenkins",
perms=0o0744)
else:
hookenv.log("Plugin %s already installed" % plugin_filename)
return plugin_path
示例5: token
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def token(self, value=None):
"""Get or set the admin token from/to the local state."""
if value is not None:
# Save the token to a file
host.write_file(
paths.ADMIN_TOKEN, value.encode("utf-8"), owner="root",
group="root", perms=0o0600)
if not os.path.exists(paths.ADMIN_TOKEN):
return None
with open(paths.ADMIN_TOKEN, 'r') as f:
return f.read().strip()
示例6: configure_cert
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def configure_cert(self, cert, key, cn=None):
"""Configure service SSL cert and key
Write out service SSL certificate and key for Apache.
@param cert string SSL Certificate
@param key string SSL Key
@param cn string Canonical name for service
"""
if os_utils.snap_install_requested():
ssl_dir = '/var/snap/{snap_name}/common/etc/nginx/ssl'.format(
snap_name=self.primary_snap)
else:
ssl_dir = os.path.join('/etc/apache2/ssl/', self.name)
if not cn:
cn = os_ip.resolve_address(endpoint_type=os_ip.INTERNAL)
ch_host.mkdir(path=ssl_dir)
if cn:
cert_filename = 'cert_{}'.format(cn)
key_filename = 'key_{}'.format(cn)
else:
cert_filename = 'cert'
key_filename = 'key'
ch_host.write_file(path=os.path.join(ssl_dir, cert_filename),
content=cert.encode('utf-8'))
ch_host.write_file(path=os.path.join(ssl_dir, key_filename),
content=key.encode('utf-8'))
示例7: test_writes_content_to_a_file
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def test_writes_content_to_a_file(self, os_, log, getgrnam, getpwnam):
# Curly brackets here demonstrate that we are *not* rendering
# these strings with Python's string formatting. This is a
# change from the original behavior per Bug #1195634.
uid = 123
gid = 234
owner = 'some-user-{foo}'
group = 'some-group-{bar}'
path = '/some/path/{baz}'
contents = b'what is {juju}'
perms = 0o644
fileno = 'some-fileno'
getpwnam.return_value.pw_uid = uid
getgrnam.return_value.gr_gid = gid
with patch_open() as (mock_open, mock_file):
mock_file.fileno.return_value = fileno
host.write_file(path, contents, owner=owner, group=group,
perms=perms)
getpwnam.assert_called_with('some-user-{foo}')
getgrnam.assert_called_with('some-group-{bar}')
mock_open.assert_called_with('/some/path/{baz}', 'wb')
os_.fchown.assert_called_with(fileno, uid, gid)
os_.fchmod.assert_called_with(fileno, perms)
mock_file.write.assert_called_with(b'what is {juju}')
示例8: test_writes_binary_contents
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def test_writes_binary_contents(self, os_, log):
path = '/some/path/{baz}'
fmtstr = six.u('what is {juju}\N{TRADE MARK SIGN}').encode('UTF-8')
fileno = 'some-fileno'
with patch_open() as (mock_open, mock_file):
mock_file.fileno.return_value = fileno
host.write_file(path, fmtstr)
mock_open.assert_called_with('/some/path/{baz}', 'wb')
mock_file.write.assert_called_with(fmtstr)
示例9: render
# 需要导入模块: from charmhelpers.core import host [as 别名]
# 或者: from charmhelpers.core.host import write_file [as 别名]
def render(source, target, context, owner='root', group='root',
perms=0o444, templates_dir=None, encoding='UTF-8', template_loader=None):
"""
Render a template.
The `source` path, if not absolute, is relative to the `templates_dir`.
The `target` path should be absolute. It can also be `None`, in which
case no file will be written.
The context should be a dict containing the values to be replaced in the
template.
The `owner`, `group`, and `perms` options will be passed to `write_file`.
If omitted, `templates_dir` defaults to the `templates` folder in the charm.
The rendered template will be written to the file as well as being returned
as a string.
Note: Using this requires python-jinja2; if it is not installed, calling
this will attempt to use charmhelpers.fetch.apt_install to install it.
"""
try:
from jinja2 import FileSystemLoader, Environment, exceptions
except ImportError:
try:
from charmhelpers.fetch import apt_install
except ImportError:
hookenv.log('Could not import jinja2, and could not import '
'charmhelpers.fetch to install it',
level=hookenv.ERROR)
raise
apt_install('python-jinja2', fatal=True)
from jinja2 import FileSystemLoader, Environment, exceptions
if template_loader:
template_env = Environment(loader=template_loader)
else:
if templates_dir is None:
templates_dir = os.path.join(hookenv.charm_dir(), 'templates')
template_env = Environment(loader=FileSystemLoader(templates_dir))
try:
source = source
template = template_env.get_template(source)
except exceptions.TemplateNotFound as e:
hookenv.log('Could not load template %s from %s.' %
(source, templates_dir),
level=hookenv.ERROR)
raise e
content = template.render(context)
if target is not None:
target_dir = os.path.dirname(target)
if not os.path.exists(target_dir):
# This is a terrible default directory permission, as the file
# or its siblings will often contain secrets.
host.mkdir(os.path.dirname(target), owner, group, perms=0o755)
host.write_file(target, content.encode(encoding), owner, group, perms)
return content