2022-08-09 14:23:41 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-01-17 23:25:02 +01:00
|
|
|
#
|
2020-11-24 15:05:44 +01:00
|
|
|
# download and unpack a element-web tarball.
|
2017-01-17 23:25:02 +01:00
|
|
|
#
|
|
|
|
# Allows `bundles` to be extracted to a common directory, and a link to
|
|
|
|
# config.json to be added.
|
|
|
|
|
|
|
|
import argparse
|
2022-08-09 14:23:41 +02:00
|
|
|
import errno
|
2017-01-17 23:25:02 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2017-01-18 22:51:49 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-01-17 23:25:02 +01:00
|
|
|
import tarfile
|
2017-09-05 14:54:51 +02:00
|
|
|
import shutil
|
2017-09-20 15:38:41 +02:00
|
|
|
import glob
|
2022-08-09 14:23:41 +02:00
|
|
|
from urllib.request import urlretrieve
|
2017-01-18 22:51:49 +01:00
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
|
|
|
|
class DeployException(Exception):
|
|
|
|
pass
|
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
def create_relative_symlink(linkname, target):
|
|
|
|
relpath = os.path.relpath(target, os.path.dirname(linkname))
|
2022-08-09 14:23:41 +02:00
|
|
|
print("Symlink %s -> %s" % (linkname, relpath))
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.symlink(relpath, linkname)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.EEXIST:
|
|
|
|
# atomic modification
|
|
|
|
os.symlink(relpath, linkname + ".tmp")
|
|
|
|
os.rename(linkname + ".tmp", linkname)
|
|
|
|
else:
|
|
|
|
raise e
|
2017-01-17 23:25:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def move_bundles(source, dest):
|
|
|
|
"""Move the contents of the 'bundles' directory to a common dir
|
|
|
|
|
|
|
|
We check that we will not be overwriting anything before we proceed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
source (str): path to 'bundles' within the extracted tarball
|
|
|
|
dest (str): target common directory
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not os.path.isdir(dest):
|
|
|
|
os.mkdir(dest)
|
|
|
|
|
|
|
|
# build a map from source to destination, checking for non-existence as we go.
|
|
|
|
renames = {}
|
|
|
|
for f in os.listdir(source):
|
|
|
|
dst = os.path.join(dest, f)
|
|
|
|
if os.path.exists(dst):
|
2022-08-09 14:23:41 +02:00
|
|
|
print(
|
2017-09-05 14:54:51 +02:00
|
|
|
"Skipping bundle. The bundle includes '%s' which we have previously deployed."
|
2017-01-17 23:25:02 +01:00
|
|
|
% f
|
|
|
|
)
|
2017-09-05 14:54:51 +02:00
|
|
|
else:
|
|
|
|
renames[os.path.join(source, f)] = dst
|
2017-01-17 23:25:02 +01:00
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
for (src, dst) in renames.items():
|
|
|
|
print("Move %s -> %s" % (src, dst))
|
2017-01-17 23:25:02 +01:00
|
|
|
os.rename(src, dst)
|
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
class Deployer:
|
|
|
|
def __init__(self):
|
|
|
|
self.packages_path = "."
|
|
|
|
self.bundles_path = None
|
|
|
|
self.should_clean = False
|
2022-08-09 14:23:41 +02:00
|
|
|
self.symlink_latest = None
|
2017-07-20 12:02:10 +02:00
|
|
|
# filename -> symlink path e.g 'config.localhost.json' => '../localhost/config.json'
|
2017-09-20 18:04:31 +02:00
|
|
|
self.symlink_paths = {}
|
2017-01-18 22:51:49 +01:00
|
|
|
self.verify_signature = True
|
2017-01-17 23:25:02 +01:00
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
def fetch(self, tarball, extract_path):
|
|
|
|
"""Download a tarball, verifies it if needed, and unpacks it
|
2017-01-17 23:25:02 +01:00
|
|
|
|
|
|
|
Returns:
|
2022-08-09 14:23:41 +02:00
|
|
|
(str) the path to the unpacked directory
|
2017-01-17 23:25:02 +01:00
|
|
|
"""
|
2022-08-09 14:23:41 +02:00
|
|
|
print("Fetching %s to %s" % (tarball, extract_path))
|
2017-01-17 23:25:02 +01:00
|
|
|
|
2017-01-18 22:51:49 +01:00
|
|
|
name_str = os.path.basename(tarball).replace(".tar.gz", "")
|
|
|
|
extracted_dir = os.path.join(extract_path, name_str)
|
|
|
|
if os.path.exists(extracted_dir):
|
|
|
|
raise DeployException('Cannot unpack %s: %s already exists' % (
|
|
|
|
tarball, extracted_dir))
|
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
downloaded = False
|
|
|
|
if tarball.startswith("http://") or tarball.startswith("https://"):
|
2017-01-18 22:51:49 +01:00
|
|
|
tarball = self.download_and_verify(tarball)
|
2017-01-17 23:25:02 +01:00
|
|
|
print("Downloaded file: %s" % tarball)
|
|
|
|
downloaded = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
with tarfile.open(tarball) as tar:
|
|
|
|
tar.extractall(extract_path)
|
|
|
|
finally:
|
|
|
|
if self.should_clean and downloaded:
|
|
|
|
os.remove(tarball)
|
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
print("Extracted into: %s" % extracted_dir)
|
|
|
|
return extracted_dir
|
|
|
|
|
|
|
|
def deploy(self, extracted_dir):
|
|
|
|
"""Applies symlinks and handles the bundles directory on an extracted tarball"""
|
|
|
|
print("Deploying %s" % extracted_dir)
|
2017-01-17 23:25:02 +01:00
|
|
|
|
2017-09-20 18:04:31 +02:00
|
|
|
if self.symlink_paths:
|
2022-08-09 14:23:41 +02:00
|
|
|
for link_path, file_path in self.symlink_paths.items():
|
2017-07-20 12:02:10 +02:00
|
|
|
create_relative_symlink(
|
2017-09-20 18:04:31 +02:00
|
|
|
target=file_path,
|
|
|
|
linkname=os.path.join(extracted_dir, link_path)
|
2017-07-20 12:02:10 +02:00
|
|
|
)
|
2017-01-17 23:25:02 +01:00
|
|
|
|
|
|
|
if self.bundles_path:
|
|
|
|
extracted_bundles = os.path.join(extracted_dir, 'bundles')
|
|
|
|
move_bundles(source=extracted_bundles, dest=self.bundles_path)
|
|
|
|
|
2017-09-05 14:54:51 +02:00
|
|
|
# replace the extracted_bundles dir (which may not be empty if some
|
|
|
|
# bundles were skipped) with a symlink to the common dir.
|
|
|
|
shutil.rmtree(extracted_bundles)
|
2017-01-17 23:25:02 +01:00
|
|
|
create_relative_symlink(
|
|
|
|
target=self.bundles_path,
|
|
|
|
linkname=extracted_bundles,
|
|
|
|
)
|
2022-08-09 14:23:41 +02:00
|
|
|
|
|
|
|
if self.symlink_latest:
|
|
|
|
create_relative_symlink(
|
|
|
|
target=extracted_dir,
|
|
|
|
linkname=self.symlink_latest,
|
|
|
|
)
|
2017-01-17 23:25:02 +01:00
|
|
|
|
2017-01-18 22:51:49 +01:00
|
|
|
def download_and_verify(self, url):
|
|
|
|
tarball = self.download_file(url)
|
|
|
|
|
|
|
|
if self.verify_signature:
|
|
|
|
sigfile = self.download_file(url + ".asc")
|
|
|
|
subprocess.check_call(["gpg", "--verify", sigfile, tarball])
|
|
|
|
|
|
|
|
return tarball
|
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
def download_file(self, url):
|
|
|
|
if not os.path.isdir(self.packages_path):
|
|
|
|
os.mkdir(self.packages_path)
|
|
|
|
local_filename = os.path.join(self.packages_path,
|
|
|
|
url.split('/')[-1])
|
2017-01-18 22:51:49 +01:00
|
|
|
sys.stdout.write("Downloading %s -> %s..." % (url, local_filename))
|
|
|
|
sys.stdout.flush()
|
|
|
|
urlretrieve(url, local_filename)
|
|
|
|
print ("Done")
|
2017-01-17 23:25:02 +01:00
|
|
|
return local_filename
|
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
|
2017-01-17 23:25:02 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser("Deploy a Riot build on a web server.")
|
|
|
|
parser.add_argument(
|
|
|
|
"-p", "--packages-dir", default="./packages", help=(
|
|
|
|
"The directory to download the tarball into. (Default: '%(default)s')"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-e", "--extract-path", default="./deploys", help=(
|
|
|
|
"The location to extract .tar.gz files to. (Default: '%(default)s')"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-b", "--bundles-dir", nargs='?', default="./bundles", help=(
|
|
|
|
"A directory to move the contents of the 'bundles' directory to. A \
|
|
|
|
symlink to the bundles directory will also be written inside the \
|
|
|
|
extracted tarball. Example: './bundles'. \
|
|
|
|
(Default: '%(default)s')"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c", "--clean", action="store_true", default=False, help=(
|
|
|
|
"Remove .tar.gz files after they have been downloaded and extracted. \
|
|
|
|
(Default: %(default)s)"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-04-17 19:31:36 +02:00
|
|
|
"--include", nargs='*', default=['./config*.json'], help=(
|
2017-09-20 18:04:31 +02:00
|
|
|
"Symlink these files into the root of the deployed tarball. \
|
|
|
|
Useful for config files and home pages. Supports glob syntax. \
|
|
|
|
(Default: '%(default)s')"
|
2017-01-17 23:25:02 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2022-08-09 14:23:41 +02:00
|
|
|
"-s", "--symlink", dest="symlink", default="./latest", help=(
|
|
|
|
"Write a symlink to this location pointing to the extracted tarball. \
|
|
|
|
New builds will keep overwriting this symlink. The symlink will point \
|
|
|
|
to the webapp directory INSIDE the tarball."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"target", help=(
|
|
|
|
"filename of extracted directory, tarball, or URL to download."
|
2017-01-17 23:25:02 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
deployer = Deployer()
|
|
|
|
deployer.packages_path = args.packages_dir
|
|
|
|
deployer.bundles_path = args.bundles_dir
|
|
|
|
deployer.should_clean = args.clean
|
2022-08-09 14:23:41 +02:00
|
|
|
deployer.symlink_latest = args.symlink
|
2017-09-20 15:25:33 +02:00
|
|
|
|
2017-09-20 18:22:47 +02:00
|
|
|
for include in args.include:
|
|
|
|
deployer.symlink_paths.update({ os.path.basename(pth): pth for pth in glob.iglob(include) })
|
2017-09-20 18:04:31 +02:00
|
|
|
|
2022-08-09 14:23:41 +02:00
|
|
|
if os.path.isdir(args.target):
|
|
|
|
# If the given directory contains a single directory then use that instead, the ci package wraps in an extra dir
|
|
|
|
files = os.listdir(args.target)
|
|
|
|
if len(files) == 1 and os.path.isdir(os.path.join(args.target, files[0])):
|
|
|
|
extracted_dir = os.path.join(args.target, files[0])
|
|
|
|
else:
|
|
|
|
extracted_dir = args.target
|
|
|
|
else:
|
|
|
|
extracted_dir = deployer.fetch(args.target, args.extract_path)
|
|
|
|
deployer.deploy(extracted_dir)
|