Class: Puppetfactory::Plugins::ShellUser

Inherits:
Puppetfactory::Plugins show all
Defined in:
lib/puppetfactory/plugins/shell_user.rb

Instance Attribute Summary

Attributes inherited from Puppetfactory::Plugins

#weight

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ShellUser

Returns a new instance of ShellUser.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/puppetfactory/plugins/shell_user.rb', line 6

def initialize(options)
  super(options)

  @weight      = 1
  @usersuffix  = options[:usersuffix]
  @puppet      = options[:puppet]
  @master      = options[:master]
  @templatedir = options[:templatedir]
  @shell       = `which pfsh`.chomp

  # don't like this coupling, but I don't see a better way
  @groups = ['pe-puppet','puppetfactory']
  @groups << 'docker' if options[:plugins].include? :Docker
end

Instance Method Details

#create(username, password) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppetfactory/plugins/shell_user.rb', line 21

def create(username, password)
  unless username =~ /^[a-z_][a-z0-9_]{2,30}$/
    $logger.error "Invalid username. '#{username}' does not match regex /^[a-z_][a-z0-9_]{2,30}$/"
    raise "Invalid username #{username}."
  end

  crypted = password.crypt("$5$a1")
  output, status = Open3.capture2e('adduser', username, '-p', crypted, '-G', @groups.join(','), '--shell', @shell)
  unless status.success?
    $logger.error "Could not create system user #{username}: #{output}"
    raise "Could not create system user #{username}"
  end

  # Create shared folder to map and create puppet.conf
  FileUtils.mkdir_p "/home/#{username}/puppet"
  File.open("/home/#{username}/puppet/puppet.conf","w") do |f|
    f.write ERB.new(File.read("#{@templatedir}/puppet.conf.erb")).result(binding)
  end

  $logger.info "System user #{username} created successfully"
  true
end

#delete(username) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/puppetfactory/plugins/shell_user.rb', line 44

def delete(username)
  output, status = Open3.capture2e('userdel', '-fr', username)
  if status.success?
    $logger.info "System user #{username} removed successfully"
    return true
  else
    $logger.warn "Could not remove system user #{username}: #{output}"
    return false
  end
end

#userinfo(username, extended = false) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/puppetfactory/plugins/shell_user.rb', line 60

def userinfo(username, extended = false)
  # build the basic user object, can be added to by other plugins
  {
    :username => username,
    :console  => "#{username}@#{@usersuffix}",
    :certname => "#{username}.#{@usersuffix}",
  }
end

#usersObject



55
56
57
58
# File 'lib/puppetfactory/plugins/shell_user.rb', line 55

def users
  usernames = Dir.glob('/home/*').map { |path| File.basename path }
  usernames.reject { |username| ['centos', 'git', 'showoff', 'training', 'vagrant'].include? username }
end