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.



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

def initialize(options)
  super(options)

  @master       = options[:master]
  @confdir      = options[:confdir]
  @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



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
53
54
55
# File 'lib/puppetfactory/plugins/user_environment.rb', line 21

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_R(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



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

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

#deploy(username) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppetfactory/plugins/user_environment.rb', line 62

def deploy(username)
  # TODO: temporary hack to keep this from disrupting current deliveries.
  return if @codestage == @environments
  environment = Puppetfactory::Helpers.environment_name(username)

  begin
    FileUtils.cp_r("#{@codestage}/#{environment}/*", "#{@environments}/#{environment}/")
    FileUtils.chown_R('pe-puppet', 'pe-puppet', "#{@environments}/#{environment}")

    RestClient::Resource.new(
      "https://#{@master}:8140/puppet-admin-api/v1/environment-cache?environment=#{environment}",
      :ssl_client_cert  =>  OpenSSL::X509::Certificate.new(File.read("#{@confdir}/ssl/certs/#{@master}.pem")),
      :ssl_client_key   =>  OpenSSL::PKey::RSA.new(File.read("#{@confdir}/ssl/private_keys/#{@master}.pem")),
      :ssl_ca_file      =>  "#{@confdir}/ssl/ca/ca_crt.pem",
      :verify_ssl       =>  OpenSSL::SSL::VERIFY_PEER
    ).delete
  rescue => e
    $logger.error "Deploying environment #{environment} failed: #{e.message}"
    $logger.debug e.backtrace
    raise "Error deploying environment #{environment}."
  end
end

#redeploy(username) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/puppetfactory/plugins/user_environment.rb', line 85

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