Class: Puppetfactory::Plugins::Gitea

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

Instance Attribute Summary

Attributes inherited from Puppetfactory::Plugins

#weight

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gitea

Returns a new instance of Gitea.



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
# File 'lib/puppetfactory/plugins/gitea.rb', line 8

def initialize(options)
  super(options)

  @cache_dir      = '/var/cache/puppetfactory/gitea'
  @lockfile       = '/var/cache/puppetfactory/gitea.lock'
  @templatedir    = options[:templatedir]
  @suffix         = options[:usersuffix]
  @controlrepo    = options[:controlrepo]
  @reponame       = File.basename(@controlrepo, '.git')
  @repopath       = "#{@cache_dir}/#{@reponame}"
  @gitea_upstream = options[:gitea_upstream]       || "https://github.com/puppetlabs-education/#{@controlrepo}"
  @gitea_cmd      = options[:gitea_cmd]            || '/home/git/go/bin/gitea'
  @admin_username = options[:gitea_admin_username] || 'root'
  @admin_password = options[:gitea_admin_password] || 'puppetlabs'
  @gitea_port     = options[:gitea_port]           || '3000'
  @gitea_user     = options[:gitea_user]           || 'git'
  @gitea_homedir  = Dir.home(@gitea_user)

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

  # gitea will scream if the admin's .ssh directory doesn't exist
  FileUtils.mkdir_p(File.expand_path("~#{@admin_username}/.ssh"))

  migrate_repo! unless File.directory?(@repopath)
end

Instance Method Details

#create(username, password) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppetfactory/plugins/gitea.rb', line 35

def create(username, password)
  begin
    # since we're changing directories, none of this can be done concurrently; lock it all.
    #
    # TODO: consider forking worker processes so that CWD doesn't leak between threads.
    File.open(@lockfile, 'w') do |file|
      file.flock(File::LOCK_EX)

      make_user(username, password)
      $logger.debug "Created Gitea user #{username}."
      make_branch(username)
      $logger.debug "Created Gitea branch #{username}."
      add_collaborator(@admin_username, @reponame, username, 'write')
      $logger.info "Created Gitea collaborator #{username}."
    end
  rescue => e
    $logger.error "Error configuring Gitea for #{username}: #{e.message}"
    $logger.error e.backtrace.join("\n")
    return false
  end

  true
end

#delete(username) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppetfactory/plugins/gitea.rb', line 59

def delete(username)
  begin
    remove_user(username)
    $logger.info "Removed Gitea user #{username}."
  rescue => e
    $logger.error "Error removing Gitea user #{username}: #{e.message}"
    return false
  end

  true
end