def __init__(self,url,username,token,plugins_list,options): """This class represents a method for performing all the jenkins configurations to run Isilon Jenkins @param url: the base jenkins url example. jenkins.com @param username: ldap username @param token: Jenkins user token provided with every user @param plugins_list: path to plugins.json """ self.url = url self.username = username self.token = token self.plugins_list = plugins_list self.console = Console(self.url,self.username,self.token) self.src_jobs = job_actions(self.url,self.username,self.token) self.options = options try: self._set_configs() except Exception as e: print e
class Configure(object): """ This class represents configuration of a remote jenkins instance @note: requires Jenkins system with base install @note: requires security is disables """ def __init__(self,url,username,token,plugins_list,options): """This class represents a method for performing all the jenkins configurations to run Isilon Jenkins @param url: the base jenkins url example. jenkins.com @param username: ldap username @param token: Jenkins user token provided with every user @param plugins_list: path to plugins.json """ self.url = url self.username = username self.token = token self.plugins_list = plugins_list self.console = Console(self.url,self.username,self.token) self.src_jobs = job_actions(self.url,self.username,self.token) self.options = options try: self._set_configs() except Exception as e: print e def _set_configs(self): """ Send all needed json configs to jenkins host [src] configs/<config>.json [dest] /tmp/<config>.json @note: make sure script all use " not ' """ print "\n[set_jsons]" cfg_dir = 'configs' configs = os.listdir(cfg_dir) for config in configs: output = self.console.send_json('%s/%s' % (cfg_dir,config)) if output: print output def set_token(self,new_token): """ Set a new token if provided @param new_token: String of Jenkins user token """ self.token = new_token def _load_json(self,file,verbose=False): """ Loads all the plugins from the /configs/plugins.json """ with open(file) as data_file: data = json.load(data_file) if verbose: for key, value in data.iteritems(): print '{:10} : {:10}'.format(key,value) return data def plugins_install(self,ignore_version=True): """ Installs all the plugins using Jenkins Restful commands @param ignore_version:not used in this function directly @note: Verify successful by going to: http://<jenkins_url>/pluginManager/installed """ print "\n [plugins_install]" plugins = self._load_json(self.plugins_list) plug_mgmt = Plugin(self.url,self.url,self.token) failed_plugins = {} updates = [] for name,version in plugins.iteritems(): print "\n[%s %s]" % (name, version) plug_mgmt.install(name, version) print "wait for plugin installation 2 min" time.sleep(120) for name,version in plugins.iteritems(): if not self._plugin_exists(plug_mgmt, name, version): failed_plugins[name]=version print "\nfailed plugins list:" if len(failed_plugins) <=0: print "None" for name,version in failed_plugins.iteritems(): print "%s : %s" return failed_plugins def _plugin_exists(self,plug_mgmt,name,version,wait=300,ignore_version=True): """ Waits for all the plugins to load via jenkins restful calls @param plug_mgmt: plugin object in lib.jenkins_api @param name: plugin short name from configs/plugins.json @param version: plugin version @param wait: int, number of seconds to wait for plugin to be available @param ignore_version: boolean, ignore version when checking for if the plugin exists """ cnt = 0 hook = False while not hook: hook = plug_mgmt.exists(name,version,ignore_version=ignore_version) print "[%s %s] waiting %s | timeout %s" % (name,version,cnt,wait) print "Plugin status: %s" % hook time.sleep(1) cnt+=1 if cnt > wait: return False return True def get_auth_token(self): """ get the authentication token for a specific user on jenkins """ output = self.console.send('groovy/get_auth_token.groovy').strip() print "#%s#" % output self.set_token(output) return output def get_users(self): """ Get a list of all active users from jenkins """ output = self.console.send('groovy/get_users.groovy') print output def get_groups(self): """ Get a list of all active groups on Jenkins """ output = self.console.send('groovy/get_groups.groovy') print output def set_email(self): """ Set the admin email address @note: Verify successful by going to: http://<jenkins_url>/configure """ print "\n[set_email]" output = self.console.send('groovy/set_email.groovy') if output: print output sys.exit(1) def set_bugzilla(self): """ Set bugzilla configurations @note: Verify successful by going to: http://<jenkins_url>/configure """ print "\n[set_bugzilla]" output = self.console.send('groovy/set_bugzilla.groovy') if output: print output sys.exit(1) def set_publish_over_ssh(self): """ setup all configurations for set_publish_over_ssh plugin @note: Verify successful by going to: http://<jenkins_url>/configure """ print "\n[set_publish_over_ssh]" output = self.console.send('groovy/set_publish_over_ssh.groovy') print output def set_publish_over_ssh_add_hosts(self): """ adds a host to set_publish_over_ssh settings @note: Verify successful by going to: http://<jenkins_url>/configure """ print "\n[set_publish_over_ssh_add_hosts]" output = self.console.send('groovy/set_publish_over_ssh_add_hosts.groovy') print output def set_master_executors(self): """ adds jenkins master executors @note: Verify successful by going to: http://<jenkins_url>/computer """ print "\n[set_master_executors]" output = self.console.send('groovy/set_master_executors.groovy') if output: print output sys.exit(1) def set_views(self): """ Sets up all the jenkins views and tabs @note: Verify successful by going to: http://<jenkins_url> """ print "\n[set_views]" output = self.console.send('groovy/set_views.groovy') print output def set_ownership(self): """ Setups up the ownership plugin configurations @note: Verify successful by going to: http://<jenkins_url>/configure """ print "\n[set_ownership]" output = self.console.send('groovy/set_ownership.groovy') print output def set_RoleBasedAuth(self): """ Setup all RoleBaseAuth plugin configurations @note: Verify successful by going to: http://<jenkins_url>/configureSecurity """ print "\n[set_RoleBasedAuth]" output = self.console.send('groovy/set_RoleBasedAuth.groovy') print output def set_RoleBasedAuthPopulate(self): """ Populates the authentication model @note: Verify successful by going to: http://<jenkins_url>/role-strategy/manage-roles """ print "\n[set_RoleBasedAuthPopulate]" output = self.console.send('groovy/set_RoleBasedAuthPopulate.groovy') print output def set_ldap(self): """ Setup all ldap configurations @note: Verify successful by going to: http://<jenkins_url>/configureSecurity/ """ print "\n[set_ldap]" output = self.console.send('groovy/set_ldap.groovy') print output def set_global_credentials(self): """ Setup all the credentials user across jenkins @note: Verify successful by going to: http://<jenkins_url>/credential-store/ """ print "\n[set_global_credentials]" output = self.console.send('groovy/set_global_credentials.groovy') print output def set_managedScripts(self): """ Setup all Managed scripts @note: Verify successful by going to: http://<jenkins_url>/configfiles/ """ print "\n[set_managedScripts]" output = self.console.send('groovy/set_managedScripts.groovy') print output def set_markup_formatter(self): """ Setup when html text is submitted its run as html @note: Verify successful by going to: http://<jenkins_url>/configureSecurity/ """ print "\n[set_markup_formatter.]" output = self.console.send('groovy/set_markup_formatter.groovy') print output def set_remote_executors(self): """ Setup all remote executors @note: Verify successful by going to: http://<jenkins_url>/computer/ """ output = self.console.send('groovy/set_remote_executors.groovy') print output def sync_bootstrap(self): """ Creates a bootstrap job syncs it to jenkins instance @note: Verify successful by going to: http://<jenkins_url>/job/bootstrap/ """ print "\n[sync_bootstrap]" job_name = "bootstrap" bootstrap_xml = "" with open('jobs/bootstrap/config.xml', 'r') as f: bootstrap_xml = f.read() if self.src_jobs.post(bootstrap_xml, job_name)==False: print "[updates] updating latest bootstrap job" self.src_jobs.delete(job_name) self.src_jobs.post(bootstrap_xml, job_name) def run_bootstrap(self): """ run a bootstrap job remotely @param remote_action: supply an action """ print "\n[run_bootstrap]" rest = Restful(self.url) output = rest.send('POST', 'buildByToken/build?job=bootstrap&token=bootstrap',strict=False) def restart_jenkins(self): """ restart the jenkins instance for plugins and settings """ print "\n[restart_jenkins]" system = System_jenkins(self.url,self.url,self.token) system.restart() def remote_pass(self): """ pass nothing in to test _set_configs() """ pass def tests(self): """ run tests """ tests = Functional(self.url,self.url,self.token) tests.svn_checkout_master()