1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#!/usr/bin/python
# Copyright (C) 2013-2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
''' A Morph configuration extension for adding arbitrary files to a system
It will read the manifest files specified in the environment variable
INSTALL_FILES, then use the contens of those files to determine which files
to install into the target system.
'''
import cliapp
import os
import errno
import re
import sys
import shlex
import shutil
import stat
try:
import jinja2
jinja_available = True
except ImportError:
jinja_available = False
class InstallFilesConfigureExtension(cliapp.Application):
def process_args(self, args):
if not 'INSTALL_FILES' in os.environ:
return
target_root = args[0]
manifests = shlex.split(os.environ['INSTALL_FILES'])
for manifest in manifests:
self.install_manifest(manifest, target_root)
def install_manifest(self, manifest, target_root):
manifest_dir = os.path.dirname(manifest)
with open(manifest) as f:
entries = f.readlines()
for entry in entries:
self.install_entry(entry, manifest_dir, target_root)
def force_symlink(self, source, link_name):
try:
os.symlink(source, link_name)
except OSError as e:
if e.errno == errno.EEXIST:
os.remove(link_name)
os.symlink(source, link_name)
def install_entry(self, entry, manifest_root, target_root):
m = re.match('(template )?(overwrite )?'
'([0-7]+) ([0-9]+) ([0-9]+) (\S+)', entry)
if m:
template = m.group(1)
overwrite = m.group(2)
mode = int(m.group(3), 8) # mode is octal
uid = int(m.group(4))
gid = int(m.group(5))
path = m.group(6)
else:
raise cliapp.AppException('Invalid manifest entry, '
'format: [template] [overwrite] '
'<octal mode> <uid decimal> <gid decimal> <filename>')
dest_path = os.path.join(target_root, './' + path)
if stat.S_ISDIR(mode):
if os.path.exists(dest_path) and not overwrite:
dest_stat = os.stat(dest_path)
if (mode != dest_stat.st_mode
or uid != dest_stat.st_uid
or gid != dest_stat.st_gid):
raise cliapp.AppException('"%s" exists and is not '
'identical to directory '
'"%s"' % (dest_path, entry))
else:
os.mkdir(dest_path, mode)
os.chown(dest_path, uid, gid)
os.chmod(dest_path, mode)
elif stat.S_ISLNK(mode):
if os.path.lexists(dest_path) and not overwrite:
raise cliapp.AppException('Symlink already exists at %s'
% dest_path)
else:
linkdest = os.readlink(os.path.join(manifest_root,
'./' + path))
self.force_symlink(linkdest, dest_path)
os.lchown(dest_path, uid, gid)
elif stat.S_ISREG(mode):
if os.path.lexists(dest_path) and not overwrite:
raise cliapp.AppException('File already exists at %s'
% dest_path)
else:
if template:
if not jinja_available:
raise cliapp.AppException(
"Failed to install template file `%s': "
'install-files templates require jinja2'
% path)
loader = jinja2.FileSystemLoader(manifest_root)
env = jinja2.Environment(loader=loader,
keep_trailing_newline=True)
env.get_template(path).stream(os.environ).dump(dest_path)
else:
shutil.copyfile(os.path.join(manifest_root, './' + path),
dest_path)
os.chown(dest_path, uid, gid)
os.chmod(dest_path, mode)
else:
raise cliapp.AppException('Mode given in "%s" is not a file,'
' symlink or directory' % entry)
InstallFilesConfigureExtension().run()
|