labdb_manager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 909b21cc1ddbba3c422a33191657951acbf636da
4
+ data.tar.gz: e96a44db487b6f1e77fcdecd74c840aa073daf4d
5
+ SHA512:
6
+ metadata.gz: 6b178845e45f09eb9784d02e41ed17a1171525e7ace255d2f2f75aa037402971dbdbcd210ea3e695c6352c3c56ca4d56d5fe4c1d069d5d16d6d46e2e59adb913
7
+ data.tar.gz: f785e8ad00ade7fa8687f278705453162fff86748085bbcf25fe40048f0c89705974db5a2926bd37833215826e22e5f17cc60e9286a2e219a3d5d00ea047a3b0
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in labdb_manager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Colin J. Fuller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # LabdbManager
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'labdb_manager'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install labdb_manager
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/labdb_manager/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/labdb ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'labdb_manager'
3
+
4
+ if __FILE__ == $0 then
5
+ first_arg = ARGV.shift
6
+ if first_arg == "--dry-run" then
7
+ run_task ARGV[0], args: ARGV[1..-1], dry_run: true
8
+ else
9
+ run_task first_arg, args: ARGV
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'labdb_manager/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "labdb_manager"
8
+ spec.version = LabdbManager::VERSION
9
+ spec.authors = ["Colin J. Fuller"]
10
+ spec.email = ["cjfuller@gmail.com"]
11
+ spec.summary = %q{Management script for labdb.}
12
+ spec.description = %q{Script for updating, helping to install, and managing labdb.}
13
+ spec.homepage = "https://github.com/cjfuller/labdb_manager"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module LabdbManager
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,288 @@
1
+ require "securerandom"
2
+
3
+ require "labdb_manager/version"
4
+
5
+ COMMAND_PREFIX = "--> "
6
+ CONFIRM_PREFIX = "--? "
7
+
8
+ #git parameters
9
+ BR_DEPLOY_STAGING = 'deploy_staging'
10
+ BR_DEPLOY = 'deploy'
11
+ BR_REMOTE = 'master'
12
+ REM_NAME = 'origin'
13
+ PROJECT_URL = 'https://github.com/cjfuller/labdb.git'
14
+ DEFAULT_REPO_PATH = '~/labdb'
15
+ DEFAULT_BACKUP_DIR = '~/backups'
16
+ AUTO_MERGE_MESSAGE = '"auto merge by manage.py"'
17
+
18
+ #database backend
19
+ PG_DUMP_PATH = `which pg_dump`.strip
20
+
21
+ #paths to config files
22
+ HOSTNAME_CFG_FILE = 'config/full_hostname.txt'
23
+ SECRET_CFG_FILE = 'config/secret_token.txt'
24
+
25
+ COLORS = {
26
+ green: "\033[92m",
27
+ yellow: "\033[93m",
28
+ red: "\033[91m",
29
+ off: "\033[0m"
30
+ }
31
+
32
+ # define methods on string for each color
33
+ class String
34
+ COLORS.each do |c, s|
35
+ define_method c do
36
+ s + self + COLORS[:off]
37
+ end
38
+ end
39
+ end
40
+
41
+ def print_command(cmd)
42
+ ### Print a command being run in a standard format
43
+ puts (COMMAND_PREFIX + cmd).green
44
+ end
45
+
46
+ def confirm(cmd)
47
+ # Ask the user for confirmation of the specified command
48
+ #
49
+ # If confirmation is not given, exit the program immediately without running
50
+ # the command.
51
+ puts (CONFIRM_PREFIX + cmd).yellow
52
+ response = gets
53
+ unless ['yes', 'y'].include? response.downcase then
54
+ exit 0
55
+ end
56
+ cmd
57
+ end
58
+
59
+ SHELL_COMMAND_LIST = []
60
+
61
+ def queue_command(**kwargs, &bl)
62
+ SHELL_COMMAND_LIST << ShellCommand.new(bl, **kwargs)
63
+ end
64
+
65
+ def run_queued_commands(dry_run: false)
66
+ SHELL_COMMAND_LIST.each do |c|
67
+ c.call(dry_run: dry_run)
68
+ end
69
+ SHELL_COMMAND_LIST.clear
70
+ end
71
+
72
+ class ShellCommand
73
+ # A command that gets run in a login shell.
74
+
75
+ def self.prepend_login_shell(cmd)
76
+ "/bin/bash --login -c #{cmd}"
77
+ end
78
+
79
+ def initialize(cmd_fct, args: [], requires_sudo: false, exit_on_fail: true)
80
+ @cmd_fct = cmd_fct
81
+ @requires_sudo = requires_sudo
82
+ @exit_on_fail = exit_on_fail
83
+ @args = args
84
+ end
85
+
86
+ def call(dry_run: false)
87
+ # Run the command.
88
+ command_string = @cmd_fct.call *@args
89
+ puts (COMMAND_PREFIX + command_string).green
90
+ unless dry_run then
91
+ `#{ShellCommand.prepend_login_shell command_string}`
92
+ if $?.exitstatus > 0 and @exit_on_fail
93
+ puts ("Encountered an unresolvable error while running #{command_string}. " +
94
+ "Please resolve the problem manually and re-run").red
95
+ exit($?.exitstatus)
96
+ end
97
+ end
98
+ $?.exitstatus
99
+ end
100
+
101
+ def to_s
102
+ @cmd_fct.call
103
+ end
104
+ end
105
+
106
+ # git commands
107
+ def check_for_staging_branch
108
+ # Check (via process return code) if the staging branch already exists.
109
+ #
110
+ # @return 0 if the branch exists, nonzero otherwise
111
+
112
+ "git show-ref --verify --quiet refs/heads/#{BR_DEPLOY_STAGING}"
113
+ end
114
+
115
+ def clean_up_staging_branch
116
+ # Delete the staging branch. Don't check for its existence.
117
+ confirm "git checkout #{BR_DEPLOY} && git branch -d #{BR_DEPLOY_STAGING}"
118
+ end
119
+
120
+ def create_staging_branch
121
+ # Create the staging branch. Don't check for its existence.
122
+ "git checkout #{BR_DEPLOY} && git branch #{BR_DEPLOY_STAGING}"
123
+ end
124
+
125
+ def fetch_remote_changes
126
+ # Download changes from the central labdb repo set up as a remote.
127
+ #
128
+ # The remote name and branch to download are set up in the constants above.
129
+
130
+ "git checkout #{BR_REMOTE} && git pull #{REM_NAME} #{BR_REMOTE}"
131
+ end
132
+
133
+ def stage_changes
134
+ # Merge changes from the downloaded branch into the staging branch.
135
+ #
136
+ # This is where problems should arise if there are merge conflicts. If this
137
+ # happens and you need to get back to a workable state, you can force the
138
+ # load of the (still unchanged) deploy branch using `manage.py revert-
139
+ # failure`.
140
+
141
+ ("git checkout #{BR_DEPLOY_STAGING} && " +
142
+ "git merge -m #{AUTO_MERGE_MESSAGE} #{BR_REMOTE}")
143
+ end
144
+
145
+ def merge_into_production
146
+ # Merge changes from staging into production.
147
+ ("git checkout #{BR_DEPLOY} && " +
148
+ "git merge -m #{AUTO_MERGE_MESSAGE} #{BR_DEPLOY_STAGING}")
149
+ end
150
+
151
+ def revert_merge_failure
152
+ # Revert a merge and go back to production in case conflicts arise.
153
+ "git reset --merge && git checkout #{BR_DEPLOY}"
154
+ end
155
+
156
+ # application commands
157
+
158
+ def bundle_install
159
+ "bundle install"
160
+ end
161
+
162
+ def bundle_update
163
+ confirm "bundle update"
164
+ end
165
+
166
+ def precompile_assets
167
+ "bundle exec rake assets:precompile"
168
+ end
169
+
170
+ # database commands
171
+
172
+ def create_backup
173
+ suffix = "_labdb_backup.dump"
174
+ backup_timestring = Time.now.strftime("%Y%m%d_%H%M%S")
175
+ fn = backup_timestring + suffix
176
+ fn_full = File.expand_path(fn, DEFAULT_BACKUP_DIR)
177
+ [PG_DUMP_PATH,
178
+ "-h localhost labdb > #{fn_full}",
179
+ "&&",
180
+ "tar cjf #{fn_full}.tar.bz2 -C {DEFAULT_BACKUP_DIR} {fn}",
181
+ "&&",
182
+ "rm -f #{fn_full}"
183
+ ].join(" ")
184
+ end
185
+
186
+ def create_production_db
187
+ "RAILS_ENV=production bundle exec rake db:setup"
188
+ end
189
+
190
+ # server status commands
191
+
192
+ def restart_server
193
+ "supervisorctl restart labdb"
194
+ end
195
+
196
+ def run_devserver
197
+ "bundle exec puma --config config/puma.rb"
198
+ end
199
+
200
+ # other commands not using the shell
201
+
202
+ def set_hostname(hostname: nil)
203
+ if hostname.nil?
204
+ puts ("Please enter the full hostname of the machine.\n" +
205
+ '(i.e. the part that would appear including the https:// ' +
206
+ 'in a url but before any other slashes):').yellow
207
+ CONFIRM_PREFIX.yellow
208
+ hostname = gets
209
+ File.open(HOSTNAME_CFG_FILE, 'w') do |f|
210
+ f.write hostname
211
+ end
212
+ end
213
+ end
214
+
215
+ def generate_application_secret
216
+ secret = SecureRandom.hex(64) # 512 bits
217
+ puts (COMMAND_PREFIX +
218
+ "Writing application secret to #{SECRET_CFG_FILE}").green
219
+ File.open(SECRET_CFG_FILE, 'w') do |f|
220
+ f.write secret
221
+ end
222
+ File.chmod(0600, SECRET_CFG_FILE)
223
+ ok
224
+ end
225
+
226
+ # task helpers
227
+
228
+ def ok
229
+ puts "OK".green
230
+ end
231
+
232
+ # tasks
233
+
234
+ def update
235
+ queue_command {create_backup}
236
+ if ShellCommand.new(
237
+ Proc.new {check_for_staging_branch}, exit_on_fail: false).call == 0 then
238
+ queue_command {clean_up_staging_branch}
239
+ end
240
+ queue_command {create_staging_branch}
241
+ queue_command {fetch_remote_changes}
242
+ queue_command {stage_changes}
243
+ queue_command {bundle_install}
244
+ queue_command {precompile_assets}
245
+ queue_command {merge_into_production}
246
+ queue_command {restart_server}
247
+ end
248
+
249
+ def backup
250
+ queue_command {create_backup}
251
+ end
252
+
253
+ def force_update_deps
254
+ queue_command bundle_update
255
+ end
256
+
257
+ def secret
258
+ generate_application_secret
259
+ end
260
+
261
+ def hostname(hostname: nil)
262
+ set_hostname(hostname: hostname)
263
+ end
264
+
265
+ def install
266
+ queue_command {create_production_db}
267
+ end
268
+
269
+ def revert_failure
270
+ queue_command {revert_merge_failure}
271
+ end
272
+
273
+ def restart
274
+ queue_command {restart_server}
275
+ end
276
+
277
+ def devserver
278
+ queue_command {run_devserver}
279
+ end
280
+
281
+ def help
282
+ end
283
+
284
+ def run_task(t, args: [], dry_run: false)
285
+ send t, *args
286
+ run_queued_commands dry_run: dry_run
287
+ ok
288
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: labdb_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Colin J. Fuller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Script for updating, helping to install, and managing labdb.
42
+ email:
43
+ - cjfuller@gmail.com
44
+ executables:
45
+ - labdb
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/labdb
55
+ - labdb_manager.gemspec
56
+ - lib/labdb_manager.rb
57
+ - lib/labdb_manager/version.rb
58
+ homepage: https://github.com/cjfuller/labdb_manager
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Management script for labdb.
82
+ test_files: []