Class: RedmineInstaller::Redmine

Inherits:
TaskModule show all
Defined in:
lib/redmine-installer/redmine.rb

Constant Summary collapse

REQUIRED_FILES =
[
  'app',
  'lib',
  'config',
  'public',
  'db',
  'Gemfile',
  'Rakefile',
  'config.ru',
  File.join('lib', 'redmine'),
  File.join('lib', 'redmine.rb'),
]
DEFAULT_BACKUP_ROOT =
File.join(Dir.home, 'redmine-backups')
BACKUP_EXCLUDE_FILES =
['log/', 'tmp/']
CHECK_N_INACCESSIBLE_FILES =
10
FILES_DIR =
'files'

Constants included from Utils

Utils::PROGRESSBAR_FORMAT

Instance Attribute Summary collapse

Attributes inherited from TaskModule

#task

Instance Method Summary collapse

Methods included from Utils

#class_name, #create_dir, #env_user, #error, #logger, #ok, #pastel, #print_title, #prompt, #run_command

Constructor Details

#initialize(task, root = nil) ⇒ Redmine

Returns a new instance of Redmine.



30
31
32
33
34
35
36
37
# File 'lib/redmine-installer/redmine.rb', line 30

def initialize(task, root=nil)
  super(task)
  @root = root.to_s

  if (dump = task.options.database_dump)
    @database_dump_to_load = File.expand_path(dump)
  end
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



7
8
9
# File 'lib/redmine-installer/redmine.rb', line 7

def database
  @database
end

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/redmine-installer/redmine.rb', line 8

def root
  @root
end

Instance Method Details

#bundle_pathObject



67
68
69
# File 'lib/redmine-installer/redmine.rb', line 67

def bundle_path
  File.join(root, '.bundle')
end

#check_running_stateObject

Check if redmine is running based on PID files.



167
168
169
170
171
172
173
174
175
# File 'lib/redmine-installer/redmine.rb', line 167

def check_running_state
  if running?
    if prompt.yes?("Your app is running based on PID files (#{pids_files.join(', ')}). Do you want continue?", default: false)
      logger.warn("App is running (pids: #{pids_files.join(', ')}). Ignore it and continue.")
    else
      error('App is running')
    end
  end
end

#clean_upObject



559
560
# File 'lib/redmine-installer/redmine.rb', line 559

def clean_up
end

#configuration_yml_pathObject



43
44
45
# File 'lib/redmine-installer/redmine.rb', line 43

def configuration_yml_path
  File.join(root, 'config', 'configuration.yml')
end

#copy_importants_from(other_redmine) ⇒ Object

Copy important files which cannot be deleted



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/redmine-installer/redmine.rb', line 357

def copy_importants_from(other_redmine)
  Dir.chdir(root) do
    # Copy database.yml
    FileUtils.cp(other_redmine.database_yml_path, database_yml_path)

    # Copy configuration.yml
    if File.exist?(other_redmine.configuration_yml_path)
      FileUtils.cp(other_redmine.configuration_yml_path, configuration_yml_path)
    end

    # Copy Gemfile.local
    if File.exist?(other_redmine.gemfile_local_path)
      FileUtils.cp(other_redmine.gemfile_local_path, gemfile_local_path)
    end

    # Copy files
    if task.options.copy_files_with_symlink
      FileUtils.rm_rf(files_path)
      FileUtils.ln_s(other_redmine.files_path, root)
    else
      FileUtils.cp_r(other_redmine.files_path, root)
    end

    # Copy old logs
    FileUtils.mkdir_p(log_path)
    Dir.glob(File.join(other_redmine.log_path, 'redmine_installer_*')).each do |log|
      FileUtils.cp(log, log_path)
    end

    # Copy bundle config
    if Dir.exist?(other_redmine.bundle_path)
      FileUtils.mkdir_p(bundle_path)
      FileUtils.cp_r(other_redmine.bundle_path, root)
    end
  end

  # Copy 'keep' files (base on options)
  Array(task.options.keep).each do |path|
    origin_path = File.join(other_redmine.root, path)
    next unless File.exist?(origin_path)

    # Ensure folder
    target_dir = File.join(root, File.dirname(path))
    FileUtils.mkdir_p(target_dir)

    # Copy recursive
    FileUtils.cp_r(origin_path, target_dir)
  end

  logger.info('Important files was copyied')
end

#copy_missing_plugins_from(other_redmine) ⇒ Object

New package may not have all plugins



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/redmine-installer/redmine.rb', line 435

def copy_missing_plugins_from(other_redmine)
  missing = []

  yield_missing_plugins(other_redmine.plugins_path, plugins_path) do |from, to|
    missing << [from, to]
  end

  yield_missing_plugins(other_redmine.easy_plugins_path, easy_plugins_path) do |from, to|
    missing << [from, to]
  end

  missing_plugin_names = missing.map{|(from, to)| File.basename(from) }
  logger.info("Missing plugins: #{missing_plugin_names.join(', ')}")

  if missing.empty?
    return
  end

  puts
  if !prompt.yes?("Your application contains plugins that are not present in the package (#{missing_plugin_names.join(', ')}). Would you like to copy them?")
    return
  end

  missing.each do |(from, to)|
    FileUtils.cp_r(from, to)
    logger.info("Copied #{from} to #{to}")
  end
end

#create_configuration_ymlObject

Create and configure configuration For now only email



189
190
191
192
193
194
# File 'lib/redmine-installer/redmine.rb', line 189

def create_configuration_yml
  print_title('Creating email configuration')

  @configuration = Configuration.create_config(self)
  logger.info("Configuration initialized #{@configuration}")
end

#create_database_ymlObject

Create and configure rails database



179
180
181
182
183
184
# File 'lib/redmine-installer/redmine.rb', line 179

def create_database_yml
  print_title('Creating database configuration')

  @database = Database.create_config(self)
  logger.info("Database initialized #{@database}")
end

#database_yml_pathObject



39
40
41
# File 'lib/redmine-installer/redmine.rb', line 39

def database_yml_path
  File.join(root, 'config', 'database.yml')
end

#delete_rootObject

# => [‘.’, ‘..’] def empty_root?

Dir.entries(root).size <= 2

end



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/redmine-installer/redmine.rb', line 277

def delete_root
  Dir.chdir(root) do
    Dir.entries('.').each do |entry|
      next if entry == '.' || entry == '..'
      next if entry == FILES_DIR && task.options.copy_files_with_symlink

      begin
        FileUtils.remove_entry_secure(entry)
      rescue
        if task.options.copy_files_with_symlink
          print_title('Cannot automatically empty ' + root + '. Please manually empty this directory (except for ' + root + '/' + FILES_DIR + ') and then hit any key to continue...')
        else
          print_title('Cannot automatically empty ' + root + '. Please manually empty this directory and then hit any key to continue...')
        end
        STDIN.getch
        break
      end
    end
  end

  logger.info("#{root} content was deleted")
end

#easy_plugins_pathObject



59
60
61
# File 'lib/redmine-installer/redmine.rb', line 59

def easy_plugins_path
  File.join(plugins_path, 'easyproject', 'easy_plugins')
end

#ensure_and_valid_rootObject

Ask for REDMINE_ROOT (if wasnt set) and check access rights



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/redmine-installer/redmine.rb', line 106

def ensure_and_valid_root
  if root.empty?
    puts
    @root = prompt.ask('Path to redmine root:', required: true, default: 'redmine')
  end

  @root = File.expand_path(@root)

  unless Dir.exist?(@root)
    create_dir(@root)
  end

  logger.info("REDMINE_ROOT: #{@root}")

  unreadable_files = []
  all_directories = []

  Find.find(@root).each do |item|
    if unreadable_files.size > CHECK_N_INACCESSIBLE_FILES
      break
    end

    # Installer only need read permission for a few files
    # but for sure it checks all of them
    if !File.readable?(item)
      unreadable_files << item
      next
    end

    # Actualy this permission should not be needed
    # becase deletable is checked by parent directory
    # if !File.writable?(item)
    #   unreadable_files << item
    # end

    # Parent directory of the root can have any permission
    if item != @root
      all_directories << File.dirname(item)
    end
  end

  if unreadable_files.any?
    error "Application root contains unreadable files. Make sure that all files in #{@root} are readable for user #{env_user} (limit #{CHECK_N_INACCESSIBLE_FILES} files: #{unreadable_files.join(', ')})"
  end

  unwritable_directories = []

  all_directories.uniq!
  all_directories.each do |item|
    if !File.writable?(item)
      unwritable_directories << item
    end
  end

  if unwritable_directories.any?
    error "Application root contains unwritable directories. Make sure that all directories in #{@root} are writable for user #{env_user} (limit #{CHECK_N_INACCESSIBLE_FILES} files: #{unwritable_directories.join(', ')})"
  end
end

#files_pathObject



51
52
53
# File 'lib/redmine-installer/redmine.rb', line 51

def files_path
  File.join(root, FILES_DIR)
end

#gemfile_local_pathObject



47
48
49
# File 'lib/redmine-installer/redmine.rb', line 47

def gemfile_local_path
  File.join(root, 'Gemfile.local')
end

#installObject

Run install commands (command might ask for additional informations)



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/redmine-installer/redmine.rb', line 198

def install
  print_title('Redmine installing')

  Dir.chdir(root) do
    # Gems can be locked on bad version
    FileUtils.rm_f('Gemfile.lock')

    # Install new gems
    bundle_install

    # Generate secret token
    rake_generate_secret_token

    # Ensuring database
    rake_db_create

    # Load database dump (if was set via CLI or attach on package)
    load_database_dump

    # Migrating
    rake_db_migrate

    # Plugin migrating
    rake_redmine_plugin_migrate

    # Install easyproject
    rake_easyproject_install if easyproject?
  end
end

#load_profile(profile) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redmine-installer/redmine.rb', line 79

def load_profile(profile)
  @root = profile.redmine_root if root.empty?
  @backup_type = profile.backup_type
  @backup_root = profile.backup_root || profile.backup_dir

  # Convert setting from v1
  case @backup_type
  when :full_backup
    @backup_type = :full
  when :backup, :only_database
    @backup_type = :database
  end

  # Only valid setting
  unless [:full, :database, :nothing].include?(@backup_type)
    @backup_type = nil
  end
end

#log_pathObject



63
64
65
# File 'lib/redmine-installer/redmine.rb', line 63

def log_path
  File.join(root, 'log')
end

#make_backupObject

Backup:

  • full redmine (except log, tmp)

  • production database



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/redmine-installer/redmine.rb', line 485

def make_backup
  print_title('Data backup')

  @backup_type ||= prompt.select('What type of backup do you want?',
    'Full (redmine root and database)' => :full,
    'Only database' => :database,
    'Nothing' => :nothing)

  logger.info("Backup type: #{@backup_type}")

  # Dangerous option
  if @backup_type == :nothing
    if prompt.yes?('Are you sure you dont want backup?', default: false)
      logger.info('Backup option nothing was confirmed')
      return
    else
      @backup_type = nil
      return make_backup
    end
  end

  @backup_root ||= prompt.ask('Where to save backup:', required: true, default: DEFAULT_BACKUP_ROOT)
  @backup_root = File.expand_path(@backup_root)

  @backup_dir = File.join(@backup_root, Time.now.strftime('backup_%d%m%Y_%H%M%S'))
  create_dir(@backup_dir)

  files_to_backup = []
  Dir.chdir(root) do
    case @backup_type
    when :full
      files_to_backup = Dir.glob(File.join('**', '{*,.*}'))
    end
  end

  if files_to_backup.any?
    files_to_backup.delete_if do |path|
      path.start_with?(*BACKUP_EXCLUDE_FILES)
    end

    @backup_package = File.join(@backup_dir, 'redmine.zip')

    Dir.chdir(root) do
      puts
      puts 'Files backuping'
      Zip::File.open(@backup_package, Zip::File::CREATE) do |zipfile|
        progressbar = TTY::ProgressBar.new(PROGRESSBAR_FORMAT, total: files_to_backup.size, frequency: 2, clear: true)

        files_to_backup.each do |entry|
          zipfile.add(entry, entry)
          progressbar.advance(1)
        end

        progressbar.finish
      end
    end

    puts "Files backed up on #{@backup_package}"
    logger.info('Files backed up')
  end

  @database = Database.init(self)
  @database.make_backup(@backup_dir)

  puts "Database backed up on #{@database.backup}"
  logger.info('Database backed up')
end

#move_from(other_redmine) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/redmine-installer/redmine.rb', line 300

def move_from(other_redmine)
  Dir.chdir(other_redmine.root) do

    # Bundler save plugin with absolute paths
    # which is not pointing to the temporary directory
    bundle_index = File.join(Dir.pwd, '.bundle/plugin/index')

    if File.exist?(bundle_index)
      index = YAML.load_file(bundle_index, aliases: true)

      # { load_paths: { PLUGIN_NAME: *PATHS } }
      #
      if index.has_key?('load_paths')
        load_paths = index['load_paths']
        if load_paths.is_a?(Hash)
          load_paths.each do |_, paths|
            paths.each do |path|
              path.sub!(other_redmine.root, root)
            end
          end
        end
      end

      # { plugin_paths: { PLUGIN_NAME: PATH } }
      #
      if index.has_key?('plugin_paths')
        plugin_paths = index['plugin_paths']
        if plugin_paths.is_a?(Hash)
          plugin_paths.each do |_, path|
            path.sub!(other_redmine.root, root)
          end
        end
      end

      File.write(bundle_index, index.to_yaml)

      logger.info("Bundler plugin index from #{other_redmine.root} into #{root}")
    else
      logger.info("Bundler plugin index from #{other_redmine.root} not found")
    end

    Dir.entries('.').each do |entry|
      next if entry == '.' || entry == '..'

      if entry == FILES_DIR && task.options.copy_files_with_symlink
        FileUtils.rm(entry)
      else
        FileUtils.mv(entry, root)
      end
    end
  end

  logger.info("Copyied from #{other_redmine.root} into #{root}")
end

#pids_filesObject



71
72
73
# File 'lib/redmine-installer/redmine.rb', line 71

def pids_files
  Dir.glob(File.join(root, 'tmp', 'pids', '*'))
end

#plugins_pathObject



55
56
57
# File 'lib/redmine-installer/redmine.rb', line 55

def plugins_path
  File.join(root, 'plugins')
end

#restore_dbObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/redmine-installer/redmine.rb', line 252

def restore_db
  print_title('Database restoring')

  @database = Database.init(self)

  Dir.chdir(root) do
    # Load database dump (if was set via CLI)
    load_database_dump

    # Migrating
    rake_db_migrate

    # Plugin migrating
    rake_redmine_plugin_migrate

    # Install easyproject
    rake_easyproject_install if easyproject?
  end
end

#running?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/redmine-installer/redmine.rb', line 75

def running?
  pids_files.any?
end

#save_profile(profile) ⇒ Object



98
99
100
101
102
# File 'lib/redmine-installer/redmine.rb', line 98

def save_profile(profile)
  profile.redmine_root = @root
  profile.backup_type = @backup_type
  profile.backup_root = @backup_root
end

#upgradeObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/redmine-installer/redmine.rb', line 228

def upgrade
  print_title('Redmine upgrading')

  Dir.chdir(root) do
    # Gems can be locked on bad version
    FileUtils.rm_f('Gemfile.lock')

    # Install new gems
    bundle_install

    # Generate secret token
    rake_generate_secret_token

    # Migrating
    rake_db_migrate

    # Plugin migrating
    rake_redmine_plugin_migrate

    # Install easyproject
    rake_easyproject_install if easyproject?
  end
end

#valid_optionsObject



553
554
555
556
557
# File 'lib/redmine-installer/redmine.rb', line 553

def valid_options
  if @database_dump_to_load && !(File.exist?(@database_dump_to_load) && File.file?(@database_dump_to_load))
    error "Database dump #{@database_dump_to_load} does not exist (path is expanded)."
  end
end

#validateObject



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/redmine-installer/redmine.rb', line 464

def validate
  # Check for required files
  Dir.chdir(root) do
    REQUIRED_FILES.each do |path|
      unless File.exist?(path)
        error "Redmine #{root} is not valid. Directory '#{path}' is missing."
      end
    end
  end

  # Plugins are in right dir
  Dir.glob(File.join(root, 'vendor', 'plugins', '*')).each do |path|
    if File.directory?(path)
      error "Plugin should be on plugins dir. On vendor/plugins is #{path}"
    end
  end
end

#yield_missing_plugins(source_directory, target_directory) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/redmine-installer/redmine.rb', line 409

def yield_missing_plugins(source_directory, target_directory)
  if !Dir.exist?(source_directory)
    return
  end

  Dir.chdir(source_directory) do
    Dir.entries('.').each do |plugin|
      next if plugin == '.' || plugin == '..'

      # Plugin is not directory
      unless File.directory?(plugin)
        next
      end

      to = File.join(target_directory, plugin)

      # Plugins does not exist
      unless Dir.exist?(to)
        yield File.expand_path(plugin), File.expand_path(to)
      end
    end
  end
end