Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Config

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength



18
19
20
21
# File 'lib/config.rb', line 18

def initialize(args={}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  set_values(args)
  check_values
end

Instance Attribute Details

#database_urlObject (readonly)

Returns the value of attribute database_url.



5
6
7
# File 'lib/config.rb', line 5

def database_url
  @database_url
end

#destination_db_urlObject (readonly)

Returns the value of attribute destination_db_url.



5
6
7
# File 'lib/config.rb', line 5

def destination_db_url
  @destination_db_url
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



5
6
7
# File 'lib/config.rb', line 5

def dry_run
  @dry_run
end

#files_locationObject (readonly)

Returns the value of attribute files_location.



5
6
7
# File 'lib/config.rb', line 5

def files_location
  @files_location
end

#if_backupObject (readonly)

Returns the value of attribute if_backup.



5
6
7
# File 'lib/config.rb', line 5

def if_backup
  @if_backup
end

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/config.rb', line 5

def limit
  @limit
end

#move_logsObject (readonly)

Returns the value of attribute move_logs.



5
6
7
# File 'lib/config.rb', line 5

def move_logs
  @move_logs
end

#org_idObject (readonly)

Returns the value of attribute org_id.



5
6
7
# File 'lib/config.rb', line 5

def org_id
  @org_id
end

#remove_orphansObject (readonly)

Returns the value of attribute remove_orphans.



5
6
7
# File 'lib/config.rb', line 5

def remove_orphans
  @remove_orphans
end

#repo_idObject (readonly)

Returns the value of attribute repo_id.



5
6
7
# File 'lib/config.rb', line 5

def repo_id
  @repo_id
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



5
6
7
# File 'lib/config.rb', line 5

def threshold
  @threshold
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/config.rb', line 5

def user_id
  @user_id
end

Instance Method Details

#abort_message(intro) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/config.rb', line 128

def abort_message(intro)
  "\n#{intro} Example usage:\n"+
  "\n  $ bin/travis_backup 'postgres://my_database_url' --threshold 6\n" +
  "\nor using in code:\n" +
  "\n  Backup.new(database_url: 'postgres://my_database_url', threshold: 6)\n" +
  "\nYou can also set it using environment variables or configuration files.\n"
end

#argv_optionsObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/config.rb', line 136

def argv_options
  argv_copy = ARGV.clone
  options = {}
  OptionParser.new do |opt|
    opt.on('-b', '--backup') { |o| options[:if_backup] = o }
    opt.on('-d', '--dry_run') { |o| options[:dry_run] = o }
    opt.on('-l', '--limit X') { |o| options[:limit] = o.to_i }
    opt.on('-t', '--threshold X') { |o| options[:threshold] = o.to_i }
    opt.on('-f', '--files_location X') { |o| options[:files_location] = o }
    opt.on('-u', '--user_id X') { |o| options[:user_id] = o.to_i }
    opt.on('-r', '--repo_id X') { |o| options[:repo_id] = o.to_i }
    opt.on('-o', '--org_id X') { |o| options[:org_id] = o.to_i }
    opt.on('--move_logs') { |o| options[:move_logs] = o }
    opt.on('--remove_orphans') { |o| options[:remove_orphans] = o }
    opt.on('--destination_db_url X') { |o| options[:destination_db_url] = o }
  end.parse!

  options[:database_url] = ARGV.shift if ARGV[0]
  argv_copy.each do |arg|
    ARGV.push(arg)
  end
  options
end

#check_valuesObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/config.rb', line 107

def check_values
  if !@move_logs && !@remove_orphans && !@threshold
    message = abort_message("Please provide the threshold argument. Data younger than it will be omitted. " +
      "Threshold defines number of months from now.")
    abort message
  end

  if !@database_url
    message = abort_message("Please provide proper database URL.")
    abort message
  end

  if (@move_logs && !@destination_db_url)
    abort "\nFor moving logs you need to specify your destination database. Example usage:\n" +
    "\n  $ bin/travis_backup 'postgres://source_url' --move_logs --destination_db_url 'postgres://destination_url'\n" +
    "\nor using in code:\n" +
    "\n  Backup.new(database_url: 'postgres://source_url', destination_db_url: 'postgres://destination_url', move_logs: true)\n" +
    "\nYou can also set it using environment variables or configuration files.\n"  
  end
end

#first_not_nil(*arr) ⇒ Object



160
161
162
# File 'lib/config.rb', line 160

def first_not_nil(*arr)
  arr.compact.first
end

#set_values(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
# File 'lib/config.rb', line 23

def set_values(args)
  config = yaml_load('config/settings.yml')
  connection_details = yaml_load('config/database.yml')
  argv_opts = argv_options
  @if_backup = first_not_nil(
    args[:if_backup],
    argv_opts[:if_backup],
    ENV['IF_BACKUP'],
    config.dig('backup', 'if_backup'),
    true
  )
  @dry_run = first_not_nil(
    args[:dry_run],
    argv_opts[:dry_run],
    ENV['BACKUP_DRY_RUN'],
    config.dig('backup', 'dry_run'),
    false
  )
  @limit = first_not_nil(
    args[:limit],
    argv_opts[:limit],
    ENV['BACKUP_LIMIT'],
    config.dig('backup', 'limit'),
    1000
  )
  @threshold = first_not_nil(
    args[:threshold],
    argv_opts[:threshold],
    ENV['BACKUP_THRESHOLD'],
    config.dig('backup', 'threshold')
  )
  @files_location = first_not_nil(
    args[:files_location],
    argv_opts[:files_location],
    ENV['BACKUP_FILES_LOCATION'],
    config.dig('backup', 'files_location'),
    './dump'
  )
  @database_url = first_not_nil(
    args[:database_url],
    argv_opts[:database_url],
    ENV['DATABASE_URL'],
    connection_details.dig(ENV['RAILS_ENV'])
  )
  @user_id = first_not_nil(
    args[:user_id],
    argv_opts[:user_id],
    ENV['BACKUP_USER_ID'],
    config.dig('backup', 'user_id')
  )
  @repo_id = first_not_nil(
    args[:repo_id],
    argv_opts[:repo_id],
    ENV['BACKUP_REPO_ID'],
    config.dig('backup', 'repo_id')
  )
  @org_id = first_not_nil(
    args[:org_id],
    argv_opts[:org_id],
    ENV['BACKUP_ORG_ID'],
    config.dig('backup', 'org_id')
  )
  @move_logs = first_not_nil(
    args[:move_logs],
    argv_opts[:move_logs],
    ENV['BACKUP_MOVE_LOGS'],
    config.dig('backup', 'move_logs'),
    false
  )
  @remove_orphans = first_not_nil(
    args[:remove_orphans],
    argv_opts[:remove_orphans],
    ENV['BACKUP_REMOVE_ORPHANS'],
    config.dig('backup', 'remove_orphans'),
    false
  )
  @destination_db_url = first_not_nil(
    args[:destination_db_url],
    argv_opts[:destination_db_url],
    ENV['BACKUP_DESTINATION_DB_URL'],
    connection_details.dig(ENV['RAILS_ENV'], 'destination')
  )
end

#yaml_load(url) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/config.rb', line 164

def yaml_load(url)
  begin
    YAML.load(File.open(url))
  rescue => e
    {}
  end
end