#!/usr/bin/env ruby # frozen_string_literal: true require 'pwn' require 'optparse' opts = {} OptionParser.new do |options| options.banner = "USAGE: #{$PROGRAM_NAME} [opts] " options.on('-sIP', '--ip=IP', '') { |s| opts[:ip] = s } options.on('-dPORT', '--dest-port=PORT', '') do |port| opts[:port] = port end options.on('-UUSER', '--username=USER', '') do |user| opts[:username] = user end options.on('-PKEY', '--api-key=KEY', '') do |api_key| opts[:api_key] = api_key end options.on('-pPLUGIN', '--plugin=PLUGIN', '') do |plugins| opts[:plugins] = plugins end options.on('-L', '--[no-]list-available', '') do |list_available_bool| opts[:list_available_bool] = list_available_bool end options.on('-R', '--restart-jenkins', '') do |restart_bool| opts[:restart_jenkins] = restart_bool end end.parse! if opts.empty? puts `#{$PROGRAM_NAME} --help` exit 1 end ip = opts[:ip].to_s.scrub port = opts[:port].to_i username = opts[:username] api_key = opts[:api_key] plugins = opts[:plugins].to_s.scrub list_available_bool = if opts[:list_available_bool] true else false end restart_jenkins = true if opts[:restart_jenkins] ||= false if restart_jenkins puts 'Restarting Jenkins...' jenkins_obj.system.restart exit 0 end if port.positive? jenkins_obj = PWN::Plugins::Jenkins.connect( ip: ip, port: port, username: username, api_key: api_key ) else jenkins_obj = PWN::Plugins::Jenkins.connect( ip: ip, username: username, api_key: api_key ) end if list_available_bool puts jenkins_obj.plugin.list_available else plugin_arr = [] plugins.split(',').each do |p| plugin_arr.push(p.strip.chomp) end # In Reference to https://github.com/arangamani/jenkins_api_client/issues/287 # Bug in RubyGem jenkins_api_client 1.5.3 when interacting w/ Jenkins > 2.190 # i.e. # Crumbs turned on... # JenkinsApi::Exceptions::Forbidden: Access Denied... # Crumb expired... jenkins_obj.plugin.install(plugin_arr) end PWN::Plugins::Jenkins.disconnect(jenkins_obj: jenkins_obj)