config: refactor config get default
This change adds a new get_default library procedure to simplify getting default value of config object. Change-Id: I0546b1175b259472a10690273af611ef4bad5a99
This commit is contained in:
21
zuul/cmd/client.py
Normal file → Executable file
21
zuul/cmd/client.py
Normal file → Executable file
@@ -25,6 +25,7 @@ import time
|
||||
|
||||
import zuul.rpcclient
|
||||
import zuul.cmd
|
||||
from zuul.lib.config import get_default
|
||||
|
||||
|
||||
class Client(zuul.cmd.ZuulApp):
|
||||
@@ -122,22 +123,10 @@ class Client(zuul.cmd.ZuulApp):
|
||||
self.setup_logging()
|
||||
|
||||
self.server = self.config.get('gearman', 'server')
|
||||
if self.config.has_option('gearman', 'port'):
|
||||
self.port = self.config.get('gearman', 'port')
|
||||
else:
|
||||
self.port = 4730
|
||||
if self.config.has_option('gearman', 'ssl_key'):
|
||||
self.ssl_key = self.config.get('gearman', 'ssl_key')
|
||||
else:
|
||||
self.ssl_key = None
|
||||
if self.config.has_option('gearman', 'ssl_cert'):
|
||||
self.ssl_cert = self.config.get('gearman', 'ssl_cert')
|
||||
else:
|
||||
self.ssl_cert = None
|
||||
if self.config.has_option('gearman', 'ssl_ca'):
|
||||
self.ssl_ca = self.config.get('gearman', 'ssl_ca')
|
||||
else:
|
||||
self.ssl_ca = None
|
||||
self.port = get_default(self.config, 'gearman', 'port', 4730)
|
||||
self.ssl_key = get_default(self.config, 'gearman', 'ssl_key')
|
||||
self.ssl_cert = get_default(self.config, 'gearman', 'ssl_cert')
|
||||
self.ssl_ca = get_default(self.config, 'gearman', 'ssl_ca')
|
||||
|
||||
if self.args.func():
|
||||
sys.exit(0)
|
||||
|
||||
@@ -32,6 +32,7 @@ import tempfile
|
||||
|
||||
import zuul.cmd
|
||||
import zuul.executor.server
|
||||
from zuul.lib.config import get_default
|
||||
|
||||
# No zuul imports that pull in paramiko here; it must not be
|
||||
# imported until after the daemonization.
|
||||
@@ -63,11 +64,8 @@ class Executor(zuul.cmd.ZuulApp):
|
||||
self.args = parser.parse_args()
|
||||
|
||||
def send_command(self, cmd):
|
||||
if self.config.has_option('zuul', 'state_dir'):
|
||||
state_dir = os.path.expanduser(
|
||||
self.config.get('zuul', 'state_dir'))
|
||||
else:
|
||||
state_dir = '/var/lib/zuul'
|
||||
state_dir = get_default(self.config, 'zuul', 'state_dir',
|
||||
'/var/lib/zuul', expand_user=True)
|
||||
path = os.path.join(state_dir, 'executor.socket')
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
s.connect(path)
|
||||
@@ -114,10 +112,7 @@ class Executor(zuul.cmd.ZuulApp):
|
||||
def main(self, daemon=True):
|
||||
# See comment at top of file about zuul imports
|
||||
|
||||
if self.config.has_option('executor', 'user'):
|
||||
self.user = self.config.get('executor', 'user')
|
||||
else:
|
||||
self.user = 'zuul'
|
||||
self.user = get_default(self.config, 'executor', 'user', 'zuul')
|
||||
|
||||
if self.config.has_option('zuul', 'jobroot_dir'):
|
||||
self.jobroot_dir = os.path.expanduser(
|
||||
@@ -132,10 +127,8 @@ class Executor(zuul.cmd.ZuulApp):
|
||||
self.setup_logging('executor', 'log_config')
|
||||
self.log = logging.getLogger("zuul.Executor")
|
||||
|
||||
if self.config.has_option('executor', 'finger_port'):
|
||||
self.finger_port = int(self.config.get('executor', 'finger_port'))
|
||||
else:
|
||||
self.finger_port = DEFAULT_FINGER_PORT
|
||||
self.finger_port = int(get_default(self.config, 'executor',
|
||||
'finger_port', DEFAULT_FINGER_PORT))
|
||||
|
||||
self.start_log_streamer()
|
||||
self.change_privs()
|
||||
@@ -170,10 +163,9 @@ def main():
|
||||
|
||||
server.configure_connections(source_only=True)
|
||||
|
||||
if server.config.has_option('executor', 'pidfile'):
|
||||
pid_fn = os.path.expanduser(server.config.get('executor', 'pidfile'))
|
||||
else:
|
||||
pid_fn = '/var/run/zuul-executor/zuul-executor.pid'
|
||||
pid_fn = get_default(server.config, 'executor', 'pidfile',
|
||||
'/var/run/zuul-executor/zuul-executor.pid',
|
||||
expand_user=True)
|
||||
pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10)
|
||||
|
||||
if server.args.nodaemon:
|
||||
|
||||
@@ -27,6 +27,7 @@ import sys
|
||||
import signal
|
||||
|
||||
import zuul.cmd
|
||||
from zuul.lib.config import get_default
|
||||
|
||||
# No zuul imports here because they pull in paramiko which must not be
|
||||
# imported until after the daemonization.
|
||||
@@ -79,10 +80,8 @@ def main():
|
||||
server.read_config()
|
||||
server.configure_connections(source_only=True)
|
||||
|
||||
if server.config.has_option('zuul', 'state_dir'):
|
||||
state_dir = os.path.expanduser(server.config.get('zuul', 'state_dir'))
|
||||
else:
|
||||
state_dir = '/var/lib/zuul'
|
||||
state_dir = get_default(server.config, 'zuul', 'state_dir',
|
||||
'/var/lib/zuul', expand_user=True)
|
||||
test_fn = os.path.join(state_dir, 'test')
|
||||
try:
|
||||
f = open(test_fn, 'w')
|
||||
@@ -92,10 +91,9 @@ def main():
|
||||
print("\nUnable to write to state directory: %s\n" % state_dir)
|
||||
raise
|
||||
|
||||
if server.config.has_option('merger', 'pidfile'):
|
||||
pid_fn = os.path.expanduser(server.config.get('merger', 'pidfile'))
|
||||
else:
|
||||
pid_fn = '/var/run/zuul-merger/zuul-merger.pid'
|
||||
pid_fn = get_default(server.config, 'merger', 'pidfile',
|
||||
'/var/run/zuul-merger/zuul-merger.pid',
|
||||
expand_user=True)
|
||||
pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10)
|
||||
|
||||
if server.args.nodaemon:
|
||||
|
||||
@@ -28,6 +28,7 @@ import sys
|
||||
import signal
|
||||
|
||||
import zuul.cmd
|
||||
from zuul.lib.config import get_default
|
||||
|
||||
# No zuul imports here because they pull in paramiko which must not be
|
||||
# imported until after the daemonization.
|
||||
@@ -98,22 +99,10 @@ class Scheduler(zuul.cmd.ZuulApp):
|
||||
import zuul.lib.gearserver
|
||||
statsd_host = os.environ.get('STATSD_HOST')
|
||||
statsd_port = int(os.environ.get('STATSD_PORT', 8125))
|
||||
if self.config.has_option('gearman_server', 'listen_address'):
|
||||
host = self.config.get('gearman_server', 'listen_address')
|
||||
else:
|
||||
host = None
|
||||
if self.config.has_option('gearman_server', 'ssl_key'):
|
||||
ssl_key = self.config.get('gearman_server', 'ssl_key')
|
||||
else:
|
||||
ssl_key = None
|
||||
if self.config.has_option('gearman_server', 'ssl_cert'):
|
||||
ssl_cert = self.config.get('gearman_server', 'ssl_cert')
|
||||
else:
|
||||
ssl_cert = None
|
||||
if self.config.has_option('gearman_server', 'ssl_ca'):
|
||||
ssl_ca = self.config.get('gearman_server', 'ssl_ca')
|
||||
else:
|
||||
ssl_ca = None
|
||||
host = get_default(self.config, 'gearman_server', 'listen_address')
|
||||
ssl_key = get_default(self.config, 'gearman_server', 'ssl_key')
|
||||
ssl_cert = get_default(self.config, 'gearman_server', 'ssl_cert')
|
||||
ssl_ca = get_default(self.config, 'gearman_server', 'ssl_ca')
|
||||
zuul.lib.gearserver.GearServer(4730,
|
||||
ssl_key=ssl_key,
|
||||
ssl_cert=ssl_cert,
|
||||
@@ -161,27 +150,16 @@ class Scheduler(zuul.cmd.ZuulApp):
|
||||
nodepool = zuul.nodepool.Nodepool(self.sched)
|
||||
|
||||
zookeeper = zuul.zk.ZooKeeper()
|
||||
if self.config.has_option('zuul', 'zookeeper_hosts'):
|
||||
zookeeper_hosts = self.config.get('zuul', 'zookeeper_hosts')
|
||||
else:
|
||||
zookeeper_hosts = '127.0.0.1:2181'
|
||||
zookeeper_hosts = get_default(self.config, 'zuul', 'zookeeper_hosts',
|
||||
'127.0.0.1:2181')
|
||||
|
||||
zookeeper.connect(zookeeper_hosts)
|
||||
|
||||
if self.config.has_option('zuul', 'status_expiry'):
|
||||
cache_expiry = self.config.getint('zuul', 'status_expiry')
|
||||
else:
|
||||
cache_expiry = 1
|
||||
cache_expiry = get_default(self.config, 'zuul', 'status_expiry', 1)
|
||||
|
||||
if self.config.has_option('webapp', 'listen_address'):
|
||||
listen_address = self.config.get('webapp', 'listen_address')
|
||||
else:
|
||||
listen_address = '0.0.0.0'
|
||||
|
||||
if self.config.has_option('webapp', 'port'):
|
||||
port = self.config.getint('webapp', 'port')
|
||||
else:
|
||||
port = 8001
|
||||
listen_address = get_default(self.config, 'webapp', 'listen_address',
|
||||
'0.0.0.0')
|
||||
port = get_default(self.config, 'webapp', 'port', 8001)
|
||||
|
||||
webapp = zuul.webapp.WebApp(
|
||||
self.sched, port=port, cache_expiry=cache_expiry,
|
||||
@@ -230,10 +208,9 @@ def main():
|
||||
if scheduler.args.validate:
|
||||
sys.exit(scheduler.test_config())
|
||||
|
||||
if scheduler.config.has_option('zuul', 'pidfile'):
|
||||
pid_fn = os.path.expanduser(scheduler.config.get('zuul', 'pidfile'))
|
||||
else:
|
||||
pid_fn = '/var/run/zuul-scheduler/zuul-scheduler.pid'
|
||||
pid_fn = get_default(scheduler.config, 'zuul', 'pidfile',
|
||||
'/var/run/zuul-scheduler/zuul-scheduler.pid',
|
||||
expand_user=True)
|
||||
pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10)
|
||||
|
||||
if scheduler.args.nodaemon:
|
||||
|
||||
Reference in New Issue
Block a user