def joined(self): """Indicate the relation is connected and transmit our credentials.""" log("Updating extension interface with up-to-date data.") # Fish out the current zuul address from any relation we have. zuul_address = None for rid in relation_ids("zuul"): for unit in related_units(rid): zuul_address = relation_get( rid=rid, unit=unit, attribute="private-address") break credentials = Credentials() for rid in relation_ids("extension"): relation_settings = { "admin_username": credentials.username(), "admin_password": credentials.password(), "jenkins_url": "http://%s:8080" % unit_get("private-address"), "jenkins-admin-user": credentials.username(), "jenkins-token": credentials.token(), } relation_set(relation_id=rid, relation_settings=relation_settings) if zuul_address: relation_set(relation_id=rid, zuul_address=zuul_address) self.set_state("{relation_name}.connected")
def joined(self): """Indicate the relation is connected and communicate our URL.""" address = unit_get("private-address") log("Setting url relation to http://%s:8080" % address) relation_set(url="http://%s:8080" % address) # Export credentials to the slave so it can download # slave-agent.jnlp from the master. log("Setting relation credentials") credentials = Credentials() relation_set(username=credentials.username()) relation_set(password=credentials.token()) self.set_state("{relation_name}.connected")
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 _make_client(self): """Build a Jenkins client instance.""" creds = Credentials() user = creds.username() token = creds.token() if token is None: creds.token( self._get_token(user, creds.password(), self._packages.jenkins_version())) client = jenkins.Jenkins(self.url, user, token) try: client.get_whoami() # Handling token regeneration when the current token is invalid. # Then re-raise the exception as expected, so the retry kicks off. except jenkins.JenkinsException as e: if "401" in str(e): creds.token( self._get_token(user, creds.password(), self._packages.jenkins_version())) raise return client
def _make_client(self): """Build a Jenkins client instance.""" creds = Credentials() user = creds.username() token = creds.token() # TODO: also handle regenerated tokens if token is None: client = jenkins.Jenkins(self.url, user, creds.password()) token = client.run_script(GET_TOKEN_SCRIPT.format(user)).strip() creds.token(token) client = jenkins.Jenkins(self.url, user, token) client.get_whoami() return client
def _make_client(self): """Build a Jenkins client instance.""" creds = Credentials() user = creds.username() token = creds.token() # TODO: also handle regenerated tokens if token is None: client = jenkins.Jenkins(URL, user, creds.password()) token = client.run_script(GET_TOKEN_SCRIPT.format(user)).strip() creds.token(token) client = jenkins.Jenkins(URL, user, token) client.get_whoami() return client
def _make_client(self): """Build a Jenkins client instance.""" creds = Credentials() user = creds.username() token = creds.token() # TODO: also handle regenerated tokens if token is None: client = jenkins.Jenkins(self.url, user, creds.password()) # If we're using Jenkins >= 2.129 we need to request a new token. jenkins_version = self._packages.jenkins_version() if LooseVersion(jenkins_version) >= LooseVersion('2.129'): token = client.run_script( GET_NEW_TOKEN_SCRIPT.format(user)).strip() else: token = client.run_script( GET_LEGACY_TOKEN_SCRIPT.format(user)).strip() creds.token(token) client = jenkins.Jenkins(self.url, user, token) client.get_whoami() return client
def setUp(self): super(CredentialsTest, self).setUp() self.credentials = Credentials()
class CredentialsTest(CharmTest): def setUp(self): super(CredentialsTest, self).setUp() self.credentials = Credentials() def test_username_initial(self): """ Before the initial configuration is bootstrapped, the username is the hard-coded 'admin'. """ self.useFixture(AptInstalledJenkins(self.fakes)) self.assertEqual("admin", self.credentials.username()) def test_username(self): """ The username matches then one set in the service configuration. """ self.fakes.juju.config["username"] = "******" self.useFixture(JenkinsConfiguredAdmin(self.fakes)) self.assertEqual("joe", self.credentials.username()) def test_password_initial(self): """ Before the initial configuration is bootstrapped, the password is the one written by jenkins. """ self.useFixture(AptInstalledJenkins(self.fakes)) self.assertEqual(INITIAL_PASSWORD, self.credentials.password()) def test_password_from_config(self): """ If set, the password matches the one set in the service configuration. """ self.fakes.juju.config["password"] = "******" self.useFixture(JenkinsConfiguredAdmin(self.fakes)) self.assertEqual("sekret", self.credentials.password()) def test_password_from_local_state(self): """ If not set, the password is retrieved from the local state. """ self.useFixture(JenkinsConfiguredAdmin(self.fakes)) self.assertEqual(GENERATED_PASSWORD, self.credentials.password()) def test_token(self): """ The user's API token is initially None. """ self.useFixture(JenkinsConfiguredAdmin(self.fakes)) self.assertIsNone(self.credentials.token()) def test_token_set(self): """ The user's API token can be set, and will be saved in the local state. """ self.useFixture(JenkinsConfiguredAdmin(self.fakes)) self.assertEqual("abc", self.credentials.token("abc")) self.assertEqual("abc", hookenv.config()["_api-token"]) self.assertEqual("abc", self.credentials.token()) self.assertThat(paths.ADMIN_TOKEN, FileContains("abc")) self.assertThat(paths.ADMIN_TOKEN, HasOwnership(0, 0)) self.assertThat(paths.ADMIN_TOKEN, HasPermissions("0600"))