Commit f11827ae authored by Joshua Harlow's avatar Joshua Harlow

Fix up a ton of flake8 issues

parent 690bdce9
......@@ -207,20 +207,20 @@ def add_sources(srclist, template_params=None, aa_repo_match=None):
ks = ent['keyserver']
try:
ent['key'] = getkeybyid(ent['keyid'], ks)
except:
except Exception:
errorlist.append([source, "failed to get key from %s" % ks])
continue
if 'key' in ent:
try:
util.subp(('apt-key', 'add', '-'), ent['key'])
except:
except Exception:
errorlist.append([source, "failed add key"])
try:
contents = "%s\n" % (source)
util.write_file(ent['filename'], contents, omode="ab")
except:
except Exception:
errorlist.append([source,
"failed write to file %s" % ent['filename']])
......
......@@ -38,7 +38,7 @@ def handle(name, cfg, cloud, log, _args):
content = util.shellify(cfg["bootcmd"])
tmpf.write(util.encode_text(content))
tmpf.flush()
except:
except Exception:
util.logexc(log, "Failed to shellify bootcmd")
raise
......@@ -49,6 +49,6 @@ def handle(name, cfg, cloud, log, _args):
env['INSTANCE_ID'] = str(iid)
cmd = ['/bin/sh', tmpf.name]
util.subp(cmd, env=env, capture=False)
except:
except Exception:
util.logexc(log, "Failed to run bootcmd module %s", name)
raise
......@@ -198,7 +198,7 @@ def is_device_valid(name, partition=False):
d_type = ""
try:
d_type = device_type(name)
except:
except Exception:
LOG.warn("Query against device %s failed" % name)
return False
......
......@@ -20,8 +20,8 @@
import os
from cloudinit.settings import PER_ALWAYS
from cloudinit import log as logging
from cloudinit.settings import PER_ALWAYS
from cloudinit import util
frequency = PER_ALWAYS
......
......@@ -37,8 +37,8 @@ If cloud-init sees a 'fan' entry in cloud-config it will
"""
from cloudinit import log as logging
from cloudinit import util
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
LOG = logging.getLogger(__name__)
......
......@@ -66,7 +66,7 @@ def handle(_name, cfg, cloud, log, args):
try:
contents = "%s - %s - v. %s\n" % (uptime, ts, cver)
util.write_file(boot_fin_fn, contents)
except:
except Exception:
util.logexc(log, "Failed to write boot finished file %s", boot_fin_fn)
if cloud.datasource.is_disconnected:
......
......@@ -69,5 +69,5 @@ def handle(name, cfg, _cloud, log, _args):
try:
util.subp(['debconf-set-selections'], dconf_sel)
except:
except Exception:
util.logexc(log, "Failed to run debconf-set-selections for grub-dpkg")
......@@ -57,6 +57,6 @@ def handle(name, cfg, cloud, log, _args):
(stdout, _stderr) = util.subp(cmd)
util.multi_log("%s\n" % (stdout.strip()),
stderr=False, console=True)
except:
except Exception:
log.warn("Writing keys to the system console failed!")
raise
......@@ -111,7 +111,7 @@ def handle(name, cfg, cloud, log, args):
data = "\n".join(["set %s %s" % (k, v)
for k, v in debconf.items()]) + "\n"
util.subp(['debconf-communicate'], data)
except:
except Exception:
util.logexc(log, "Failed to run '%s' for lxd with" % dconf_comm)
# Remove the existing configuration file (forces re-generation)
......
......@@ -244,7 +244,7 @@ def handle_swapcfg(swapcfg):
LOG.debug("swap file %s already in use.", fname)
return fname
LOG.debug("swap file %s existed, but not in /proc/swaps", fname)
except:
except Exception:
LOG.warn("swap file %s existed. Error reading /proc/swaps", fname)
return fname
......@@ -379,7 +379,7 @@ def handle(_name, cfg, cloud, log, _args):
toks = WS.split(line)
if toks[3].find(comment) != -1:
continue
except:
except Exception:
pass
fstab_lines.append(line)
......@@ -390,16 +390,16 @@ def handle(_name, cfg, cloud, log, _args):
if needswap:
try:
util.subp(("swapon", "-a"))
except:
except Exception:
util.logexc(log, "Activating swap via 'swapon -a' failed")
for d in dirs:
try:
util.ensure_dir(d)
except:
except Exception:
util.logexc(log, "Failed to make '%s' config-mount", d)
try:
util.subp(("mount", "-a"))
except:
except Exception:
util.logexc(log, "Activating mounts via 'mount -a' failed")
......@@ -65,7 +65,7 @@ def handle(name, cfg, cloud, log, args):
tries = ph_cfg.get('tries')
try:
tries = int(tries)
except:
except Exception:
tries = 10
util.logexc(log, "Configuration entry 'tries' is not an integer, "
"using %s instead", tries)
......@@ -87,7 +87,7 @@ def handle(name, cfg, cloud, log, args):
for (n, path) in pubkeys.items():
try:
all_keys[n] = util.load_file(path)
except:
except Exception:
util.logexc(log, "%s: failed to open, can not phone home that "
"data!", path)
......@@ -117,6 +117,6 @@ def handle(name, cfg, cloud, log, args):
util.read_file_or_url(url, data=real_submit_keys,
retries=tries, sec_between=3,
ssl_details=util.fetch_ssl_details(cloud.paths))
except:
except Exception:
util.logexc(log, "Failed to post phone home data to %s in %s tries",
url, tries)
......@@ -52,7 +52,7 @@ MY_HOOKNAME = 'CLOUD_INIT_REMOTE_HOOK'
def handle(name, _cfg, cloud, log, _args):
try:
ud = cloud.get_userdata_raw()
except:
except Exception:
log.debug("Failed to get raw userdata in module %s", name)
return
......@@ -63,7 +63,7 @@ def handle(name, _cfg, cloud, log, _args):
"did not find %s in parsed"
" raw userdata"), name, MY_HOOKNAME)
return
except:
except Exception:
util.logexc(log, "Failed to parse query string %s into a dictionary",
ud)
raise
......
......@@ -34,5 +34,5 @@ def handle(name, cfg, cloud, log, _args):
try:
content = util.shellify(cmd)
util.write_file(out_fn, content, 0o700)
except:
except Exception:
util.logexc(log, "Failed to shellify %s into file %s", cmd, out_fn)
......@@ -35,7 +35,7 @@ def handle(name, _cfg, cloud, log, _args):
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try:
util.runparts(runparts_path)
except:
except Exception:
log.warn("Failed to run module %s (%s in %s)",
name, SCRIPT_SUBDIR, runparts_path)
raise
......@@ -35,7 +35,7 @@ def handle(name, _cfg, cloud, log, _args):
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try:
util.runparts(runparts_path)
except:
except Exception:
log.warn("Failed to run module %s (%s in %s)",
name, SCRIPT_SUBDIR, runparts_path)
raise
......@@ -35,7 +35,7 @@ def handle(name, _cfg, cloud, log, _args):
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try:
util.runparts(runparts_path)
except:
except Exception:
log.warn("Failed to run module %s (%s in %s)",
name, SCRIPT_SUBDIR, runparts_path)
raise
......@@ -36,7 +36,7 @@ def handle(name, _cfg, cloud, log, _args):
runparts_path = os.path.join(cloud.get_ipath_cur(), SCRIPT_SUBDIR)
try:
util.runparts(runparts_path)
except:
except Exception:
log.warn("Failed to run module %s (%s in %s)",
name, SCRIPT_SUBDIR, runparts_path)
raise
......@@ -37,7 +37,7 @@ def handle(name, cfg, cloud, log, _args):
try:
util.runparts(runparts_path, exe_prefix=prefix)
except:
except Exception:
log.warn("Failed to run module %s (%s in %s)",
name, SCRIPT_SUBDIR, runparts_path)
raise
......@@ -24,8 +24,8 @@ import os
from six import BytesIO
from cloudinit.settings import PER_INSTANCE
from cloudinit import log as logging
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
frequency = PER_INSTANCE
......
......@@ -155,7 +155,7 @@ def handle(_name, cfg, cloud, log, args):
cmd = filter(None, cmd) # Remove empty arguments
util.subp(cmd)
log.debug("Restarted the ssh daemon")
except:
except Exception:
util.logexc(log, "Restarting of the ssh daemon failed")
if len(errors):
......
......@@ -47,12 +47,12 @@ Example config:
"""
from cloudinit import log as logging
from cloudinit import util
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
import glob
import tempfile
import os
import tempfile
LOG = logging.getLogger(__name__)
......
......@@ -57,7 +57,7 @@ def handle(_name, cfg, cloud, log, _args):
for f in glob.glob(key_pth):
try:
util.del_file(f)
except:
except Exception:
util.logexc(log, "Failed deleting key file %s", f)
if "ssh_keys" in cfg:
......@@ -78,7 +78,7 @@ def handle(_name, cfg, cloud, log, _args):
with util.SeLinuxGuard("/etc/ssh", recursive=True):
util.subp(cmd, capture=False)
log.debug("Generated a key for %s from %s", pair[0], pair[1])
except:
except Exception:
util.logexc(log, "Failed generated a key for %s from %s",
pair[0], pair[1])
else:
......@@ -122,7 +122,7 @@ def handle(_name, cfg, cloud, log, _args):
keys.extend(cfgkeys)
apply_credentials(keys, user, disable_root, disable_root_opts)
except:
except Exception:
util.logexc(log, "Applying ssh credentials failed!")
......
......@@ -52,14 +52,14 @@ def handle(_name, cfg, cloud, log, args):
else:
try:
import_ids = user_cfg['ssh_import_id']
except:
except Exception:
log.debug("User %s is not configured for ssh_import_id", user)
continue
try:
import_ids = util.uniq_merge(import_ids)
import_ids = [str(i) for i in import_ids]
except:
except Exception:
log.debug("User %s is not correctly configured for ssh_import_id",
user)
continue
......
......@@ -40,10 +40,10 @@ It can be configured with the following option structure::
mechanism you've used to switch the init system.
"""
from cloudinit.settings import PER_INSTANCE
from cloudinit.distros import ubuntu
from cloudinit import log as logging
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
from cloudinit.distros import ubuntu
import os
import time
......
......@@ -50,8 +50,8 @@ OSFAMILIES = {
LOG = logging.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta)
class Distro(object):
__metaclass__ = abc.ABCMeta
usr_lib_exec = "/usr/lib"
hosts_fn = "/etc/hosts"
......@@ -97,7 +97,7 @@ class Distro(object):
try:
res = os.lstat('/run/systemd/system')
return stat.S_ISDIR(res.st_mode)
except:
except Exception:
return False
@abc.abstractmethod
......
......@@ -25,8 +25,8 @@ import os
from cloudinit import distros
from cloudinit import helpers
from cloudinit import log as logging
from cloudinit import util
from cloudinit import net
from cloudinit import util
from cloudinit.distros.parsers.hostname import HostnameConf
......@@ -221,7 +221,7 @@ def _maybe_remove_legacy_eth0(path="/etc/network/interfaces.d/eth0.cfg"):
msg = "removed %s with known contents" % path
else:
msg = (bmsg + " '%s' exists with user configured content." % path)
except:
except Exception:
msg = bmsg + " %s exists, but could not be read." % path
LOG.warn(msg)
......@@ -71,8 +71,8 @@ INCLUSION_SRCH = sorted(list(INCLUSION_TYPES_MAP.keys()),
key=(lambda e: 0 - len(e)))
@six.add_metaclass(abc.ABCMeta)
class Handler(object):
__metaclass__ = abc.ABCMeta
def __init__(self, frequency, version=2):
self.handler_version = version
......@@ -118,7 +118,7 @@ def run_part(mod, data, filename, payload, frequency, headers):
mod.handle_part(data, content_type, filename, payload)
else:
raise ValueError("Unknown module version %s" % (mod_ver))
except:
except Exception:
util.logexc(LOG, "Failed calling handler %s (%s, %s, %s) with "
"frequency %s", mod, content_type, filename, mod_ver,
frequency)
......@@ -157,7 +157,7 @@ def walker_handle_handler(pdata, _ctype, _filename, payload):
# register if it fails starting.
handlers.register(mod, initialized=True)
pdata['handlercount'] = curcount + 1
except:
except Exception:
util.logexc(LOG, "Failed at registering python file: %s (part "
"handler %s)", modfname, curcount)
......
......@@ -158,6 +158,6 @@ class CloudConfigPartHandler(handlers.Handler):
for i in ("\n", "\r", "\t"):
filename = filename.replace(i, " ")
self.file_names.append(filename.strip())
except:
except Exception:
util.logexc(LOG, "Failed at merging in cloud config part from %s",
filename)
......@@ -80,7 +80,7 @@ def _has_suitable_upstart():
return False
try:
(version_out, _err) = util.subp(["initctl", "version"])
except:
except Exception:
util.logexc(LOG, "initctl version failed")
return False
......
......@@ -86,7 +86,7 @@ class FileSemaphores(object):
name = canon_sem_name(name)
try:
yield self._acquire(name, freq)
except:
except Exception:
if clear_on_fail:
self.clear(name, freq)
raise
......@@ -219,7 +219,7 @@ class ConfigMerger(object):
ds_cfg = self._ds.get_config_obj()
if ds_cfg and isinstance(ds_cfg, (dict)):
d_cfgs.append(ds_cfg)
except:
except Exception:
util.logexc(LOG, "Failed loading of datasource config object "
"from %s", self._ds)
return d_cfgs
......@@ -230,7 +230,7 @@ class ConfigMerger(object):
e_fn = os.environ[CFG_ENV_NAME]
try:
e_cfgs.append(util.read_conf(e_fn))
except:
except Exception:
util.logexc(LOG, 'Failed loading of env. config from %s',
e_fn)
return e_cfgs
......@@ -251,7 +251,7 @@ class ConfigMerger(object):
if cc_fn and os.path.isfile(cc_fn):
try:
i_cfgs.append(util.read_conf(cc_fn))
except:
except Exception:
util.logexc(LOG, 'Failed loading of cloud-config from %s',
cc_fn)
return i_cfgs
......@@ -268,7 +268,7 @@ class ConfigMerger(object):
for c_fn in self._fns:
try:
cfgs.append(util.read_conf(c_fn))
except:
except Exception:
util.logexc(LOG, "Failed loading of configuration from %s",
c_fn)
......
......@@ -26,9 +26,9 @@ import re
import shlex
from cloudinit import log as logging
from cloudinit.net import network_state
from cloudinit.net.udev import generate_udev_rule
from cloudinit import util
from .udev import generate_udev_rule
from . import network_state
LOG = logging.getLogger(__name__)
......@@ -40,16 +40,16 @@ NET_CONFIG_OPTIONS = [
"pointtopoint", "media", "mtu", "hostname", "leasehours", "leasetime",
"vendor", "client", "bootfile", "server", "hwaddr", "provider", "frame",
"netnum", "endpoint", "local", "ttl",
]
]
NET_CONFIG_COMMANDS = [
"pre-up", "up", "post-up", "down", "pre-down", "post-down",
]
]
NET_CONFIG_BRIDGE_OPTIONS = [
"bridge_ageing", "bridge_bridgeprio", "bridge_fd", "bridge_gcinit",
"bridge_hello", "bridge_maxage", "bridge_maxwait", "bridge_stp",
]
]
DEFAULT_PRIMARY_INTERFACE = 'eth0'
......@@ -399,9 +399,7 @@ def config_from_klibc_net_cfg(files=None, mac_addrs=None):
def render_persistent_net(network_state):
''' Given state, emit udev rules to map
mac to ifname
'''
'''Given state, emit udev rules to map mac to ifname.'''
content = ""
interfaces = network_state.get('interfaces')
for iface in interfaces.values():
......@@ -465,7 +463,7 @@ def iface_add_attrs(iface):
def render_route(route, indent=""):
""" When rendering routes for an iface, in some cases applying a route
"""When rendering routes for an iface, in some cases applying a route
may result in the route command returning non-zero which produces
some confusing output for users manually using ifup/ifdown[1]. To
that end, we will optionally include an '|| true' postfix to each
......@@ -530,7 +528,7 @@ def iface_start_entry(iface, index):
def render_interfaces(network_state):
''' Given state, emit etc/network/interfaces content '''
'''Given state, emit etc/network/interfaces content.'''
content = ""
interfaces = network_state.get('interfaces')
......
......@@ -36,7 +36,7 @@ def from_state_file(state_file):
return network_state
class NetworkState:
class NetworkState(object):
def __init__(self, version=NETWORK_STATE_VERSION, config=None):
self.version = version
self.config = config
......@@ -53,7 +53,7 @@ class NetworkState:
def get_command_handlers(self):
METHOD_PREFIX = 'handle_'
methods = filter(lambda x: callable(getattr(self, x)) and
x.startswith(METHOD_PREFIX), dir(self))
x.startswith(METHOD_PREFIX), dir(self))
handlers = {}
for m in methods:
key = m.replace(METHOD_PREFIX, '')
......@@ -379,8 +379,9 @@ def mask2cidr(mask):
if __name__ == '__main__':
import sys
import random
import sys
from cloudinit import net
def load_config(nc):
......
......@@ -48,7 +48,7 @@ def generate_udev_rule(interface, mac):
compose_udev_equality('DRIVERS', '?*'),
compose_udev_attr_equality('address', mac),
compose_udev_setting('NAME', interface),
])
])
return '%s\n' % rule
# vi: ts=4 expandtab syntax=python
......@@ -20,10 +20,11 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cloudinit.util as util
from cloudinit.log import logging
import re
from cloudinit import logging
from cloudinit import util
from prettytable import PrettyTable
LOG = logging.getLogger()
......@@ -163,7 +164,7 @@ def route_info():
def getgateway():
try:
routes = route_info()
except:
except Exception:
pass
else:
for r in routes.get('ipv4', []):
......
......@@ -4,9 +4,9 @@ import abc
import json
import six
from ..registry import DictRegistry
from .. import (url_helper, util)
from .. import log as logging
from cloudinit import log as logging
from cloudinit.registry import DictRegistry
from cloudinit import (url_helper, util)
LOG = logging.getLogger(__name__)
......@@ -36,7 +36,7 @@ class LogHandler(ReportingHandler):
input_level = level
try:
level = getattr(logging, level.upper())
except:
except Exception:
LOG.warn("invalid level '%s', using WARN", input_level)
level = logging.WARN
self.level = level
......@@ -81,7 +81,7 @@ class WebHookHandler(ReportingHandler):
self.endpoint, data=json.dumps(event.as_dict()),
timeout=self.timeout,
retries=self.retries, ssl_details=self.ssl_details)
except:
except Exception:
LOG.warn("failed posting event: %s" % event.as_string())
......
......@@ -27,11 +27,12 @@ import xml.etree.ElementTree as ET
from xml.dom import minidom
from cloudinit.sources.helpers.azure import get_metadata_from_fabric
from cloudinit import log as logging
from cloudinit.settings import PER_ALWAYS
from cloudinit import sources
from cloudinit import util
from cloudinit.sources.helpers.azure import get_metadata_from_fabric
LOG = logging.getLogger(__name__)
......@@ -40,7 +41,8 @@ DEFAULT_METADATA = {"instance-id": "iid-AZURE-NODE"}
AGENT_START = ['service', 'walinuxagent', 'start']
BOUNCE_COMMAND = [
'sh', '-xc',
"i=$interface; x=0; ifdown $i || x=$?; ifup $i || x=$?; exit $x"]
"i=$interface; x=0; ifdown $i || x=$?; ifup $i || x=$?; exit $x"
]
BUILTIN_DS_CONFIG = {
'agent_command': AGENT_START,
......@@ -51,7 +53,7 @@ BUILTIN_DS_CONFIG = {
'policy': True,
'command': BOUNCE_COMMAND,
'hostname_command': 'hostname',
},
},
'disk_aliases': {'ephemeral0': '/dev/sdb'},
}
......@@ -60,7 +62,7 @@ BUILTIN_CLOUD_CONFIG = {
'ephemeral0': {'table_type': 'gpt',
'layout': [100],
'overwrite': True},
},
},
'fs_setup': [{'filesystem': 'ext4',
'device': 'ephemeral0.1',
'replace_fs': 'ntfs'}],
......@@ -312,7 +314,7 @@ def support_new_ephemeral(cfg):
file_count = 0
try:
file_count = util.mount_cb(device, count_files)
except:
except Exception:
return None
LOG.debug("fabric prepared ephmeral0.1 has %s files on it", file_count)
......
......@@ -4,13 +4,13 @@
# Author: Alexandru Sirbu <alexandru.sirbu@bigstep.com>
#
import json
import errno
import json
from cloudinit import log as logging
from cloudinit import sources
from cloudinit import util
from cloudinit import url_helper
from cloudinit import util
LOG = logging.getLogger(__name__)
......
......@@ -19,10 +19,11 @@ from base64 import b64decode
import os
import re
from cloudinit.cs_utils import Cepko
from cloudinit import log as logging
from cloudinit import sources
from cloudinit import util
from cloudinit.cs_utils import Cepko
LOG = logging.getLogger(__name__)
......@@ -77,7 +78,7 @@ class DataSourceCloudSigma(sources.DataSource):
try:
server_context = self.cepko.all().result
server_meta = server_context['meta']
except:
except Exception:
# TODO: check for explicit "config on", and then warn
# but since no explicit config is available now, just debug.
LOG.debug("CloudSigma: Unable to read from serial port")
......
......@@ -25,14 +25,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import time