Deploy telegraf configuration as a "non config" file (#7250)

This commit is contained in:
Andrés Álvarez 2020-04-09 20:27:59 +02:00 committed by GitHub
parent df145c7e56
commit cc6c77f301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 41 deletions

View File

@ -3980,7 +3980,7 @@
# # timeout = "5ms" # # timeout = "5ms"
# # A plugin to collect stats from Opensmtpd - a validating, recursive, and caching DNS resolver # # A plugin to collect stats from Opensmtpd - a validating, recursive, and caching DNS resolver
# [[inputs.opensmtpd]] # [[inputs.opensmtpd]]
# ## If running as a restricted user you can prepend sudo for additional access: # ## If running as a restricted user you can prepend sudo for additional access:
# #use_sudo = false # #use_sudo = false

View File

@ -3,7 +3,6 @@
import sys import sys
import os import os
import subprocess import subprocess
import time
from datetime import datetime from datetime import datetime
import shutil import shutil
import tempfile import tempfile
@ -53,9 +52,9 @@ VENDOR = "InfluxData"
DESCRIPTION = "Plugin-driven server agent for reporting metrics into InfluxDB." DESCRIPTION = "Plugin-driven server agent for reporting metrics into InfluxDB."
# SCRIPT START # SCRIPT START
prereqs = [ 'git', 'go' ] prereqs = ['git', 'go']
go_vet_command = "go tool vet -composites=true ./" go_vet_command = "go tool vet -composites=true ./"
optional_prereqs = [ 'gvm', 'fpm', 'rpmbuild' ] optional_prereqs = ['gvm', 'fpm', 'rpmbuild']
fpm_common_args = "-f -s dir --log error \ fpm_common_args = "-f -s dir --log error \
--vendor {} \ --vendor {} \
@ -74,7 +73,7 @@ fpm_common_args = "-f -s dir --log error \
PACKAGE_URL, PACKAGE_URL,
PACKAGE_LICENSE, PACKAGE_LICENSE,
MAINTAINER, MAINTAINER,
CONFIG_DIR + '/telegraf.conf', CONFIG_DIR + '/telegraf.conf.sample',
LOGROTATE_DIR + '/telegraf', LOGROTATE_DIR + '/telegraf',
POSTINST_SCRIPT, POSTINST_SCRIPT,
PREINST_SCRIPT, PREINST_SCRIPT,
@ -84,21 +83,21 @@ fpm_common_args = "-f -s dir --log error \
DESCRIPTION) DESCRIPTION)
targets = { targets = {
'telegraf' : './cmd/telegraf', 'telegraf': './cmd/telegraf',
} }
supported_builds = { supported_builds = {
'darwin': [ "amd64" ], 'darwin': ["amd64"],
"windows": [ "amd64", "i386" ], "windows": ["amd64", "i386"],
"linux": [ "amd64", "i386", "armhf", "armel", "arm64", "static_amd64", "s390x", "mipsel", "mips"], "linux": ["amd64", "i386", "armhf", "armel", "arm64", "static_amd64", "s390x", "mipsel", "mips"],
"freebsd": [ "amd64", "i386" ] "freebsd": ["amd64", "i386"]
} }
supported_packages = { supported_packages = {
"darwin": [ "tar" ], "darwin": ["tar"],
"linux": [ "deb", "rpm", "tar" ], "linux": ["deb", "rpm", "tar"],
"windows": [ "zip" ], "windows": ["zip"],
"freebsd": [ "tar" ] "freebsd": ["tar"]
} }
next_version = '1.15.0' next_version = '1.15.0'
@ -107,6 +106,7 @@ next_version = '1.15.0'
#### Telegraf Functions #### Telegraf Functions
################ ################
def print_banner(): def print_banner():
logging.info(""" logging.info("""
_____ _ __ _____ _ __
@ -118,17 +118,19 @@ def print_banner():
Build Script Build Script
""") """)
def create_package_fs(build_root): def create_package_fs(build_root):
"""Create a filesystem structure to mimic the package filesystem. """Create a filesystem structure to mimic the package filesystem.
""" """
logging.debug("Creating a filesystem hierarchy from directory: {}".format(build_root)) logging.debug("Creating a filesystem hierarchy from directory: {}".format(build_root))
# Using [1:] for the path names due to them being absolute # Using [1:] for the path names due to them being absolute
# (will overwrite previous paths, per 'os.path.join' documentation) # (will overwrite previous paths, per 'os.path.join' documentation)
dirs = [ INSTALL_ROOT_DIR[1:], LOG_DIR[1:], SCRIPT_DIR[1:], CONFIG_DIR[1:], LOGROTATE_DIR[1:], CONFIG_DIR_D[1:] ] dirs = [INSTALL_ROOT_DIR[1:], LOG_DIR[1:], SCRIPT_DIR[1:], CONFIG_DIR[1:], LOGROTATE_DIR[1:], CONFIG_DIR_D[1:]]
for d in dirs: for d in dirs:
os.makedirs(os.path.join(build_root, d)) os.makedirs(os.path.join(build_root, d))
os.chmod(os.path.join(build_root, d), 0o755) os.chmod(os.path.join(build_root, d), 0o755)
def package_scripts(build_root, config_only=False, windows=False): def package_scripts(build_root, config_only=False, windows=False):
"""Copy the necessary scripts and configuration files to the package """Copy the necessary scripts and configuration files to the package
filesystem. filesystem.
@ -136,10 +138,10 @@ def package_scripts(build_root, config_only=False, windows=False):
if config_only or windows: if config_only or windows:
logging.info("Copying configuration to build directory") logging.info("Copying configuration to build directory")
if windows: if windows:
shutil.copyfile(DEFAULT_WINDOWS_CONFIG, os.path.join(build_root, "telegraf.conf")) shutil.copyfile(DEFAULT_WINDOWS_CONFIG, os.path.join(build_root, "telegraf.conf.sample"))
else: else:
shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, "telegraf.conf")) shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, "telegraf.conf.sample"))
os.chmod(os.path.join(build_root, "telegraf.conf"), 0o644) os.chmod(os.path.join(build_root, "telegraf.conf.sample"), 0o644)
else: else:
logging.info("Copying scripts and configuration to build directory") logging.info("Copying scripts and configuration to build directory")
shutil.copyfile(INIT_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1])) shutil.copyfile(INIT_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1]))
@ -148,13 +150,15 @@ def package_scripts(build_root, config_only=False, windows=False):
os.chmod(os.path.join(build_root, SCRIPT_DIR[1:], SYSTEMD_SCRIPT.split('/')[1]), 0o644) 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")) 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) os.chmod(os.path.join(build_root, LOGROTATE_DIR[1:], "telegraf"), 0o644)
shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, CONFIG_DIR[1:], "telegraf.conf")) 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"), 0o644) os.chmod(os.path.join(build_root, CONFIG_DIR[1:], "telegraf.conf.sample"), 0o644)
def run_generate(): def run_generate():
# NOOP for Telegraf # NOOP for Telegraf
return True return True
def go_get(branch, update=False, no_uncommitted=False): def go_get(branch, update=False, no_uncommitted=False):
"""Retrieve build dependencies or restore pinned dependencies. """Retrieve build dependencies or restore pinned dependencies.
""" """
@ -165,10 +169,12 @@ def go_get(branch, update=False, no_uncommitted=False):
run("go mod download") run("go mod download")
return True return True
def run_tests(race, parallel, timeout, no_vet): def run_tests(race, parallel, timeout, no_vet):
# Currently a NOOP for Telegraf # Currently a NOOP for Telegraf
return True return True
################ ################
#### All Telegraf-specific content above this line #### All Telegraf-specific content above this line
################ ################
@ -187,14 +193,14 @@ def run(command, allow_failure=False, shell=False):
# logging.debug("Command output: {}".format(out)) # logging.debug("Command output: {}".format(out))
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if allow_failure: if allow_failure:
logging.warn("Command '{}' failed with error: {}".format(command, e.output)) logging.warning("Command '{}' failed with error: {}".format(command, e.output))
return None return None
else: else:
logging.error("Command '{}' failed with error: {}".format(command, e.output)) logging.error("Command '{}' failed with error: {}".format(command, e.output))
sys.exit(1) sys.exit(1)
except OSError as e: except OSError as e:
if allow_failure: if allow_failure:
logging.warn("Command '{}' failed with error: {}".format(command, e)) logging.warning("Command '{}' failed with error: {}".format(command, e))
return out return out
else: else:
logging.error("Command '{}' failed with error: {}".format(command, e)) logging.error("Command '{}' failed with error: {}".format(command, e))
@ -202,7 +208,8 @@ def run(command, allow_failure=False, shell=False):
else: else:
return out return out
def create_temp_dir(prefix = None):
def create_temp_dir(prefix=None):
""" Create temporary directory with optional prefix. """ Create temporary directory with optional prefix.
""" """
if prefix is None: if prefix is None:
@ -210,13 +217,14 @@ def create_temp_dir(prefix = None):
else: else:
return tempfile.mkdtemp(prefix=prefix) return tempfile.mkdtemp(prefix=prefix)
def increment_minor_version(version): def increment_minor_version(version):
"""Return the version with the minor version incremented and patch """Return the version with the minor version incremented and patch
version set to zero. version set to zero.
""" """
ver_list = version.split('.') ver_list = version.split('.')
if len(ver_list) != 3: if len(ver_list) != 3:
logging.warn("Could not determine how to increment version '{}', will just use provided version.".format(version)) logging.warning("Could not determine how to increment version '{}', will just use provided version.".format(version))
return version return version
ver_list[1] = str(int(ver_list[1]) + 1) ver_list[1] = str(int(ver_list[1]) + 1)
ver_list[2] = str(0) ver_list[2] = str(0)
@ -224,13 +232,15 @@ def increment_minor_version(version):
logging.debug("Incremented version from '{}' to '{}'.".format(version, inc_version)) logging.debug("Incremented version from '{}' to '{}'.".format(version, inc_version))
return inc_version return inc_version
def get_current_version_tag(): def get_current_version_tag():
"""Retrieve the raw git version tag. """Retrieve the raw git version tag.
""" """
version = run("git describe --exact-match --tags 2>/dev/null", version = run("git describe --exact-match --tags 2>/dev/null",
allow_failure=True, shell=True) allow_failure=True, shell=True)
return version return version
def get_current_version(): def get_current_version():
"""Parse version information from git tag output. """Parse version information from git tag output.
""" """
@ -242,15 +252,15 @@ def get_current_version():
version_tag = version_tag[1:] version_tag = version_tag[1:]
# Replace any '-'/'_' with '~' # Replace any '-'/'_' with '~'
if '-' in version_tag: if '-' in version_tag:
version_tag = version_tag.replace("-","~") version_tag = version_tag.replace("-", "~")
if '_' in version_tag: if '_' in version_tag:
version_tag = version_tag.replace("_","~") version_tag = version_tag.replace("_", "~")
return version_tag return version_tag
def get_current_commit(short=False): def get_current_commit(short=False):
"""Retrieve the current git commit. """Retrieve the current git commit.
""" """
command = None
if short: if short:
command = "git log --pretty=format:'%h' -n 1" command = "git log --pretty=format:'%h' -n 1"
else: else:
@ -258,6 +268,7 @@ def get_current_commit(short=False):
out = run(command) out = run(command)
return out.strip('\'\n\r ') return out.strip('\'\n\r ')
def get_current_branch(): def get_current_branch():
"""Retrieve the current git branch. """Retrieve the current git branch.
""" """
@ -265,6 +276,7 @@ def get_current_branch():
out = run(command) out = run(command)
return out.strip() return out.strip()
def local_changes(): def local_changes():
"""Return True if there are local un-committed changes. """Return True if there are local un-committed changes.
""" """
@ -273,6 +285,7 @@ def local_changes():
return True return True
return False return False
def get_system_arch(): def get_system_arch():
"""Retrieve current system architecture. """Retrieve current system architecture.
""" """
@ -288,6 +301,7 @@ def get_system_arch():
arch = "arm" arch = "arm"
return arch return arch
def get_system_platform(): def get_system_platform():
"""Retrieve current system platform. """Retrieve current system platform.
""" """
@ -296,6 +310,7 @@ def get_system_platform():
else: else:
return sys.platform return sys.platform
def get_go_version(): def get_go_version():
"""Retrieve version information for Go. """Retrieve version information for Go.
""" """
@ -305,6 +320,7 @@ def get_go_version():
return matches.groups()[0].strip() return matches.groups()[0].strip()
return None return None
def check_path_for(b): def check_path_for(b):
"""Check the the user's path for the provided binary. """Check the the user's path for the provided binary.
""" """
@ -314,21 +330,23 @@ def check_path_for(b):
for path in os.environ["PATH"].split(os.pathsep): for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"') path = path.strip('"')
full_path = os.path.join(path, b) full_path = os.path.join(path, b)
if os.path.isfile(full_path) and os.access(full_path, os.X_OK): if is_exe(full_path):
return full_path return full_path
def check_environ(build_dir = None):
def check_environ(build_dir=None):
"""Check environment for common Go variables. """Check environment for common Go variables.
""" """
logging.info("Checking environment...") logging.info("Checking environment...")
for v in [ "GOPATH", "GOBIN", "GOROOT" ]: for v in ["GOPATH", "GOBIN", "GOROOT"]:
logging.debug("Using '{}' for {}".format(os.environ.get(v), v)) logging.debug("Using '{}' for {}".format(os.environ.get(v), v))
cwd = os.getcwd() cwd = os.getcwd()
if build_dir is None and os.environ.get("GOPATH") and os.environ.get("GOPATH") not in cwd: if build_dir is None and os.environ.get("GOPATH") and os.environ.get("GOPATH") not in cwd:
logging.warn("Your current directory is not under your GOPATH. This may lead to build failures.") logging.warning("Your current directory is not under your GOPATH. This may lead to build failures.")
return True return True
def check_prereqs(): def check_prereqs():
"""Check user path for required dependencies. """Check user path for required dependencies.
""" """
@ -339,6 +357,7 @@ def check_prereqs():
return False return False
return True return True
def upload_packages(packages, bucket_name=None, overwrite=False): def upload_packages(packages, bucket_name=None, overwrite=False):
"""Upload provided package output to AWS S3. """Upload provided package output to AWS S3.
""" """
@ -379,9 +398,10 @@ def upload_packages(packages, bucket_name=None, overwrite=False):
n = k.set_contents_from_filename(p, replace=False) n = k.set_contents_from_filename(p, replace=False)
k.make_public() k.make_public()
else: else:
logging.warn("Not uploading file {}, as it already exists in the target bucket.".format(name)) logging.warning("Not uploading file {}, as it already exists in the target bucket.".format(name))
return True return True
def go_list(vendor=False, relative=False): def go_list(vendor=False, relative=False):
""" """
Return a list of packages Return a list of packages
@ -408,6 +428,7 @@ def go_list(vendor=False, relative=False):
packages = relative_pkgs packages = relative_pkgs
return packages return packages
def build(version=None, def build(version=None,
platform=None, platform=None,
arch=None, arch=None,
@ -415,10 +436,12 @@ def build(version=None,
race=False, race=False,
clean=False, clean=False,
outdir=".", outdir=".",
tags=[], tags=None,
static=False): static=False):
"""Build each target for the specified architecture and platform. """Build each target for the specified architecture and platform.
""" """
if tags is None:
tags = []
logging.info("Starting build for {}/{}...".format(platform, arch)) logging.info("Starting build for {}/{}...".format(platform, arch))
logging.info("Using Go version: {}".format(get_go_version())) logging.info("Using Go version: {}".format(get_go_version()))
logging.info("Using git branch: {}".format(get_current_branch())) logging.info("Using git branch: {}".format(get_current_branch()))
@ -502,6 +525,7 @@ def build(version=None,
logging.info("Time taken: {}s".format((end_time - start_time).total_seconds())) logging.info("Time taken: {}s".format((end_time - start_time).total_seconds()))
return True return True
def generate_sha256_from_file(path): def generate_sha256_from_file(path):
"""Generate SHA256 hash signature based on the contents of the file at path. """Generate SHA256 hash signature based on the contents of the file at path.
""" """
@ -510,13 +534,14 @@ def generate_sha256_from_file(path):
m.update(f.read()) m.update(f.read())
return m.hexdigest() return m.hexdigest()
def generate_sig_from_file(path): def generate_sig_from_file(path):
"""Generate a detached GPG signature from the file at path. """Generate a detached GPG signature from the file at path.
""" """
logging.debug("Generating GPG signature for file: {}".format(path)) logging.debug("Generating GPG signature for file: {}".format(path))
gpg_path = check_path_for('gpg') gpg_path = check_path_for('gpg')
if gpg_path is None: if gpg_path is None:
logging.warn("gpg binary not found on path! Skipping signature creation.") logging.warning("gpg binary not found on path! Skipping signature creation.")
return False return False
if os.environ.get("GNUPG_HOME") is not None: if os.environ.get("GNUPG_HOME") is not None:
run('gpg --homedir {} --armor --yes --detach-sign {}'.format(os.environ.get("GNUPG_HOME"), path)) run('gpg --homedir {} --armor --yes --detach-sign {}'.format(os.environ.get("GNUPG_HOME"), path))
@ -524,6 +549,7 @@ def generate_sig_from_file(path):
run('gpg --armor --detach-sign --yes {}'.format(path)) run('gpg --armor --detach-sign --yes {}'.format(path))
return True return True
def package(build_output, pkg_name, version, nightly=False, iteration=1, static=False, release=False): def package(build_output, pkg_name, version, nightly=False, iteration=1, static=False, release=False):
"""Package the output of the build process. """Package the output of the build process.
""" """
@ -659,7 +685,7 @@ def package(build_output, pkg_name, version, nightly=False, iteration=1, static=
if matches is not None: if matches is not None:
outfile = matches.groups()[0] outfile = matches.groups()[0]
if outfile is None: if outfile is None:
logging.warn("Could not determine output from packaging output!") logging.warning("Could not determine output from packaging output!")
else: else:
if nightly: if nightly:
# Strip nightly version from package name # Strip nightly version from package name
@ -677,6 +703,7 @@ def package(build_output, pkg_name, version, nightly=False, iteration=1, static=
# Cleanup # Cleanup
shutil.rmtree(tmp_build_dir) shutil.rmtree(tmp_build_dir)
def main(args): def main(args):
global PACKAGE_NAME global PACKAGE_NAME
@ -736,7 +763,7 @@ def main(args):
platforms = [args.platform] platforms = [args.platform]
for platform in platforms: for platform in platforms:
build_output.update( { platform : {} } ) build_output.update({platform: {}})
archs = [] archs = []
if args.arch == "all": if args.arch == "all":
single_build = False single_build = False
@ -758,7 +785,7 @@ def main(args):
tags=args.build_tags, tags=args.build_tags,
static=args.static): static=args.static):
return 1 return 1
build_output.get(platform).update( { arch : od } ) build_output.get(platform).update({arch: od})
# Build packages # Build packages
if args.package: if args.package:
@ -774,7 +801,7 @@ def main(args):
release=args.release) release=args.release)
if args.sign: if args.sign:
logging.debug("Generating GPG signatures for packages: {}".format(packages)) logging.debug("Generating GPG signatures for packages: {}".format(packages))
sigs = [] # retain signatures so they can be uploaded with packages sigs = [] # retain signatures so they can be uploaded with packages
for p in packages: for p in packages:
if generate_sig_from_file(p): if generate_sig_from_file(p):
sigs.append(p + '.asc') sigs.append(p + '.asc')
@ -799,6 +826,7 @@ def main(args):
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
LOG_LEVEL = logging.INFO LOG_LEVEL = logging.INFO
if '--debug' in sys.argv[1:]: if '--debug' in sys.argv[1:]:
@ -808,7 +836,7 @@ if __name__ == '__main__':
format=log_format) format=log_format)
parser = argparse.ArgumentParser(description='InfluxDB build and packaging script.') parser = argparse.ArgumentParser(description='InfluxDB build and packaging script.')
parser.add_argument('--verbose','-v','--debug', parser.add_argument('--verbose', '-v', '--debug',
action='store_true', action='store_true',
help='Use debug output') help='Use debug output')
parser.add_argument('--outdir', '-o', parser.add_argument('--outdir', '-o',
@ -886,7 +914,7 @@ if __name__ == '__main__':
parser.add_argument('--upload', parser.add_argument('--upload',
action='store_true', action='store_true',
help='Upload output packages to AWS S3') help='Upload output packages to AWS S3')
parser.add_argument('--upload-overwrite','-w', parser.add_argument('--upload-overwrite', '-w',
action='store_true', action='store_true',
help='Upload output packages to AWS S3') help='Upload output packages to AWS S3')
parser.add_argument('--bucket', parser.add_argument('--bucket',

View File

@ -43,6 +43,11 @@ if [[ ! -d /etc/telegraf/telegraf.d ]]; then
mkdir -p /etc/telegraf/telegraf.d mkdir -p /etc/telegraf/telegraf.d
fi fi
# If 'telegraf.conf' is not present use package's sample (fresh install)
if [[ ! -f /etc/telegraf/telegraf.conf ]] && [[ -f /etc/telegraf/telegraf.conf.sample ]]; then
cp /etc/telegraf/telegraf.conf.sample /etc/telegraf/telegraf.conf
fi
# Distribution-specific logic # Distribution-specific logic
if [[ -f /etc/redhat-release ]] || [[ -f /etc/SuSE-release ]]; then if [[ -f /etc/redhat-release ]] || [[ -f /etc/SuSE-release ]]; then
# RHEL-variant logic # RHEL-variant logic