~charmers/charms/trusty/galera-cluster/trunk

« back to all changes in this revision

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

[hopem, r=gnuoy] Moved mysql common code into
charmhelpers.contrib.database.mysql and syned in.

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):
178
 
    """Create or overwrite a file with the contents of a string"""
 
194
    """Create or overwrite a file with the contents of a byte string."""
179
195
    log("Writing file {} {}:{} {:o}".format(path, owner, group, perms))
180
196
    uid = pwd.getpwnam(owner).pw_uid
181
197
    gid = grp.getgrnam(group).gr_gid
182
 
    with open(path, 'w') as target:
 
198
    with open(path, 'wb') as target:
183
199
        os.fchown(target.fileno(), uid, gid)
184
200
        os.fchmod(target.fileno(), perms)
185
201
        target.write(content)
345
361
        ip_output = (line for line in ip_output if line)
346
362
        for line in ip_output:
347
363
            if line.split()[1].startswith(int_type):
348
 
                matched = re.search('.*: (bond[0-9]+\.[0-9]+)@.*', line)
 
364
                matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
349
365
                if matched:
350
366
                    interface = matched.groups()[0]
351
367
                else:
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)