gitpusshuten 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.bundle/config +2 -0
  2. data/.gitignore +4 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +53 -0
  5. data/README.md +7 -0
  6. data/bin/gitpusshuten +4 -0
  7. data/bin/heavenly +4 -0
  8. data/bin/ten +4 -0
  9. data/gitpusshuten.gemspec +26 -0
  10. data/lib/gitpusshuten/cli.rb +78 -0
  11. data/lib/gitpusshuten/command.rb +147 -0
  12. data/lib/gitpusshuten/commands/base.rb +246 -0
  13. data/lib/gitpusshuten/commands/delete.rb +27 -0
  14. data/lib/gitpusshuten/commands/help.rb +36 -0
  15. data/lib/gitpusshuten/commands/initialize.rb +61 -0
  16. data/lib/gitpusshuten/commands/push.rb +54 -0
  17. data/lib/gitpusshuten/commands/remote.rb +29 -0
  18. data/lib/gitpusshuten/commands/user.rb +252 -0
  19. data/lib/gitpusshuten/commands/version.rb +21 -0
  20. data/lib/gitpusshuten/configuration.rb +122 -0
  21. data/lib/gitpusshuten/environment.rb +70 -0
  22. data/lib/gitpusshuten/gem.rb +33 -0
  23. data/lib/gitpusshuten/git.rb +111 -0
  24. data/lib/gitpusshuten/helpers/environment/installers.rb +59 -0
  25. data/lib/gitpusshuten/helpers/environment/packages.rb +21 -0
  26. data/lib/gitpusshuten/helpers/environment/scp.rb +57 -0
  27. data/lib/gitpusshuten/helpers/environment/ssh.rb +77 -0
  28. data/lib/gitpusshuten/helpers/environment/ssh_key.rb +79 -0
  29. data/lib/gitpusshuten/helpers/environment/user.rb +70 -0
  30. data/lib/gitpusshuten/helpers/spinner.rb +98 -0
  31. data/lib/gitpusshuten/hook.rb +26 -0
  32. data/lib/gitpusshuten/hooks.rb +147 -0
  33. data/lib/gitpusshuten/initializer.rb +95 -0
  34. data/lib/gitpusshuten/local.rb +35 -0
  35. data/lib/gitpusshuten/log.rb +83 -0
  36. data/lib/gitpusshuten/modules/active_record/hooks.rb +19 -0
  37. data/lib/gitpusshuten/modules/apache/command.rb +354 -0
  38. data/lib/gitpusshuten/modules/bundler/command.rb +43 -0
  39. data/lib/gitpusshuten/modules/bundler/hooks.rb +8 -0
  40. data/lib/gitpusshuten/modules/mysql/command.rb +192 -0
  41. data/lib/gitpusshuten/modules/nanoc/hooks.rb +9 -0
  42. data/lib/gitpusshuten/modules/nginx/command.rb +447 -0
  43. data/lib/gitpusshuten/modules/passenger/command.rb +310 -0
  44. data/lib/gitpusshuten/modules/passenger/hooks.rb +4 -0
  45. data/lib/gitpusshuten/modules/rvm/command.rb +277 -0
  46. data/lib/gitpusshuten.rb +21 -0
  47. data/lib/templates/config.rb +37 -0
  48. data/lib/templates/hooks.rb +40 -0
  49. data/spec/cli_spec.rb +83 -0
  50. data/spec/command_spec.rb +76 -0
  51. data/spec/configuration_spec.rb +45 -0
  52. data/spec/environment_spec.rb +64 -0
  53. data/spec/fixtures/combined_hooks.rb +23 -0
  54. data/spec/fixtures/config.rb +23 -0
  55. data/spec/fixtures/hooks.rb +37 -0
  56. data/spec/fixtures/passenger.json +1 -0
  57. data/spec/gem_spec.rb +28 -0
  58. data/spec/git_spec.rb +59 -0
  59. data/spec/gitpusshuten_spec.rb +9 -0
  60. data/spec/hook_spec.rb +48 -0
  61. data/spec/hooks_spec.rb +150 -0
  62. data/spec/initializer_spec.rb +26 -0
  63. data/spec/log_spec.rb +20 -0
  64. data/spec/spec_helper.rb +43 -0
  65. metadata +220 -0
@@ -0,0 +1,147 @@
1
+ module GitPusshuTen
2
+ class Hooks
3
+
4
+ ##
5
+ # Contains the environment on the remote server name which
6
+ # is extracted from the selected configuration in the configuration file
7
+ attr_accessor :environment
8
+
9
+ ##
10
+ # Contains the configuration object
11
+ attr_accessor :configuration
12
+
13
+ ##
14
+ # Contains an array of GitPusshuTen::Hook objects for the current environment
15
+ attr_accessor :to_perform
16
+
17
+ ##
18
+ # Contains an array of commands to run for the currently parsed hook
19
+ # This gets reset to [] every time a new hook is being parsed
20
+ attr_accessor :commands_to_run
21
+
22
+ ##
23
+ # Initializes a new Hooks object
24
+ # Provide the environment (e.g. :staging, :production) to parse
25
+ def initialize(environment, configuration)
26
+ @environment = environment
27
+ @configuration = configuration
28
+ @to_perform = []
29
+ @commands_to_run = []
30
+ end
31
+
32
+ ##
33
+ # Parses the configuration file and loads all the
34
+ # configuration values into the GitPusshuTen::Configuration instance
35
+ def parse!(hooks_file)
36
+ if File.exist?(hooks_file)
37
+ instance_eval(File.read(hooks_file))
38
+ else
39
+ GitPusshuTen::Log.warning "Could not locate the hooks.rb file in #{hooks_file}"
40
+ end
41
+ self
42
+ end
43
+
44
+ ##
45
+ # Parses any modules that are set in the configuration (config.rb) file
46
+ def parse_modules!
47
+ configuration.additional_modules.each do |additional_module|
48
+ module_file = File.join(File.dirname(__FILE__), 'modules', additional_module.to_s, 'hooks.rb')
49
+ if File.exist?(module_file)
50
+ instance_eval(File.read(module_file))
51
+ end
52
+ end
53
+ self
54
+ end
55
+
56
+ ##
57
+ # Perform On
58
+ # Helper method used to configure the hooks.rb file
59
+ def perform_on(*environments, &configuration)
60
+ if environments.flatten.include?(environment)
61
+ configuration.call
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Pre
67
+ # A method for setting pre-hooks inside the perform_on block
68
+ # Resets the "commands_to_run" variable to an empty array so that
69
+ # there's a clean array to work with the next set of commands.
70
+ # The "commands.call" invokes all the "run(<command>)" the user
71
+ # provided in the hooks.rb configuration file and extracts the strings
72
+ # of commands to run. This array is then passed into a newly made Hook object
73
+ # which is again stored into the "to_perform" array.
74
+ def pre(name, &commands)
75
+ @commands_to_run = []
76
+ commands.call
77
+ @to_perform << Hook.new({
78
+ :type => :pre,
79
+ :name => name,
80
+ :commands => commands_to_run
81
+ })
82
+ end
83
+
84
+ ##
85
+ # Post
86
+ # A method for setting post-hooks inside the perform_on block
87
+ # Resets the "commands_to_run" variable to an empty array so that
88
+ # there's a clean array to work with the next set of commands.
89
+ # The "commands.call" invokes all the "run(<command>)" the user
90
+ # provided in the hooks.rb configuration file and extracts the strings
91
+ # of commands to run. This array is then passed into a newly made Hook object
92
+ # which is again stored into the "to_perform" array.
93
+ def post(name, &commands)
94
+ @commands_to_run = []
95
+ commands.call
96
+ @to_perform << Hook.new({
97
+ :type => :post,
98
+ :name => name,
99
+ :commands => commands_to_run
100
+ })
101
+ end
102
+
103
+ ##
104
+ # Run
105
+ # A method for setting commands on a
106
+ # post-hook or pre-hook inside the perform_on block
107
+ def run(command)
108
+ @commands_to_run << command
109
+ end
110
+
111
+ ##
112
+ # Pre Hooks
113
+ # Returns an array of pre-hooks
114
+ def pre_hooks
115
+ @to_perform.map do |hook|
116
+ next unless hook.type.eql? :pre
117
+ hook
118
+ end.compact
119
+ end
120
+
121
+ ##
122
+ # Post Hooks
123
+ # Returns an array of post-hooks
124
+ def post_hooks
125
+ @to_perform.map do |hook|
126
+ next unless hook.type.eql? :post
127
+ hook
128
+ end.compact
129
+ end
130
+
131
+ ##
132
+ # Takes an array of hooks and renders them a Hash that
133
+ # contains the name of the hook, as well as all the commands
134
+ # bundled in a single string, separated by semi-colons.
135
+ def render_commands(hooks)
136
+ hooks_hash = {}
137
+ hooks.each do |hook|
138
+ hooks_hash[hook.name] = ''
139
+ hook.commands.each do |command|
140
+ hooks_hash[hook.name] << "#{command};".gsub(/;{2,}/, ';')
141
+ end
142
+ end
143
+ hooks_hash
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,95 @@
1
+ module GitPusshuTen
2
+ class Initializer
3
+
4
+ ##
5
+ # Method to be called from CLI/Executable
6
+ def initialize(*args)
7
+ invoke_independent_command!(args)
8
+
9
+ if not File.exist?(configuration_file)
10
+ GitPusshuTen::Log.error "Couldn't find the GitPusshuTen configuration file in #{configuration_file}."
11
+ exit
12
+ end
13
+
14
+ ##
15
+ # Parse the CLI arguments
16
+ cli = GitPusshuTen::CLI.new(args)
17
+
18
+ ##
19
+ # Load in the requested environment and it's configuration
20
+ configuration = GitPusshuTen::Configuration.new(cli.environment).parse!(configuration_file)
21
+
22
+ ##
23
+ # Load in hooks
24
+ hooks = GitPusshuTen::Hooks.new(cli.environment, configuration).parse!(hooks_file).parse_modules!
25
+
26
+ ##
27
+ # Configure the environment connection establisher
28
+ environment = GitPusshuTen::Environment.new(configuration)
29
+
30
+ ##
31
+ # Bootstrap the command
32
+ GitPusshuTen::Command.new(cli, configuration, hooks, environment).perform!
33
+ end
34
+
35
+ ##
36
+ # Path to assumed configuration file
37
+ def configuration_file
38
+ gitpusshuten_root + '/config.rb'
39
+ end
40
+
41
+ ##
42
+ # Path to assumed hooks file
43
+ def hooks_file
44
+ gitpusshuten_root + '/hooks.rb'
45
+ end
46
+
47
+ ##
48
+ # Path to the assumed .gitpusshuten directory
49
+ def gitpusshuten_root
50
+ File.expand_path(File.join(Dir.pwd, '.gitpusshuten'))
51
+ end
52
+
53
+ ##
54
+ # If a command that does not rely on an initialized
55
+ # environment, run it without attemping to parse environment
56
+ # specific files.
57
+ def invoke_independent_command!(args)
58
+
59
+ ##
60
+ # Flatten Arguments to be able to test if the array is empty
61
+ # and not an empty array in an empty array
62
+ args.flatten!
63
+
64
+ ##
65
+ # Parses the CLI
66
+ cli = GitPusshuTen::CLI.new(args)
67
+
68
+ ##
69
+ # Parses the Configuration
70
+ if File.exist?(configuration_file)
71
+ configuration = GitPusshuTen::Configuration.new(cli.environment).parse!(configuration_file)
72
+ else
73
+ configuration = nil
74
+ end
75
+
76
+ ##
77
+ # Initializes the help command by default if there aren't any arguments
78
+ if args.empty?
79
+ GitPusshuTen::Command.new(cli, configuration, nil, nil)
80
+ exit
81
+ end
82
+
83
+ ##
84
+ # Append more arguments to the array below to allow more commands
85
+ # to invoke without initializing an environment
86
+ if %w[help version initialize].include? args.flatten.first
87
+ "GitPusshuTen::Commands::#{args.flatten.first.classify}".constantize.new(
88
+ cli, configuration, nil, nil
89
+ ).perform!
90
+ exit
91
+ end
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,35 @@
1
+ module GitPusshuTen
2
+ class Local
3
+
4
+ ##
5
+ # Performs a command on the local machien
6
+ def execute(command)
7
+ %x[#{command}]
8
+ end
9
+
10
+ ##
11
+ # Returns the .gitpusshuten local directory
12
+ def gitpusshuten_dir
13
+ File.join(Dir.pwd, '.gitpusshuten')
14
+ end
15
+
16
+ ##
17
+ # Returns the .gitpusshuten tmp directory
18
+ def tmp_dir
19
+ File.join(gitpusshuten_dir, 'tmp')
20
+ end
21
+
22
+ ##
23
+ # Create tmp_dir
24
+ def create_tmp_dir!
25
+ %x[mkdir -p '#{tmp_dir}']
26
+ end
27
+
28
+ ##
29
+ # Removes everything inside the tmp_dir
30
+ def remove_tmp_dir!
31
+ %x[rm -rf '#{tmp_dir}']
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ module GitPusshuTen
3
+ class Log
4
+
5
+ ##
6
+ # Displays a regular message without prefix
7
+ def self.standard(message)
8
+ puts message
9
+ to_file message
10
+ end
11
+
12
+ ##
13
+ # Displays a regular message
14
+ def self.message(message)
15
+ message = "[message] ".color(:green) + message
16
+ puts message
17
+ to_file message
18
+ end
19
+
20
+ ##
21
+ # Displays a warning message
22
+ def self.warning(message)
23
+ message = "[warning] ".color(:yellow) + message
24
+ puts message
25
+ to_file message
26
+ end
27
+
28
+ ##
29
+ # Displays an error message
30
+ def self.error(message)
31
+ message = "[error] ".color(:red) + message
32
+ puts message
33
+ to_file message
34
+ end
35
+
36
+ ##
37
+ # Silently logs messages
38
+ def self.silent(message)
39
+ to_file message
40
+ end
41
+
42
+ ##
43
+ # Logs the message to the log file
44
+ def self.to_file(message)
45
+ return unless message.is_a?(String)
46
+
47
+ ##
48
+ # Don't log if we're not working within the Gitpusshuten directory
49
+ if File.directory?(gitpusshuten_dir)
50
+
51
+ ##
52
+ # Create the log directory if it doesn't exist
53
+ if not File.directory?(log_dir)
54
+ %x[mkdir -p '#{log_dir}']
55
+ end
56
+
57
+ ##
58
+ # Remove all ANSI coloring codes for clean logging
59
+ message.gsub!(/\[\d+m/, '')
60
+
61
+ ##
62
+ # Log the message to the file (append)
63
+ File.open(File.join(log_dir, 'gitpusshuten.log'), 'a') do |file|
64
+ file << "\n#{Time.now.strftime("[%m/%d/%Y %H:%M:%S]")} #{message}"
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Returns the Gitpusshuten directory path
72
+ def self.gitpusshuten_dir
73
+ File.join(Dir.pwd, '.gitpusshuten')
74
+ end
75
+
76
+ ##
77
+ # Returns the Gitpusshuten log directory path
78
+ def self.log_dir
79
+ File.join(gitpusshuten_dir, 'log')
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,19 @@
1
+ ##
2
+ # Will check if the app_root/config/database.yml.<environment> exists
3
+ # if it does, it'll overwrite the database.yml with that environment database.
4
+ # If it does not exist, it'll leave the database.yml unchanged.
5
+ post "Setting proper database for environment. (Rails)" do
6
+ run "if [[ -f 'config/database.yml.#{environment}' ]]; then " +
7
+ "echo 'config/database.yml.#{environment} found! Using this one instead of config/database.yml';" +
8
+ "echo 'Overwriting config/database.yml with config/database.yml.#{environment}';" +
9
+ "mv 'config/database.yml.#{environment}' 'config/database.yml'; " +
10
+ "fi"
11
+ end
12
+
13
+ ##
14
+ # Will create the database if it doesn't exist yet.
15
+ # Migrates the database.
16
+ post "Migrate Database (Active Record)" do
17
+ run "rake db:create"
18
+ run "rake db:migrate"
19
+ end