def ensure_repository(self, repository_name): ensured_repository_element = Ensurance(self.__xml_root).ensure_child( 'repositories').ensure_child_with_attribute( 'repository', 'name', repository_name) if not ensured_repository_element.has_attribute('id'): ensured_repository_element.set('id', str(uuid4())) return Repository(ensured_repository_element.element)
def set_timer(self, timer, only_on_changes=False): if only_on_changes: Ensurance(self.element).ensure_child_with_attribute( 'timer', 'onlyOnChanges', 'true').set_text(timer) else: Ensurance(self.element).ensure_child('timer').set_text(timer) return self
def ensure_config_repo(configurator, git_url, plugin): config_groups = Ensurance( configurator._GoCdConfigurator__xml_root).ensure_child("config-repos") # pylint: disable=protected-access if config_repo_not_yet_present(git_url, config_groups): Ensurance(config_groups.element).append( fromstring( '<config-repo plugin="{0}"><git url="{1}" /></config-repo>'. format(plugin, git_url)))
def ensure_removal_of_template(self, template_name): matching = [template for template in self.templates if template.name == template_name] root = Ensurance(self.__xml_root) templates_element = root.ensure_child('templates').element for template in matching: templates_element.remove(template.element) if len(self.templates) == 0: root.element.remove(templates_element) return self
def __ensure_environment_variables(self, environment_variables, secure, encrypted): ensured_env_variables = Ensurance(self.element).ensure_child("environmentvariables") for env_variable in sorted(environment_variables.keys()): variable_element = ensured_env_variables.ensure_child_with_attribute("variable", "name", env_variable) if secure: variable_element.set("secure", "true") else: PossiblyMissingElement(variable_element.element).remove_attribute("secure") value_element = variable_element.ensure_child(self.__value_element_name(encrypted)) value_element.set_text(environment_variables[env_variable]) self.__sort_by_name_attribute(ensured_env_variables.element)
def ensure_artifacts(self, artifacts): if artifacts: artifacts_ensurance = Ensurance(self.__element).ensure_child("artifacts") artifacts_to_add = artifacts.difference(self.artifacts) for artifact in artifacts_to_add: artifact.append_to(artifacts_ensurance) return self
def ensure_artifacts(self, artifacts): if artifacts: artifacts_ensurance = Ensurance(self.element).ensure_child("artifacts") artifacts_to_add = artifacts.difference(self.artifacts) for artifact in artifacts_to_add: artifact.append_to(artifacts_ensurance, self.is_gocd_18_3_and_above()) return self
def ensure_removal_of_agent(self, hostname): matching = [ agent for agent in self.agents if agent.hostname == hostname ] for agent in matching: Ensurance(self.__xml_root).ensure_child('agents').element.remove( agent._element) return self
def elastic(self): gocd_version_with_elastic_outside_server_tag = 18 gocd_major_version = int(self.server_version.split('.')[0]) if gocd_major_version < gocd_version_with_elastic_outside_server_tag: elastic_element = Elastic(self.__server_element_ensurance().element.find('elastic')) else: elastic_element = Elastic(Ensurance(self.__xml_root).element.find('elastic')) return elastic_element
def ensure_elastic(self): gocd_version_with_elastic_outside_server_tag = 18 gocd_major_version = int(self.server_version.split('.')[0]) if gocd_major_version < gocd_version_with_elastic_outside_server_tag: elastic_element = self.__server_element_ensurance().ensure_child('elastic').element else: elastic_element = Ensurance(self.__xml_root).ensure_child('elastic').element return Elastic(elastic_element)
def ensure_permissions(configurator, pipeline_group, permission, roles): """ Ensure that only the ``roles`` are given the ``permission`` in the ``pipeline_group``. Arguments: pipeline_group (gomatic.PipelineGroup) permission (Permission) roles (list): List of role names to ensure """ for role in roles: ensure_role(configurator, role) authorization = Ensurance(pipeline_group.element).ensure_child('authorization') permission = authorization.ensure_child(permission.value) permission.element[:] = [] for role in roles: role_element = ET.SubElement(permission.element, tag='role') role_element.text = role
def ensure_permissions(configurator, pipeline_group, permission, roles): """ Ensure that only the ``roles`` are given the ``permission`` in the ``pipeline_group``. Arguments: pipeline_group (gomatic.PipelineGroup) permission (Permission) roles (list): List of role names to ensure """ for role in roles: ensure_role(configurator, role) authorization = Ensurance( pipeline_group.element).ensure_child('authorization') permission = authorization.ensure_child(permission.value) permission.element[:] = [] for role in roles: role_element = ET.SubElement(permission.element, tag='role') role_element.text = role
def append_to(self, element): if self.__working_dir is None: new_element = ET.fromstring('<exec command="%s"></exec>' % self.__command_and_args[0]) else: new_element = ET.fromstring('<exec command="%s" workingdir="%s"></exec>' % (self.__command_and_args[0], self.__working_dir)) for arg in self.__command_and_args[1:]: new_element.append(ET.fromstring('<arg>%s</arg>' % escape(arg))) new_element.append(ET.fromstring('<runif status="%s" />' % self.runif)) Ensurance(element).ensure_child("tasks").append(new_element) return Task(new_element)
def set_has_manual_approval(self, authorize_users=None, authorize_roles=None): approval_element = Ensurance(self.element).ensure_child_with_attribute("approval", "type", "manual").element if authorize_users or authorize_roles: auth_element = Ensurance(approval_element).ensure_child('authorization').element PossiblyMissingElement(auth_element).remove_all_children() for user in (authorize_users or []): auth_element.append(ET.fromstring('<user>{}</user>'.format(user))) for role in (authorize_roles or []): auth_element.append(ET.fromstring('<role>{}</role>'.format(role))) return self
def ensure_property(self, key, value): config_element = Ensurance( self.__element).ensure_child('configuration').element property_element = Ensurance( config_element).ensure_child_with_descendant( 'property', 'key', key).element value_tag = Ensurance(property_element).ensure_child('value') value_tag.set_text(value) return Property(property_element)
def append_to(self, element): src_type, src_value = self.src.as_xml_type_and_value if self.__dest is None: new_element = ET.fromstring( '<fetchartifact pipeline="%s" stage="%s" job="%s" %s="%s" />' % (self.__pipeline, self.__stage, self.__job, src_type, src_value)) else: new_element = ET.fromstring( '<fetchartifact pipeline="%s" stage="%s" job="%s" %s="%s" dest="%s"/>' % (self.__pipeline, self.__stage, self.__job, src_type, src_value, self.__dest)) new_element.append(ET.fromstring('<runif status="%s" />' % self.runif)) Ensurance(element).ensure_child("tasks").append(new_element) return Task(new_element)
def append_to(self, element): src_type, src_value = self.src.as_xml_type_and_value dest_parameter = "" if self.__dest is not None: dest_parameter = ' dest="%s"' % self.__dest origin_parameter = "" if self.__origin is not None: origin_parameter = ' artifactOrigin="%s"' % self.__origin new_element = ET.fromstring( ('<fetchartifact pipeline="%s" stage="%s" job="%s" %s="%s"' % (self.__pipeline, self.__stage, self.__job, src_type, src_value)) + dest_parameter + origin_parameter + '/>') new_element.append(ET.fromstring('<runif status="%s" />' % self.runif)) Ensurance(element).ensure_child("tasks").append(new_element) return Task(new_element)
def append_to(self, element): dest_parameter = "" if self.__dest is not None: dest_parameter = ' dest="%s"' % self.__dest origin_parameter = "" if self.__origin is not None: origin_parameter = ' origin="%s"' % self.__origin artifact_origin_parameter = "" if self.__artifact_origin is not None: artifact_origin_parameter = ' artifactOrigin="%s"' % self.__artifact_origin if self.__artifact_origin == 'external': properties_xml = "".join([ "<property><key>{}</key><value>{}</value></property>".format( k, str(v or '')) for k, v in self.__config.items() ]) fetch_artifact_xml = """<fetchartifact pipeline="{}" stage="{}" job="{}" artifactId="{}" artifactOrigin="{}"><configuration>{}</configuration></fetchartifact>""" new_element = ET.fromstring( fetch_artifact_xml.format(self.__pipeline, self.__stage, self.__job, self.__artifact_id, self.__artifact_origin, properties_xml)) else: src_type, src_value = self.src.as_xml_type_and_value new_element = ET.fromstring( ('<fetchartifact pipeline="%s" stage="%s" job="%s" %s="%s"' % (self.__pipeline, self.__stage, self.__job, src_type, src_value)) + dest_parameter + origin_parameter + artifact_origin_parameter + '/>') new_element.append(ET.fromstring('<runif status="%s" />' % self.runif)) Ensurance(element).ensure_child("tasks").append(new_element) return Task(new_element)
def ensure_tab(self, tab): tab_ensurance = Ensurance(self.__element).ensure_child("tabs") if self.tabs.count(tab) == 0: tab.append_to(tab_ensurance) return self
def ensure_pipeline(self, name): pipeline_element = Ensurance(self.element).ensure_child_with_attribute('pipeline', 'name', name).element return Pipeline(pipeline_element, self)
def ensure_type(self, type, version): plugin_configuration = Ensurance(self.__element).ensure_child_with_attribute('pluginConfiguration', 'id', type) plugin_configuration.set('version', version) return plugin_configuration.element
def ensure_parameters(self, parameters): parameters_ensurance = Ensurance(self.element).ensure_child("params") for key, value in parameters.iteritems(): parameters_ensurance.ensure_child_with_attribute("param", "name", key).set_text(value) return self
def ensure_stage(self, name): stage_element = Ensurance(self.element).ensure_child_with_attribute("stage", "name", name) return Stage(stage_element.element)
def set_has_manual_approval(self): Ensurance(self.element).ensure_child_with_attribute("approval", "type", "manual") return self
def __add_material(self, material): material.append_to(Ensurance(self.element).ensure_child('materials'))
def ensure_job(self, name): job_element = Ensurance(self.element).ensure_child("jobs").ensure_child_with_attribute("job", "name", name) return Job(job_element.element)
def fetch_materials(self, value): if value: PossiblyMissingElement(self.element).remove_attribute("fetchMaterials") else: Ensurance(self.element).set("fetchMaterials", "false")
def ensure_auth_configs(self): auth_config = Ensurance(self.element).ensure_child('authConfigs') return AuthConfigs(auth_config.element)
def ensure_config_repo(configurator, git_url, plugin): config_groups = Ensurance(configurator._GoCdConfigurator__xml_root).ensure_child("config-repos") # pylint: disable=protected-access gits = config_groups.element.find('config-repo//git') if not gits or not any([c for c in gits if c.url == git_url]): Ensurance(config_groups.element).append( fromstring('<config-repo plugin="{0}"><git url="{1}" /></config-repo>'.format(plugin, git_url)))
def ensure_roles(self): roles = Ensurance(self.element).ensure_child('roles') return Roles(roles.element)
def ensure_admins(self): admins = Ensurance(self.element).ensure_child('admins') return Admins(admins.element)
def ensure_package(self, name): ens = Ensurance(self.__element).ensure_child('packages').ensure_child_with_attribute('package', 'name', name) if not ens.has_attribute('id'): ens.set('id', str(uuid4())) return Package(ens.element)
def ensure_property(self, key, value): config_element = Ensurance(self.__element).ensure_child('configuration').element property_element = Ensurance(config_element).ensure_child_with_descendant('property', 'key', key).element value_tag = Ensurance(property_element).ensure_child('value') value_tag.set_text(value) return Property(property_element)
def ensure_template(self, template_name): pipeline_element = Ensurance(self.__xml_root).ensure_child('templates').ensure_child_with_attribute('pipeline', 'name', template_name).element return Pipeline(pipeline_element, 'templates')