Class: Puppetfactory::Plugins::UserEnvironment

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

Instance Attribute Summary

Attributes inherited from Puppetfactory::Plugins

#weight

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UserEnvironment

Returns a new instance of UserEnvironment.



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

def initialize(options)
  super(options)

  @codedir      = options[:codedir]
  @stagedir     = options[:stagedir]
  @puppetcode   = options[:puppetcode]
  @templatedir  = options[:templatedir]
  @environments = options[:environments]
  @repomodel    = options[:repomodel]
  @codestage   = "#{@stagedir}/environments"
end

Instance Method Details

#create(username, password) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppetfactory/plugins/user_environment.rb', line 18

def create(username, password)
  environment = "#{@codestage}/#{Puppetfactory::Helpers.environment_name(username)}"

  begin
    # configure environment
    FileUtils.mkdir_p "#{environment}/manifests"
    FileUtils.mkdir_p "#{environment}/modules"

    File.open("#{environment}/manifests/site.pp", 'w') do |f|
      f.write ERB.new(File.read("#{@templatedir}/site.pp.erb")).result(binding)
    end

    # Copy userprefs module into user environment
    if Dir.exist?("#{@codedir}/modules/userprefs") then
      FileUtils.cp_r("#{@codedir}/modules/userprefs", "#{environment}/modules/")
    elsif Dir.exist?("#{@environments}/production/modules/userprefs") then
      FileUtils.cp_r("#{@environments}/production/modules/userprefs", "#{environment}/modules/")
    else
      $logger.warn "Module userprefs not found in global or production modulepath"
    end

    # make sure the user and pe-puppet can access all the needful
    FileUtils.chown_R(username, 'pe-puppet', environment)
    FileUtils.chmod(0750, environment)

    deploy(username)

  rescue => e
    $logger.error "Error creating user environment for #{username}"
    $logger.error e.message
    return false
  end

  true
end

#delete(username) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/puppetfactory/plugins/user_environment.rb', line 54

def delete(username)
  environment = "#{@environments}/#{Puppetfactory::Helpers.environment_name(username)}"
  FileUtils.rm_rf environment

  # also delete any prefixed environments. Is this even a good idea?
  FileUtils.rm_rf "#{@environments}/#{username}_*" if @repomodel == :peruser
end

#deploy(username) ⇒ Object



62
63
64
65
66
# File 'lib/puppetfactory/plugins/user_environment.rb', line 62

def deploy(username)
  environment = Puppetfactory::Helpers.environment_name(username)

  FileUtils.cp_r("#{@codestage}/#{environment}/.", "#{@environments}/#{environment}")
end

#redeploy(username) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppetfactory/plugins/user_environment.rb', line 68

def redeploy(username)
  begin
    if username == 'production'
      raise "Can't redeploy production environment"
    end
    delete(username)
    deploy(username)

  rescue => e
    raise "Error redeploying environment #{username}: #{e.message}"
  end
end