py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-07-07 00:32:38 +02:00
|
|
|
import argparse
|
2017-11-06 03:10:47 +01:00
|
|
|
import configparser
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2013-11-01 00:00:30 +01:00
|
|
|
import re
|
2020-06-11 00:54:34 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-02-24 00:34:03 +01:00
|
|
|
import tempfile
|
2022-03-26 00:15:40 +01:00
|
|
|
from typing import List
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-02-24 00:34:03 +01:00
|
|
|
import yaml
|
2018-11-15 10:56:25 +01:00
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.insert(0, BASE_DIR)
|
|
|
|
|
|
|
|
from scripts.lib.puppet_cache import setup_puppet_modules
|
|
|
|
from scripts.lib.zulip_tools import assert_running_as_root, parse_os_release
|
2021-03-09 07:57:26 +01:00
|
|
|
|
2018-11-19 19:50:25 +01:00
|
|
|
assert_running_as_root()
|
2013-10-07 22:08:16 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Run Puppet")
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--force", "-f", action="store_true", help="Do not prompt with proposed changes"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--noop", action="store_true", help="Do not apply the changes")
|
|
|
|
parser.add_argument("--config", default="/etc/zulip/zulip.conf", help="Alternate zulip.conf path")
|
2020-07-07 00:32:38 +02:00
|
|
|
args, extra_args = parser.parse_known_args()
|
2013-10-07 22:08:16 +02:00
|
|
|
|
2016-07-23 20:33:58 +02:00
|
|
|
config = configparser.RawConfigParser()
|
2020-07-07 01:15:15 +02:00
|
|
|
config.read(args.config)
|
2013-10-07 22:08:16 +02:00
|
|
|
|
2021-05-25 03:05:40 +02:00
|
|
|
setup_puppet_modules()
|
|
|
|
|
2020-05-20 22:42:10 +02:00
|
|
|
distro_info = parse_os_release()
|
2013-11-01 00:00:30 +01:00
|
|
|
puppet_config = """
|
|
|
|
Exec { path => "/usr/sbin:/usr/bin:/sbin:/bin" }
|
|
|
|
"""
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for pclass in re.split(r"\s*,\s*", config.get("machine", "puppet_classes")):
|
2021-06-29 00:06:46 +02:00
|
|
|
if " " in pclass:
|
|
|
|
print(
|
|
|
|
f"The `machine.puppet_classes` setting in {args.config} must be comma-separated, not space-separated!"
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
2020-06-10 06:41:04 +02:00
|
|
|
puppet_config += f"include {pclass}\n"
|
2013-11-01 00:00:30 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# We use the Puppet configuration from the same Zulip checkout as this script
|
2018-12-06 20:37:46 +01:00
|
|
|
scripts_path = os.path.join(BASE_DIR, "scripts")
|
|
|
|
puppet_module_path = os.path.join(BASE_DIR, "puppet")
|
2021-05-25 03:05:40 +02:00
|
|
|
puppet_cmd = [
|
|
|
|
"puppet",
|
|
|
|
"apply",
|
|
|
|
f"--modulepath={puppet_module_path}:/srv/zulip-puppet-cache/current",
|
|
|
|
"-e",
|
|
|
|
puppet_config,
|
|
|
|
]
|
2020-08-01 08:46:21 +02:00
|
|
|
if args.noop:
|
|
|
|
puppet_cmd += ["--noop"]
|
2013-10-07 22:08:16 +02:00
|
|
|
puppet_cmd += extra_args
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Set the scripts path to be a factor so it can be used by Puppet code
|
2018-12-06 20:37:46 +01:00
|
|
|
puppet_env = os.environ.copy()
|
2020-07-07 01:15:15 +02:00
|
|
|
puppet_env["FACTER_zulip_conf_path"] = args.config
|
2018-12-06 20:37:46 +01:00
|
|
|
puppet_env["FACTER_zulip_scripts_path"] = scripts_path
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# This is to suppress Puppet warnings with ruby 2.7.
|
2021-02-12 08:20:45 +01:00
|
|
|
if (distro_info["ID"], distro_info["VERSION_ID"]) in [("ubuntu", "20.04")]:
|
2020-05-20 22:42:10 +02:00
|
|
|
puppet_env["RUBYOPT"] = "-W0"
|
|
|
|
|
2022-03-26 00:15:40 +01:00
|
|
|
|
|
|
|
def noop_would_change(puppet_cmd: List[str]) -> bool:
|
|
|
|
# --noop does not work with --detailed-exitcodes; see
|
|
|
|
# https://tickets.puppetlabs.com/browse/PUP-686
|
2021-02-24 00:34:03 +01:00
|
|
|
try:
|
|
|
|
lastrun_file = tempfile.NamedTemporaryFile()
|
|
|
|
subprocess.check_call(
|
2022-03-26 00:15:40 +01:00
|
|
|
# puppet_cmd may already contain --noop, but it is safe to
|
|
|
|
# supply twice
|
|
|
|
[*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name],
|
2021-02-24 00:34:03 +01:00
|
|
|
env=puppet_env,
|
|
|
|
)
|
|
|
|
|
2021-08-02 23:19:49 +02:00
|
|
|
with open(lastrun_file.name) as lastrun:
|
2021-02-24 00:34:03 +01:00
|
|
|
lastrun_data = yaml.safe_load(lastrun)
|
2022-03-26 00:15:40 +01:00
|
|
|
return lastrun_data.get("resources", {}).get("out_of_sync", 0) != 0
|
2021-02-24 00:34:03 +01:00
|
|
|
finally:
|
|
|
|
lastrun_file.close()
|
2016-07-01 19:15:46 +02:00
|
|
|
|
2022-03-26 00:15:40 +01:00
|
|
|
|
|
|
|
if not args.noop and not args.force:
|
|
|
|
if not noop_would_change([*puppet_cmd, "--show_diff"]):
|
|
|
|
sys.exit(0)
|
|
|
|
|
2016-07-01 19:15:46 +02:00
|
|
|
do_apply = None
|
2021-02-12 08:20:45 +01:00
|
|
|
while do_apply != "y":
|
2016-07-01 19:15:46 +02:00
|
|
|
sys.stdout.write("Apply changes? [y/N] ")
|
2017-08-28 03:36:39 +02:00
|
|
|
sys.stdout.flush()
|
2016-07-01 19:15:46 +02:00
|
|
|
do_apply = sys.stdin.readline().strip().lower()
|
2021-02-12 08:20:45 +01:00
|
|
|
if do_apply == "" or do_apply == "n":
|
2016-07-01 19:15:46 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2022-03-26 00:16:01 +01:00
|
|
|
if args.noop and args.force:
|
|
|
|
if noop_would_change(puppet_cmd):
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
ret = subprocess.call([*puppet_cmd, "--detailed-exitcodes"], env=puppet_env)
|
2016-07-01 19:15:46 +02:00
|
|
|
# ret = 0 => no changes, no errors
|
|
|
|
# ret = 2 => changes, no errors
|
|
|
|
# ret = 4 => no changes, yes errors
|
|
|
|
# ret = 6 => changes, yes errors
|
|
|
|
if ret != 0 and ret != 2:
|
|
|
|
sys.exit(1)
|