Merge pull request #9 from gardlt/bug/pep8/fixing-small-lint-issues

[bug] fix-pep8-linting-issues
This commit is contained in:
Brandon B. Jozsa 2017-03-05 17:10:00 -05:00 committed by GitHub
commit 8d054558ab
7 changed files with 106 additions and 79 deletions

View File

@ -4,7 +4,6 @@ sudo: required
python: python:
- "2.7" - "2.7"
- "3.6"
before_install: before_install:
- export LIGGIT2_VERSION=0.24.0 - export LIGGIT2_VERSION=0.24.0
@ -19,10 +18,11 @@ before_install:
- cd .. - cd ..
- pip install -r test-requirements.txt - pip install -r test-requirements.txt
- pip install -r requirements.txt - pip install -r requirements.txt
# - flake8
install: install:
- pip install . - pip install .
script: script:
- armada -c examples/armada.yaml - flake8
- armada -h
# - armada -c examples/armada.yaml -d

View File

@ -24,7 +24,6 @@ class Armada(object):
self.tiller = Tiller() self.tiller = Tiller()
def find_release_chart(self, known_releases, name): def find_release_chart(self, known_releases, name):
''' '''
Find a release given a list of known_releases and a release name Find a release given a list of known_releases and a release name
@ -33,7 +32,6 @@ class Armada(object):
if chart_name == name: if chart_name == name:
return chart, values return chart, values
def sync(self): def sync(self):
''' '''
Syncronize Helm with the Armada Config(s) Syncronize Helm with the Armada Config(s)
@ -42,7 +40,8 @@ class Armada(object):
# extract known charts on tiller right now # extract known charts on tiller right now
known_releases = self.tiller.list_charts() known_releases = self.tiller.list_charts()
for release in known_releases: for release in known_releases:
LOG.debug("Release {}, Version {} found on tiller".format(release[0], release[1])) LOG.debug("Release %s, Version %s found on tiller", release[0],
release[1])
for entry in self.config['armada']['charts']: for entry in self.config['armada']['charts']:
@ -52,8 +51,7 @@ class Armada(object):
if chart.release_name is None: if chart.release_name is None:
continue continue
# initialize helm chart and request a
# initialize helm chart and request a
# protoc helm chart object which will # protoc helm chart object which will
# pull the sources down and walk the # pull the sources down and walk the
# dependencies # dependencies
@ -64,43 +62,61 @@ class Armada(object):
if chart.release_name in [x[0] for x in known_releases]: if chart.release_name in [x[0] for x in known_releases]:
# indicate to the end user what path we are taking # indicate to the end user what path we are taking
LOG.info("Upgrading release {}".format(chart.release_name)) LOG.info("Upgrading release %s", chart.release_name)
# extract the installed chart and installed values from the latest release so we can compare to the intended state # extract the installed chart and installed values from the
installed_chart, installed_values = self.find_release_chart(known_releases, chart.release_name) # latest release so we can compare to the intended state
installed_chart, installed_values = self.find_release_chart(
known_releases, chart.release_name)
# show delta for both the chart templates and the chart values # show delta for both the chart templates and the chart values
# TODO(alanmeadows) account for .files differences once we support those # TODO(alanmeadows) account for .files differences
self.show_diff(chart, installed_chart, installed_values, chartbuilder.dump(), values) # once we support those
self.show_diff(chart, installed_chart,
installed_values, chartbuilder.dump(), values)
# do actual update # do actual update
self.tiller.update_release(protoc_chart, self.args.dry_run, chart.release_name, disable_hooks=chart.upgrade.no_hooks, values=yaml.safe_dump(values)) self.tiller.update_release(protoc_chart, self.args.dry_run,
chart.release_name,
disable_hooks=chart.
upgrade.no_hooks,
values=yaml.safe_dump(values))
# process install # process install
else: else:
LOG.info("Installing release {}".format(chart.release_name)) LOG.info("Installing release %s", chart.release_name)
self.tiller.install_release(protoc_chart, self.args.dry_run, chart.release_name, chart.namespace, values=yaml.safe_dump(values)) self.tiller.install_release(protoc_chart,
self.args.dry_run,
chart.release_name,
chart.namespace,
values=yaml.safe_dump(values))
LOG.debug("Cleaning up chart source in %s",
LOG.debug("Cleaning up chart source in {}".format(chartbuilder.source_directory)) chartbuilder.source_directory)
chartbuilder.source_cleanup() chartbuilder.source_cleanup()
def show_diff(self, chart, installed_chart, installed_values, target_chart, target_values): def show_diff(self, chart, installed_chart,
installed_values, target_chart, target_values):
''' '''
Produce a unified diff of the installed chart vs our intention Produce a unified diff of the installed chart vs our intention
TODO(alanmeadows): This needs to be rewritten to produce better TODO(alanmeadows): This needs to be rewritten to produce better
unified diff output and avoid the use of print unified diff output and avoid the use of print
''' '''
chart_diff = list(difflib.unified_diff(installed_chart.SerializeToString().split('\n'), target_chart.split('\n'))) chart_diff = list(difflib.unified_diff(installed_chart
.SerializeToString()
.split('\n'),
target_chart.split('\n')))
if len(chart_diff) > 0: if len(chart_diff) > 0:
LOG.info("Chart Unified Diff ({})".format(chart.release_name)) LOG.info("Chart Unified Diff (%s)", chart.release_name)
for line in chart_diff: for line in chart_diff:
print line print line
values_diff = list(difflib.unified_diff(installed_values.split('\n'), yaml.safe_dump(target_values).split('\n'))) values_diff = list(difflib.unified_diff(installed_values.split('\n'),
yaml
.safe_dump(target_values)
.split('\n')))
if len(values_diff) > 0: if len(values_diff) > 0:
LOG.info("Values Unified Diff ({})".format(chart.release_name)) LOG.info("Values Unified Diff (%s)", chart.release_name)
for line in values_diff: for line in values_diff:
print line print line

View File

@ -12,7 +12,7 @@ import yaml
class ChartBuilder(object): class ChartBuilder(object):
''' '''
This class handles taking chart intentions as a paramter and This class handles taking chart intentions as a paramter and
turning those into proper protoc helm charts that can be turning those into proper protoc helm charts that can be
pushed to tiller. pushed to tiller.
@ -20,7 +20,6 @@ class ChartBuilder(object):
source from external resources where necessary source from external resources where necessary
''' '''
def __init__(self, chart, parent=None): def __init__(self, chart, parent=None):
''' '''
Initialize the ChartBuilder class Initialize the ChartBuilder class
@ -55,15 +54,23 @@ class ChartBuilder(object):
tmpdir = tempfile.mkdtemp(prefix='armada', dir='/tmp') tmpdir = tempfile.mkdtemp(prefix='armada', dir='/tmp')
self._source_tmp_dir = tmpdir self._source_tmp_dir = tmpdir
if self.parent: if self.parent:
LOG.info("Cloning {}/{} as dependency for {}".format(self.chart.source.location, self.chart.source.subpath, self.parent)) LOG.info("Cloning %s/%s as dependency for %s",
self.chart.source.location,
self.chart.source.subpath,
self.parent)
else: else:
LOG.info("Cloning {}/{} for release {}".format(self.chart.source.location, self.chart.source.subpath, self.chart.release_name)) LOG.info("Cloning %s/%s for release %s",
self.chart.source.location,
self.chart.source.subpath,
self.chart.release_name)
pygit2.clone_repository(self.chart.source.location, tmpdir) pygit2.clone_repository(self.chart.source.location, tmpdir)
return os.path.join(tmpdir, self.chart.source.subpath) return os.path.join(tmpdir, self.chart.source.subpath)
else: else:
LOG.exception("Unknown source type {} for chart {}" .format(self.chart.name, self.chart.source.type)) LOG.exception("Unknown source type %s for chart %s",
self.chart.name,
self.chart.source.type)
def source_cleanup(self): def source_cleanup(self):
''' '''
@ -71,13 +78,13 @@ class ChartBuilder(object):
''' '''
shutil.rmtree(self._source_tmp_dir) shutil.rmtree(self._source_tmp_dir)
def get_metadata(self): def get_metadata(self):
''' '''
Process metadata Process metadata
''' '''
# extract Chart.yaml to construct metadata # extract Chart.yaml to construct metadata
chart_yaml = dotify(yaml.load(open(os.path.join(self.source_directory, 'Chart.yaml')).read())) chart_yaml = dotify(yaml.load(open(
os.path.join(self.source_directory, 'Chart.yaml')).read()))
# construct Metadata object # construct Metadata object
return Metadata( return Metadata(
@ -86,7 +93,6 @@ class ChartBuilder(object):
version=chart_yaml.version version=chart_yaml.version
) )
def get_files(self): def get_files(self):
''' '''
Return (non-template) files in this chart Return (non-template) files in this chart
@ -95,7 +101,6 @@ class ChartBuilder(object):
''' '''
return [] return []
def get_values(self): def get_values(self):
''' '''
Return the chart (default) values Return the chart (default) values
@ -103,14 +108,15 @@ class ChartBuilder(object):
# create config object representing unmarshaled values.yaml # create config object representing unmarshaled values.yaml
if os.path.exists(os.path.join(self.source_directory, 'values.yaml')): if os.path.exists(os.path.join(self.source_directory, 'values.yaml')):
raw_values = open(os.path.join(self.source_directory, 'values.yaml')).read() raw_values = open(os.path.join(self.source_directory,
'values.yaml')).read()
else: else:
LOG.warn("No values.yaml in {}, using empty values".format(self.source_directory)) LOG.warn("No values.yaml in %s, using empty values",
self.source_directory)
raw_values = '' raw_values = ''
return Config(raw=raw_values) return Config(raw=raw_values)
def get_templates(self): def get_templates(self):
''' '''
Return all the chart templates Return all the chart templates
@ -119,15 +125,19 @@ class ChartBuilder(object):
# process all files in templates/ as a template to attach to the chart # process all files in templates/ as a template to attach to the chart
# building a Template object # building a Template object
templates = [] templates = []
if not os.path.exists(os.path.join(self.source_directory, 'templates')): if not os.path.exists(os.path.join(self.source_directory,
LOG.warn("Chart {} has no templates directory, no templates will be deployed".format(self.chart.name)) 'templates')):
for root, dirs, files in os.walk(os.path.join(self.source_directory, 'templates'), topdown=True): LOG.warn("Chart %s has no templates directory,"
for file in files: "no templates will be deployed", self.chart.name)
templates.append(Template(name=file, data=open(os.path.join(root, file), 'r').read())) for root, _, files in os.walk(os.path.join(self.source_directory,
'templates'), topdown=True):
for tpl_file in files:
templates.append(Template(name=tpl_file,
data=open(os.path.join(root,
tpl_file),
'r').read()))
return templates return templates
def get_helm_chart(self): def get_helm_chart(self):
''' '''
Return a helm chart object Return a helm chart object
@ -137,9 +147,11 @@ class ChartBuilder(object):
return self._helm_chart return self._helm_chart
# dependencies # dependencies
# [process_chart(x, is_dependency=True) for x in chart.dependencies] # [process_chart(x, is_dependency=True) for x in chart.dependencies]
dependencies=[] dependencies = []
for chart in self.chart.dependencies: for chart in self.chart.dependencies:
LOG.info("Building dependency chart {} for release {}".format(chart.name, self.chart.release_name)) LOG.info("Building dependency chart %s for release %s", chart.name,
self.chart.release_name)
dependencies.append(ChartBuilder(chart).get_helm_chart()) dependencies.append(ChartBuilder(chart).get_helm_chart())
helm_chart = Chart( helm_chart = Chart(
@ -153,12 +165,11 @@ class ChartBuilder(object):
self._helm_chart = helm_chart self._helm_chart = helm_chart
return helm_chart return helm_chart
def dump(self): def dump(self):
''' '''
This method is used to dump a chart object as a serialized string so that we can perform a diff This method is used to dump a chart object as a
serialized string so that we can perform a diff
It should recurse into dependencies It should recurse into dependencies
''' '''
return self.get_helm_chart().SerializeToString() return self.get_helm_chart().SerializeToString()

View File

@ -1,9 +1,9 @@
from logutil import LOG
from kubernetes import client, config from kubernetes import client, config
class K8s(object): class K8s(object):
'''
Object to obtain the local kube config file
'''
def __init__(self): def __init__(self):
''' '''
Initialize connection to Kubernetes Initialize connection to Kubernetes

View File

@ -1,17 +1,20 @@
# various utilties to support logging
import logging import logging
from logging.handlers import SysLogHandler from logging.handlers import SysLogHandler
LOG = logging.getLogger('armada') LOG = logging.getLogger('armada')
LOG_FORMAT='%(asctime)s %(name)-12s %(levelname)-8s %(message)s' LOG_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
LOG_DATE = '%m-%d %H:%M' LOG_DATE = '%m-%d %H:%M'
def setup_logging(args): def setup_logging(args):
'''
:params args - logging information arguments in cli
various utilties to support logging
'''
level = logging.DEBUG if args.debug else logging.INFO level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(level=level, format=LOG_FORMAT, date_fmt=LOG_DATE) logging.basicConfig(level=level, format=LOG_FORMAT, date_fmt=LOG_DATE)
handler = SysLogHandler(address='/dev/log') handler = SysLogHandler(address='/dev/log')
syslog_formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s') syslog_formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s')
handler.setFormatter(syslog_formatter) handler.setFormatter(syslog_formatter)
LOG.addHandler(handler) LOG.addHandler(handler)

View File

@ -1,16 +1,16 @@
from hapi.services.tiller_pb2 import ReleaseServiceStub, ListReleasesRequest, InstallReleaseRequest, UpdateReleaseRequest from hapi.services.tiller_pb2 import ReleaseServiceStub, ListReleasesRequest, \
InstallReleaseRequest, UpdateReleaseRequest
from hapi.chart.config_pb2 import Config from hapi.chart.config_pb2 import Config
from logutil import LOG
from k8s import K8s from k8s import K8s
import grpc import grpc
TILLER_PORT=44134 TILLER_PORT = 44134
TILLER_VERSION=b'2.1.3' TILLER_VERSION = b'2.1.3'
TILLER_TIMEOUT=300 TILLER_TIMEOUT = 300
class Tiller(object): class Tiller(object):
''' '''
The Tiller class supports communication and requests to the Tiller Helm The Tiller class supports communication and requests to the Tiller Helm
service over gRPC service over gRPC
''' '''
@ -27,7 +27,6 @@ class Tiller(object):
# be fed at runtime as an override # be fed at runtime as an override
self.timeout = TILLER_TIMEOUT self.timeout = TILLER_TIMEOUT
@property @property
def metadata(self): def metadata(self):
''' '''
@ -35,7 +34,6 @@ class Tiller(object):
''' '''
return [(b'x-helm-api-client', TILLER_VERSION)] return [(b'x-helm-api-client', TILLER_VERSION)]
def get_channel(self): def get_channel(self):
''' '''
Return a tiller channel Return a tiller channel
@ -44,7 +42,6 @@ class Tiller(object):
tiller_port = self._get_tiller_port() tiller_port = self._get_tiller_port()
return grpc.insecure_channel('%s:%s' % (tiller_ip, tiller_port)) return grpc.insecure_channel('%s:%s' % (tiller_ip, tiller_port))
def _get_tiller_pod(self): def _get_tiller_pod(self):
''' '''
Search all namespaces for a pod beginning with tiller-deploy* Search all namespaces for a pod beginning with tiller-deploy*
@ -55,7 +52,6 @@ class Tiller(object):
if i.metadata.name.startswith('tiller-deploy'): if i.metadata.name.startswith('tiller-deploy'):
return i return i
def _get_tiller_ip(self): def _get_tiller_ip(self):
''' '''
Returns the tiller pod's IP address by searching all namespaces Returns the tiller pod's IP address by searching all namespaces
@ -63,12 +59,10 @@ class Tiller(object):
pod = self._get_tiller_pod() pod = self._get_tiller_pod()
return pod.status.pod_ip return pod.status.pod_ip
def _get_tiller_port(self): def _get_tiller_port(self):
'''Stub method to support arbitrary ports in the future''' '''Stub method to support arbitrary ports in the future'''
return TILLER_PORT return TILLER_PORT
def list_releases(self): def list_releases(self):
''' '''
List Helm Releases List Helm Releases
@ -77,7 +71,6 @@ class Tiller(object):
req = ListReleasesRequest() req = ListReleasesRequest()
return stub.ListReleases(req, self.timeout, metadata=self.metadata) return stub.ListReleases(req, self.timeout, metadata=self.metadata)
def list_charts(self): def list_charts(self):
''' '''
List Helm Charts from Latest Releases List Helm Charts from Latest Releases
@ -88,13 +81,15 @@ class Tiller(object):
for x in self.list_releases(): for x in self.list_releases():
try: try:
latest_release = x.releases[-1] latest_release = x.releases[-1]
charts.append((latest_release.name, latest_release.version, latest_release.chart, latest_release.config.raw)) charts.append((latest_release.name, latest_release.version,
latest_release.chart,
latest_release.config.raw))
except IndexError: except IndexError:
continue continue
return charts return charts
def update_release(self, chart, dry_run, name, disable_hooks=False,
def update_release(self, chart, dry_run, name, disable_hooks=False, values=None): values=None):
''' '''
Update a Helm Release Update a Helm Release
''' '''
@ -112,8 +107,8 @@ class Tiller(object):
disable_hooks=disable_hooks, disable_hooks=disable_hooks,
values=values, values=values,
name=name) name=name)
return stub.UpdateRelease(release_request, self.timeout, metadata=self.metadata) return stub.UpdateRelease(release_request, self.timeout,
metadata=self.metadata)
def install_release(self, chart, dry_run, name, namespace, values=None): def install_release(self, chart, dry_run, name, namespace, values=None):
''' '''
@ -133,4 +128,6 @@ class Tiller(object):
values=values, values=values,
name=name, name=name,
namespace=namespace) namespace=namespace)
return stub.InstallRelease(release_request, self.timeout, metadata=self.metadata) return stub.InstallRelease(release_request,
self.timeout,
metadata=self.metadata)

View File

@ -10,5 +10,5 @@ setenv=
[flake8] [flake8]
ignore=E302,H306 ignore=E302,H306
exclude= libgit2-0.24.0, .git, .idea, .tox, *.egg-info, *.eggs, bin, dist exclude= libgit2-0.24.0, .git, .idea, .tox, *.egg-info, *.eggs, bin, dist, hapi