Class: PuppetPulp::PulpAdmin

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-pulp/pulp_admin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ PulpAdmin

Returns a new instance of PulpAdmin.



8
9
10
11
# File 'lib/puppet-pulp/pulp_admin.rb', line 8

def initialize(username, password)
  @username = username
  @password = password
end

Instance Attribute Details

#loginObject

Returns the value of attribute login.



6
7
8
# File 'lib/puppet-pulp/pulp_admin.rb', line 6

def 
  @login
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/puppet-pulp/pulp_admin.rb', line 6

def password
  @password
end

Instance Method Details

#create(repo_id, params = {}) ⇒ Object



13
14
15
16
17
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
# File 'lib/puppet-pulp/pulp_admin.rb', line 13

def create(repo_id, params = {})
  

  cmd = "pulp-admin puppet repo create --repo-id=\"#{repo_id}\""

  [:display_name,
   :description,
   :feed,
   :serve_http,
   :serve_https].each do |m|
    cmd << " --#{m.to_s.gsub '_', '-'}=\"#{params[m]}\"" unless params[m].nil?
  end

  if params[:queries]
    cmd << " --queries=\"#{params[:queries].join ','}\""
  end

  if params[:notes]
    cmd << " " + params[:notes].keys.sort.map { |k| "--note \"#{k}=#{params[:notes][k]}\"" }.join(' ')
  end

  output = `#{cmd}`
  raise "Could not create repo: #{output}" unless output =~ /Successfully created repository \[#{repo_id}\]/

  if params[:schedules]
    params[:schedules].each do |s|
      output = `pulp-admin puppet repo sync schedules create --repo-id=\"#{repo_id}\" -s \"#{s}\"`
      raise "Could not create schedule: #{output}" unless output =~ /Schedule successfully created/
    end
  end
end

#destroy(repo_id) ⇒ Object



45
46
47
48
49
# File 'lib/puppet-pulp/pulp_admin.rb', line 45

def destroy(repo_id)
  
  output = `pulp-admin puppet repo delete --repo-id="#{repo_id}"`
  raise "Could not remove repo: #{output}" unless output =~ /Repository \[#{repo_id}\] successfully deleted/
end

#reposObject



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
85
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet-pulp/pulp_admin.rb', line 51

def repos
  

  output = `pulp-admin puppet repo list --details`
  repos = parse_repos(output).map do |repo|
    description = repo['Description'] == 'None' ? nil : repo['Description']
    distributors_config = repo['Distributors']['Config']
    importers = repo['Importers']
    importers_config = importers['Config']
    feed = importers_config['Feed'] unless importers_config.nil?
    notes = repo['Notes'].nil? ? { } : repo['Notes']

    queries = importers_config && importers_config['Queries'] || ''
    queries = queries.split(/,/).map { |x| x.strip }

    schedules = importers && importers['Scheduled Syncs'] || ''
    schedules = schedules.split(/,/).map { |x| x.strip }

    serve_http = distributors_config['Serve Http'] unless distributors_config.nil?
    serve_http = serve_http.is_a?(String) ? serve_http == 'True' : true

    serve_https = distributors_config['Serve Https'] unless distributors_config.nil?
    serve_https = serve_https == 'True'

    props = {
      :id => repo['Id'],
      :display_name => repo['Display Name'],
      :description => description,
      :notes => notes,
      :feed => feed,
      :queries => queries,
      :schedules => schedules,
      :serve_http => serve_http,
      :serve_https => serve_https
    }

    # UGARY -- We might want to be 1.8-able one day
    result = Object.new
    singleton_class = class << result; self end

    setter = lambda do |val|
      `pulp-admin puppet repo update --repo-id=#{props[:id]} #{val}`
    end

    #getters
    props.each do |k,v|
      singleton_class.send(:define_method, k, lambda { v })
    end

    [:display_name,
     :description,
     :feed,
     :serve_http,
     :serve_https].each do |m|
      singleton_class.send :define_method, "#{m}=" do |v|
        setter.call "--#{m.to_s.gsub('_', '-')}=\"#{v}\""
      end
    end

    singleton_class.send :define_method, :queries= do |arr|
      setter.call "--queries=\"#{arr.join ','}\""
    end

    # Easier to test
    me = self
    singleton_class.send :define_method, :schedules= do |arr|
      repos = me.send :`, "pulp-admin puppet repo sync schedules list --repo-id=\"#{props[:id]}\""
      repos.split(/\n/).each do |l|
        if l =~ /^Id:\s*(.+)/
          output = me.send :`, "pulp-admin puppet repo sync schedules delete --repo-id=\"#{props[:id]}\" --schedule-id=\"#{$1}\""
          raise "Could not delete old schedule: #{output}" unless output =~ /Schedule successfully deleted/
        end
      end

      arr.each do |s|
        output = me.send :`, "pulp-admin puppet repo sync schedules create --repo-id=\"#{props[:id]}\" -s \"#{s}\""
        raise "Could not create schedule: #{output}" unless output =~ /Schedule successfully created/
      end
    end

    singleton_class.send :define_method, :notes= do |map|
      notes = []
      map.keys.sort.each do |k|
        notes << "--note \"#{k}=#{map[k]}\""
      end
      setter.call notes.join ' '
    end

    result
  end

  repos.inject({}) do |memo,x|
    memo.merge!({x.id => x})
  end
end