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



21
22
23
24
# File 'lib/config.rb', line 21

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

Instance Attribute Details

#database_urlObject

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

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

Returns the value of attribute dry_run.



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

def dry_run
  @dry_run
end

#files_locationObject

Returns the value of attribute files_location.



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

def files_location
  @files_location
end

#id_gapObject

Returns the value of attribute id_gap.



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

def id_gap
  @id_gap
end

#if_backupObject

Returns the value of attribute if_backup.



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

def if_backup
  @if_backup
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#load_from_filesObject

Returns the value of attribute load_from_files.



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

def load_from_files
  @load_from_files
end

#move_logsObject

Returns the value of attribute move_logs.



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

def move_logs
  @move_logs
end

#org_idObject

Returns the value of attribute org_id.



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

def org_id
  @org_id
end

#orphans_tableObject

Returns the value of attribute orphans_table.



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

def orphans_table
  @orphans_table
end

#remove_orphansObject

Returns the value of attribute remove_orphans.



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

def remove_orphans
  @remove_orphans
end

#repo_idObject

Returns the value of attribute repo_id.



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

def repo_id
  @repo_id
end

#thresholdObject

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

#user_idObject

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



153
154
155
156
157
158
159
160
161
# File 'lib/config.rb', line 153

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

#argv_optionsObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/config.rb', line 163

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('--orphans_table X') { |o| options[:orphans_table] = o }
    opt.on('--destination_db_url X') { |o| options[:destination_db_url] = o }
    opt.on('--load_from_files') { |o| options[:load_from_files] = o }
    opt.on('--id_gap X') { |o| options[:id_gap] = o.to_i }
  end.parse!

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

#check_valuesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/config.rb', line 131

def check_values
  if !@move_logs && !@remove_orphans && !@threshold && !@user_id && !@org_id && !@repo_id && !@load_from_files
    message = abort_message("Please provide the threshold argument. Data younger than it will be omitted. " +
      "Threshold defines number of months from now. Alternatively you can define user_id, org_id or repo_id " +
      "to remove whole user, organization or repository with all dependencies.")
    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



190
191
192
# File 'lib/config.rb', line 190

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

#set_values(args) ⇒ Object



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/config.rb', line 26

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
  )
  @orphans_table = first_not_nil(
    args[:orphans_table],
    argv_opts[:orphans_table],
    ENV['BACKUP_ORPHANS_TABLE'],
    config.dig('backup', 'orphans_table'),
    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')
  )
  @load_from_files = first_not_nil(
    args[:load_from_files],
    argv_opts[:load_from_files],
    ENV['BACKUP_LOAD_FROM_FILES'],
    config.dig('backup', 'load_from_files'),
    false
  )
  @id_gap = first_not_nil(
    args[:id_gap],
    argv_opts[:id_gap],
    ENV['BACKUP_ID_GAP'],
    config.dig('backup', 'id_gap'),
    1000
  )
end

#yaml_load(url) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/config.rb', line 194

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