~1chb1n/charms/trusty/ceph-osd/15.10-stable-flip-tests-helper-syncs

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Liam Young
  • Date: 2015-01-26 11:51:28 UTC
  • Revision ID: liam.young@canonical.com-20150126115128-f6jz5gwbbfz5861p
[gnuoy,trivial] Pre-release charmhelper sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
1
17
"""Tools for working with the host system"""
2
18
# Copyright 2012 Canonical Ltd.
3
19
#
168
184
            log("Removing non-directory file {} prior to mkdir()".format(path))
169
185
            os.unlink(realpath)
170
186
            os.makedirs(realpath, perms)
171
 
        os.chown(realpath, uid, gid)
172
187
    elif not path_exists:
173
188
        os.makedirs(realpath, perms)
174
 
        os.chown(realpath, uid, gid)
 
189
    os.chown(realpath, uid, gid)
 
190
    os.chmod(realpath, perms)
175
191
 
176
192
 
177
193
def write_file(path, content, owner='root', group='root', perms=0o444):
389
405
    *  0 => Installed revno is the same as supplied arg
390
406
    * -1 => Installed revno is less than supplied arg
391
407
 
 
408
    This function imports apt_cache function from charmhelpers.fetch if
 
409
    the pkgcache argument is None. Be sure to add charmhelpers.fetch if
 
410
    you call this function, or pass an apt_pkg.Cache() instance.
392
411
    '''
393
412
    import apt_pkg
394
413
    if not pkgcache:
407
426
        os.chdir(cur)
408
427
 
409
428
 
410
 
def chownr(path, owner, group):
 
429
def chownr(path, owner, group, follow_links=True):
411
430
    uid = pwd.getpwnam(owner).pw_uid
412
431
    gid = grp.getgrnam(group).gr_gid
 
432
    if follow_links:
 
433
        chown = os.chown
 
434
    else:
 
435
        chown = os.lchown
413
436
 
414
437
    for root, dirs, files in os.walk(path):
415
438
        for name in dirs + files:
416
439
            full = os.path.join(root, name)
417
440
            broken_symlink = os.path.lexists(full) and not os.path.exists(full)
418
441
            if not broken_symlink:
419
 
                os.chown(full, uid, gid)
 
442
                chown(full, uid, gid)
 
443
 
 
444
 
 
445
def lchownr(path, owner, group):
 
446
    chownr(path, owner, group, follow_links=False)