Created
May 13, 2014 00:37
-
-
Save mechamogera/a0763cc30b71971dfe2f to your computer and use it in GitHub Desktop.
JenkinsのRole-based Authorization Strategy pluginを利用した認証においてUserをRoleにAssignする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A sample Gemfile | |
source "https://rubygems.org" | |
gem 'trollop' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'trollop' | |
require 'rexml/document' | |
require 'rexml/formatters/pretty' | |
require 'fileutils' | |
opts = Trollop::options do | |
opt :role_type, "role type", :type => :string, :default => 'globalRoles' | |
opt :role_name, "role name", :type => :string, :default => 'admin' | |
opt :user, "user", :type => :strings, :multi => true, :required => true | |
opt :config_path, "jenkins config.xml path", :type => :string, :default => '/var/lib/jenkins/config.xml' | |
opt :backup , "backup config.xml", :default => true | |
opt :output_stdout, 'do not update config.xml but output changed xml to stdout', :default => false | |
end | |
opts[:user].flatten! | |
doc = nil | |
File.open(opts[:config_path]) do |f| | |
doc = REXML::Document.new(f) | |
end | |
sids = doc.elements["hudson/authorizationStrategy/roleMap[@type='#{opts[:role_type]}']/role[@name='#{opts[:role_name]}']/assignedSIDs"] | |
sids.each_element do |element| | |
sids.delete_element(element) | |
end | |
opts[:user].each do |user| | |
sids.add_element("sid").add_text(user) | |
end | |
pretty_formatter = REXML::Formatters::Pretty.new | |
if opts[:output_stdout] | |
pretty_formatter.write(doc, $stdout) | |
else | |
FileUtils.cp(opts[:config_path], "#{opts[:config_path]}.bak.#{Time.now.strftime("%Y%m%d%H%M%S")}") if opts[:backup] | |
File.open(opts[:config_path], "w") do |f| | |
pretty_formatter.write(doc, f) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment