2016-03-02 15:17:15 +00:00
|
|
|
#!/usr/bin/python -u
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
2016-04-15 16:01:33 +00:00
|
|
|
from datetime import datetime
|
2016-01-08 15:47:09 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import hashlib
|
|
|
|
import re
|
2016-04-15 16:01:33 +00:00
|
|
|
import logging
|
|
|
|
import argparse
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
################
|
|
|
|
#### Telegraf Variables
|
|
|
|
################
|
|
|
|
|
|
|
|
# Packaging variables
|
|
|
|
PACKAGE_NAME = "telegraf"
|
2019-04-15 23:07:47 +00:00
|
|
|
USER = "telegraf"
|
|
|
|
GROUP = "telegraf"
|
2016-01-08 15:47:09 +00:00
|
|
|
INSTALL_ROOT_DIR = "/usr/bin"
|
|
|
|
LOG_DIR = "/var/log/telegraf"
|
|
|
|
SCRIPT_DIR = "/usr/lib/telegraf/scripts"
|
|
|
|
CONFIG_DIR = "/etc/telegraf"
|
2017-03-08 15:26:33 +00:00
|
|
|
CONFIG_DIR_D = "/etc/telegraf/telegraf.d"
|
2016-01-08 15:47:09 +00:00
|
|
|
LOGROTATE_DIR = "/etc/logrotate.d"
|
|
|
|
|
|
|
|
INIT_SCRIPT = "scripts/init.sh"
|
|
|
|
SYSTEMD_SCRIPT = "scripts/telegraf.service"
|
|
|
|
LOGROTATE_SCRIPT = "etc/logrotate.d/telegraf"
|
|
|
|
DEFAULT_CONFIG = "etc/telegraf.conf"
|
2016-02-22 22:16:46 +00:00
|
|
|
DEFAULT_WINDOWS_CONFIG = "etc/telegraf_windows.conf"
|
2016-01-08 15:47:09 +00:00
|
|
|
POSTINST_SCRIPT = "scripts/post-install.sh"
|
|
|
|
PREINST_SCRIPT = "scripts/pre-install.sh"
|
2016-02-23 17:25:07 +00:00
|
|
|
POSTREMOVE_SCRIPT = "scripts/post-remove.sh"
|
|
|
|
PREREMOVE_SCRIPT = "scripts/pre-remove.sh"
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
# Default AWS S3 bucket for uploads
|
2016-04-15 16:01:33 +00:00
|
|
|
DEFAULT_BUCKET = "dl.influxdata.com/telegraf/artifacts"
|
2016-03-02 15:17:15 +00:00
|
|
|
|
|
|
|
CONFIGURATION_FILES = [
|
|
|
|
CONFIG_DIR + '/telegraf.conf',
|
|
|
|
LOGROTATE_DIR + '/telegraf',
|
|
|
|
]
|
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
# META-PACKAGE VARIABLES
|
|
|
|
PACKAGE_LICENSE = "MIT"
|
|
|
|
PACKAGE_URL = "https://github.com/influxdata/telegraf"
|
|
|
|
MAINTAINER = "support@influxdb.com"
|
|
|
|
VENDOR = "InfluxData"
|
|
|
|
DESCRIPTION = "Plugin-driven server agent for reporting metrics into InfluxDB."
|
|
|
|
|
|
|
|
# SCRIPT START
|
2020-04-09 18:27:59 +00:00
|
|
|
prereqs = ['git', 'go']
|
2016-03-02 15:17:15 +00:00
|
|
|
go_vet_command = "go tool vet -composites=true ./"
|
2020-04-09 18:27:59 +00:00
|
|
|
optional_prereqs = ['gvm', 'fpm', 'rpmbuild']
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
fpm_common_args = "-f -s dir --log error \
|
|
|
|
--vendor {} \
|
|
|
|
--url {} \
|
|
|
|
--license {} \
|
|
|
|
--maintainer {} \
|
|
|
|
--config-files {} \
|
|
|
|
--config-files {} \
|
|
|
|
--after-install {} \
|
|
|
|
--before-install {} \
|
2016-02-23 17:25:07 +00:00
|
|
|
--after-remove {} \
|
|
|
|
--before-remove {} \
|
2019-04-15 23:07:47 +00:00
|
|
|
--rpm-attr 755,{},{}:{} \
|
2016-01-08 15:47:09 +00:00
|
|
|
--description \"{}\"".format(
|
|
|
|
VENDOR,
|
|
|
|
PACKAGE_URL,
|
|
|
|
PACKAGE_LICENSE,
|
|
|
|
MAINTAINER,
|
2020-04-09 18:27:59 +00:00
|
|
|
CONFIG_DIR + '/telegraf.conf.sample',
|
2016-01-08 15:47:09 +00:00
|
|
|
LOGROTATE_DIR + '/telegraf',
|
|
|
|
POSTINST_SCRIPT,
|
|
|
|
PREINST_SCRIPT,
|
2016-02-23 17:25:07 +00:00
|
|
|
POSTREMOVE_SCRIPT,
|
|
|
|
PREREMOVE_SCRIPT,
|
2019-04-15 23:07:47 +00:00
|
|
|
USER, GROUP, LOG_DIR,
|
2016-01-08 15:47:09 +00:00
|
|
|
DESCRIPTION)
|
|
|
|
|
|
|
|
targets = {
|
2020-04-09 18:27:59 +00:00
|
|
|
'telegraf': './cmd/telegraf',
|
2016-01-08 15:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
supported_builds = {
|
2020-04-09 18:27:59 +00:00
|
|
|
'darwin': ["amd64"],
|
|
|
|
"windows": ["amd64", "i386"],
|
|
|
|
"linux": ["amd64", "i386", "armhf", "armel", "arm64", "static_amd64", "s390x", "mipsel", "mips"],
|
|
|
|
"freebsd": ["amd64", "i386"]
|
2016-01-08 15:47:09 +00:00
|
|
|
}
|
2016-03-02 15:17:15 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
supported_packages = {
|
2020-04-09 18:27:59 +00:00
|
|
|
"darwin": ["tar"],
|
|
|
|
"linux": ["deb", "rpm", "tar"],
|
|
|
|
"windows": ["zip"],
|
|
|
|
"freebsd": ["tar"]
|
2016-01-08 15:47:09 +00:00
|
|
|
}
|
2016-03-02 15:17:15 +00:00
|
|
|
|
2020-03-18 23:21:48 +00:00
|
|
|
next_version = '1.15.0'
|
2017-09-22 23:49:28 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
################
|
|
|
|
#### Telegraf Functions
|
|
|
|
################
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def print_banner():
|
|
|
|
logging.info("""
|
|
|
|
_____ _ __
|
|
|
|
/__ \\___| | ___ __ _ _ __ __ _ / _|
|
|
|
|
/ /\\/ _ \\ |/ _ \\/ _` | '__/ _` | |_
|
|
|
|
/ / | __/ | __/ (_| | | | (_| | _|
|
|
|
|
\\/ \\___|_|\\___|\\__, |_| \\__,_|_|
|
|
|
|
|___/
|
|
|
|
Build Script
|
|
|
|
""")
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
def create_package_fs(build_root):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Create a filesystem structure to mimic the package filesystem.
|
|
|
|
"""
|
|
|
|
logging.debug("Creating a filesystem hierarchy from directory: {}".format(build_root))
|
2016-03-02 15:17:15 +00:00
|
|
|
# Using [1:] for the path names due to them being absolute
|
|
|
|
# (will overwrite previous paths, per 'os.path.join' documentation)
|
2020-04-09 18:27:59 +00:00
|
|
|
dirs = [INSTALL_ROOT_DIR[1:], LOG_DIR[1:], SCRIPT_DIR[1:], CONFIG_DIR[1:], LOGROTATE_DIR[1:], CONFIG_DIR_D[1:]]
|
2016-03-02 15:17:15 +00:00
|
|
|
for d in dirs:
|
2016-04-15 16:01:33 +00:00
|
|
|
os.makedirs(os.path.join(build_root, d))
|
2016-03-02 15:17:15 +00:00
|
|
|
os.chmod(os.path.join(build_root, d), 0o755)
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-05-06 15:46:29 +00:00
|
|
|
def package_scripts(build_root, config_only=False, windows=False):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Copy the necessary scripts and configuration files to the package
|
|
|
|
filesystem.
|
|
|
|
"""
|
2016-05-06 15:46:29 +00:00
|
|
|
if config_only or windows:
|
|
|
|
logging.info("Copying configuration to build directory")
|
|
|
|
if windows:
|
2020-04-09 18:27:59 +00:00
|
|
|
shutil.copyfile(DEFAULT_WINDOWS_CONFIG, os.path.join(build_root, "telegraf.conf.sample"))
|
2016-05-06 15:46:29 +00:00
|
|
|
else:
|
2020-04-09 18:27:59 +00:00
|
|
|
shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, "telegraf.conf.sample"))
|
|
|
|
os.chmod(os.path.join(build_root, "telegraf.conf.sample"), 0o644)
|
2016-03-02 15:17:15 +00:00
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.info("Copying scripts and configuration to build directory")
|
2016-03-02 15:17:15 +00:00
|
|
|
shutil.copyfile(INIT_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1]))
|
|
|
|
os.chmod(os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1]), 0o644)
|
|
|
|
shutil.copyfile(SYSTEMD_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], SYSTEMD_SCRIPT.split('/')[1]))
|
|
|
|
os.chmod(os.path.join(build_root, SCRIPT_DIR[1:], SYSTEMD_SCRIPT.split('/')[1]), 0o644)
|
|
|
|
shutil.copyfile(LOGROTATE_SCRIPT, os.path.join(build_root, LOGROTATE_DIR[1:], "telegraf"))
|
|
|
|
os.chmod(os.path.join(build_root, LOGROTATE_DIR[1:], "telegraf"), 0o644)
|
2020-04-09 18:27:59 +00:00
|
|
|
shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, CONFIG_DIR[1:], "telegraf.conf.sample"))
|
|
|
|
os.chmod(os.path.join(build_root, CONFIG_DIR[1:], "telegraf.conf.sample"), 0o644)
|
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
|
|
|
|
def run_generate():
|
|
|
|
# NOOP for Telegraf
|
|
|
|
return True
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def go_get(branch, update=False, no_uncommitted=False):
|
|
|
|
"""Retrieve build dependencies or restore pinned dependencies.
|
|
|
|
"""
|
|
|
|
if local_changes() and no_uncommitted:
|
|
|
|
logging.error("There are uncommitted changes in the current directory.")
|
|
|
|
return False
|
2020-01-16 22:38:06 +00:00
|
|
|
logging.info("Retrieving dependencies...")
|
|
|
|
run("go mod download")
|
2016-03-02 15:17:15 +00:00
|
|
|
return True
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-03-02 18:06:16 +00:00
|
|
|
def run_tests(race, parallel, timeout, no_vet):
|
|
|
|
# Currently a NOOP for Telegraf
|
|
|
|
return True
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
################
|
|
|
|
#### All Telegraf-specific content above this line
|
|
|
|
################
|
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def run(command, allow_failure=False, shell=False):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Run shell command (convenience wrapper around subprocess).
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
out = None
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("{}".format(command))
|
2016-01-08 15:47:09 +00:00
|
|
|
try:
|
|
|
|
if shell:
|
|
|
|
out = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=shell)
|
|
|
|
else:
|
|
|
|
out = subprocess.check_output(command.split(), stderr=subprocess.STDOUT)
|
2016-04-15 16:01:33 +00:00
|
|
|
out = out.decode('utf-8').strip()
|
|
|
|
# logging.debug("Command output: {}".format(out))
|
2016-01-08 15:47:09 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if allow_failure:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Command '{}' failed with error: {}".format(command, e.output))
|
2016-01-08 15:47:09 +00:00
|
|
|
return None
|
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.error("Command '{}' failed with error: {}".format(command, e.output))
|
2016-01-08 15:47:09 +00:00
|
|
|
sys.exit(1)
|
|
|
|
except OSError as e:
|
|
|
|
if allow_failure:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Command '{}' failed with error: {}".format(command, e))
|
2016-01-08 15:47:09 +00:00
|
|
|
return out
|
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.error("Command '{}' failed with error: {}".format(command, e))
|
2016-01-08 15:47:09 +00:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
return out
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
|
|
|
def create_temp_dir(prefix=None):
|
2016-04-15 16:01:33 +00:00
|
|
|
""" Create temporary directory with optional prefix.
|
|
|
|
"""
|
2016-01-23 00:09:38 +00:00
|
|
|
if prefix is None:
|
2016-03-02 15:17:15 +00:00
|
|
|
return tempfile.mkdtemp(prefix="{}-build.".format(PACKAGE_NAME))
|
2016-01-23 00:09:38 +00:00
|
|
|
else:
|
|
|
|
return tempfile.mkdtemp(prefix=prefix)
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def increment_minor_version(version):
|
|
|
|
"""Return the version with the minor version incremented and patch
|
|
|
|
version set to zero.
|
|
|
|
"""
|
|
|
|
ver_list = version.split('.')
|
|
|
|
if len(ver_list) != 3:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Could not determine how to increment version '{}', will just use provided version.".format(version))
|
2016-04-15 16:01:33 +00:00
|
|
|
return version
|
|
|
|
ver_list[1] = str(int(ver_list[1]) + 1)
|
|
|
|
ver_list[2] = str(0)
|
|
|
|
inc_version = '.'.join(ver_list)
|
|
|
|
logging.debug("Incremented version from '{}' to '{}'.".format(version, inc_version))
|
|
|
|
return inc_version
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
def get_current_version_tag():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve the raw git version tag.
|
|
|
|
"""
|
2017-09-22 23:49:28 +00:00
|
|
|
version = run("git describe --exact-match --tags 2>/dev/null",
|
2020-04-09 18:27:59 +00:00
|
|
|
allow_failure=True, shell=True)
|
2016-03-02 15:17:15 +00:00
|
|
|
return version
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_current_version():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Parse version information from git tag output.
|
|
|
|
"""
|
2016-03-02 15:17:15 +00:00
|
|
|
version_tag = get_current_version_tag()
|
2017-09-22 23:49:28 +00:00
|
|
|
if not version_tag:
|
|
|
|
return None
|
2016-05-05 19:01:24 +00:00
|
|
|
# Remove leading 'v'
|
2016-03-02 15:17:15 +00:00
|
|
|
if version_tag[0] == 'v':
|
2016-04-15 16:01:33 +00:00
|
|
|
version_tag = version_tag[1:]
|
2016-05-05 19:01:24 +00:00
|
|
|
# Replace any '-'/'_' with '~'
|
|
|
|
if '-' in version_tag:
|
2020-04-09 18:27:59 +00:00
|
|
|
version_tag = version_tag.replace("-", "~")
|
2016-05-05 19:01:24 +00:00
|
|
|
if '_' in version_tag:
|
2020-04-09 18:27:59 +00:00
|
|
|
version_tag = version_tag.replace("_", "~")
|
2016-05-05 19:01:24 +00:00
|
|
|
return version_tag
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_current_commit(short=False):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve the current git commit.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
if short:
|
|
|
|
command = "git log --pretty=format:'%h' -n 1"
|
|
|
|
else:
|
|
|
|
command = "git rev-parse HEAD"
|
|
|
|
out = run(command)
|
|
|
|
return out.strip('\'\n\r ')
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_current_branch():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve the current git branch.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
command = "git rev-parse --abbrev-ref HEAD"
|
|
|
|
out = run(command)
|
|
|
|
return out.strip()
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def local_changes():
|
|
|
|
"""Return True if there are local un-committed changes.
|
|
|
|
"""
|
|
|
|
output = run("git diff-files --ignore-submodules --").strip()
|
|
|
|
if len(output) > 0:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_system_arch():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve current system architecture.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
arch = os.uname()[4]
|
|
|
|
if arch == "x86_64":
|
|
|
|
arch = "amd64"
|
2016-04-15 16:01:33 +00:00
|
|
|
elif arch == "386":
|
|
|
|
arch = "i386"
|
2017-09-18 21:22:54 +00:00
|
|
|
elif "arm64" in arch:
|
|
|
|
arch = "arm64"
|
2016-04-15 16:01:33 +00:00
|
|
|
elif 'arm' in arch:
|
|
|
|
# Prevent uname from reporting full ARM arch (eg 'armv7l')
|
|
|
|
arch = "arm"
|
2016-01-08 15:47:09 +00:00
|
|
|
return arch
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_system_platform():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve current system platform.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
if sys.platform.startswith("linux"):
|
|
|
|
return "linux"
|
|
|
|
else:
|
|
|
|
return sys.platform
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def get_go_version():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Retrieve version information for Go.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
out = run("go version")
|
|
|
|
matches = re.search('go version go(\S+)', out)
|
|
|
|
if matches is not None:
|
|
|
|
return matches.groups()[0].strip()
|
|
|
|
return None
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def check_path_for(b):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Check the the user's path for the provided binary.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
def is_exe(fpath):
|
|
|
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
|
|
|
|
|
|
|
for path in os.environ["PATH"].split(os.pathsep):
|
|
|
|
path = path.strip('"')
|
|
|
|
full_path = os.path.join(path, b)
|
2020-04-09 18:27:59 +00:00
|
|
|
if is_exe(full_path):
|
2016-01-08 15:47:09 +00:00
|
|
|
return full_path
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
|
|
|
def check_environ(build_dir=None):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Check environment for common Go variables.
|
|
|
|
"""
|
|
|
|
logging.info("Checking environment...")
|
2020-04-09 18:27:59 +00:00
|
|
|
for v in ["GOPATH", "GOBIN", "GOROOT"]:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("Using '{}' for {}".format(os.environ.get(v), v))
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
cwd = os.getcwd()
|
2016-03-02 15:17:15 +00:00
|
|
|
if build_dir is None and os.environ.get("GOPATH") and os.environ.get("GOPATH") not in cwd:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Your current directory is not under your GOPATH. This may lead to build failures.")
|
2016-04-15 16:01:33 +00:00
|
|
|
return True
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def check_prereqs():
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Check user path for required dependencies.
|
|
|
|
"""
|
|
|
|
logging.info("Checking for dependencies...")
|
2016-01-08 15:47:09 +00:00
|
|
|
for req in prereqs:
|
2016-04-15 16:01:33 +00:00
|
|
|
if not check_path_for(req):
|
|
|
|
logging.error("Could not find dependency: {}".format(req))
|
|
|
|
return False
|
2016-03-02 15:17:15 +00:00
|
|
|
return True
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def upload_packages(packages, bucket_name=None, overwrite=False):
|
|
|
|
"""Upload provided package output to AWS S3.
|
|
|
|
"""
|
|
|
|
logging.debug("Uploading files to bucket '{}': {}".format(bucket_name, packages))
|
2016-01-23 00:09:38 +00:00
|
|
|
try:
|
|
|
|
import boto
|
|
|
|
from boto.s3.key import Key
|
2016-04-15 16:01:33 +00:00
|
|
|
from boto.s3.connection import OrdinaryCallingFormat
|
|
|
|
logging.getLogger("boto").setLevel(logging.WARNING)
|
2016-01-23 00:09:38 +00:00
|
|
|
except ImportError:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.warn("Cannot upload packages without 'boto' Python library!")
|
|
|
|
return False
|
|
|
|
logging.info("Connecting to AWS S3...")
|
|
|
|
# Up the number of attempts to 10 from default of 1
|
|
|
|
boto.config.add_section("Boto")
|
|
|
|
boto.config.set("Boto", "metadata_service_num_attempts", "10")
|
|
|
|
c = boto.connect_s3(calling_format=OrdinaryCallingFormat())
|
2016-01-23 00:09:38 +00:00
|
|
|
if bucket_name is None:
|
2016-03-02 15:17:15 +00:00
|
|
|
bucket_name = DEFAULT_BUCKET
|
2016-01-23 00:09:38 +00:00
|
|
|
bucket = c.get_bucket(bucket_name.split('/')[0])
|
2016-01-08 15:47:09 +00:00
|
|
|
for p in packages:
|
2016-01-23 00:09:38 +00:00
|
|
|
if '/' in bucket_name:
|
|
|
|
# Allow for nested paths within the bucket name (ex:
|
2016-03-02 15:17:15 +00:00
|
|
|
# bucket/folder). Assuming forward-slashes as path
|
2016-01-23 00:09:38 +00:00
|
|
|
# delimiter.
|
|
|
|
name = os.path.join('/'.join(bucket_name.split('/')[1:]),
|
|
|
|
os.path.basename(p))
|
|
|
|
else:
|
|
|
|
name = os.path.basename(p)
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("Using key: {}".format(name))
|
|
|
|
if bucket.get_key(name) is None or overwrite:
|
|
|
|
logging.info("Uploading file {}".format(name))
|
2016-01-08 15:47:09 +00:00
|
|
|
k = Key(bucket)
|
|
|
|
k.key = name
|
2016-04-15 16:01:33 +00:00
|
|
|
if overwrite:
|
2016-01-08 15:47:09 +00:00
|
|
|
n = k.set_contents_from_filename(p, replace=True)
|
|
|
|
else:
|
|
|
|
n = k.set_contents_from_filename(p, replace=False)
|
|
|
|
k.make_public()
|
|
|
|
else:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Not uploading file {}, as it already exists in the target bucket.".format(name))
|
2016-04-15 16:01:33 +00:00
|
|
|
return True
|
2016-03-02 15:17:15 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def go_list(vendor=False, relative=False):
|
|
|
|
"""
|
|
|
|
Return a list of packages
|
|
|
|
If vendor is False vendor package are not included
|
|
|
|
If relative is True the package prefix defined by PACKAGE_URL is stripped
|
|
|
|
"""
|
|
|
|
p = subprocess.Popen(["go", "list", "./..."], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
packages = out.split('\n')
|
|
|
|
if packages[-1] == '':
|
|
|
|
packages = packages[:-1]
|
|
|
|
if not vendor:
|
|
|
|
non_vendor = []
|
|
|
|
for p in packages:
|
|
|
|
if '/vendor/' not in p:
|
|
|
|
non_vendor.append(p)
|
|
|
|
packages = non_vendor
|
|
|
|
if relative:
|
|
|
|
relative_pkgs = []
|
|
|
|
for p in packages:
|
|
|
|
r = p.replace(PACKAGE_URL, '.')
|
|
|
|
if r != '.':
|
|
|
|
relative_pkgs.append(r)
|
|
|
|
packages = relative_pkgs
|
|
|
|
return packages
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
def build(version=None,
|
|
|
|
platform=None,
|
|
|
|
arch=None,
|
|
|
|
nightly=False,
|
|
|
|
race=False,
|
|
|
|
clean=False,
|
2016-04-15 16:01:33 +00:00
|
|
|
outdir=".",
|
2020-04-09 18:27:59 +00:00
|
|
|
tags=None,
|
2016-04-15 16:01:33 +00:00
|
|
|
static=False):
|
|
|
|
"""Build each target for the specified architecture and platform.
|
|
|
|
"""
|
2020-04-09 18:27:59 +00:00
|
|
|
if tags is None:
|
|
|
|
tags = []
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.info("Starting build for {}/{}...".format(platform, arch))
|
|
|
|
logging.info("Using Go version: {}".format(get_go_version()))
|
|
|
|
logging.info("Using git branch: {}".format(get_current_branch()))
|
|
|
|
logging.info("Using git commit: {}".format(get_current_commit()))
|
|
|
|
if static:
|
|
|
|
logging.info("Using statically-compiled output.")
|
|
|
|
if race:
|
|
|
|
logging.info("Race is enabled.")
|
|
|
|
if len(tags) > 0:
|
|
|
|
logging.info("Using build tags: {}".format(','.join(tags)))
|
|
|
|
|
|
|
|
logging.info("Sending build output to: {}".format(outdir))
|
2016-01-08 15:47:09 +00:00
|
|
|
if not os.path.exists(outdir):
|
|
|
|
os.makedirs(outdir)
|
2016-04-15 16:01:33 +00:00
|
|
|
elif clean and outdir != '/' and outdir != ".":
|
|
|
|
logging.info("Cleaning build directory '{}' before building.".format(outdir))
|
2016-01-08 15:47:09 +00:00
|
|
|
shutil.rmtree(outdir)
|
|
|
|
os.makedirs(outdir)
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.info("Using version '{}' for build.".format(version))
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
tmp_build_dir = create_temp_dir()
|
2016-04-15 16:01:33 +00:00
|
|
|
for target, path in targets.items():
|
|
|
|
logging.info("Building target: {}".format(target))
|
2016-01-08 15:47:09 +00:00
|
|
|
build_command = ""
|
2016-04-15 16:01:33 +00:00
|
|
|
|
|
|
|
# Handle static binary output
|
|
|
|
if static is True or "static_" in arch:
|
|
|
|
if "static_" in arch:
|
|
|
|
static = True
|
|
|
|
arch = arch.replace("static_", "")
|
|
|
|
build_command += "CGO_ENABLED=0 "
|
|
|
|
|
|
|
|
# Handle variations in architecture output
|
2018-08-29 19:28:00 +00:00
|
|
|
goarch = arch
|
2016-04-15 16:01:33 +00:00
|
|
|
if arch == "i386" or arch == "i686":
|
2018-08-29 19:28:00 +00:00
|
|
|
goarch = "386"
|
2017-09-18 21:22:54 +00:00
|
|
|
elif "arm64" in arch:
|
2018-08-29 19:28:00 +00:00
|
|
|
goarch = "arm64"
|
2016-04-15 16:01:33 +00:00
|
|
|
elif "arm" in arch:
|
2018-08-29 19:28:00 +00:00
|
|
|
goarch = "arm"
|
2019-02-04 21:50:13 +00:00
|
|
|
elif arch == "mipsel":
|
|
|
|
goarch = "mipsle"
|
2018-08-29 19:28:00 +00:00
|
|
|
build_command += "GOOS={} GOARCH={} ".format(platform, goarch)
|
2016-04-15 16:01:33 +00:00
|
|
|
|
2016-03-02 15:17:15 +00:00
|
|
|
if "arm" in arch:
|
|
|
|
if arch == "armel":
|
|
|
|
build_command += "GOARM=5 "
|
|
|
|
elif arch == "armhf" or arch == "arm":
|
|
|
|
build_command += "GOARM=6 "
|
|
|
|
elif arch == "arm64":
|
2016-04-15 16:01:33 +00:00
|
|
|
# TODO(rossmcdonald) - Verify this is the correct setting for arm64
|
2016-03-02 15:17:15 +00:00
|
|
|
build_command += "GOARM=7 "
|
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.error("Invalid ARM architecture specified: {}".format(arch))
|
|
|
|
logging.error("Please specify either 'armel', 'armhf', or 'arm64'.")
|
|
|
|
return False
|
2016-03-02 15:17:15 +00:00
|
|
|
if platform == 'windows':
|
2016-04-15 16:01:33 +00:00
|
|
|
target = target + '.exe'
|
|
|
|
build_command += "go build -o {} ".format(os.path.join(outdir, target))
|
2016-01-08 15:47:09 +00:00
|
|
|
if race:
|
|
|
|
build_command += "-race "
|
2016-04-15 16:01:33 +00:00
|
|
|
if len(tags) > 0:
|
|
|
|
build_command += "-tags {} ".format(','.join(tags))
|
|
|
|
|
2017-09-22 23:49:28 +00:00
|
|
|
ldflags = [
|
|
|
|
'-w', '-s',
|
|
|
|
'-X', 'main.branch={}'.format(get_current_branch()),
|
|
|
|
'-X', 'main.commit={}'.format(get_current_commit(short=True))]
|
|
|
|
if version:
|
|
|
|
ldflags.append('-X')
|
|
|
|
ldflags.append('main.version={}'.format(version))
|
|
|
|
build_command += ' -ldflags="{}" '.format(' '.join(ldflags))
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if static:
|
2017-09-22 23:49:28 +00:00
|
|
|
build_command += " -a -installsuffix cgo "
|
2016-04-15 16:01:33 +00:00
|
|
|
build_command += path
|
|
|
|
start_time = datetime.utcnow()
|
2016-01-08 15:47:09 +00:00
|
|
|
run(build_command, shell=True)
|
2016-04-15 16:01:33 +00:00
|
|
|
end_time = datetime.utcnow()
|
|
|
|
logging.info("Time taken: {}s".format((end_time - start_time).total_seconds()))
|
|
|
|
return True
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2017-05-31 19:29:39 +00:00
|
|
|
def generate_sha256_from_file(path):
|
|
|
|
"""Generate SHA256 hash signature based on the contents of the file at path.
|
2016-04-15 16:01:33 +00:00
|
|
|
"""
|
2017-05-31 19:29:39 +00:00
|
|
|
m = hashlib.sha256()
|
2016-03-02 15:17:15 +00:00
|
|
|
with open(path, 'rb') as f:
|
2017-05-31 19:29:39 +00:00
|
|
|
m.update(f.read())
|
2016-03-02 15:17:15 +00:00
|
|
|
return m.hexdigest()
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def generate_sig_from_file(path):
|
|
|
|
"""Generate a detached GPG signature from the file at path.
|
|
|
|
"""
|
|
|
|
logging.debug("Generating GPG signature for file: {}".format(path))
|
|
|
|
gpg_path = check_path_for('gpg')
|
|
|
|
if gpg_path is None:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("gpg binary not found on path! Skipping signature creation.")
|
2016-04-15 16:01:33 +00:00
|
|
|
return False
|
|
|
|
if os.environ.get("GNUPG_HOME") is not None:
|
|
|
|
run('gpg --homedir {} --armor --yes --detach-sign {}'.format(os.environ.get("GNUPG_HOME"), path))
|
|
|
|
else:
|
|
|
|
run('gpg --armor --detach-sign --yes {}'.format(path))
|
|
|
|
return True
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-05-05 19:01:24 +00:00
|
|
|
def package(build_output, pkg_name, version, nightly=False, iteration=1, static=False, release=False):
|
2016-04-15 16:01:33 +00:00
|
|
|
"""Package the output of the build process.
|
|
|
|
"""
|
2016-01-08 15:47:09 +00:00
|
|
|
outfiles = []
|
|
|
|
tmp_build_dir = create_temp_dir()
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("Packaging for build output: {}".format(build_output))
|
|
|
|
logging.info("Using temporary directory: {}".format(tmp_build_dir))
|
2016-01-08 15:47:09 +00:00
|
|
|
try:
|
2016-03-02 15:17:15 +00:00
|
|
|
for platform in build_output:
|
2016-01-08 15:47:09 +00:00
|
|
|
# Create top-level folder displaying which platform (linux, etc)
|
2016-04-15 16:01:33 +00:00
|
|
|
os.makedirs(os.path.join(tmp_build_dir, platform))
|
2016-03-02 15:17:15 +00:00
|
|
|
for arch in build_output[platform]:
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.info("Creating packages for {}/{}".format(platform, arch))
|
2016-03-02 15:17:15 +00:00
|
|
|
# Create second-level directory displaying the architecture (amd64, etc)
|
|
|
|
current_location = build_output[platform][arch]
|
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
# Create directory tree to mimic file system of package
|
2016-03-02 15:17:15 +00:00
|
|
|
build_root = os.path.join(tmp_build_dir,
|
|
|
|
platform,
|
|
|
|
arch,
|
2016-06-02 15:14:18 +00:00
|
|
|
PACKAGE_NAME)
|
2016-04-15 16:01:33 +00:00
|
|
|
os.makedirs(build_root)
|
2016-03-02 15:17:15 +00:00
|
|
|
|
|
|
|
# Copy packaging scripts to build directory
|
2016-05-06 15:46:29 +00:00
|
|
|
if platform == "windows":
|
2016-04-15 16:01:33 +00:00
|
|
|
# For windows and static builds, just copy
|
|
|
|
# binaries to root of package (no other scripts or
|
|
|
|
# directories)
|
2016-05-06 15:46:29 +00:00
|
|
|
package_scripts(build_root, config_only=True, windows=True)
|
|
|
|
elif static or "static_" in arch:
|
2016-04-15 16:01:33 +00:00
|
|
|
package_scripts(build_root, config_only=True)
|
2016-03-15 13:11:55 +00:00
|
|
|
else:
|
|
|
|
create_package_fs(build_root)
|
|
|
|
package_scripts(build_root)
|
2016-03-02 15:17:15 +00:00
|
|
|
|
|
|
|
for binary in targets:
|
2016-04-15 16:01:33 +00:00
|
|
|
# Copy newly-built binaries to packaging directory
|
2016-03-02 15:17:15 +00:00
|
|
|
if platform == 'windows':
|
|
|
|
binary = binary + '.exe'
|
2016-04-15 16:01:33 +00:00
|
|
|
if platform == 'windows' or static or "static_" in arch:
|
2016-03-15 13:11:55 +00:00
|
|
|
# Where the binary should go in the package filesystem
|
|
|
|
to = os.path.join(build_root, binary)
|
|
|
|
# Where the binary currently is located
|
|
|
|
fr = os.path.join(current_location, binary)
|
|
|
|
else:
|
|
|
|
# Where the binary currently is located
|
|
|
|
fr = os.path.join(current_location, binary)
|
|
|
|
# Where the binary should go in the package filesystem
|
|
|
|
to = os.path.join(build_root, INSTALL_ROOT_DIR[1:], binary)
|
2016-04-15 16:01:33 +00:00
|
|
|
shutil.copy(fr, to)
|
2016-03-02 15:17:15 +00:00
|
|
|
|
|
|
|
for package_type in supported_packages[platform]:
|
2019-07-23 20:20:39 +00:00
|
|
|
if package_type == "rpm" and arch in ["mipsel", "mips"]:
|
2019-02-04 21:50:13 +00:00
|
|
|
continue
|
2016-03-02 15:17:15 +00:00
|
|
|
# Package the directory structure for each package type for the platform
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("Packaging directory '{}' as '{}'.".format(build_root, package_type))
|
2016-05-05 19:01:24 +00:00
|
|
|
name = pkg_name
|
2016-01-23 00:09:38 +00:00
|
|
|
# Reset version, iteration, and current location on each run
|
|
|
|
# since they may be modified below.
|
2016-01-08 15:47:09 +00:00
|
|
|
package_version = version
|
|
|
|
package_iteration = iteration
|
2016-04-15 16:01:33 +00:00
|
|
|
if "static_" in arch:
|
|
|
|
# Remove the "static_" from the displayed arch on the package
|
|
|
|
package_arch = arch.replace("static_", "")
|
2016-12-05 16:45:02 +00:00
|
|
|
elif package_type == "rpm" and arch == 'armhf':
|
|
|
|
package_arch = 'armv6hl'
|
2016-04-15 16:01:33 +00:00
|
|
|
else:
|
|
|
|
package_arch = arch
|
2017-09-22 23:49:28 +00:00
|
|
|
if not version:
|
|
|
|
package_version = "{}~{}".format(next_version, get_current_commit(short=True))
|
2016-04-15 16:01:33 +00:00
|
|
|
package_iteration = "0"
|
2016-03-02 15:17:15 +00:00
|
|
|
package_build_root = build_root
|
|
|
|
current_location = build_output[platform][arch]
|
2016-04-15 16:01:33 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
if package_type in ['zip', 'tar']:
|
2016-03-02 15:17:15 +00:00
|
|
|
# For tars and zips, start the packaging one folder above
|
|
|
|
# the build root (to include the package name)
|
|
|
|
package_build_root = os.path.join('/', '/'.join(build_root.split('/')[:-1]))
|
2016-01-08 15:47:09 +00:00
|
|
|
if nightly:
|
2016-04-15 16:01:33 +00:00
|
|
|
if static or "static_" in arch:
|
|
|
|
name = '{}-static-nightly_{}_{}'.format(name,
|
|
|
|
platform,
|
|
|
|
package_arch)
|
|
|
|
else:
|
|
|
|
name = '{}-nightly_{}_{}'.format(name,
|
|
|
|
platform,
|
|
|
|
package_arch)
|
2016-01-08 15:47:09 +00:00
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
if static or "static_" in arch:
|
|
|
|
name = '{}-{}-static_{}_{}'.format(name,
|
|
|
|
package_version,
|
|
|
|
platform,
|
|
|
|
package_arch)
|
|
|
|
else:
|
|
|
|
name = '{}-{}_{}_{}'.format(name,
|
|
|
|
package_version,
|
|
|
|
platform,
|
|
|
|
package_arch)
|
2016-03-02 16:42:58 +00:00
|
|
|
current_location = os.path.join(os.getcwd(), current_location)
|
|
|
|
if package_type == 'tar':
|
2016-05-05 19:01:24 +00:00
|
|
|
tar_command = "cd {} && tar -cvzf {}.tar.gz ./*".format(package_build_root, name)
|
2016-03-02 16:42:58 +00:00
|
|
|
run(tar_command, shell=True)
|
2016-05-05 19:01:24 +00:00
|
|
|
run("mv {}.tar.gz {}".format(os.path.join(package_build_root, name), current_location), shell=True)
|
2016-03-02 16:42:58 +00:00
|
|
|
outfile = os.path.join(current_location, name + ".tar.gz")
|
|
|
|
outfiles.append(outfile)
|
|
|
|
elif package_type == 'zip':
|
2016-05-05 19:01:24 +00:00
|
|
|
zip_command = "cd {} && zip -r {}.zip ./*".format(package_build_root, name)
|
2016-03-02 16:42:58 +00:00
|
|
|
run(zip_command, shell=True)
|
2016-05-05 19:01:24 +00:00
|
|
|
run("mv {}.zip {}".format(os.path.join(package_build_root, name), current_location), shell=True)
|
2016-03-02 16:42:58 +00:00
|
|
|
outfile = os.path.join(current_location, name + ".zip")
|
|
|
|
outfiles.append(outfile)
|
2016-04-15 16:01:33 +00:00
|
|
|
elif package_type not in ['zip', 'tar'] and static or "static_" in arch:
|
|
|
|
logging.info("Skipping package type '{}' for static builds.".format(package_type))
|
2016-02-22 22:16:46 +00:00
|
|
|
else:
|
2017-05-05 21:29:40 +00:00
|
|
|
if package_type == 'rpm' and release and '~' in package_version:
|
|
|
|
package_version, suffix = package_version.split('~', 1)
|
2020-05-14 07:41:58 +00:00
|
|
|
# The ~ indicates that this is a prerelease so we give it a leading 0.
|
2017-05-05 21:29:40 +00:00
|
|
|
package_iteration = "0.%s" % suffix
|
2016-04-15 16:01:33 +00:00
|
|
|
fpm_command = "fpm {} --name {} -a {} -t {} --version {} --iteration {} -C {} -p {} ".format(
|
|
|
|
fpm_common_args,
|
|
|
|
name,
|
|
|
|
package_arch,
|
|
|
|
package_type,
|
|
|
|
package_version,
|
|
|
|
package_iteration,
|
|
|
|
package_build_root,
|
|
|
|
current_location)
|
2016-03-02 16:42:58 +00:00
|
|
|
if package_type == "rpm":
|
2019-03-22 21:02:45 +00:00
|
|
|
fpm_command += "--directories /var/log/telegraf --directories /etc/telegraf --depends coreutils --depends shadow-utils --rpm-posttrans {}".format(POSTINST_SCRIPT)
|
2016-03-02 16:42:58 +00:00
|
|
|
out = run(fpm_command, shell=True)
|
|
|
|
matches = re.search(':path=>"(.*)"', out)
|
|
|
|
outfile = None
|
|
|
|
if matches is not None:
|
|
|
|
outfile = matches.groups()[0]
|
|
|
|
if outfile is None:
|
2020-04-09 18:27:59 +00:00
|
|
|
logging.warning("Could not determine output from packaging output!")
|
2016-03-02 16:42:58 +00:00
|
|
|
else:
|
|
|
|
if nightly:
|
2016-04-15 16:01:33 +00:00
|
|
|
# Strip nightly version from package name
|
|
|
|
new_outfile = outfile.replace("{}-{}".format(package_version, package_iteration), "nightly")
|
|
|
|
os.rename(outfile, new_outfile)
|
|
|
|
outfile = new_outfile
|
|
|
|
else:
|
|
|
|
if package_type == 'rpm':
|
|
|
|
# rpm's convert any dashes to underscores
|
|
|
|
package_version = package_version.replace("-", "_")
|
2016-03-02 16:42:58 +00:00
|
|
|
outfiles.append(os.path.join(os.getcwd(), outfile))
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.debug("Produced package files: {}".format(outfiles))
|
2016-01-08 15:47:09 +00:00
|
|
|
return outfiles
|
|
|
|
finally:
|
|
|
|
# Cleanup
|
|
|
|
shutil.rmtree(tmp_build_dir)
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
def main(args):
|
|
|
|
global PACKAGE_NAME
|
2016-01-08 15:47:09 +00:00
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.release and args.nightly:
|
|
|
|
logging.error("Cannot be both a nightly and a release.")
|
2016-03-02 15:17:15 +00:00
|
|
|
return 1
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.nightly:
|
|
|
|
args.iteration = 0
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
# Pre-build checks
|
|
|
|
check_environ()
|
2016-03-02 15:17:15 +00:00
|
|
|
if not check_prereqs():
|
|
|
|
return 1
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.build_tags is None:
|
|
|
|
args.build_tags = []
|
2016-03-02 15:17:15 +00:00
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
args.build_tags = args.build_tags.split(',')
|
|
|
|
|
|
|
|
orig_commit = get_current_commit(short=True)
|
|
|
|
orig_branch = get_current_branch()
|
|
|
|
|
|
|
|
if args.platform not in supported_builds and args.platform != 'all':
|
2018-05-04 21:18:59 +00:00
|
|
|
logging.error("Invalid build platform: {}".format(args.platform))
|
2016-04-15 16:01:33 +00:00
|
|
|
return 1
|
2016-02-18 04:57:33 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
build_output = {}
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.branch != orig_branch and args.commit != orig_commit:
|
|
|
|
logging.error("Can only specify one branch or commit to build from.")
|
|
|
|
return 1
|
|
|
|
elif args.branch != orig_branch:
|
|
|
|
logging.info("Moving to git branch: {}".format(args.branch))
|
|
|
|
run("git checkout {}".format(args.branch))
|
|
|
|
elif args.commit != orig_commit:
|
|
|
|
logging.info("Moving to git commit: {}".format(args.commit))
|
|
|
|
run("git checkout {}".format(args.commit))
|
|
|
|
|
|
|
|
if not args.no_get:
|
|
|
|
if not go_get(args.branch, update=args.update, no_uncommitted=args.no_uncommitted):
|
2016-03-02 15:17:15 +00:00
|
|
|
return 1
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.generate:
|
|
|
|
if not run_generate():
|
2016-03-02 15:17:15 +00:00
|
|
|
return 1
|
|
|
|
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.test:
|
|
|
|
if not run_tests(args.race, args.parallel, args.timeout, args.no_vet):
|
2016-03-02 15:17:15 +00:00
|
|
|
return 1
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
platforms = []
|
|
|
|
single_build = True
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.platform == 'all':
|
2016-03-02 15:17:15 +00:00
|
|
|
platforms = supported_builds.keys()
|
2016-01-08 15:47:09 +00:00
|
|
|
single_build = False
|
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
platforms = [args.platform]
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
for platform in platforms:
|
2020-04-09 18:27:59 +00:00
|
|
|
build_output.update({platform: {}})
|
2016-01-08 15:47:09 +00:00
|
|
|
archs = []
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.arch == "all":
|
2016-01-08 15:47:09 +00:00
|
|
|
single_build = False
|
|
|
|
archs = supported_builds.get(platform)
|
|
|
|
else:
|
2016-04-15 16:01:33 +00:00
|
|
|
archs = [args.arch]
|
2016-03-02 15:17:15 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
for arch in archs:
|
2016-04-15 16:01:33 +00:00
|
|
|
od = args.outdir
|
2016-01-08 15:47:09 +00:00
|
|
|
if not single_build:
|
2016-04-15 16:01:33 +00:00
|
|
|
od = os.path.join(args.outdir, platform, arch)
|
|
|
|
if not build(version=args.version,
|
|
|
|
platform=platform,
|
|
|
|
arch=arch,
|
|
|
|
nightly=args.nightly,
|
|
|
|
race=args.race,
|
|
|
|
clean=args.clean,
|
|
|
|
outdir=od,
|
|
|
|
tags=args.build_tags,
|
|
|
|
static=args.static):
|
2016-03-02 15:17:15 +00:00
|
|
|
return 1
|
2020-04-09 18:27:59 +00:00
|
|
|
build_output.get(platform).update({arch: od})
|
2016-01-08 15:47:09 +00:00
|
|
|
|
|
|
|
# Build packages
|
2016-04-15 16:01:33 +00:00
|
|
|
if args.package:
|
2016-01-08 15:47:09 +00:00
|
|
|
if not check_path_for("fpm"):
|
2016-04-15 16:01:33 +00:00
|
|
|
logging.error("FPM ruby gem required for packaging. Stopping.")
|
2016-01-08 15:47:09 +00:00
|
|
|
return 1
|
2016-04-15 16:01:33 +00:00
|
|
|
packages = package(build_output,
|
2016-05-05 19:01:24 +00:00
|
|
|
args.name,
|
2016-04-15 16:01:33 +00:00
|
|
|
args.version,
|
|
|
|
nightly=args.nightly,
|
|
|
|
iteration=args.iteration,
|
|
|
|
static=args.static,
|
|
|
|
release=args.release)
|
|
|
|
if args.sign:
|
|
|
|
logging.debug("Generating GPG signatures for packages: {}".format(packages))
|
2020-04-09 18:27:59 +00:00
|
|
|
sigs = [] # retain signatures so they can be uploaded with packages
|
2016-04-15 16:01:33 +00:00
|
|
|
for p in packages:
|
|
|
|
if generate_sig_from_file(p):
|
|
|
|
sigs.append(p + '.asc')
|
|
|
|
else:
|
|
|
|
logging.error("Creation of signature for package [{}] failed!".format(p))
|
|
|
|
return 1
|
|
|
|
packages += sigs
|
|
|
|
if args.upload:
|
|
|
|
logging.debug("Files staged for upload: {}".format(packages))
|
|
|
|
if args.nightly:
|
|
|
|
args.upload_overwrite = True
|
|
|
|
if not upload_packages(packages, bucket_name=args.bucket, overwrite=args.upload_overwrite):
|
|
|
|
return 1
|
|
|
|
logging.info("Packages created:")
|
2017-05-31 19:29:39 +00:00
|
|
|
for filename in packages:
|
|
|
|
logging.info("%s (SHA256=%s)",
|
|
|
|
os.path.basename(filename),
|
|
|
|
generate_sha256_from_file(filename))
|
2016-04-15 16:01:33 +00:00
|
|
|
if orig_branch != get_current_branch():
|
|
|
|
logging.info("Moving back to original git branch: {}".format(args.branch))
|
|
|
|
run("git checkout {}".format(orig_branch))
|
2016-03-02 15:17:15 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
return 0
|
|
|
|
|
2020-04-09 18:27:59 +00:00
|
|
|
|
2016-01-08 15:47:09 +00:00
|
|
|
if __name__ == '__main__':
|
2016-04-15 16:01:33 +00:00
|
|
|
LOG_LEVEL = logging.INFO
|
|
|
|
if '--debug' in sys.argv[1:]:
|
|
|
|
LOG_LEVEL = logging.DEBUG
|
|
|
|
log_format = '[%(levelname)s] %(funcName)s: %(message)s'
|
|
|
|
logging.basicConfig(level=LOG_LEVEL,
|
|
|
|
format=log_format)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='InfluxDB build and packaging script.')
|
2020-04-09 18:27:59 +00:00
|
|
|
parser.add_argument('--verbose', '-v', '--debug',
|
2016-04-15 16:01:33 +00:00
|
|
|
action='store_true',
|
|
|
|
help='Use debug output')
|
|
|
|
parser.add_argument('--outdir', '-o',
|
|
|
|
metavar='<output directory>',
|
|
|
|
default='./build/',
|
|
|
|
type=os.path.abspath,
|
|
|
|
help='Output directory')
|
|
|
|
parser.add_argument('--name', '-n',
|
|
|
|
metavar='<name>',
|
2016-05-05 19:01:24 +00:00
|
|
|
default=PACKAGE_NAME,
|
2016-04-15 16:01:33 +00:00
|
|
|
type=str,
|
|
|
|
help='Name to use for package name (when package is specified)')
|
|
|
|
parser.add_argument('--arch',
|
|
|
|
metavar='<amd64|i386|armhf|arm64|armel|all>',
|
|
|
|
type=str,
|
|
|
|
default=get_system_arch(),
|
|
|
|
help='Target architecture for build output')
|
|
|
|
parser.add_argument('--platform',
|
|
|
|
metavar='<linux|darwin|windows|all>',
|
|
|
|
type=str,
|
|
|
|
default=get_system_platform(),
|
|
|
|
help='Target platform for build output')
|
|
|
|
parser.add_argument('--branch',
|
|
|
|
metavar='<branch>',
|
|
|
|
type=str,
|
|
|
|
default=get_current_branch(),
|
|
|
|
help='Build from a specific branch')
|
|
|
|
parser.add_argument('--commit',
|
|
|
|
metavar='<commit>',
|
|
|
|
type=str,
|
|
|
|
default=get_current_commit(short=True),
|
|
|
|
help='Build from a specific commit')
|
|
|
|
parser.add_argument('--version',
|
|
|
|
metavar='<version>',
|
|
|
|
type=str,
|
|
|
|
default=get_current_version(),
|
|
|
|
help='Version information to apply to build output (ex: 0.12.0)')
|
|
|
|
parser.add_argument('--iteration',
|
|
|
|
metavar='<package iteration>',
|
2016-05-05 19:01:24 +00:00
|
|
|
type=str,
|
|
|
|
default="1",
|
2016-04-15 16:01:33 +00:00
|
|
|
help='Package iteration to apply to build output (defaults to 1)')
|
|
|
|
parser.add_argument('--stats',
|
|
|
|
action='store_true',
|
|
|
|
help='Emit build metrics (requires InfluxDB Python client)')
|
|
|
|
parser.add_argument('--stats-server',
|
|
|
|
metavar='<hostname:port>',
|
|
|
|
type=str,
|
|
|
|
help='Send build stats to InfluxDB using provided hostname and port')
|
|
|
|
parser.add_argument('--stats-db',
|
|
|
|
metavar='<database name>',
|
|
|
|
type=str,
|
|
|
|
help='Send build stats to InfluxDB using provided database name')
|
|
|
|
parser.add_argument('--nightly',
|
|
|
|
action='store_true',
|
|
|
|
help='Mark build output as nightly build (will incremement the minor version)')
|
|
|
|
parser.add_argument('--update',
|
|
|
|
action='store_true',
|
|
|
|
help='Update build dependencies prior to building')
|
|
|
|
parser.add_argument('--package',
|
|
|
|
action='store_true',
|
|
|
|
help='Package binary output')
|
|
|
|
parser.add_argument('--release',
|
|
|
|
action='store_true',
|
|
|
|
help='Mark build output as release')
|
|
|
|
parser.add_argument('--clean',
|
|
|
|
action='store_true',
|
|
|
|
help='Clean output directory before building')
|
|
|
|
parser.add_argument('--no-get',
|
|
|
|
action='store_true',
|
|
|
|
help='Do not retrieve pinned dependencies when building')
|
|
|
|
parser.add_argument('--no-uncommitted',
|
|
|
|
action='store_true',
|
|
|
|
help='Fail if uncommitted changes exist in the working directory')
|
|
|
|
parser.add_argument('--upload',
|
|
|
|
action='store_true',
|
|
|
|
help='Upload output packages to AWS S3')
|
2020-04-09 18:27:59 +00:00
|
|
|
parser.add_argument('--upload-overwrite', '-w',
|
2016-04-15 16:01:33 +00:00
|
|
|
action='store_true',
|
|
|
|
help='Upload output packages to AWS S3')
|
|
|
|
parser.add_argument('--bucket',
|
|
|
|
metavar='<S3 bucket name>',
|
|
|
|
type=str,
|
|
|
|
default=DEFAULT_BUCKET,
|
|
|
|
help='Destination bucket for uploads')
|
|
|
|
parser.add_argument('--generate',
|
|
|
|
action='store_true',
|
|
|
|
help='Run "go generate" before building')
|
|
|
|
parser.add_argument('--build-tags',
|
|
|
|
metavar='<tags>',
|
|
|
|
help='Optional build tags to use for compilation')
|
|
|
|
parser.add_argument('--static',
|
|
|
|
action='store_true',
|
|
|
|
help='Create statically-compiled binary output')
|
|
|
|
parser.add_argument('--sign',
|
|
|
|
action='store_true',
|
|
|
|
help='Create GPG detached signatures for packages (when package is specified)')
|
|
|
|
parser.add_argument('--test',
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests (does not produce build output)')
|
|
|
|
parser.add_argument('--no-vet',
|
|
|
|
action='store_true',
|
|
|
|
help='Do not run "go vet" when running tests')
|
|
|
|
parser.add_argument('--race',
|
|
|
|
action='store_true',
|
|
|
|
help='Enable race flag for build output')
|
|
|
|
parser.add_argument('--parallel',
|
|
|
|
metavar='<num threads>',
|
|
|
|
type=int,
|
|
|
|
help='Number of tests to run simultaneously')
|
|
|
|
parser.add_argument('--timeout',
|
|
|
|
metavar='<timeout>',
|
|
|
|
type=str,
|
|
|
|
help='Timeout for tests before failing')
|
|
|
|
args = parser.parse_args()
|
|
|
|
print_banner()
|
|
|
|
sys.exit(main(args))
|