def bootstrap_jenkins(): status_set("maintenance", "Bootstrapping Jenkins configuration") service = Service() service.check_ready() configuration = Configuration() configuration.bootstrap() set_state("jenkins.bootstrapped")
def configure_admin(): remove_state("jenkins.configured.admin") status_set("maintenance", "Configuring admin user") users = Users() users.configure_admin() api = Api() api.reload() api.wait() # Wait for the service to be fully up set_state("jenkins.configured.admin")
def upgrade_jenkins(): if config("release") == "bundle": packages = Packages() if packages.jenkins_upgradable(): status_set("maintenance", "Upgrading Jenkins") packages.install_jenkins() api = Api() api.wait() # Wait for the upgrade to finish packages.clean_old_plugins() unitdata.kv().set("jenkins.plugins.last_update", 0) update_plugins() else: log("No newer jenkins package is available")
def configure_plugins(): if get_state("extension.connected"): # We've been driven by an extension, let it take control over # plugin. log("External relation detected - skip configuring plugins") return status_set("maintenance", "Configuring plugins") remove_state("jenkins.configured.plugins") plugins = Plugins() plugins.install(config("plugins")) api = Api() api.wait() # Wait for the service to be fully up set_state("jenkins.configured.plugins")
def update_plugins(): last_update = unitdata.kv().get("jenkins.plugins.last_update") if last_update is None: unitdata.kv().set("jenkins.plugins.last_update", 0) last_update = 0 # Only try to update plugins when the interval configured has passed update_interval = time.time() - (config("plugins-auto-update-interval") * 60) if (last_update < update_interval): status_set("maintenance", "Updating plugins") plugins = Plugins() plugins.update(config("plugins")) api = Api() api.wait() # Wait for the service to be fully up unitdata.kv().set("jenkins.plugins.last_update", time.time())
def configure_admin(): remove_state("jenkins.configured.admin") status_set("maintenance", "Configuring admin user") users = Users() users.configure_admin() api = Api() api.reload() api.wait() # Wait for the service to be fully up # Inform any extension that the username/password changed if get_state("extension.connected"): extension_relation = (RelationBase.from_state("extension.connected")) extension_relation.joined() set_state("jenkins.configured.admin")
def configure_admin(): remove_state("jenkins.configured.admin") api = Api() status_set("maintenance", "Configuring Jenkins public url") configuration = Configuration() needs_restart = configuration.set_url() if needs_restart: status_set("maintenance", "Restarting Jenkins") service_restart('jenkins') api.wait() status_set("maintenance", "Configuring proxy settings") configuration.configure_proxy() service_restart('jenkins') api.wait() status_set("maintenance", "Configuring admin user") users = Users() users.configure_admin() api.reload() api.wait() # Wait for the service to be fully up # Inform any extension that the username/password changed if get_state("extension.connected"): extension_relation = (RelationBase.from_state("extension.connected")) extension_relation.joined() set_state("jenkins.configured.admin")
def update_nrpe_config(nagios): unit_data = unitdata.kv() nagios_hostname = unit_data.get('nagios.hostname', None) nagios_host_context = unit_data.get('nagios.host_context', None) # require the nrpe-external-master relation to provide the host context if in_relation_hook() and relation_id().\ startswith('nrpe-external-master:'): rel = relation_get() if 'nagios_host_context' in rel: nagios_host_context = rel['nagios_host_context'] unit_data.set('nagios.host_context', nagios_host_context) # We have to strip the nagios host context from the nagios hostname # since the nagios.add_check will put it back again... nagios_hostname = rel['nagios_hostname'] if nagios_hostname.startswith(nagios_host_context + '-'): nagios_hostname = nagios_hostname[len(nagios_host_context + '-'):] unit_data.set('nagios.hostname', nagios_hostname) if not nagios_hostname or not nagios_host_context: return # The above boilerplate is needed until this issue is fixed: # # https://github.com/cmars/nrpe-external-master-interface/issues/6 status_set('maintenance', 'Updating Nagios configs') creds = Credentials() check = [ '/usr/lib/nagios/plugins/check_http', '-H', 'localhost', '-p', '8080', '-u', urlparse(Api().url).path, '-a', "{}:{}".format(creds.username(), creds.token()), ] nagios.add_check(check, name="check_jenkins_http", description="Verify Jenkins HTTP is up.", context=nagios_host_context, unit=nagios_hostname) status_set('active', 'Ready')
def update_plugins(): last_update = unitdata.kv().get("jenkins.plugins.last_update") if last_update is None: unitdata.kv().set("jenkins.plugins.last_update", 0) last_update = 0 # Only try to update plugins when the interval configured has passed update_interval = time.time() - (config("plugins-auto-update-interval") * 60) if (last_update < update_interval): status_set("maintenance", "Updating plugins") remove_state("jenkins.updated.plugins") plugins = Plugins() plugins.update(config("plugins")) api = Api() api.wait() # Wait for the service to be fully up # Restart jenkins if any plugin got updated last_restart = unitdata.kv().get("jenkins.last_restart") or 0 last_plugin_update_time = ( unitdata.kv().get("jenkins.plugins.last_plugin_update_time") or 0) if (last_restart < last_plugin_update_time): restart() unitdata.kv().set("jenkins.plugins.last_restart", time.time()) set_state("jenkins.updated.plugins") unitdata.kv().set("jenkins.plugins.last_update", time.time())
def set_jenkins_dir(storage_dir=paths.HOME): status_set("maintenance", "Configuring Jenkins storage") jenkins_installed = get_state("apt.installed.jenkins") if jenkins_installed: service_stop('jenkins') if storage_dir is paths.HOME: log("Setting Jenkins to use local storage") Storage().unlink_home() else: log("Setting Jenkins to use storage at {}".format(storage_dir)) Storage().link_home(storage_dir) if jenkins_installed: status_set("maintenance", "Restarting Jenkins") service_start('jenkins') Service().check_ready() if get_state('jenkins.bootstrapped'): # JENKINS_HOME just changed trigger bootstrap again remove_state("jenkins.bootstrapped") bootstrap_jenkins() else: status_set('active', 'Ready')
def install_jenkins(): status_set("maintenance", "Installing Jenkins") packages = Packages() packages.install_jenkins()
def stop(): service_stop("jenkins") status_set("maintenance", "Jenkins stopped")
def ready(): status_set("active", "Jenkins is running")
def upgrade_jenkins(): status_set("maintenance", "Upgrading Jenkins") packages = Packages() packages.install_jenkins()
def configure_tools(): remove_state("jenkins.configured.tools") status_set("maintenance", "Installing tools") packages = Packages() packages.install_tools() set_state("jenkins.configured.tools")
def plugins_layer(): try: plugins = Plugins() except PluginSiteError as e: status_set("error", e.message) return plugins