Class: Puppetfactory::Plugins::CodeManager

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

Instance Attribute Summary

Attributes inherited from Puppetfactory::Plugins

#weight

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CodeManager

Returns a new instance of CodeManager.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppetfactory/plugins/code_manager.rb', line 8

def initialize(options)
  super(options)

  @puppet       = options[:puppet]
  @gitserver    = options[:gitserver]
  @repomodel    = options[:repomodel]
  @controlrepo  = options[:controlrepo]
  @environments = options[:environments]
  @sources      = '/etc/puppetlabs/puppet/hieradata/sources.yaml' # we can hardcode these assumptions
  @meep         = '/etc/puppetlabs/enterprise/conf.d/common.conf' # because CM is PE only.
  @pattern      = "#{@gitserver}/%s/#{@controlrepo}"

  # the rest of this method is for the big boys only
  return unless Process.euid == 0

  # in case this is the first run and these doesn't exist yet
  unless File.exist? @sources
    FileUtils.mkdir_p File.dirname @sources
    File.open(@sources, 'w') { |f| f.write( { 'puppet_enterprise::master::code_manager::sources' => {} }.to_yaml) }
  end
  File.open(@meep, 'w') { |f| f.write('') } unless File.exist? @meep

  if options[:codedir]
    # ensure sane file permissions
    FileUtils.chown_R('pe-puppet', 'pe-puppet', options[:codedir])
    FileUtils.chmod(0755, options[:codedir])
  end
end

Instance Method Details

#create(username, password) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppetfactory/plugins/code_manager.rb', line 37

def create(username, password)
  begin
    environment = "#{@environments}/#{Puppetfactory::Helpers.environment_name(username)}"
    FileUtils.mkdir_p environment
    FileUtils.chown(username, 'pe-puppet', environment)
    FileUtils.chmod(0750, environment)

    # We don't need to add sources unless we're using a repo per student.
    if @repomodel == :peruser
      File.open(@sources) do |file|
        # make sure we don't have any concurrency issues
        file.flock(File::LOCK_EX)

        # We need to duplicate the sources list in the MEEP config for recovery options
        # I'd like to add it to code-manager.conf too and avoid the delay of running
        # puppet, but that's a race condition that we cannot accept.
        File.open(@meep) do |anotherfile|
          anotherfile.flock(File::LOCK_EX)

          source = {
            'remote'  => sprintf(@pattern, username),
            'prefix'  => (@repomodel == :peruser),
          }

          sources = YAML.load_file(@sources)
          meep    = Hocon::Parser::ConfigDocumentFactory.parse_file(@meep)

          sources['puppet_enterprise::master::code_manager::sources'][username] = source
          meep = meep.set_config_value(
                      "\"puppet_enterprise::master::code_manager::sources\".#{username}",
                      Hocon::ConfigValueFactory.from_any_ref(source)
                    )

          # Ruby 1.8.7, why don't you just go away now
          File.open(@sources, 'w') { |f| f.write(sources.to_yaml) }
          File.open(@meep, 'w')    { |f| f.write(meep.render)     }
          $logger.info "Created Code Manager source for #{username}"
        end
      end
    end
  rescue => e
    $logger.error "Cannot create Code Manager source for #{username}"
    $logger.error e.backtrace
    return false
  end

  true
end

#delete(username) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppetfactory/plugins/code_manager.rb', line 86

def delete(username)
  begin
    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

    File.open(@sources) do |file|
      # make sure we don't have any concurrency issues
      file.flock(File::LOCK_EX)

      # We need to duplicate the sources list in the MEEP config for recovery options
      # I'd like to add it to code-manager.conf too and avoid the delay of running
      # puppet, but that's a race condition that we cannot accept.
      File.open(@meep) do |anotherfile|
        anotherfile.flock(File::LOCK_EX)

        source = {
          'remote'  => sprintf(@pattern, username),
          'prefix'  => (@repomodel == :peruser),
        }

        sources = YAML.load_file(@sources)
        sources['puppet_enterprise::master::code_manager::sources'].delete username rescue nil

        meep = Hocon::Parser::ConfigDocumentFactory.parse_file(@meep)
        meep = meep.remove_value("\"puppet_enterprise::master::code_manager::sources\".#{username}")

        # Ruby 1.8.7, why don't you just go away now
        File.open(@sources, 'w') { |f| f.write(sources.to_yaml) }
        File.open(@meep, 'w')    { |f| f.write(meep.render)     }
        $logger.info "Removed Code Manager source for #{username}"
      end
    end
  rescue => e
    $logger.error "Cannot remove Code Manager source for #{username}"
    $logger.error e.backtrace
    return false
  end

  true
end

#deploy(username) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppetfactory/plugins/code_manager.rb', line 130

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

  output, status = Open3.capture2e(@puppet, 'code', 'deploy', environment, '--wait')
  unless status.success?
    $logger.error "Failed to deploy environment #{environment} for #{username}"
    $logger.error output
    return false
  end

  $logger.info "Deployed environment #{environment} for #{username}"
  true
end

#redeploy(username) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/puppetfactory/plugins/code_manager.rb', line 144

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

  true
end