Class: Rump

Inherits:
Thor show all
Includes:
Logging
Defined in:
lib/rump.rb

Constant Summary

Constants included from Logging

Logging::ESCAPES

Instance Method Summary collapse

Methods included from Logging

#emit, #error, #info, #warn

Methods inherited from Thor

handle_argument_error

Constructor Details

#initializeRump

Returns a new instance of Rump.



48
49
50
51
52
# File 'lib/rump.rb', line 48

def initialize
  super
  @root = Pathname.new(Dir.getwd)
  @install_root = Pathname.new(File.expand_path(File.join(File.dirname(__FILE__))))
end

Instance Method Details

#clone(repository, directory = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rump.rb', line 55

def clone(repository, directory=nil)
  abort_unless_git_installed
  directory ||= File.basename(repository.split('/').last, '.git')
  command = "git clone -q #{repository} #{directory}"
  system(command)

  # A cloned repo may have submodules - automatically initialise them.
  if File.exists?(File.join(directory, '.gitmodules'))
    Dir.chdir(directory) do
      system("git submodule init")
      system("git submodule update")
    end
  end
end

#freeze(*args) ⇒ Object



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
# File 'lib/rump.rb', line 101

def freeze(*args)
  abort_unless_git_installed

  commands = []
  if args.size >= 2
    project    = args[0]
    repository = args[1]
    commands << { :command => "git submodule add #{repository} #{@root.join('vendor', project)}" }
    if args.detect { |arg| arg =~ /^--release\=(.+)/ }
      version = $1
      commands << { :command => "git checkout -b #{version} #{version}", :directory => @root.join('vendor', project) }
    end
    commands << { :command => "git commit --quiet -am 'Froze in #{repository} as a submodule'" }
  else
    commands << { :command => "git submodule add git://github.com/puppetlabs/puppet.git #{@root.join('vendor', 'puppet')}" }
    commands << { :command => "git submodule add git://github.com/puppetlabs/facter.git #{@root.join('vendor', 'facter')}" }
    commands << { :command => "git commit --quiet -am 'Froze in Puppet + Facter as submodules'" }
  end

  commands.each do |attrs|
    dir = attrs[:directory] || @root
    Dir.chdir(dir) do
      exit(2) unless system(attrs[:command])
    end
  end

  info "Freezing complete."
end

#go(*puppet_args) ⇒ Object



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
# File 'lib/rump.rb', line 71

def go(*puppet_args)
  if ENV['USER'] != "root"
    warn "You should probably be root when running this! Proceeding anyway..."
  end

  args = []
  if has_frozen_components?
    args << "ruby"
    Dir.glob("#{@root.join('vendor')}/*").each do |dir|
      args << "-I #{@root.join('vendor', dir, 'lib')}"
    end
    args << "#{@root.join('vendor', 'puppet', 'bin', 'puppet')}"
    info "Using frozen Puppet from #{@root.join('vendor', 'puppet')}."
  else
    abort_unless_puppet_installed(:message => "Please either install it on your system or freeze it with 'rump freeze'")
    args << "puppet"
  end
  args << "--modulepath #{@root.join('modules')}"
  args << "--confdir #{@root.join('etc')}" unless puppet_args.include?("--confdir")
  args << "--vardir #{@root.join('var')}" unless puppet_args.include?("--vardir")
  args << "#{@root.join('manifests', 'site.pp')}"

  args += puppet_args

  command = args.join(' ')
  puts command if args.include?("--debug")
  system(command) ? exit(0) : exit(2)
end

#init(project) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rump.rb', line 182

def init(project)
  scaffold(project)
  repo_path = @root.join(project)
  template_path = @install_root.join('generators', 'git')

  Dir.chdir(repo_path) do
    commands = [ "git init --quiet --template=#{template_path}",
                 "git add .",
                 "git commit --quiet -am 'Initial commit.'" ]
    commands.each do |command|
      system(command)
    end
  end

  info "Your new Rump project has been initialised in #{repo_path}"
end

#scaffold(project) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rump.rb', line 131

def scaffold(project)
  [ @root + project,
    @root + project + 'manifests',
    @root + project + 'modules',
    @root + project + 'var/reports',
    @root + project + 'vendor' ].each do |directory|
    FileUtils.mkdir_p(directory)
  end

  File.open(@root.join(project, 'README.md'), 'w') do |f|
    f << <<-README.gsub(/^ {8}/, '')
      #{project} manifests
      #{"=" * project.size}==========

      modules/ <= Puppet modules
      manifests/ <= Puppet nodes
      vendor/ <= frozen Puppet + Facter

      Running Puppet with Rump
      ------------------------

      From within this directory, run:

          rump go

      You can pass options to Puppet after the 'go':

          rump go --debug --test

      Freezing Puppet
      ---------------

      Firstly, you need to create a git repository:

          git init

      Now you can freeze Puppet:

          rump freeze

      Once Rump has frozen Puppet, commit the changes:

          git commit -m 'added facter + puppet submodules' .

      Now Rump will use the frozen Puppet when you run 'rump go'.

    README
  end
end

#whoami(address = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rump.rb', line 200

def whoami(address=nil)
  # getter
  if address
    name  = address[/^(.+)\s+</, 1]
    email = address[/<(.+)>$/, 1]

    unless name && email
      abort("Supplied address isn't a valid rfc2822 email address")
    end

    system("git config user.name '#{name}'")
    system("git config user.email '#{email}'")
  # setter
  else
    name = `git config user.name`.strip
    email = `git config user.email`.strip

    if name.empty? || email.empty?
      warn "You don't have a name or email set."
    else
      puts "#{name} <#{email}>"
    end
  end
end