rappa 0.0.3 → 0.0.4

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.
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
  Rappa is a tool which lets you package your rack based application e.g. Sinatra, Rails etc for easy deployment to a ThunderCat container.
4
4
  Visit the ThunderCat project to understand how this works.
5
5
 
6
+ [![Build Status](https://travis-ci.org/masterthought/rappa.png?branch=master)](https://travis-ci.org/masterthought/rappa)
7
+ [![Code Climate](https://codeclimate.com/repos/51d89bb913d6376ff8018f91/badges/607d6b3238c1b52da631/gpa.png)](https://codeclimate.com/repos/51d89bb913d6376ff8018f91/feed)
8
+
6
9
  ## Background
7
10
 
8
11
  Rappa is written in ruby and was created to simplify the package and deploy process of Sinatra and Rails apps. The idea is to have a single artifact
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/rappa_error'
2
+
3
+ class PropertyValidator
4
+
5
+ def initialize(config,file)
6
+ @config = config
7
+ @file = file
8
+ end
9
+
10
+ def input_directory
11
+ unless @config[:input_directory].nil?
12
+ @config[:file_name] = (@config[:input_directory] == '.') ? 'default' : @file.basename(@config[:input_directory])
13
+ end
14
+ check_property(@config[:input_directory], :input_directory)
15
+ input_directory = append_trailing_slash(@config[:input_directory])
16
+ raise RappaError, "input directory: #{input_directory} does not exist" unless @file.exists?(input_directory)
17
+ input_directory
18
+ end
19
+
20
+ def output_directory
21
+ check_property(@config[:output_directory], :output_directory)
22
+ @config[:output_directory]
23
+ end
24
+
25
+ def input_archive
26
+ check_property(@config[:input_archive], :input_archive)
27
+ input_archive = @config[:input_archive]
28
+ raise RappaError, "input archive: #{@config[:input_archive]} does not exist" unless @file.exists?(@config[:input_archive])
29
+ input_archive
30
+ end
31
+
32
+ def output_archive
33
+ check_property(@config[:output_archive], :output_archive)
34
+ @config[:output_archive]
35
+ end
36
+
37
+ def input_rap
38
+ check_property(@config[:input_rap], :input_rap)
39
+ raise RappaError, "input rap: #{@config[:input_rap]} does not exist" unless @file.exists?(@config[:input_rap])
40
+ @config[:input_rap]
41
+ end
42
+
43
+ def url
44
+ check_property(@config[:url], :url)
45
+ @config[:url]
46
+ end
47
+
48
+ def api_key
49
+ check_property(@config[:api_key], :api_key)
50
+ @config[:api_key]
51
+ end
52
+
53
+
54
+ def validate_name(name)
55
+ raise RappaError, "a rap archive already exists with the name: #{@config[:file_name]}.rap - please remove it and try again" if @file.exists?(name)
56
+ end
57
+
58
+ private
59
+
60
+ def check_property(property, property_type)
61
+ raise RappaError, "property #{property_type} is mandatory but was not supplied" if property.nil? or property.empty?
62
+ end
63
+
64
+ def append_trailing_slash(path)
65
+ path = "#{path}/" if path[-1] != '/'
66
+ path
67
+ end
68
+
69
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/rappa_error'
2
+
3
+ class RapValidator
4
+
5
+ SUPPORTED_SERVERS = %w(thin unicorn webrick)
6
+
7
+ def initialize(file,yaml=YAML)
8
+ @file = file
9
+ @yaml = yaml
10
+ end
11
+
12
+ def validate_package(directory)
13
+ @rap = get_rap_file(directory + '/rap.yml')
14
+ validate_server_type
15
+ validate_scripts
16
+ validate_details
17
+ end
18
+
19
+ def validate_is_rap_archive(file, input_archive)
20
+ base_name = file.basename(input_archive)
21
+ extension = file.extname(base_name)
22
+ raise RappaError, "input archive: #{input_archive} is not a valid .rap archive" unless extension == '.rap'
23
+ end
24
+
25
+ private
26
+
27
+ def validate_details
28
+ raise RappaError, 'rap.yml :pids is required' if nil_or_empty?(@rap[:pids])
29
+ raise RappaError, 'rap.yml :name is required' if nil_or_empty?(@rap[:name])
30
+ raise RappaError, 'rap.yml :description is required' if nil_or_empty?(@rap[:description])
31
+ raise RappaError, 'rap.yml :version is required' if nil_or_empty?(@rap[:version])
32
+ end
33
+
34
+ def validate_scripts
35
+ raise RappaError, 'rap.yml :start_script is required' if nil_or_empty?(@rap[:start_script])
36
+ raise RappaError, 'rap.yml :stop_script is required' if nil_or_empty?(@rap[:stop_script])
37
+ end
38
+
39
+ def validate_server_type
40
+ raise RappaError, "rap.yml :server_type is required and must be one of: #{SUPPORTED_SERVERS}" if nil_or_empty?(@rap[:server_type])
41
+ raise RappaError, "rap.yml :server_type supplied: #{@rap[:server_type]} is not in the supported server list: #{SUPPORTED_SERVERS}" unless SUPPORTED_SERVERS.include?(@rap[:server_type])
42
+ end
43
+
44
+ def get_rap_file(rap_file_path)
45
+ if @file.exists?(rap_file_path)
46
+ @yaml.load_file(rap_file_path)
47
+ else
48
+ raise RappaError, 'rap.yml file is required - please run rappa generate to create a sample rap.yml'
49
+ end
50
+ end
51
+
52
+ def nil_or_empty?(item)
53
+ item.nil? or item.empty?
54
+ end
55
+
56
+ end
data/src/rappa.rb CHANGED
@@ -2,61 +2,42 @@ require 'zip/zip'
2
2
  require 'fileutils'
3
3
  require 'yaml'
4
4
  require 'rest-client'
5
-
6
- class RappaError < Exception;
7
- end
5
+ require File.dirname(__FILE__) + '/rap_validator'
6
+ require File.dirname(__FILE__) + '/property_validator'
8
7
 
9
8
  class Rappa
10
9
 
11
- SUPPORTED_SERVERS = %w(thin unicorn webrick)
12
-
13
- def initialize(config={},rest_client=RestClient,file=File)
10
+ def initialize(config={}, rest_client=RestClient, file=File, rap_validator=RapValidator, property_validator=PropertyValidator)
14
11
  @config = config
15
12
  @file = file
16
13
  @rest_client = rest_client
17
- unless @config[:input_directory].nil?
18
- @config[:file_name] = (@config[:input_directory] == '.') ? 'default' : @file.basename(@config[:input_directory])
19
- end
14
+ @property_validator= property_validator.new(@config,@file)
15
+ @rap_validator = rap_validator.new(@file)
20
16
  end
21
17
 
22
18
  def package
23
- check_property(@config[:input_directory], :input_directory)
24
- check_property(@config[:output_directory], :output_directory)
25
- input_directory = append_trailing_slash(@config[:input_directory])
26
- raise RappaError, "input directory: #{input_directory} does not exist" unless @file.exists?(input_directory)
27
- output_directory = @config[:output_directory]
19
+ input_directory = @property_validator.input_directory
20
+ output_directory = @property_validator.output_directory
28
21
  FileUtils.mkdir_p output_directory unless @file.exists?(output_directory)
29
- name = "#{@config[:output_directory]}/#{@config[:file_name]}.rap"
30
- raise RappaError, "a rap archive already exists with the name: #{@config[:file_name]}.rap - please remove it and try again" if @file.exists?(name)
31
- validate(input_directory)
32
- Zip::ZipFile.open(name, Zip::ZipFile::CREATE) do |zip_file|
33
- Dir[@file.join(input_directory, '**', '**')].each do |file|
34
- zip_file.add(file.sub(input_directory, ''), file)
35
- end
36
- end
22
+ name = "#{output_directory}/#{@config[:file_name]}.rap"
23
+ @property_validator.validate_name(name)
24
+ @rap_validator.validate_package(input_directory)
25
+ package_zip(input_directory, name)
37
26
  end
38
27
 
39
28
  def expand
40
- check_property(@config[:input_archive], :input_archive)
41
- check_property(@config[:output_archive], :output_archive)
42
- raise RappaError, "input archive: #{@config[:input_archive]} does not exist" unless @file.exists?(@config[:input_archive])
43
- validate_is_rap_archive
44
- output_directory = @config[:output_archive]
29
+ input_archive = @property_validator.input_archive
30
+ output_directory = @property_validator.output_archive
31
+ @rap_validator.validate_is_rap_archive(@file,input_archive)
45
32
  FileUtils.mkdir_p output_directory unless @file.exists?(output_directory)
46
- Zip::ZipFile.open(@config[:input_archive]) { |zip_file|
47
- zip_file.each { |f|
48
- f_path=@file.join(calculate_destination, f.name)
49
- FileUtils.mkdir_p(@file.dirname(f_path))
50
- zip_file.extract(f, f_path) { true } unless @file.exist?(f_path) # true will overwrite existing files.
51
- }
52
- }
33
+ expand_zip
53
34
  end
54
35
 
55
36
  def deploy
56
- check_property(@config[:input_rap], :input_rap)
57
- check_property(@config[:url], :url)
58
- check_property(@config[:api_key], :api_key)
59
- @rest_client.put "#{@config[:url]}?key=#{@config[:api_key]}", :file => @file.new(@config[:input_rap])
37
+ input_rap = @property_validator.input_rap
38
+ api_key = @property_validator.api_key
39
+ url = @property_validator.url
40
+ @rest_client.put "#{url}?key=#{ api_key}", :file => @file.new(input_rap)
60
41
  end
61
42
 
62
43
  def generate
@@ -73,42 +54,30 @@ class Rappa
73
54
 
74
55
  private
75
56
 
76
- def check_property(property, property_type)
77
- raise RappaError, "property #{property_type} is mandatory but was not supplied" if property.nil? or property.empty?
57
+ def expand_zip
58
+ Zip::ZipFile.open(@config[:input_archive]) { |zip_file|
59
+ zip_file.each { |f|
60
+ f_path=@file.join(calculate_destination, f.name)
61
+ FileUtils.mkdir_p(@file.dirname(f_path))
62
+ zip_file.extract(f, f_path) { true } unless @file.exist?(f_path) # true will overwrite existing files.
63
+ }
64
+ }
78
65
  end
79
66
 
80
- def validate(directory)
81
- rap_file = directory + '/rap.yml'
82
- if @file.exists?(rap_file)
83
- rap = YAML.load_file(rap_file)
84
- raise RappaError, "rap.yml :server_type is required and must be one of: #{SUPPORTED_SERVERS}" if rap[:server_type].nil? or rap[:server_type].empty?
85
- raise RappaError, "rap.yml :server_type supplied: #{rap[:server_type]} is not in the supported server list: #{SUPPORTED_SERVERS}" unless SUPPORTED_SERVERS.include?(rap[:server_type])
86
- raise RappaError, 'rap.yml :start_script is required' if rap[:start_script].nil? or rap[:start_script].empty?
87
- raise RappaError, 'rap.yml :stop_script is required' if rap[:stop_script].nil? or rap[:stop_script].empty?
88
- raise RappaError, 'rap.yml :pids is required' if rap[:pids].nil? or rap[:pids].empty?
89
- raise RappaError, 'rap.yml :name is required' if rap[:name].nil? or rap[:name].empty?
90
- raise RappaError, 'rap.yml :description is required' if rap[:description].nil? or rap[:description].empty?
91
- raise RappaError, 'rap.yml :version is required' if rap[:version].nil? or rap[:version].empty?
92
- else
93
- raise RappaError, 'rap.yml file is required - please run rappa generate to create a sample rap.yml'
67
+ def package_zip(input_directory, name)
68
+ Zip::ZipFile.open(name, Zip::ZipFile::CREATE) do |zip_file|
69
+ Dir[@file.join(input_directory, '**', '**')].each do |file|
70
+ zip_file.add(file.sub(input_directory, ''), file)
71
+ end
94
72
  end
95
73
  end
96
74
 
97
- def validate_is_rap_archive
98
- base_name = @file.basename(@config[:input_archive])
99
- extension = @file.extname(base_name)
100
- raise RappaError, "input archive: #{@config[:input_archive]} is not a valid .rap archive" unless extension == '.rap'
101
- end
102
-
103
75
  def calculate_destination
104
76
  base_name = @file.basename(@config[:input_archive])
105
77
  name = base_name.chomp(@file.extname(base_name))
106
78
  @config[:output_archive] + '/' + name
107
79
  end
108
80
 
109
- def append_trailing_slash(path)
110
- path = "#{path}/" if path[-1] != '/'
111
- path
112
- end
81
+
113
82
 
114
83
  end
@@ -0,0 +1,2 @@
1
+ class RappaError < Exception;
2
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rappa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-05 00:00:00.000000000 Z
12
+ date: 2013-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -104,6 +104,9 @@ extra_rdoc_files:
104
104
  - README.md
105
105
  files:
106
106
  - src/rappa.rb
107
+ - src/rap_validator.rb
108
+ - src/property_validator.rb
109
+ - src/rappa_error.rb
107
110
  - README.md
108
111
  - bin/rappa
109
112
  homepage: https://github.com/masterthought/rappa