Class: Pawnee::Base

Inherits:
Thor
  • Object
show all
Includes:
Actions, Roles, Invocation, Modified, SshConnection, Thor::Actions, ThorSsh::Actions
Defined in:
lib/pawnee/pawnee/base.rb,
lib/pawnee/pawnee/roles.rb,
lib/pawnee/pawnee/setup.rb

Overview

The pawnee gem provides the Pawnee::Base class, which includes actions from the thor gem, thor-ssh gem, and the pawnee gem its self. Any class that inherits from Pawnee::Base will automatically be registered as a recipe.

Direct Known Subclasses

CLI

Defined Under Namespace

Modules: Roles Classes: Railtie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Roles

included

Methods included from SshConnection

#reconnect!

Methods included from Modified

included, #modified?, #modify_block, #track_modification!

Methods included from Invocation

#pawnee_setup_invocations

Methods included from Actions

#add_user_to_group, #compile, #create_user, #delete_user, #insert_into_file, #install_package, #install_packages, #installed_package_version, #package_installed?, #pawnee_setup_actions, #remove_package, #remove_packages, #update_package_list, #user

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ Base

Creates an instance of the pawnee recipe

Parameters

args<Array>

An array of objects. The objects are applied to their respective accessors declared with argument.

options<Hash>

An options hash that will be available as self.options. The hash given is converted to a hash with indifferent access, magic predicates (options.skip?) and then frozen.

config<Hash>

Configuration for this Thor class.

def initialize(*args)

Options

:server  -  This can be:
              1) a connected ssh connection (created by Net::SSH.start)
              2) an Array of options to pass to Net::SSH.start
              3) a domain name for the first argument to Net::SSH.start


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
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pawnee/pawnee/base.rb', line 48

def initialize(args=[], options={}, config={})
  pawnee_setup_invocations(args, options, config)

  pawnee_setup_actions(args, options, config) do
    
    # We need to change Thor::Options to use Pawnee::Options on 
    # invoke so we can include the defaults from the config file,
    # so here we copy in the initialize from thor/base.rb#initialize
    parse_options = self.class.class_options
  
    # The start method splits inbound arguments at the first argument
    # that looks like an option (starts with - or --). It then calls
    # new, passing in the two halves of the arguments Array as the
    # first two parameters.
  
    if options.is_a?(Array)
      task_options  = config.delete(:task_options) # hook for start
      parse_options = parse_options.merge(task_options) if task_options
      array_options, hash_options = options, {}
    else
      # Handle the case where the class was explicitly instantiated
      # with pre-parsed options.
      array_options, hash_options = [], options
    end
  
    # Let Thor::Options parse the options first, so it can remove
    # declared options from the array. This will leave us with
    # a list of arguments that weren't declared.
    # --- We change Thor::Options to Pawnee::Options to pull in
    # the config default's
    opts = Pawnee::Options.new(parse_options, hash_options)
    self.options = opts.parse(array_options)
  
    # If unknown options are disallowed, make sure that none of the
    # remaining arguments looks like an option.
    opts.check_unknown! if self.class.check_unknown_options?(config)
  
    # Add the remaining arguments from the options parser to the
    # arguments passed in to initialize. Then remove any positional
    # arguments declared using #argument (this is primarily used
    # by Thor::Group). Tis will leave us with the remaining
    # positional arguments.
    thor_args = Thor::Arguments.new(self.class.arguments)
    thor_args.parse(args + opts.remaining).each { |k,v| send("#{k}=", v) }
    args = thor_args.remaining
  
    @args = args
    #-- end copy from thor/base.rb#initialize
  end
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



28
29
30
# File 'lib/pawnee/pawnee/base.rb', line 28

def server
  @server
end

#server_optionsObject

Returns the value of attribute server_options.



28
29
30
# File 'lib/pawnee/pawnee/base.rb', line 28

def server_options
  @server_options
end

#setup_with_connectionObject

Returns the value of attribute setup_with_connection.



28
29
30
# File 'lib/pawnee/pawnee/base.rb', line 28

def setup_with_connection
  @setup_with_connection
end

Class Method Details

.gem_nameObject

Guess the gem name based on the class name



117
118
119
# File 'lib/pawnee/pawnee/base.rb', line 117

def self.gem_name
  self.name.gsub(/[:][:]Base$/, '').gsub(/^[^:]+[:][:]/, '').gsub(/([A-Z]+)([A-Z][a-z])/,'\1-\2').gsub(/([a-z\d])([A-Z])/,'\1-\2').downcase
end

.setup(gem_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pawnee/pawnee/setup.rb', line 11

def self.setup(gem_name)
  # Setup the railtie
  require "pawnee/#{gem_name.gsub(/^pawnee[-]/, '')}/base"
  
  if defined?(Rails) && defined?(Rails::Railtie)
    Railtie.initializer "#{gem_name}.configure_rails_initialization" do
      gem_recipie_name = gem_name.gsub(/^pawnee[-]/, '')
      require "#{gem_recipie_name}/base"
    end
  end
end

Instance Method Details

#setupObject

All recipies should subclass Pawnee::Base and implement setup to install everything needed for the gem. Setup should be able to be called multiple times



105
106
107
# File 'lib/pawnee/pawnee/base.rb', line 105

def setup
  raise 'this gem does not implement the setup method'
end

#teardownObject

All recipies should also implement teardown to uninstall anything that gets installed



112
113
114
# File 'lib/pawnee/pawnee/base.rb', line 112

def teardown
  raise 'this gem does not implement the teardown method'
end