Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Discover jenkins with autoip host name instead of host:port (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
takirala authored and colin-msphere committed Jun 25, 2018
1 parent 4e338ee commit 1f2b881
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions scripts/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
import xml.etree.ElementTree as ET


def populate_jenkins_config_xml(config_xml, master, name, host, port, role, user):
marathon_autoip_dns_url = 'http://{}.marathon.autoip.dcos.thisdcos.directory:{}'


def populate_jenkins_config_xml(config_xml, master, name, port, role, user):
"""Modifies a Jenkins master's 'config.xml' at runtime. Essentially, this
replaces certain configuration options of the Mesos plugin, such as the
framework name and the Jenkins URL that agents use to connect back to the
master.
:param config_xml: the path to Jenkins' 'config.xml' file
:param name: the name of the framework, e.g. 'jenkins'
:param host: the Mesos agent the task is running on
:param port: the Mesos port the task is running on
:param role: The role passed to the internal Jenkins configuration that denotes which resources can be launched
:param user: the user the task is running on
Expand All @@ -30,7 +32,8 @@ def populate_jenkins_config_xml(config_xml, master, name, host, port, role, user

_find_and_set(mesos, './master', master)
_find_and_set(mesos, './frameworkName', name)
_find_and_set(mesos, './jenkinsURL', "http://{}:{}".format(host, port))
# This used to be host and port. Switching over to DNS Name to address COPS-3395.
_find_and_set(mesos, './jenkinsURL', marathon_autoip_dns_url.format(name, port), True)
_find_and_set(mesos, './role', role)
_find_and_set(mesos, './slavesUser', user)

Expand All @@ -48,7 +51,7 @@ def populate_jenkins_location_config(location_xml, url):
:type url: str
"""
tree, root = _get_xml_root(location_xml)
_find_and_set(root, 'jenkinsUrl', url)
_find_and_set(root, 'jenkinsUrl', url, True)
tree.write(location_xml)


Expand Down Expand Up @@ -116,15 +119,13 @@ def main():

# optional environment variables
jenkins_root_url = os.getenv(
'JENKINS_ROOT_URL',
"http://{}:{}".format(marathon_host, marathon_nginx_port))

'JENKINS_ROOT_URL',
marathon_autoip_dns_url.format(jenkins_framework_name, marathon_nginx_port))

populate_jenkins_config_xml(
os.path.join(jenkins_home_dir, 'config.xml'),
mesos_master,
jenkins_framework_name,
marathon_host,
marathon_nginx_port,
jenkins_agent_role,
jenkins_agent_user)
Expand Down Expand Up @@ -158,7 +159,7 @@ def _get_xml_root(config_xml):
return tuple([tree, root])


def _find_and_set(element, term, new_text):
def _find_and_set(element, term, new_text, write_if_empty=False):
"""Find the desired term within the XML element and replace
its text with text.
Expand All @@ -168,8 +169,12 @@ def _find_and_set(element, term, new_text):
:type term: str
:param new_text: New element text
:type new_text: str
:param write_if_empty : If set to True, the value is updated only if empty.
If set to False, the value is always updated.
:type write_if_empty: bool
"""
element.find(term).text = new_text
if not write_if_empty or write_if_empty and not element.find(term).text:
element.find(term).text = new_text


if __name__ == '__main__':
Expand Down

0 comments on commit 1f2b881

Please sign in to comment.