Skip to content

Commit 674d7b9

Browse files
committed
add the options to use pgp2 to sign and verify the build caches
1 parent 5f993d0 commit 674d7b9

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

lib/spack/spack/binary_distribution.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def tarball_path_name(spec, ext):
128128
tarball_name(spec, ext))
129129

130130

131-
def build_tarball(spec, outdir, force=False, rel=False, key=None):
131+
def build_tarball(spec, outdir, force=False, rel=False, sign=False, key=None):
132132
"""
133133
Build a tarball from given spec and put it into the directory structure
134134
used at the mirror (following <tarball_directory_name>).
@@ -172,38 +172,34 @@ def build_tarball(spec, outdir, force=False, rel=False, key=None):
172172
os.path.basename(spec.prefix))
173173

174174
# Sign the packages.
175-
# spack gpg sign [--key key] tarfile_path
176-
# spack gpg sign [--key key] tarfile_path + '/spec.yaml'
177-
if key == None:
178-
keys = Gpg.signing_keys()
179-
# if len(keys) == 1:
180-
# key = keys[0]
181-
# elif not keys:
182-
# raise RuntimeError('no signing keys are available')
183-
# else:
184-
# raise RuntimeError('multiple signing keys are available; '
185-
# 'please choose one')
186-
# temporary to test adding and extracting .asc files
187-
path1 = '%s.asc' % tarfile_path
188-
with open(path1, 'a'):
189-
os.utime(path1, None)
190-
path2 = '%s.asc' % specfile_path
191-
with open(path2, 'a'):
192-
os.utime(path2, None)
193-
# temporary to test adding and extracting .asc files
194-
#Gpg.sign(key, path1, '%s.asc' % path1)
195-
#Gpg.sign(key, path2, '%s.asc' % path2)
196-
175+
if sign:
176+
if key == None:
177+
keys = Gpg.signing_keys()
178+
if len(keys) == 1:
179+
key = keys[0]
180+
elif not keys:
181+
raise RuntimeError('no signing keys are available')
182+
else:
183+
raise RuntimeError('multiple signing keys are available; '
184+
'please choose one')
185+
Gpg.sign(key, specfile_path, '%s.asc' % specfile_path)
186+
Gpg.sign(key, tarfile_path, '%s.asc' % tarfile_path)
187+
else:
188+
path1 = '%s.asc' % tarfile_path
189+
with open(path1, 'a'):
190+
os.utime(path1, None)
191+
path2 = '%s.asc' % specfile_path
192+
with open(path2, 'a'):
193+
os.utime(path2, None)
197194
with closing(tarfile.open(spackfile_path, 'w')) as tar:
198195
tar.add(name='%s' % tarfile_path, arcname='%s' % tarfile_name)
199196
tar.add(name='%s' % specfile_path, arcname='%s' % specfile_name)
200197
tar.add(name='%s.asc' % tarfile_path, arcname='%s.asc' % tarfile_name)
201198
tar.add(name='%s.asc' % specfile_path,
202199
arcname='%s.asc' % specfile_name)
203-
os.remove(tarfile_path)
204-
205-
os.remove(path1)
206-
os.remove(path2)
200+
os.remove(tarfile_path)
201+
os.remove('%s.asc' % tarfile_path)
202+
os.remove('%s.asc' % specfile_path)
207203

208204

209205
def download_tarball(spec):
@@ -229,34 +225,38 @@ def download_tarball(spec):
229225
return None
230226

231227

232-
def extract_tarball(spec, filename):
228+
def extract_tarball(spec, filename, verify=False):
233229
"""
234230
extract binary tarball for given package into install area
235231
"""
232+
if arg.verify:
233+
verify = True
236234
installpath = install_directory_name(spec)
237235
mkdirp(installpath)
238236
stagepath = os.path.dirname(filename)
237+
spackfile_name = tarball_name(spec, '.spack')
238+
spackfile_path = os.path.join(stagepath, spackfile_name)
239239
tarfile_name = tarball_name(spec, '.tar.gz')
240240
tarfile_path = os.path.join(stagepath, tarfile_name)
241241
specfile_name = tarball_name(spec, '.spec.yaml')
242242
specfile_path = os.path.join(stagepath, tarfile_name)
243-
with closing(tarfile.open(filename, 'r')) as tar:
243+
244+
with closing(tarfile.open(spackfile_path, 'r')) as tar:
244245
tar.extract(specfile_name, stagepath)
245246
tar.extract(specfile_name + '.asc', stagepath)
246-
247-
# spack gpg verify os.path.join(package.prefix, 'spec.yaml')
248-
249247
tar.extract(tarfile_name, stagepath)
250248
tar.extract(tarfile_name + '.asc', stagepath)
251249

252-
# spack gpg verify tarfile_path
250+
if verify:
251+
Gpg.verify(specfile_path, '%s.asc' % specfile_path)
252+
Gpg.verify(tarfile_path, '%s.asc' % tarfile_path)
253253

254254
with closing(tarfile.open(tarfile_path, 'r')) as tar:
255255
tar.extractall(path=join_path(installpath, '..'))
256256

257-
#os.remove(tarfile_path)
257+
os.remove(tarfile_path)
258258
os.remove(tarfile_path + '.asc')
259-
#os.remove(specfile_path)
259+
os.remove(specfile_path)
260260
os.remove(specfile_path + '.asc')
261261

262262

lib/spack/spack/cmd/buildcache.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def setup_parser(subparser):
4848
" before creating tarballs.")
4949
create.add_argument('-f', '--force', action='store_true',
5050
help="overwrite tarball if it exists.")
51+
create.add_argument('-s', '--sign', action='store_true',
52+
help="sign the tarballs with gpg2")
53+
create.add_argument('-k', '--key', metavar='key',
54+
type=str, default=None,
55+
help="gpg2 key for signing.")
5156
create.add_argument('-d', '--directory', metavar='directory',
5257
type=str, default='.',
5358
help="directory in which to save the tarballs.")
@@ -59,6 +64,8 @@ def setup_parser(subparser):
5964
install = subparsers.add_parser('install')
6065
install.add_argument('-f', '--force', action='store_true',
6166
help="overwrite install directory if it exists.")
67+
install.add_argument('-v', '--verify', action='store_true',
68+
help="verify buildcache gpg2 signature")
6269
install.add_argument(
6370
'packages', nargs=argparse.REMAINDER,
6471
help="specs of packages to install biuldache for")
@@ -94,7 +101,7 @@ def createtarball(args):
94101
spack.binary_distribution.prepare()
95102
for spec in specs:
96103
tty.msg('creating binary cache file for package %s ' % spec.format())
97-
build_tarball(spec, outdir, args.force, args.rel)
104+
build_tarball(spec, outdir, args.force, args.rel, args.sign, args.key)
98105

99106

100107
def installtarball(args):
@@ -128,7 +135,7 @@ def install_tarball(spec, args):
128135
if tarball:
129136
tty.msg('Installing buildcache for spec %s' % spec.format())
130137
spack.binary_distribution.prepare()
131-
spack.binary_distribution.extract_tarball(spec, tarball)
138+
spack.binary_distribution.extract_tarball(spec, tarball, args)
132139
spack.binary_distribution.relocate_package(spec)
133140
spack.store.db.reindex(spack.store.layout)
134141
else:

0 commit comments

Comments
 (0)