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.



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

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.



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

def database
  @database
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#bundle_pathObject



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

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

#check_running_stateObject

Check if redmine is running based on PID files.



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

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



531
532
# File 'lib/redmine-installer/redmine.rb', line 531

def clean_up
end

#configuration_yml_pathObject



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

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

#copy_importants_from(other_redmine) ⇒ Object

Copy important files which cannot be deleted



346
347
348
349
350
351
352
353
354
355
356
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
# File 'lib/redmine-installer/redmine.rb', line 346

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



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/redmine-installer/redmine.rb', line 400

def copy_missing_plugins_from(other_redmine)
  # Copy missing redmine plugins
  Dir.chdir(other_redmine.plugins_path) do
    Dir.entries('.').each do |plugin|
      next if plugin == '.' || plugin == '..'

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

      to = File.join(plugins_path, plugin)

      # Plugins does not exist
      unless Dir.exist?(to)
        FileUtils.cp_r(plugin, to)
      end
    end
  end

  # Copy missing client modification plugin
  if easyproject?
    old_modifications = Dir.glob(File.join(other_redmine.easy_plugins_path, 'modification_*'))
    old_modifications.each do |old_modification_path|
      next if !File.directory?(old_modification_path)

      basename = File.basename(old_modification_path)

      new_modification_path = File.join(easy_plugins_path, basename)
      next if File.exist?(new_modification_path)

      FileUtils.cp_r(old_modification_path, new_modification_path)
    end
  end
end

#create_configuration_ymlObject

Create and configure configuration For now only email



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

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



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

def create_database_yml
  print_title('Creating database configuration')

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

#database_yml_pathObject



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

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

#delete_rootObject

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

Dir.entries(root).size <= 2

end



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/redmine-installer/redmine.rb', line 276

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

      FileUtils.remove_entry_secure(entry)
    end
  end

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

#easy_plugins_pathObject



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

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



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

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



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

def files_path
  File.join(root, FILES_DIR)
end

#gemfile_local_pathObject



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

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

#installObject

Run install commands (command might ask for additional informations)



197
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
# File 'lib/redmine-installer/redmine.rb', line 197

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

    # 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

    # Generate secret token
    rake_generate_secret_token

    # Install easyproject
    rake_easyproject_install if easyproject?
  end
end

#load_profile(profile) ⇒ Object



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

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



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

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

#make_backupObject

Backup:

  • full redmine (except log, tmp)

  • production database



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
# File 'lib/redmine-installer/redmine.rb', line 457

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



289
290
291
292
293
294
295
296
297
298
299
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
# File 'lib/redmine-installer/redmine.rb', line 289

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)

      # { 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



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

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

#plugins_pathObject



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

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

#restore_dbObject



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

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)


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

def running?
  pids_files.any?
end

#save_profile(profile) ⇒ Object



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

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

#upgradeObject



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

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

    # Migrating
    rake_db_migrate

    # Plugin migrating
    rake_redmine_plugin_migrate

    # Generate secret token
    rake_generate_secret_token

    # Install easyproject
    rake_easyproject_install if easyproject?
  end
end

#valid_optionsObject



525
526
527
528
529
# File 'lib/redmine-installer/redmine.rb', line 525

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



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/redmine-installer/redmine.rb', line 436

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. Missing #{path}."
      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