def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)
python类fchmod()的实例源码
def write_file(path, content, owner='root', group='root', perms=0o444):
"""Create or overwrite a file with the contents of a byte string."""
uid = pwd.getpwnam(owner).pw_uid
gid = grp.getgrnam(group).gr_gid
# lets see if we can grab the file and compare the context, to avoid doing
# a write.
existing_content = None
existing_uid, existing_gid = None, None
try:
with open(path, 'rb') as target:
existing_content = target.read()
stat = os.stat(path)
existing_uid, existing_gid = stat.st_uid, stat.st_gid
except:
pass
if content != existing_content:
log("Writing file {} {}:{} {:o}".format(path, owner, group, perms),
level=DEBUG)
with open(path, 'wb') as target:
os.fchown(target.fileno(), uid, gid)
os.fchmod(target.fileno(), perms)
if six.PY3 and isinstance(content, six.string_types):
content = content.encode('UTF-8')
target.write(content)
return
# the contents were the same, but we might still need to change the
# ownership.
if existing_uid != uid:
log("Changing uid on already existing content: {} -> {}"
.format(existing_uid, uid), level=DEBUG)
os.chown(path, uid, -1)
if existing_gid != gid:
log("Changing gid on already existing content: {} -> {}"
.format(existing_gid, gid), level=DEBUG)
os.chown(path, -1, gid)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)
def create_shell_script(filename, shell_script):
shell_script = '#!/bin/bash\n' + 'echo Entered ' + filename + '\n' + shell_script
shell_script = shell_script + 'echo Exited ' + filename + '\n'
with open(filename, 'w') as file_:
file_.write(shell_script)
fd = os.open(filename, os.O_RDONLY)
os.fchmod(fd, 0755)
os.close(fd)
def create_shell_script(filename, shell_script):
shell_script = '#!/bin/bash\n' + 'echo Entered ' + filename + '\n' + shell_script
shell_script = shell_script + 'echo Exited ' + filename + '\n'
with open(filename, 'w') as file_:
file_.write(shell_script)
fd = os.open(filename, os.O_RDONLY)
os.fchmod(fd, 0755)
os.close(fd)
def create_shell_script(filename, shell_script):
shell_script = '#!/bin/bash\n' + 'echo Entered ' + filename + '\n' + shell_script
shell_script = shell_script + 'echo Exited ' + filename + '\n'
with open(filename, 'w') as file_:
file_.write(shell_script)
fd = os.open(filename, os.O_RDONLY)
os.fchmod(fd, 0755)
os.close(fd)
def write_pid_file(self):
with open(PID_PATH,'w+') as f:
os.fchmod(f.fileno(), 0666)
f.write(str(self.pid))
f.flush()
def write_pid_file(self):
with open(PID_PATH,'w+') as f:
os.fchmod(f.fileno(), 0666)
f.write(str(self.pid))
f.flush()
def write_pid_file(self):
with open(PID_PATH,'w+') as f:
os.fchmod(f.fileno(), 0666)
f.write(str(self.pid))
f.flush()
def set_pid(self, pid):
with open(self.pid_path,'w+') as f:
os.fchmod(f.fileno(), 0666)
f.write(str(pid))
f.flush()
# both contexts
def write_pid_file(self):
with open(PID_PATH,'w+') as f:
os.fchmod(f.fileno(), 0666)
f.write(str(self.pid))
f.flush()
def mkstemp(*args, **kwargs):
"""Wrap tempfile.mkstemp, setting the permissions of the created temporary
file as specified in settings (see bug 1983).
"""
fd, name = tempfile.mkstemp(*args, **kwargs)
if hasattr(os, 'fchmod'):
os.fchmod(fd, settings.POOTLE_SYNC_FILE_MODE)
else:
os.chmod(name, settings.POOTLE_SYNC_FILE_MODE)
return fd, name
def main():
module = icon_lib.IconModule(FLAGS.chroot_path)
os.mkdir(os.path.join(FLAGS.chroot_path, 'systemid'))
tool_path = os.path.join(FLAGS.chroot_path, 'icon', 'systemid')
os.makedirs(tool_path, exist_ok=True)
script = os.path.join(tool_path, 'startup.sh')
with open(script, 'w') as fh:
os.fchmod(fh.fileno(), 0o755)
fh.write("""\
#!/bin/bash
e2fsck -p LABEL=SYSTEMID
mount -o data=journal,noatime,sync LABEL=SYSTEMID /systemid
. /systemid/systemid
echo ${SYSTEMID} > /etc/hostname
hostname --file /etc/hostname
grep ${SYSTEMID} /etc/hosts >/dev/null || echo "127.0.2.1 ${SYSTEMID}" >> /etc/hosts
""")
with module.ServiceFile('systemid.service') as fh:
fh.write("""
[Unit]
Description=Mount /systemid and configure from it
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
Before=sysinit.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/icon/systemid/startup.sh
[Install]
WantedBy=sysinit.target
""")
module.EnableService('systemid.service')
def main():
base = os.path.dirname(sys.argv[0])
unsigned_manifest = os.path.join(FLAGS.image_dir, 'manifest.json.unsigned')
signed_manifest = os.path.join(FLAGS.image_dir, 'manifest.json')
with tempfile.NamedTemporaryFile(dir=FLAGS.image_dir, delete=False) as fh:
try:
Exec(
os.path.join(base, 'build_manifest.py'),
'--default-rollout', str(FLAGS.default_rollout),
'--image-dir', FLAGS.image_dir,
'--old-manifest', unsigned_manifest,
'--max-images', str(FLAGS.max_images),
stdout=fh)
os.rename(fh.name, unsigned_manifest)
except:
os.unlink(fh.name)
raise
with tempfile.NamedTemporaryFile(dir=FLAGS.image_dir, delete=False) as fh, open(unsigned_manifest, 'r') as fh_in:
try:
args = [
os.path.join(base, 'wrap_file.py'),
'--cert', FLAGS.cert,
'--key', FLAGS.key,
]
for other_cert in FLAGS.other_certs or []:
args.extend([
'--other-cert', other_cert,
])
Exec(
*args,
stdin=fh_in,
stdout=fh)
os.fchmod(fh.fileno(), 0o644)
os.rename(fh.name, signed_manifest)
except:
os.unlink(fh.name)
raise
def write_file(path, content, owner='root', group='root', perms=0o444):
"""Create or overwrite a file with the contents of a byte string."""
uid = pwd.getpwnam(owner).pw_uid
gid = grp.getgrnam(group).gr_gid
# lets see if we can grab the file and compare the context, to avoid doing
# a write.
existing_content = None
existing_uid, existing_gid = None, None
try:
with open(path, 'rb') as target:
existing_content = target.read()
stat = os.stat(path)
existing_uid, existing_gid = stat.st_uid, stat.st_gid
except:
pass
if content != existing_content:
log("Writing file {} {}:{} {:o}".format(path, owner, group, perms),
level=DEBUG)
with open(path, 'wb') as target:
os.fchown(target.fileno(), uid, gid)
os.fchmod(target.fileno(), perms)
if six.PY3 and isinstance(content, six.string_types):
content = content.encode('UTF-8')
target.write(content)
return
# the contents were the same, but we might still need to change the
# ownership.
if existing_uid != uid:
log("Changing uid on already existing content: {} -> {}"
.format(existing_uid, uid), level=DEBUG)
os.chown(path, uid, -1)
if existing_gid != gid:
log("Changing gid on already existing content: {} -> {}"
.format(existing_gid, gid), level=DEBUG)
os.chown(path, -1, gid)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)
def write_file(path, content, owner='root', group='root', perms=0o444):
"""Create or overwrite a file with the contents of a byte string."""
uid = pwd.getpwnam(owner).pw_uid
gid = grp.getgrnam(group).gr_gid
# lets see if we can grab the file and compare the context, to avoid doing
# a write.
existing_content = None
existing_uid, existing_gid = None, None
try:
with open(path, 'rb') as target:
existing_content = target.read()
stat = os.stat(path)
existing_uid, existing_gid = stat.st_uid, stat.st_gid
except:
pass
if content != existing_content:
log("Writing file {} {}:{} {:o}".format(path, owner, group, perms),
level=DEBUG)
with open(path, 'wb') as target:
os.fchown(target.fileno(), uid, gid)
os.fchmod(target.fileno(), perms)
if six.PY3 and isinstance(content, six.string_types):
content = content.encode('UTF-8')
target.write(content)
return
# the contents were the same, but we might still need to change the
# ownership.
if existing_uid != uid:
log("Changing uid on already existing content: {} -> {}"
.format(existing_uid, uid), level=DEBUG)
os.chown(path, uid, -1)
if existing_gid != gid:
log("Changing gid on already existing content: {} -> {}"
.format(existing_gid, gid), level=DEBUG)
os.chown(path, -1, gid)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)
def write_file(path, content, owner='root', group='root', perms=0o444):
"""Create or overwrite a file with the contents of a byte string."""
uid = pwd.getpwnam(owner).pw_uid
gid = grp.getgrnam(group).gr_gid
# lets see if we can grab the file and compare the context, to avoid doing
# a write.
existing_content = None
existing_uid, existing_gid = None, None
try:
with open(path, 'rb') as target:
existing_content = target.read()
stat = os.stat(path)
existing_uid, existing_gid = stat.st_uid, stat.st_gid
except:
pass
if content != existing_content:
log("Writing file {} {}:{} {:o}".format(path, owner, group, perms),
level=DEBUG)
with open(path, 'wb') as target:
os.fchown(target.fileno(), uid, gid)
os.fchmod(target.fileno(), perms)
if six.PY3 and isinstance(content, six.string_types):
content = content.encode('UTF-8')
target.write(content)
return
# the contents were the same, but we might still need to change the
# ownership.
if existing_uid != uid:
log("Changing uid on already existing content: {} -> {}"
.format(existing_uid, uid), level=DEBUG)
os.chown(path, uid, -1)
if existing_gid != gid:
log("Changing gid on already existing content: {} -> {}"
.format(existing_gid, gid), level=DEBUG)
os.chown(path, -1, gid)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)