repository.rb 3.41 KB
Newer Older
Christopher Huhn's avatar
Christopher Huhn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# Copyright 2013-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
#
# Authors:
#  Christopher Huhn   <c.huhn@gsi.de>
#  Dennis Klein       <d.klein@gsi.de>
#  Victor Penso       <v.penso@gsi.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

property :arch,         Array,  default: ['amd64']
property :components,   Array,  default: %w[main]
property :deb_src,      [true, false], default: false
Christopher Huhn's avatar
Christopher Huhn committed
26
property :distribution, Array,  default: [node['lsb']['codename']]
Christopher Huhn's avatar
Christopher Huhn committed
27
28
29
30
31
property :user,         String, default: node['debmirror']['user']
property :key,          [String, nil]
property :keyring,      [String, nil], default: node['debmirror']['keyring']
property :server,       String, default: 'deb.debian.org'
property :proto,        String, default: 'http'
Christopher Huhn's avatar
Christopher Huhn committed
32
property :path,         String, default: '/'
Christopher Huhn's avatar
Christopher Huhn committed
33
34
35
36
37
property :mirror_dir,   [String, nil]
property :options,      Array,  default: []
property :script_dir,   String, default: node['debmirror']['script_dir']
property :rsync_extra,  [Array, String, nil],
         # turn strings into an array, default to ['trace']
Christopher Huhn's avatar
Christopher Huhn committed
38
39
40
41
42
43
44
         coerce: proc { |x|
  if x.is_a?(String)
    Array[x]
  else
    x.nil? ? %w[trace] : x
  end
}
Christopher Huhn's avatar
Christopher Huhn committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

default_action :add

action :add do
  # TODO: use ruby-gpgme for key management
  if new_resource.key

    package 'gnupg'

    home = node['debmirror']['base_dir']
    keyring = new_resource.keyring

    directory ::File.dirname(keyring) do
      owner new_resource.user
    end

    # fingerprint = `gpg --with-colons --with-fingerprint <<<"#{new_resource.key}" | grep ...`

    # TODO: avoid re-runs
    execute "Adding repository key for #{new_resource.name}" do
      command "gpg --no-default-keyring --keyring #{keyring}" \
              " --import <<-EOD\n#{new_resource.key}\nEOD"
      user user
      # without $HOME gpg tries to create /root/.gnupg :(
Christopher Huhn's avatar
Christopher Huhn committed
69
      environment('HOME' => home)
Christopher Huhn's avatar
Christopher Huhn committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
      # not_if { `gpg --no-default-keyring --keyring #{keyring} --with-colons --fingerprint`match %r{^fpr:+#{fingerpring}:$} }
    end
  end

  storage = new_resource.mirror_dir ||
            "#{node['debmirror']['base_dir']}/#{new_resource.name}"

  # Make sure the archive directory exists
  directory storage do
    owner new_resource.user
    recursive true
  end

  # Generate the mirror script
  template "#{new_resource.script_dir}/#{new_resource.name}.sh" do
    source 'debmirror.sh.erb'
    mode '0755'
    variables(
      release: new_resource.distribution,
      arch:    new_resource.arch,
      section: new_resource.components,
      server:  new_resource.server,
      proto:   new_resource.proto,
      path:    new_resource.path,
      storage: storage,
      keyring: new_resource.keyring,
      options: new_resource.options,
      rsync_extra: new_resource.rsync_extra
    )
  end
end

action :remove do
  file "#{script_dir}/#{name}.sh" do
Christopher Huhn's avatar
Christopher Huhn committed
104
    action :delete
Christopher Huhn's avatar
Christopher Huhn committed
105
106
107
108
  end

  # TODO: Remove key from keyring?
end