lunchy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +49 -0
  3. data/bin/lunchy +50 -0
  4. data/lib/lunchy.rb +74 -0
  5. data/lunchy.gemspec +18 -0
  6. metadata +61 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Mike Perham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ lunchy
2
+ =================
3
+
4
+ A friendly wrapper for launchctl. Start your agents and go to lunch!
5
+
6
+ Don't you hate OSX's launchctl? You have to give it exact filenames. The syntax is annoying different from Linux's simple init system and overly verbose. It's just not a very developer-friendly tool.
7
+
8
+ Lunchy aims to be that friendly tool by wrapping launchctl and providing a few simple operations that you perform all the time:
9
+
10
+ - ls [pattern]
11
+ - start [pattern]
12
+ - stop [pattern]
13
+ - restart [pattern]
14
+ - status [pattern]
15
+
16
+ where pattern is just a substring that matches the agent's plist filename. Make sure you use a unique pattern or Lunchy will operate on the first matching agent.
17
+
18
+ So instead of:
19
+
20
+ launchctl load ~/Library/LaunchAgents/io.redis.redis-server.plist
21
+
22
+ you can do this:
23
+
24
+ lunchy start redis
25
+
26
+ and:
27
+
28
+ > lunchy ls
29
+ com.danga.memcached
30
+ com.google.keystone.agent
31
+ com.mysql.mysqld
32
+ io.redis.redis-server
33
+ org.mongodb.mongod
34
+
35
+ Lunchy isn't a great name but gem names are like domains, most of the good ones are taken. :-(
36
+
37
+
38
+ Installation
39
+ ---------------
40
+
41
+ gem install lunchy
42
+
43
+ Lunchy is written in Ruby because I'm a good Ruby developer and a poor Bash developer. Help welcome.
44
+
45
+
46
+ About
47
+ -----------------
48
+
49
+ Mike Perham, [@mperham](http://twitter.com/mperham), [mikeperham.com](http://mikeperham.com/)
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib" if $0 == __FILE__
4
+ require 'optparse'
5
+ require 'lunchy'
6
+
7
+ CONFIG = { :verbose => false }
8
+ OPERATIONS = %w(start stop restart ls status)
9
+
10
+ option_parser = OptionParser.new do |opts|
11
+ opts.banner = "Lunchy #{Lunchy::VERSION}, the friendly launchctl wrapper\nUsage: #{__FILE__} [#{OPERATIONS.join('|')}] [options]"
12
+
13
+ opts.on("-v", "--verbose", "Show command executions") do |verbose|
14
+ CONFIG[:verbose] = true
15
+ end
16
+
17
+ opts.separator <<-EOS
18
+
19
+ Supported commands:
20
+
21
+ ls [pattern] Show the list of installed agents, with optional [pattern] filter
22
+ start [pattern] Start the first agent matching [pattern]
23
+ stop [pattern] Stop the first agent matching [pattern]
24
+ restart [pattern] Stop and start the first agent matching [pattern]
25
+ status [pattern] Show the PID and label for all agents, with optional [pattern] filter
26
+
27
+ Example:
28
+ lunchy ls
29
+ lunchy start redis
30
+ lunchy stop mongo
31
+ lunchy status mysql
32
+ EOS
33
+ end
34
+ option_parser.parse!
35
+
36
+
37
+ op = ARGV.shift
38
+ if OPERATIONS.include?(op)
39
+ begin
40
+ Lunchy.new.send(op.to_sym, ARGV)
41
+ rescue ArgumentError => ex
42
+ puts ex.message
43
+ rescue Exception => e
44
+ puts "Uh oh, I didn't expect this:"
45
+ puts e.message
46
+ puts e.backtrace.join("\n")
47
+ end
48
+ else
49
+ puts option_parser.help
50
+ end
@@ -0,0 +1,74 @@
1
+ class Lunchy
2
+ VERSION = '0.1.0'
3
+
4
+ def start(params)
5
+ raise ArgumentError, "start [name]" if params.empty?
6
+ name = params[0]
7
+ (_, file) = plists.detect { |k,v| k =~ /#{name}/i }
8
+ return puts "No daemon found matching '#{name}'" if !name
9
+ execute("launchctl load #{file}")
10
+ end
11
+
12
+ def stop(params)
13
+ raise ArgumentError, "stop [name]" if params.empty?
14
+ name = params[0]
15
+ (_, file) = plists.detect { |k,v| k =~ /#{name}/i }
16
+ return puts "No daemon found matching '#{name}'" if !name
17
+ execute("launchctl unload #{file}")
18
+ end
19
+
20
+ def restart(params)
21
+ stop(params)
22
+ start(params)
23
+ end
24
+
25
+ def status(params)
26
+ pattern = params[0]
27
+ cmd = "launchctl list"
28
+ cmd << " | grep -i \"#{pattern}\"" if pattern
29
+ execute(cmd)
30
+ end
31
+
32
+ def ls(params)
33
+ agents = plists.keys
34
+ agents = agents.grep(/#{params[0]}/) if !params.empty?
35
+ puts agents.sort.join("\n")
36
+ end
37
+
38
+ private
39
+
40
+ def execute(cmd)
41
+ puts "Executing: #{cmd}" if verbose?
42
+ puts `#{cmd}`
43
+ end
44
+
45
+ def plists
46
+ @plists ||= begin
47
+ plists = {}
48
+ %w(/Library/LaunchAgents ~/Library/LaunchAgents).each do |dir|
49
+ Dir["#{File.expand_path(dir)}/*.plist"].each do |filename|
50
+ plists[File.basename(filename, ".plist")] = filename
51
+ end
52
+ end
53
+ plists
54
+ end
55
+ end
56
+
57
+ # def daemons
58
+ # @daemons ||= begin
59
+ # content = `launchctl list | grep -v -i "^-\\|anonymous"`
60
+ # daemons = []
61
+ # content.each_line do |x|
62
+ # data = x.split(' ')
63
+ # daemons << {
64
+ # :pid => data[0].to_i,
65
+ # :name => data[2]
66
+ # }
67
+ # end
68
+ # end
69
+ # end
70
+
71
+ def verbose?
72
+ CONFIG[:verbose]
73
+ end
74
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lunchy"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "lunchy"
7
+ s.version = Lunchy::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mike Perham"]
10
+ s.email = ["mperham@gmail.com"]
11
+ s.homepage = "http://github.com/mperham/lunch"
12
+ s.summary = s.description = %q{Friendly wrapper around launchctl}
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
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lunchy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Mike Perham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-23 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Friendly wrapper around launchctl
18
+ email:
19
+ - mperham@gmail.com
20
+ executables:
21
+ - lunchy
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - LICENSE
28
+ - README.md
29
+ - bin/lunchy
30
+ - lib/lunchy.rb
31
+ - lunchy.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/mperham/lunch
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.6.2
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Friendly wrapper around launchctl
60
+ test_files: []
61
+