ruploy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p290@ruploy"
8
+
9
+ #
10
+ # Uncomment following line if you want options to be set only for given project.
11
+ #
12
+ # PROJECT_JRUBY_OPTS=( --1.9 )
13
+
14
+ #
15
+ # First we attempt to load the desired environment directly from the environment
16
+ # file. This is very fast and efficient compared to running through the entire
17
+ # CLI and selector. If you want feedback on which environment was used then
18
+ # insert the word 'use' after --create as this triggers verbose mode.
19
+ #
20
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
21
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
22
+ then
23
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
24
+
25
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
26
+ then
27
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
28
+ fi
29
+ else
30
+ # If the environment file has not yet been created, use the RVM CLI to select.
31
+ if ! rvm --create "$environment_id"
32
+ then
33
+ echo "Failed to create RVM environment '${environment_id}'."
34
+ exit 1
35
+ fi
36
+ fi
37
+
38
+ #
39
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
40
+ # it be automatically loaded. Uncomment the following and adjust the filename if
41
+ # necessary.
42
+ #
43
+ # filename=".gems"
44
+ # if [[ -s "$filename" ]]
45
+ # then
46
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
47
+ # fi
48
+
49
+ # If you use bundler, this might be useful to you:
50
+ # if command -v bundle && [[ -s Gemfile ]]
51
+ # then
52
+ # bundle install
53
+ # fi
54
+
55
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ruploy.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # Ruploy
2
+
3
+ If you are managing Rack applications on your server and use RVM to work with
4
+ several versions of Ruby, that you use either Passenger-Standalone or Thin, Ruploy can help you.
5
+
6
+ Ruploy can generate init.d scripts to start/restart/stop your Rack applications like any services.
7
+
8
+ ## Installation
9
+
10
+ gem install ruploy
11
+
12
+ ## Usage
13
+
14
+ Ruploy comes with two commands, `generate` and `deploy`.
15
+
16
+ ### Generate
17
+
18
+ The `generate` command is the one in charge of generating init.d scripts. It
19
+ take one parameter which is the name of the script you want to create. All other
20
+ parameters will be given to the server script as options.
21
+
22
+ You can pass several options to `generate`. If an option is missing, ruploy will
23
+ enter interactive mode to ask you what it needs. If you want to use the default
24
+ values for the options you didn't provided, use the `--use-defaults` option.
25
+
26
+ * `--name NAME` Name of your application (default: current directory name)
27
+ * `--directory PATH` Root path of the application (default: current directory)
28
+ * `--address HOST` Bind to HOST address (default: 127.0.0.1)
29
+ * `--port NUMBER` Use the given port number (default: 3000)
30
+ * `--environment ENV` Framework environment (default: production)
31
+ * `--user USERNAME` User to run as. Ignored unless running as root (default: www-data)
32
+ * `--log-file FILENAME` Where to write log messages (default: /var/log/rack-$PROCNAME-$PORT.log)
33
+ * `--pid-file FILENAME` Where to store the PID file (default: /var/lock/rack-$PROCNAME-$PORT)
34
+ * `--server-type SERVER` Server type, can be "thin" or "passenger" (default: thin)
35
+ * `--dependencies DEPS` Dependencies of the init script (default: apache2)
36
+ * `--independent` Print the generic code in the file instead of including it
37
+ * `--use-defaults` Do not ask for missing informations and use default values
38
+
39
+ Here is an example of `ruploy generate` usage :
40
+
41
+ $ ruploy generate my-script \
42
+ --name MyApp \
43
+ --directory /path/to/my/app \
44
+ --address 0.0.0.0 \
45
+ --port 4242 \
46
+ --environment development \
47
+ --user some-user \
48
+ --log-file /var/log/myapp.log \
49
+ --pid-file /var/lock/myapp \
50
+ --server-type passenger \
51
+ --dependencies "apache2 mysql"
52
+
53
+ We could do exactly the same thing with the interactive mode (hitting `<return>`
54
+ will use the default value) :
55
+
56
+ $ ruploy generate my-script
57
+ name |ruploy| MyApp
58
+ address |127.0.0.1| 0.0.0.0
59
+ port |3000| 4242
60
+ directory |/home/simonc/ruby/ruploy| /path/to/my/app
61
+ environment |production| development
62
+ log_file |/var/log/rack-$PROCNAME-$PORT.log| /var/log/myapp.log
63
+ pid_file |/var/lock/rack-$PROCNAME-$PORT| /var/lock/myapp
64
+ user |www-data| some-user
65
+ dependencies |apache2| apache2 mysql
66
+ server_type |thin| passenger
67
+ options |--daemonize|
68
+
69
+ You may have notice the `options` question. You can pass here any additionnal
70
+ option, it will be passed to the server commande (passenger or thin). Any
71
+ argument on the command-line (except for the first one) will be added to this
72
+ list.
73
+
74
+ The two previous examples would generate the following init.d script :
75
+
76
+ #! /bin/sh
77
+ ### BEGIN INIT INFO
78
+ # Provides: myapp
79
+ # Required-Start: $remote_fs $syslog
80
+ # Required-Stop: $remote_fs $syslog
81
+ # Should-Start: apache2 mysql
82
+ # Should-Stop: apache2 mysql
83
+ # Default-Start: 2 3 4 5
84
+ # Default-Stop: 0 1 6
85
+ # Short-Description: Start/Stop MyApp
86
+ # Description: Manage the actions related to the passenger instance of MyApp
87
+ # you can use start, stop, restart and status
88
+ ### END INIT INFO
89
+
90
+ NAME="MyApp"
91
+ PROCNAME="myapp"
92
+
93
+ DIRECTORY="/path/to/my/app"
94
+ ADDRESS="0.0.0.0"
95
+ PORT="4242"
96
+ ENVIRONMENT="development"
97
+ USER="some-user"
98
+ LOGFILE="/var/log/myapp.log"
99
+ PIDFILE="/var/lock/myapp"
100
+ OPTIONS="--daemonize"
101
+ SERVER="passenger"
102
+
103
+ . "/Users/happynoff/.rvm/gems/ruby-1.9.2-p290@ruploy/gems/ruploy-0.0.1/data/ruploy-base.sh"
104
+
105
+ If you just want to change some variables but not all, use the `--use-defaults`
106
+ option :
107
+
108
+ $ ruploy --name HelloWorld --directory /some/path --use-defaults
109
+
110
+ ### Deploy
111
+
112
+ The `deploy` command links your script in `/etc/init.d` and then calls
113
+ `update-rc.d`.
114
+
115
+ It takes two parameters. The first one is the script name, the second is the
116
+ service name you want to use and is optional :
117
+
118
+ $ ruploy deploy my-script service-name
119
+ Deploying service-name... [OK]
120
+
121
+ $ ruploy deploy my-script
122
+ Deploying my-script... [OK]
123
+
124
+ ## Contribution
125
+
126
+ Feel free to fork Ruploy and make pull requests !
127
+
128
+ Raise issues if you have any problem or feature requests.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/ruploy ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'commander/import'
4
+ require 'ruploy'
5
+
6
+ program :name, 'Ruploy'
7
+ program :version, '0.0.1'
8
+ program :description, 'Generates init.d scripts to manage Rack apps using RVM'
9
+
10
+ command :generate do |c|
11
+ c.syntax = 'ruploy generate init_script [options]'
12
+ c.description = 'Generates an init.d script for a given Rack application and prints it in init_script.'
13
+
14
+ c.option '-n', '--name NAME', 'Name of your application (default: current directory name)'
15
+ c.option '-d', '--directory PATH', 'Root path of the application (default: current directory)'
16
+ c.option '-a', '--address HOST', 'Bind to HOST address (default: 127.0.0.1)'
17
+ c.option '-p', '--port NUMBER', 'Use the given port number (default: 3000)'
18
+ c.option '-e', '--environment ENV', 'Framework environment (default: production)'
19
+ c.option '-u', '--user USERNAME', 'User to run as. Ignored unless running as root (default: www-data)'
20
+ c.option '--log-file FILENAME', 'Where to write log messages (default: /var/log/rack-$PROCNAME-$PORT.log)'
21
+ c.option '--pid-file FILENAME', 'Where to store the PID file (default: /var/lock/rack-$PROCNAME-$PORT)'
22
+ c.option '-s', '--server-type SERVER', 'Server type, can be "thin" or "passenger" (default: thin)'
23
+ c.option '--dependencies DEPS', 'Dependencies of the init script (default: apache2)'
24
+ c.option '-i', '--independent', 'Print the generic code in the file instead of including it'
25
+ c.option '--use-defaults', 'Do not ask for missing informations and use default values'
26
+
27
+ c.action do |args, opts|
28
+ script_file = args.shift
29
+ app_config = Ruploy.get_config(args, opts.__hash__)
30
+ init_file = Ruploy.generate_init_file(app_config, opts.independent)
31
+
32
+ File.open(script_file, 'w') do |f|
33
+ f.puts init_file
34
+ end
35
+
36
+ File.chmod(0755, script_file)
37
+ end
38
+ end
39
+ alias_command :g, :generate
40
+ default_command :generate
41
+
42
+ command :deploy do |c|
43
+ c.syntax = 'ruploy deploy init_script [service_name]'
44
+ c.description = 'Copies the init script to /etc/init.d/<service_name> and calls update-rc.d.'
45
+
46
+ c.option '-f', '--force', 'Force symlink creation if a file already exists'
47
+
48
+ c.action do |args, opts|
49
+ script, name = args
50
+ path = File.expand_path(script)
51
+ name ||= File.basename(script)
52
+ target = "/etc/init.d/#{name}"
53
+
54
+ print "Deploying #{name}... "
55
+
56
+ File.delete(target) if File.exists?(target) && opts.force
57
+ File.symlink(path, target)
58
+ `update-rc.d #{name} defaults`
59
+
60
+ puts '[OK]'
61
+ end
62
+ end
@@ -0,0 +1,132 @@
1
+ # Outputs [OK] or [KO] given the previous status code 0=ok *=ko
2
+ ok_ko() {
3
+ local status=$?
4
+
5
+ case $status in
6
+ 0)
7
+ echo "[OK]"
8
+ ;;
9
+ *)
10
+ echo "[KO]"
11
+ ;;
12
+ esac
13
+
14
+ return $status
15
+ }
16
+
17
+ # returns the name of the running server process
18
+ server_process() {
19
+ case $SERVER in
20
+ passenger)
21
+ echo "nginx"
22
+ ;;
23
+ thin)
24
+ echo "thin"
25
+ ;;
26
+ *)
27
+ echo "unknown_server"
28
+ ;;
29
+ esac
30
+ }
31
+
32
+ # Returns the PID of the given server instance
33
+ server_pid() {
34
+ if [ -e "$PIDFILE" ]; then
35
+ if pidof $(server_process) | tr ' ' '\n' | grep -w $(cat $PIDFILE); then
36
+ return 0
37
+ fi
38
+ fi
39
+ return 1
40
+ }
41
+
42
+ # Returns the log-file option for the given server type
43
+ logfile_option() {
44
+ case $SERVER in
45
+ passenger)
46
+ echo "--log-file"
47
+ ;;
48
+ thin)
49
+ echo "--log"
50
+ ;;
51
+ *)
52
+ echo "unknown_server"
53
+ ;;
54
+ esac
55
+ }
56
+
57
+ # Returns the pid-file option for the given server type
58
+ pidfile_option() {
59
+ case $SERVER in
60
+ passenger)
61
+ echo "--pid-file"
62
+ ;;
63
+ thin)
64
+ echo "--pid"
65
+ ;;
66
+ *)
67
+ echo "unknown_server"
68
+ ;;
69
+ esac
70
+ }
71
+
72
+ # Starts the given server instance
73
+ ruploy_start() {
74
+ echo -n "Starting ${NAME}... "
75
+ $SERVER start $DIRECTORY \
76
+ --address $ADDRESS \
77
+ --port $PORT \
78
+ --environment $ENVIRONMENT \
79
+ --user $USER \
80
+ $(pidfile_option) $PIDFILE \
81
+ $(logfile_option) $LOGFILE \
82
+ $OPTIONS > /dev/null
83
+ ok_ko
84
+ }
85
+
86
+ # Stops the given server instance
87
+ ruploy_stop() {
88
+ echo -n "Stopping ${NAME}... "
89
+ $SERVER stop --pid-file $PIDFILE > /dev/null 2>&1
90
+ ok_ko
91
+ }
92
+
93
+ # Gives the status of the given server
94
+ ruploy_status() {
95
+ PID=$(server_pid) || true
96
+ if [ -n "$PID" ]; then
97
+ echo "${NAME} is running (pid $PID)."
98
+ return 0
99
+ else
100
+ echo "${NAME} is NOT running."
101
+ return 1
102
+ fi
103
+ }
104
+
105
+ # Loading the rvm environment of the application
106
+ . $DIRECTORY/.rvmrc
107
+
108
+ case "$1" in
109
+ restart)
110
+ ruploy_stop
111
+ ruploy_start
112
+ exit $?
113
+ ;;
114
+ start)
115
+ ruploy_start
116
+ exit $?
117
+ ;;
118
+ status)
119
+ ruploy_status
120
+ exit $?
121
+ ;;
122
+ stop)
123
+ ruploy_stop
124
+ exit $?
125
+ ;;
126
+ *)
127
+ echo "Usage: $0 {restart|start|status|stop}"
128
+ exit 1
129
+ ;;
130
+ esac
131
+
132
+ exit 0
@@ -0,0 +1,28 @@
1
+ #! /bin/sh
2
+ ### BEGIN INIT INFO
3
+ # Provides: {{proc_name}}
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Should-Start: {{dependencies}}
7
+ # Should-Stop: {{dependencies}}
8
+ # Default-Start: 2 3 4 5
9
+ # Default-Stop: 0 1 6
10
+ # Short-Description: Start/Stop {{name}}
11
+ # Description: Manage the actions related to the passenger instance of {{name}}
12
+ # you can use start, stop, restart and status
13
+ ### END INIT INFO
14
+
15
+ NAME="{{name}}"
16
+ PROCNAME="{{proc_name}}"
17
+
18
+ DIRECTORY="{{directory}}"
19
+ ADDRESS="{{address}}"
20
+ PORT="{{port}}"
21
+ ENVIRONMENT="{{environment}}"
22
+ USER="{{user}}"
23
+ LOGFILE="{{log_file}}"
24
+ PIDFILE="{{pid_file}}"
25
+ OPTIONS="{{options}}"
26
+ SERVER="{{server_type}}"
27
+
28
+ {{{ruploy_base}}}
data/lib/ruploy.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'mustache'
2
+
3
+ module Ruploy
4
+ DEFAULTS = {
5
+ :name => File.basename(File.expand_path '.'),
6
+ :address => '127.0.0.1',
7
+ :port => 3000,
8
+ :directory => File.expand_path('.'),
9
+ :environment => 'production',
10
+ :log_file => '/var/log/rack-$PROCNAME-$PORT.log',
11
+ :pid_file => '/var/lock/rack-$PROCNAME-$PORT',
12
+ :user => 'www-data',
13
+ :dependencies => 'apache2',
14
+ :server_type => 'thin'
15
+ }
16
+
17
+ class << self
18
+ def get_config(args, opts)
19
+ config = DEFAULTS.merge(opts)
20
+
21
+ unless opts[:use_defaults]
22
+ config.merge! (config.keys - opts.keys).inject({}) { |h, key|
23
+ h[key] = ask("#{key} ") { |q| q.default = DEFAULTS[key] }
24
+ h
25
+ }
26
+ end
27
+
28
+ config[:options] = '--daemonize'
29
+ config[:options] << " #{args.join(' ')}" if args.any?
30
+ config[:proc_name] = config[:name].gsub(/\W/, '_').squeeze('_').downcase
31
+
32
+ return config
33
+ end
34
+
35
+ def generate_init_file(config, independant=false)
36
+ ruploy_init = File.expand_path('../../data/ruploy-init.mustache', __FILE__)
37
+ ruploy_base = File.expand_path('../../data/ruploy-base.sh', __FILE__)
38
+ template = File.read(ruploy_init)
39
+ config = config.dup
40
+
41
+ config[:ruploy_base] = independant ? File.read(ruploy_base) : %Q(. "#{ruploy_base}")
42
+
43
+ Mustache.render(template, config)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Ruploy
2
+ VERSION = "0.0.1"
3
+ end
data/ruploy.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ruploy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ruploy"
7
+ s.version = Ruploy::VERSION
8
+ s.authors = ["Simon COURTOIS"]
9
+ s.email = ["scourtois@cubyx.fr"]
10
+ s.homepage = ""
11
+ s.summary = %q{Ruploy generates init.d scripts to manage Rack apps using RVM}
12
+ s.description = %q{If you want to manage several Rack apps using different versions of Ruby via RVM, Ruploy can help you. It handles gemsets too !}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "commander"
20
+ s.add_dependency "mustache"
21
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon COURTOIS
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: commander
16
+ requirement: &2161000480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2161000480
25
+ - !ruby/object:Gem::Dependency
26
+ name: mustache
27
+ requirement: &2160999840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2160999840
36
+ description: If you want to manage several Rack apps using different versions of Ruby
37
+ via RVM, Ruploy can help you. It handles gemsets too !
38
+ email:
39
+ - scourtois@cubyx.fr
40
+ executables:
41
+ - ruploy
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - .rvmrc
47
+ - Gemfile
48
+ - README.md
49
+ - Rakefile
50
+ - bin/ruploy
51
+ - data/ruploy-base.sh
52
+ - data/ruploy-init.mustache
53
+ - lib/ruploy.rb
54
+ - lib/ruploy/version.rb
55
+ - ruploy.gemspec
56
+ homepage: ''
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.10
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Ruploy generates init.d scripts to manage Rack apps using RVM
80
+ test_files: []