Commit ca0c87d4 authored by Adam Lewenberg's avatar Adam Lewenberg
Browse files

skip configuration file if it does not exist

parent 0f425342
AllCops:
TargetRubyVersion: 2.5
Style/MethodCallWithoutArgsParentheses:
Enabled: false
Style/MultilineIfThen:
Enabled: false
Style/RedundantParentheses:
Enabled: false
Style/ParenthesesAroundCondition:
Enabled: false
Style/NegatedIf:
Enabled: false
Style/TrailingCommaInLiteral:
Enabled: false
Style/IfUnlessModifier:
Enabled: false
Style/RedundantSelf:
Enabled: false
Style/DefWithParentheses:
Enabled: false
Style/BarePercentLiterals:
Enabled: false
......@@ -10,10 +10,10 @@
# NOTE: The code assumes that the script is located in "tools" subdirectory
# in the root of the repo.
require 'yaml'
require 'pathname'
require 'optparse'
require 'English'
def exit_with_error(msg)
puts "error: #{msg}"
......@@ -28,7 +28,6 @@ end
########################################################################
########################################################################
class ManageUser
attr_accessor :verbose
attr_accessor :dryrun
attr_accessor :hiera_data
......@@ -38,7 +37,7 @@ class ManageUser
def initialize()
@verbose = false
@dryrun = true
@dryrun = false
@data_source = nil
@git_commit = false
@classname = 'users::stanford_users'
......@@ -60,6 +59,11 @@ class ManageUser
end
def read_configuration()
if (!File.file?(@config_file)) then
# No config file, so nothing to do.
return
end
config_data = YAML.load_file(@config_file)
if (config_data.key?('classname')) then
......@@ -94,7 +98,7 @@ class ManageUser
@data_source = repo_root.join("modules/#{classname_prefix}/data/common.yaml")
self.progress("data_source is #{@data_source}")
unless File.exist?(@data_source)
exit_with_error("Unable to locate data source at #{@data_source.to_s}")
exit_with_error("Unable to locate data source at #{@data_source}")
end
self.set_hiera_data()
......@@ -113,15 +117,16 @@ class ManageUser
def get_user_info_from_lsdb(sunetid)
# lookup a user's information in lsdb via a remctl call.
account = `remctl lsdb user show #{sunetid}`
rv = $?
rv = $CHILD_STATUS
self.progress("remctl return status is '#{rv}'")
self.progress("remctl returned '#{account}'")
# if the remctl command failed, then there is no ticket or remctl
# client is not installed.
unless rv.success?
msg = 'Please make sure remctl is installed, you have a valid kerberos ticket,
and you have the rights to execute remctl lsdb queries.'
msg = 'Please make sure remctl is installed, ' \
'you have a valid kerberos ticket, ' \
'and you have the rights to execute remctl lsdb queries.'
exit_with_error(msg)
end
......@@ -143,10 +148,10 @@ class ManageUser
account = self.get_user_info_from_lsdb(sunetid)
# create a hash with all user attributes
u = Hash.new
u = {}
account.each_line do |line|
attrib, val = line.strip.split(/:\s*/, 2)
next if val.nil? or val.empty?
next if val.nil? || val.empty?
u[attrib] = val unless u.key?(attrib)
end
self.progress("u is #{u}")
......@@ -156,7 +161,7 @@ class ManageUser
# if there is no attribute 'Services' or there is no 'afs' value there
# then the user has a base sponsored sunet id or inactive
unless u.key?('Services') and u['Services'].include?('afs')
unless u.key?('Services') && u['Services'].include?('afs')
u['home'] = "/home/#{sunetid}"
end
......@@ -173,13 +178,14 @@ class ManageUser
end
def delete_user(sunetid)
# check if the user already exists in the data source
# check if the user already exists in the data source
if @hiera_data[@classname].key?(sunetid)
just_deleted = @hiera_data[@classname].delete(sunetid)
self.progress('Deleted the following data from hiera:')
self.progress(just_deleted.to_yaml)
else
exit_without_error("The user #{sunetid} is not in the hiera data. Nothing to do.")
exit_without_error("The user #{sunetid} is not in the hiera data. " \
'Nothing to do.')
end
self.save('delete', sunetid)
......@@ -206,65 +212,69 @@ class ManageUser
@hiera_data[@classname].each do |sunetid_data|
sunetid = sunetid_data[0]
data = sunetid_data[1]
uid = data['uid']
print <<~HEREDOC
#{sunetid}:
uid: #{data['uid']}
gid: #{data['gid']}
comment: #{data['comment']}
home: #{data['home']}
shell: #{data['shell']}
#{sunetid}:
uid: #{data['uid']}
gid: #{data['gid']}
comment: #{data['comment']}
home: #{data['home']}
shell: #{data['shell']}
HEREDOC
end
end
def git_commit_message(action, sunetid)
case action
when 'add'
msg = "added user #{sunetid} to"
when 'delete'
msg = "deleted user #{sunetid} from"
when 'refresh'
msg = 'refreshed all users in'
else
exit_with_error("cannot do a git commit with action '#{action}'")
end
return "#{msg} users module data source (#{@classname})."
end
def make_git_commit(action, sunetid)
if (@git_commit) then
if (@dryrun) then
self.dryrun_progress("committing to git")
self.dryrun_progress('committing to git')
else
# commit the change to git, watch for errors
# construct git commit message.
if (action == 'add') then
msg = "added user #{sunetid} to"
elsif (action == 'delete') then
msg = "deleted user #{sunetid} from"
elsif (action == 'refresh') then
msg = "refreshed all users in"
else
exit_with_error("cannot do a git commit with action '#{action}'")
end
commit_msg = "#{msg} users module data source (#{@classname})."
commit_msg = git_commit_message(action, sunetid)
if system('git add ' + @data_source.to_s)
unless system(%Q[git commit -m "#{commit_msg}"])
exit_with_error('Error committing the change to git. Please do it manually.')
unless system(%Q(git commit -m "#{commit_msg}"))
exit_with_error('Error committing the change to git. ' \
'Please do it manually.')
end
else
exit_with_error('Error adding the data source file to git. Please add and commit manually.')
exit_with_error('Error adding the data source file to git. ' \
'Please add and commit manually.')
end
puts <<-EOS
puts <<~INSTRUCT
User "#{sunetid}" has been successfully added. The commit has not yet been
pushed to the remote. To complete this change, run:
User "#{sunetid}" has been successfully added. The commit has not yet been
pushed to the remote. To complete this change, run:
git push
EOS
git push
INSTRUCT
end
else
# Skipping git commit
self.progress("skipping git commit")
self.progress('skipping git commit')
end
return
end
def write_data_source_file()
if (@dryrun) then
dryrun_progress("writing changes to '#{@data_source}'")
else
File.open(@data_source, 'w') {|f| f.write @hiera_data.to_yaml }
File.open(@data_source, 'w') { |f| f.write @hiera_data.to_yaml }
end
end
......@@ -272,9 +282,7 @@ class ManageUser
self.write_data_source_file()
self.make_git_commit(action, sunetid)
end
end
########################################################################
########################################################################
......@@ -285,6 +293,7 @@ ARGV << '-h' if ARGV.empty?
options = {}
optparse = OptionParser.new do |opts|
# rubocop:disable Layout/LineLength
opts.banner = "Usage: manage-user (add|delete) <sunet_id> [-v] [-c classname]\n" \
' manage-user (list|refresh) [-v] [-c classname]'
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
......@@ -300,10 +309,12 @@ optparse = OptionParser.new do |opts|
end
class_default = 'users'
opts.on('-c', '--classname CLASSNAME',
"-c Puppet class where users are defined (defaults to '#{class_default}')") do |lib|
opts.on('-c',
'--classname CLASSNAME',
"-c Puppet class where users are defined (defaults to '#{class_default}')") do |lib|
options[:classname] = lib
end
# rubocop:enable Layout/LineLength
end.parse!
optparse.parse!
......@@ -328,6 +339,12 @@ if (options.key?(:classname)) then
muser.progress("overriding classname with #{classname}")
end
valid_actions = %w[
add
delete
refresh
list
]
action = ARGV.shift
sunetid = ARGV.shift
......@@ -337,12 +354,13 @@ muser.progress("classname: #{classname}")
muser.progress("dryrun: #{muser.dryrun}")
muser.progress("gitcommit: #{muser.git_commit}")
if ((action =~ /^(add|delete)$/) && (! sunetid)) then
if ((action =~ /^(add|delete)$/) && (!sunetid)) then
exit_with_error("You must provide a sunetid with the #{action} action.")
end
if (action !~ /^(add|delete|refresh|list)$/) then
exit_with_error("The action must be one of 'add', 'delete', 'list', or 'refresh'")
if (!valid_actions.include?(action)) then
actions1 = valid_actions.map { |a| "'#{a}'" }
exit_with_error("The action must be one of: #{actions1.join(', ')}")
end
## # #### # #### # #### # #### # #### # #### # #### # #### # #### # ####
......@@ -354,12 +372,15 @@ muser.progress(muser.hiera_data)
## # #### # #### # #### # #### # #### # #### # #### # #### # #### # ####
### Take action.
if (action == 'add') then
case action
when 'add'
muser.add_user(sunetid)
elsif (action == 'delete') then
when 'delete'
muser.delete_user(sunetid)
elsif (action == 'refresh') then
when 'refresh'
muser.refresh_data()
elsif (action == 'list') then
when 'list'
muser.list_data()
else
exit_with_error("unknown action '#{action}'")
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment