Skip to content

Commit

Permalink
Move the exception handling to reactive/jenkins.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alejdg committed Apr 22, 2020
1 parent eedc241 commit 9bacf3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/charms/layer/jenkins/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import urllib

from charmhelpers.core import hookenv, host
from charmhelpers.core.hookenv import status_set
from charms.layer.jenkins import paths
from charms.layer.jenkins.api import Api

from jenkins_plugin_manager.plugin import UpdateCenter


class PluginSiteError(Exception):
def __init__(self):
self.message = ("The configured plugin-site doesn't provide an "
"update-center.json file or is not acessible.")


class Plugins(object):
"""Manage Jenkins plugins."""

Expand All @@ -23,11 +28,7 @@ def __init__(self):
urllib.request.urlopen(update_center)
self.update_center = UpdateCenter(uc_url=update_center)
except urllib.error.HTTPError:
status_set(
"blocked",
"The configured plugin-site doesn't provide an "
"update-center.json file or is not acessible.")
raise
raise PluginSiteError()

def install(self, plugins):
"""Install the given plugins, optionally removing unlisted ones.
Expand Down
13 changes: 11 additions & 2 deletions reactive/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)
from charms.layer.jenkins.users import Users
from charms.layer.jenkins.plugins import Plugins
from charms.layer.jenkins.plugins import PluginSiteError
from charms.layer.jenkins.api import Api
from charms.layer.jenkins.credentials import Credentials
from charms.layer.jenkins.service import Service
Expand All @@ -64,6 +65,14 @@ def install_dependencies():
packages.install_dependencies()


def plugins_layer():
try:
plugins = Plugins()
except PluginSiteError as e:
status_set("error", e.message)
return plugins


# Dynamically create an OR-ed chain of @when_not, so install_dependencies
# will get triggered whenever one or more dependencies are unmet (typically
# at install time).
Expand Down Expand Up @@ -166,7 +175,7 @@ def configure_plugins():
return
status_set("maintenance", "Configuring plugins")
remove_state("jenkins.configured.plugins")
plugins = Plugins()
plugins = plugins_layer()
plugins.install(config("plugins"))
api = Api()
api.wait() # Wait for the service to be fully up
Expand All @@ -186,7 +195,7 @@ def update_plugins():
update_interval = time.time() - (config("plugins-auto-update-interval") * 60)
if (last_update < update_interval):
status_set("maintenance", "Updating plugins")
plugins = Plugins()
plugins = plugins_layer()
plugins.update(config("plugins"))
api = Api()
api.wait() # Wait for the service to be fully up
Expand Down

0 comments on commit 9bacf3f

Please sign in to comment.