upstart-exporter 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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in upstart-exporter.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../lib/'))
3
+
4
+ require 'upstart-exporter'
5
+ require 'optparse'
6
+
7
+ USAGE = "Usage: upstart-export -p PROCFILE -n APPNAME"
8
+ options = {}
9
+
10
+ OptionParser.new do |opts|
11
+
12
+ opts.on("-p", "--procfile PROCFILE", "Procfile to export") do |p|
13
+ options[:procfile] = p
14
+ end
15
+
16
+ opts.on("-n", "--appname NAME", "Application name") do |n|
17
+ options[:app_name] = n
18
+ end
19
+
20
+ end.parse!
21
+
22
+ abort(USAGE) unless options[:procfile] && options[:app_name]
23
+
24
+
25
+ begin
26
+ Upstart::Exporter.new(options).export
27
+ rescue Upstart::ExportError => e
28
+ abort(e.to_s)
29
+ end
@@ -0,0 +1,5 @@
1
+ module Upstart
2
+ class Exporter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,180 @@
1
+ require "upstart-exporter/version"
2
+
3
+ module Upstart
4
+ class ExportError < RuntimeError; end
5
+
6
+ class Exporter
7
+ DEFAULTS = {
8
+ 'helper_dir' => '/var/local/upstart_helpers/',
9
+ 'upstart_dir' => '/etc/init/',
10
+ 'run_user' => 'service'
11
+ }
12
+ CONF = '/etc/upstart-exporter.yaml'
13
+
14
+ attr_accessor :helper_dir, :upstart_dir, :run_user
15
+
16
+ def initialize(options)
17
+ process_opts(options)
18
+ read_global_opts
19
+ check_dir(@helper_dir)
20
+ check_dir(@upstart_dir)
21
+ end
22
+
23
+
24
+ def read_global_opts
25
+ config = if FileTest.file?(CONF)
26
+ YAML::load(File.read(CONF))
27
+ else
28
+ STDERR.puts "#{CONF} not found"
29
+ {}
30
+ end
31
+ %w{helper_dir upstart_dir run_user}.each do |param|
32
+ value = if config[param]
33
+ config[param]
34
+ else
35
+ STDERR.puts "Param #{param} is not set, taking default value #{DEFAULTS[param]}"
36
+ DEFAULTS[param]
37
+ end
38
+ self.send "#{param}=", value
39
+ end
40
+ end
41
+
42
+ def process_opts(options)
43
+ process_procfile(options[:procfile])
44
+ process_appname(options[:app_name])
45
+ end
46
+
47
+ def process_procfile(name)
48
+ error "#{name} is not a readable file" unless FileTest.file?(name)
49
+ @commands = {}
50
+ content = File.read(name)
51
+ content.lines.each do |line|
52
+ line.chomp!
53
+ if line =~ /^(\w+?):(.*)$/
54
+ label = $1
55
+ command = $2
56
+ @commands[label] = command
57
+ else
58
+ error "procfile lines should have the following format: 'some_label: command'"
59
+ end
60
+ end
61
+ end
62
+
63
+ def process_appname(app_name)
64
+ error "Application name should contain only letters (and underscore) and be nonempty, so #{app_name.inspect} is not suitable" unless app_name =~ /^\w+$/
65
+ @app_name = "fb-#{app_name}"
66
+ end
67
+
68
+ def check_dir(dir)
69
+ FileUtils.mkdir_p(dir) unless FileTest.directory?(dir)
70
+ error "Path #{dir} does not exist" unless FileTest.directory?(dir)
71
+ elements = [''] + dir.split('/').select{|el| !el.empty?}
72
+ path = ''
73
+ elements.each do |el|
74
+ path += (el + '/')
75
+ if FileTest.world_writable?(path)
76
+ error "Path #{path} (in #{dir}) is writable by others"
77
+ end
78
+ end
79
+ end
80
+
81
+ def export
82
+ clear
83
+ export_app
84
+ @commands.each do |cmd_name, cmd|
85
+ export_command(cmd_name, cmd)
86
+ end
87
+ end
88
+
89
+ HELPER_TPL = <<-HEREDOC
90
+ #!/bin/sh
91
+ %s
92
+ HEREDOC
93
+
94
+ APP_TPL = <<-HEREDOC
95
+ pre-start script
96
+
97
+ bash << "EOF"
98
+ mkdir -p /var/log/%s
99
+ chown -R %s /var/log/%s
100
+ EOF
101
+
102
+ end script
103
+ HEREDOC
104
+
105
+ COMMAND_TPL = <<-HEREDOC
106
+ start on starting %s
107
+ stop on stopping %s
108
+ HEREDOC
109
+
110
+
111
+ COMMAND_REAL_TPL = <<-HEREDOC
112
+ start on starting %s
113
+ stop on stopping %s
114
+ respawn
115
+
116
+ exec su - %s -c '/bin/sh %s >> /var/log/%s/%s.log 2>&1'
117
+ HEREDOC
118
+
119
+ def upstart_conf
120
+ File.join(@upstart_dir, "#{@app_name}.conf")
121
+ end
122
+
123
+ def app_cmd(cmd_name)
124
+ "#{@app_name}-#{cmd_name}"
125
+ end
126
+
127
+ def upstart_cmd_conf(cmd_name)
128
+ File.join(@upstart_dir, "#{app_cmd(cmd_name)}.conf")
129
+ end
130
+
131
+ def helper_cmd_conf(cmd_name)
132
+ File.join(@helper_dir, "#{app_cmd(cmd_name)}.sh")
133
+ end
134
+
135
+ def clear
136
+ FileUtils.rm(upstart_conf) if FileTest.file?(upstart_conf)
137
+ Dir[upstart_cmd_conf('*')].each do |f|
138
+ FileUtils.rm(f)
139
+ end
140
+ Dir[helper_cmd_conf('*')].each do |f|
141
+ FileUtils.rm(f)
142
+ end
143
+ end
144
+
145
+ def export_app
146
+ app_conf = APP_TPL % [@app_name, @run_user, @app_name]
147
+ File.open(upstart_conf, 'w') do |f|
148
+ f.write(app_conf)
149
+ end
150
+ end
151
+
152
+ def export_cmd_helper(cmd_name, cmd)
153
+ helper_script_cont = HELPER_TPL % [cmd]
154
+ File.open(helper_cmd_conf(cmd_name), 'w') do |f|
155
+ f.write(helper_script_cont)
156
+ end
157
+ end
158
+
159
+ def export_cmd_upstart_confs(cmd_name)
160
+ cmd_upstart_conf_content = COMMAND_TPL % [@app_name, @app_name]
161
+ File.open(upstart_cmd_conf(cmd_name), 'w') do |f|
162
+ f.write(cmd_upstart_conf_content)
163
+ end
164
+
165
+ cmd_upstart_conf_content_real = COMMAND_REAL_TPL % [app_cmd(cmd_name), app_cmd(cmd_name), @run_user, helper_cmd_conf(cmd_name), @app_name, cmd_name]
166
+ File.open(upstart_cmd_conf(cmd_name + '-real'), 'w') do |f|
167
+ f.write(cmd_upstart_conf_content_real)
168
+ end
169
+ end
170
+
171
+ def export_command(cmd_name, cmd)
172
+ export_cmd_helper(cmd_name, cmd)
173
+ export_cmd_upstart_confs(cmd_name)
174
+ end
175
+
176
+ def error(msg)
177
+ raise Upstart::ExportError, msg
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "upstart-exporter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "upstart-exporter"
7
+ s.version = Upstart::Exporter::VERSION
8
+ s.authors = ["Ilya Averyanov"]
9
+ s.email = ["ilya@averyanov.org"]
10
+ s.homepage = ""
11
+ s.summary = %q{Gem for converting Procfile-like files to upstart scripts}
12
+ s.description = %q{Gem for converting Procfile-like files to upstart scripts}
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
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upstart-exporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ilya Averyanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Gem for converting Procfile-like files to upstart scripts
15
+ email:
16
+ - ilya@averyanov.org
17
+ executables:
18
+ - upstart-export
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - bin/upstart-export
26
+ - lib/upstart-exporter.rb
27
+ - lib/upstart-exporter/version.rb
28
+ - upstart-exporter.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.10
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Gem for converting Procfile-like files to upstart scripts
53
+ test_files: []