示例#1
0
class JobChunks(object):
	'''
	Represents a group of projects that exist in our repo and are being created in Jenkins. These groups vary
	by environment and depending on if they are for packaging.

	For instance, if the repo has a _version at the end it's a dev environment. Similarly, if the job is for 
	packaging it will be created accordingly.
	'''

	def __init__(self, stage, job_type=None, version=None):
		c = EmploymentConfig('employment.conf')
		self.JENKINS_URL=c.getJenkinsURL()
		self.KILN_PREFIX = c.getKilnPrefix()
		self.JOB_TYPE = job_type
		self.STAGE = stage
		self.VERSION = version

		c = EmploymentConfig('employment.conf')
		self.KILN_PREFIX = c.getKilnPrefix()
		self.KILN_USERNAME = c.getKilnUsername()
		self.KILN_PASSWORD = c.getKilnPassword()
		self.k = Anvil(self.KILN_PREFIX)
		self.k.create_session(self.KILN_USERNAME, self.KILN_PASSWORD)
		#self.k.create_session_by_prompting()

		group1 = c.getGroupProjects('group1')
		self.group1name = c.getGroupName('group1')

		group2 = c.getGroupProjects('group2')
		self.group2name = c.getGroupName('group2')

		self.app_groups = [group1, group2]

	def _jenkins_connect(self):
		'''
		Create a object holding a connection to jenkins. Assign this function to a variable.

		Ex. j = _jenkins_connect()
		'''
		return Jenkins(self.JENKINS_URL)

	def _build_project_string(self, app_name):
		'''
		Build the project string. Should this just be a variable?
		'''

		if self.JOB_TYPE == 'package':
			if self.STAGE == 'dev':
				string_return = "PKG_" + app_name + "_" + self.VERSION
			else:
				string_return = "PKG_" + app_name
		else:
			if self.STAGE == 'dev':
				string_return = app_name + "_" + self.VERSION
			else:
				string_return = app_name
		return string_return

	def _build_trig_string(self):
		'''
		Create the name of the job that will trigger all the others.
		'''
		if self.JOB_TYPE == 'package':
			if self.STAGE == 'dev':
				string_return = "PKG_dev_" + self.VERSION
			else:
				string_return = "PKG_master"
		else:
			if self.STAGE == 'dev':
				string_return = "TEST_dev_" + self.VERSION
			else:
				string_return = "TEST_master"
		return string_return

	def _build_kiln_string(self, app_name):
		'''
		Build the string for the name of the app as it would appear in kiln.
		'''
		if self.STAGE == 'dev':
			string_return = app_name + "_" + self.VERSION
		else:
			string_return = app_name
		return string_return

	def _get_repo_base(self, group):
		'''
		Create the base repo url based on the group.
		'''

		if group == 0:
			base_url = "https://%s.kilnhg.com/Code/src/%s/" % (self.KILN_PREFIX, self.group1name)
		elif group == 1:
			base_url = "https://%s.kilnhg.com/Code/src/%s/" % (self.KILN_PREFIX, self.group2name)
		else:
			base_url=None
		return base_url

	def _jobs_in_jenkins(self):
		'''
		Returns an array of all jobs from config that are in Jenkins.
		'''
		j = self._jenkins_connect()
		jobs = []
		for group in self.app_groups:
			base_url = self._get_repo_base(self.app_groups.index(group))
			for project in group:
				project_name = self._build_project_string(project)
				repo_url = base_url + project
				if j.job_exists(project_name):
					jobs.append(Project(project, repo_url, self.STAGE, self.JOB_TYPE, self.VERSION))
		return jobs

	def _job_in_kiln(self):
		'''
		Returns array of projects in kiln
		'''
		jobs = []
		for group in self.app_groups:
			base_url = self._get_repo_base(self.app_groups.index(group))
			for project in group:
				project_name = self._build_kiln_string(project)
				repo_url = base_url + project_name
				if self.k.check_repo(project_name):
					print "Found %s in kiln" % project_name
					jobs.append(Project(project, repo_url, self.STAGE, self.JOB_TYPE, self.VERSION))
		return jobs

	def _build_app_list(self):
		'''
		Return a comma  of projects that we will pass to a trigger job.
		'''
		apps = self._jobs_in_jenkins()
		app_string = ''
		for app in apps:
			if apps.index(app) < (len(apps)-1):
				app_string = app_string + self._build_project_string(app.name) + ', '
			else:
				app_string = app_string + self._build_project_string(app.name)
		return app_string

	def delete(self):
	    print "*" * 60
	    print "Doing delete"
	    print "*" * 60
	    jobs = self._jobs_in_jenkins()
	    for job in jobs:
	    	print "Removing Jenkins job for " + self._build_project_string(job.name)
	        jobs[jobs.index(job)].jenkins_remove_job()

	def create(self):
	    print "*" * 60
	    print "Creating jobs"
	    print "*" * 60
	    jobs = self._job_in_kiln()
	    for job in jobs:
	    	print "Creating Jenkins job for " + self._build_project_string(job.name)
	        jobs[jobs.index(job)].jenkins_create_job()

	def create_trig(self):
		build_apps = self._build_app_list()
		print "Using app list: " + build_apps
		j = self._jenkins_connect()
		trigger_job = self._build_trig_string()
		if j.job_exists(trigger_job):
			print "Job " + trigger_job + " already exists!"
		else:
			print "Creating trigger job " + trigger_job
			response = j.create(trigger_job, 'templates/trigger.xml',
							job_list=build_apps)
			print response

	def delete_trig(self):
		j = self._jenkins_connect()
		trigger_job = self._build_trig_string()
		if j.job_exists(trigger_job):
			print "Deleting trigger job " + trigger_job
			return j.delete(trigger_job)
		else:
			print trigger_job + " doesn't seem to exist."

	def init_build(self):
		'''
示例#2
0
class Project(object):
	'''
	Represents a project
	'''

	def __init__(self, project_name, repo_url,stage,job_type=None, version=None):
		self.name = project_name

		if version != None:
			self.PROJECT = project_name + '_' + version
			self.VERSION = version
		else:
			self.PROJECT = project_name
			self.VERSION = None

		self.REPO_URL = repo_url
		self.STAGE = stage
		self.job_type=job_type

		c = EmploymentConfig('employment.conf')

		self.TEST_SCRIPT = c.getJenkinsScript('test')
		self.PACKAGE_SCRIPT = c.getJenkinsScript('package')
		self.UPLOAD_SCRIPT = c.getJenkinsScript('upload')
		self.JENKINS_URL = c.getJenkinsURL()
		self.PKG_prefix = c.getPacakgePrefix()

		self.KILN_PREFIX = c.getKilnPrefix()
		self.KILN_USERNAME = c.getKilnUsername()
		self.KILN_PASSWORD = c.getKilnPassword()
		self.k = Anvil(self.KILN_PREFIX)
		self.k.create_session(self.KILN_USERNAME, self.KILN_PASSWORD)

		self.EMAIL_TO = c.getJenkinsEmail()

		if self.job_type == 'package':
			if self.STAGE == 'dev':
				self.JENKINS_STRING = self.PKG_prefix + "_" + project_name + "_" + self.VERSION
			else:
				self.JENKINS_STRING = self.PKG_prefix + "_" + project_name
		else:
			if self.STAGE == 'dev':
				self.JENKINS_STRING = project_name + "_" + self.VERSION
			else:
				self.JENKINS_STRING = project_name

		if self.STAGE == 'dev':
			self.KILN_STRING = project_name + "_" + self.VERSION
		else:
			self.KILN_STRING = project_name

	def kiln_connect():
		anvil = Anvil(self.KILN_PREFIX)
		anvil.create_session(self.KILN_USERNAME, self.KILN_PASSWORD)
		return anvil

	def kiln_repos():
		anvil = kiln_connect()
		repos = anvil.get_repos()
		return repos

	def _jenkins_connect(self):
		return Jenkins(self.JENKINS_URL)

	def _kiln_check(self,check_project):
		anvil = self.kiln_connect()
		repos = anvil.get_repos()
		for repo in repos:
		    if repo.name == check_project:
		    	return True

	def jenkins_create_job(self):
		'''
		Create Jenkins job based on object. If it is a package job the corresponding
		template xml file will be used.
		'''
		j = self._jenkins_connect()
		if self.k.check_repo(self.KILN_STRING):
			if self.job_type == 'package':
				if j.job_exists(self.JENKINS_STRING):
					print "Job " + self.JENKINS_STRING + " already exists in Jenkins."
				else:
					response = j.create(self.JENKINS_STRING, 'templates/PKG_template.xml',
										repo_url=self.REPO_URL,
										project_name=self.name, 
										email_notify=self.EMAIL_TO,
										test_script=self.TEST_SCRIPT,
										package_script=self.PACKAGE_SCRIPT,
										stage=self.STAGE,
										version=self.VERSION,
										upload_script=self.UPLOAD_SCRIPT)
					print response
			else:
				if j.job_exists(self.JENKINS_STRING):
					print "Job " + self.JENKINS_STRING + " already exists in Jenkins."
				else:
					response = j.create(self.JENKINS_STRING, 'templates/template.xml',
										repo_url=self.REPO_URL,
										project_name=self.name,
										email_notify=self.EMAIL_TO,
										test_script=self.TEST_SCRIPT)
					print response
		else:
			print "Project " + self.PROJECT + " not found in kiln."

	def jenkins_remove_job(self):
		j = self._jenkins_connect()
		if j.job_exists(self.JENKINS_STRING):
			j.delete(self.JENKINS_STRING)
		else:
			print "Skipping, doesn't exist in Jenkins"
		
	def jenkins_build(self):
		j = self._jenkins_connect()
		j.build(self.JENKINS_STRING)

	def write_jenkins_xml(self):
        '''
        Output xml file of job.
        '''
		j = self._jenkins_connect()
		xml_output = self.JENKINS_STRING + ".xml"
		f = open(xml_output, 'w')
		f.write(j.get_config_xml(self.JENKINS_STRING))
		f.close()