~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/application/device.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/application'
 
2
require 'puppet/util/network_device'
 
3
 
 
4
 
 
5
class Puppet::Application::Device < Puppet::Application
 
6
 
 
7
  should_parse_config
 
8
  run_mode :agent
 
9
 
 
10
  attr_accessor :args, :agent, :host
 
11
 
 
12
  def preinit
 
13
    # Do an initial trap, so that cancels don't get a stack trace.
 
14
    Signal.trap(:INT) do
 
15
      $stderr.puts "Cancelling startup"
 
16
      exit(0)
 
17
    end
 
18
 
 
19
    {
 
20
      :waitforcert => nil,
 
21
      :detailed_exitcodes => false,
 
22
      :verbose => false,
 
23
      :debug => false,
 
24
      :centrallogs => false,
 
25
      :setdest => false,
 
26
    }.each do |opt,val|
 
27
      options[opt] = val
 
28
    end
 
29
 
 
30
    @args = {}
 
31
  end
 
32
 
 
33
  option("--centrallogging")
 
34
  option("--debug","-d")
 
35
  option("--verbose","-v")
 
36
 
 
37
  option("--detailed-exitcodes") do |arg|
 
38
    options[:detailed_exitcodes] = true
 
39
  end
 
40
 
 
41
  option("--logdest DEST", "-l DEST") do |arg|
 
42
    begin
 
43
      Puppet::Util::Log.newdestination(arg)
 
44
      options[:setdest] = true
 
45
    rescue => detail
 
46
      puts detail.backtrace if Puppet[:debug]
 
47
      $stderr.puts detail.to_s
 
48
    end
 
49
  end
 
50
 
 
51
  option("--waitforcert WAITFORCERT", "-w") do |arg|
 
52
    options[:waitforcert] = arg.to_i
 
53
  end
 
54
 
 
55
  option("--port PORT","-p") do |arg|
 
56
    @args[:Port] = arg
 
57
  end
 
58
 
 
59
    def help
 
60
      <<-HELP
 
61
 
 
62
puppet-device(8) -- Manage remote network devices
 
63
========
 
64
 
 
65
SYNOPSIS
 
66
--------
 
67
Retrieves all configurations from the puppet master and apply
 
68
them to the remote devices configured in /etc/puppet/device.conf.
 
69
 
 
70
Currently must be run out periodically, using cron or something similar.
 
71
 
 
72
USAGE
 
73
-----
 
74
  puppet device [-d|--debug] [--detailed-exitcodes] [-V|--version]
 
75
                [-h|--help] [-l|--logdest syslog|<file>|console]
 
76
                [-v|--verbose] [-w|--waitforcert <seconds>]
 
77
 
 
78
 
 
79
DESCRIPTION
 
80
-----------
 
81
Once the client has a signed certificate for a given remote device, it will 
 
82
retrieve its configuration and apply it.
 
83
 
 
84
USAGE NOTES
 
85
-----------
 
86
One need a /etc/puppet/device.conf file with the following content:
 
87
 
 
88
[remote.device.fqdn]
 
89
type <type>
 
90
url <url>
 
91
 
 
92
where:
 
93
 * type: the current device type (the only value at this time is cisco)
 
94
 * url: an url allowing to connect to the device
 
95
 
 
96
Supported url must conforms to:
 
97
 scheme://user:password@hostname/?query
 
98
 
 
99
 with:
 
100
  * scheme: either ssh or telnet
 
101
  * user: username, can be omitted depending on the switch/router configuration
 
102
  * password: the connection password
 
103
  * query: this is device specific. Cisco devices supports an enable parameter whose
 
104
  value would be the enable password.
 
105
 
 
106
OPTIONS
 
107
-------
 
108
Note that any configuration parameter that's valid in the configuration file
 
109
is also a valid long argument.  For example, 'server' is a valid configuration
 
110
parameter, so you can specify '--server <servername>' as an argument.
 
111
 
 
112
* --debug:
 
113
  Enable full debugging.
 
114
 
 
115
* --detailed-exitcodes:
 
116
  Provide transaction information via exit codes.  If this is enabled, an
 
117
  exit code of '2' means there were changes, and an exit code of '4' means
 
118
  that there were failures during the transaction. This option only makes
 
119
  sense in conjunction with --onetime.
 
120
 
 
121
* --help:
 
122
  Print this help message
 
123
 
 
124
* --logdest:
 
125
  Where to send messages.  Choose between syslog, the console, and a log file.
 
126
  Defaults to sending messages to syslog, or the console if debugging or
 
127
  verbosity is enabled.
 
128
 
 
129
* --verbose:
 
130
  Turn on verbose reporting.
 
131
 
 
132
* --waitforcert:
 
133
  This option only matters for daemons that do not yet have certificates
 
134
  and it is enabled by default, with a value of 120 (seconds).  This causes
 
135
  +puppet agent+ to connect to the server every 2 minutes and ask it to sign a
 
136
  certificate request.  This is useful for the initial setup of a puppet
 
137
  client.  You can turn off waiting for certificates by specifying a time
 
138
  of 0.
 
139
 
 
140
EXAMPLE
 
141
-------
 
142
      $ puppet device --server puppet.domain.com
 
143
 
 
144
AUTHOR
 
145
------
 
146
Brice Figureau
 
147
 
 
148
 
 
149
COPYRIGHT
 
150
---------
 
151
Copyright (c) 2011 Puppet Labs, LLC 
 
152
Licensed under the Apache 2.0 License
 
153
      HELP
 
154
    end
 
155
 
 
156
 
 
157
  def main
 
158
    vardir = Puppet[:vardir]
 
159
    confdir = Puppet[:confdir]
 
160
    certname = Puppet[:certname]
 
161
 
 
162
    # find device list
 
163
    require 'puppet/util/network_device/config'
 
164
    devices = Puppet::Util::NetworkDevice::Config.devices
 
165
    if devices.empty?
 
166
      Puppet.err "No device found in #{Puppet[:deviceconfig]}"
 
167
      exit(1)
 
168
    end
 
169
    devices.each_value do |device|
 
170
      begin
 
171
        Puppet.info "starting applying configuration to #{device.name} at #{device.url}"
 
172
 
 
173
        # override local $vardir and $certname
 
174
        Puppet.settings.set_value(:confdir, File.join(Puppet[:devicedir], device.name), :cli)
 
175
        Puppet.settings.set_value(:vardir, File.join(Puppet[:devicedir], device.name), :cli)
 
176
        Puppet.settings.set_value(:certname, device.name, :cli)
 
177
 
 
178
        # this will reload and recompute default settings and create the devices sub vardir, or we hope so :-)
 
179
        Puppet.settings.use :main, :agent, :ssl
 
180
 
 
181
        # this init the device singleton, so that the facts terminus
 
182
        # and the various network_device provider can use it
 
183
        Puppet::Util::NetworkDevice.init(device)
 
184
 
 
185
        # ask for a ssl cert if needed, but at least
 
186
        # setup the ssl system for this device.
 
187
        setup_host
 
188
 
 
189
        require 'puppet/configurer'
 
190
        configurer = Puppet::Configurer.new
 
191
        report = configurer.run(:network_device => true)
 
192
      rescue => detail
 
193
        puts detail.backtrace if Puppet[:trace]
 
194
        Puppet.err detail.to_s
 
195
      ensure
 
196
        Puppet.settings.set_value(:vardir, vardir, :cli)
 
197
        Puppet.settings.set_value(:confdir, confdir, :cli)
 
198
        Puppet.settings.set_value(:certname, certname, :cli)
 
199
      end
 
200
    end
 
201
  end
 
202
 
 
203
  # Handle the logging settings.
 
204
  def setup_logs
 
205
    if options[:debug] or options[:verbose]
 
206
      Puppet::Util::Log.newdestination(:console)
 
207
      if options[:debug]
 
208
        Puppet::Util::Log.level = :debug
 
209
      else
 
210
        Puppet::Util::Log.level = :info
 
211
      end
 
212
    end
 
213
 
 
214
    Puppet::Util::Log.newdestination(:syslog) unless options[:setdest]
 
215
  end
 
216
 
 
217
  def setup_host
 
218
    @host = Puppet::SSL::Host.new
 
219
    waitforcert = options[:waitforcert] || (Puppet[:onetime] ? 0 : 120)
 
220
    cert = @host.wait_for_cert(waitforcert)
 
221
  end
 
222
 
 
223
  def setup
 
224
    setup_logs
 
225
 
 
226
    args[:Server] = Puppet[:server]
 
227
    if options[:centrallogs]
 
228
      logdest = args[:Server]
 
229
 
 
230
      logdest += ":" + args[:Port] if args.include?(:Port)
 
231
      Puppet::Util::Log.newdestination(logdest)
 
232
    end
 
233
 
 
234
    Puppet.settings.use :main, :agent, :device, :ssl
 
235
 
 
236
    # Always ignoreimport for agent. It really shouldn't even try to import,
 
237
    # but this is just a temporary band-aid.
 
238
    Puppet[:ignoreimport] = true
 
239
 
 
240
    # We need to specify a ca location for all of the SSL-related i
 
241
    # indirected classes to work; in fingerprint mode we just need
 
242
    # access to the local files and we don't need a ca.
 
243
    Puppet::SSL::Host.ca_location = :remote
 
244
 
 
245
    Puppet::Transaction::Report.indirection.terminus_class = :rest
 
246
 
 
247
    # Override the default; puppetd needs this, usually.
 
248
    # You can still override this on the command-line with, e.g., :compiler.
 
249
    Puppet[:catalog_terminus] = :rest
 
250
 
 
251
    Puppet[:facts_terminus] = :network_device
 
252
 
 
253
    Puppet::Resource::Catalog.indirection.cache_class = :yaml
 
254
  end
 
255
end