paasmaker-interface 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +19 -0
  2. data/README.md +91 -0
  3. data/lib/paasmaker.rb +192 -0
  4. metadata +66 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Daniel Foote
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ Paasmaker Ruby Interface Library
2
+ ================================
3
+
4
+ This is a simple Ruby library that is designed to read in the Paasmaker
5
+ configuration, falling back to a custom configuration file in development.
6
+
7
+ Usage - Rails
8
+ -------------
9
+
10
+ If you want to use this with your Rails project, do the following steps:
11
+
12
+ * Add paasmaker-interface to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'paasmaker-interface'
16
+ ```
17
+
18
+ * Update your database.yml file to look like the following:
19
+
20
+ ```yaml
21
+ <% require 'paasmaker' %>
22
+ <% interface = Paasmaker::Interface.new(['../project-configuration.yml']) %>
23
+ <% database = interface.get_service('postgres') %>
24
+
25
+ production:
26
+ adapter: postgresql
27
+ database: "<%= database['database'] %>"
28
+ host: "<%= database['hostname'] %>"
29
+ username: "<%= database['username'] %>"
30
+ password: "<%= database['password'] %>"
31
+ port: "<%= database['port'] %>"
32
+ ```
33
+
34
+ And update your adapter as appropriate for your application.
35
+
36
+ Usage - Other applications
37
+ --------------------------
38
+
39
+ To read in the configuration details in other applications, you can instantiate
40
+ the Interface class directly and work with it.
41
+
42
+ ```ruby
43
+ require 'paasmaker'
44
+
45
+ interface = Paasmaker::Interface.new(['../project-configuration.yml'])
46
+
47
+ interface.is_on_paasmaker?()
48
+
49
+ service = interface.get_service('service')
50
+ ```
51
+
52
+ Example Configuration Overrides
53
+ -------------------------------
54
+
55
+ Example YAML configuration file:
56
+
57
+ ```yaml
58
+ services:
59
+ parameters:
60
+ foo: bar
61
+
62
+ application:
63
+ name: test
64
+ version: 1
65
+ workspace: Test
66
+ workspace_stub: test
67
+ ```
68
+
69
+ Example JSON configuration file:
70
+
71
+ ```json
72
+ {
73
+ "services": {
74
+ "parameters": {
75
+ "foo": "bar"
76
+ }
77
+ },
78
+ "application": {
79
+ "name": "test",
80
+ "version": 1,
81
+ "workspace": "Test",
82
+ "workspace_stub": "test"
83
+ }
84
+ }
85
+ ```
86
+
87
+ Testing
88
+ -------
89
+
90
+ To run the unit tests for the interface, just execute test.rb
91
+ in the root of the project.
data/lib/paasmaker.rb ADDED
@@ -0,0 +1,192 @@
1
+ # Copyright (c) 2013 Daniel Foote.
2
+ #
3
+ # See the file LICENSE for copying permission.
4
+ # The licence is the MIT licence.
5
+
6
+ # TODO: What Gem is required to make YAML work?
7
+ require 'yaml'
8
+ require 'json'
9
+
10
+ module Paasmaker
11
+
12
+ # Exception raised when something goes wrong
13
+ # with the interface. The message should indicate
14
+ # what went wrong.
15
+ class InterfaceException < Exception
16
+ end
17
+
18
+ # An interface class for Paasmaker. It can parse
19
+ # the Paasmaker environment variables and supply them
20
+ # to your application. Alternately, it can load override
21
+ # configuration files to fill in missing values if it is
22
+ # not running on Paasmaker.
23
+ class Interface
24
+ # Create a new instance of the interface class. Supply
25
+ # an array of override path names. For example, supply
26
+ # <tt>['../project-name.json']</tt> to load a configuration file
27
+ # one level above your project root.
28
+ def initialize(override_paths)
29
+ @override_paths = override_paths
30
+ @is_on_paasmaker = false
31
+ @variables = {}
32
+ @services = {}
33
+ # It's over 9000.
34
+ @port = 9001
35
+
36
+ parse_metadata()
37
+ end
38
+
39
+ # Helper function to parse the Paasmaker metadata. You
40
+ # should not call this externally.
41
+ def parse_metadata() # :nodoc:
42
+ raw_services = ENV['PM_SERVICES']
43
+ raw_metadata = ENV['PM_METADATA']
44
+ raw_port = ENV['PM_PORT']
45
+
46
+ if raw_services and raw_metadata
47
+ # We're running on Paasmaker.
48
+ @is_on_paasmaker = true
49
+
50
+ # Load from environment variables.
51
+ @variables = JSON.parse(raw_metadata)
52
+ @services = JSON.parse(raw_services)
53
+
54
+ if raw_port
55
+ @port = raw_port.to_i()
56
+ end
57
+ else
58
+ # Not running on Paasmaker, load from
59
+ # a configuration file.
60
+ load_configuration_file()
61
+ end
62
+ end
63
+
64
+ # Helper function to find and load a configuration file.
65
+ # You should not need to call this externally.
66
+ def load_configuration_file() # :nodoc:
67
+ @override_paths.each do |path|
68
+ if File.exist?(path)
69
+
70
+ if path.end_with?('.yml')
71
+ # It's YAML, parse it as such.
72
+ data = YAML.load_file(path)
73
+ elsif path.end_with?('.json')
74
+ # It's JSON.
75
+ data = JSON.parse(IO.read(path))
76
+ end
77
+
78
+ store_configuration(path, data)
79
+ return
80
+ end
81
+
82
+ end
83
+
84
+ # If we got here, we were not able to load any files.
85
+ raise InterfaceException, "Unable to find any configuration files to load."
86
+ end
87
+
88
+ # Helper function to store a loaded configuration. You should
89
+ # not call this externally.
90
+ def store_configuration(path, data) # :nodoc:
91
+ # Validate the data first, filling in some missing blanks.
92
+ if not data.has_key?('services')
93
+ data['services'] = {}
94
+ end
95
+
96
+ if not data.has_key?('workspace')
97
+ data['workspace'] = {}
98
+ end
99
+
100
+ if not data.has_key?('node')
101
+ data['node'] = {}
102
+ end
103
+
104
+ if not data.has_key?('application')
105
+ raise InterfaceException, "You must have application data in your configuration file."
106
+ end
107
+
108
+ if data.has_key?('port')
109
+ @port = data['port']
110
+ end
111
+
112
+ required_keys = ['name', 'version', 'workspace', 'workspace_stub']
113
+ required_keys.each do |key|
114
+ if not data['application'].has_key?(key)
115
+ raise InterfaceException, "Missing required key #{key} in application section."
116
+ end
117
+ end
118
+
119
+ # Store it all away.
120
+ @services = data['services']
121
+ @variables = data
122
+ end
123
+
124
+ # Return true if the application is running on Paasmaker.
125
+ def is_on_paasmaker?()
126
+ return @is_on_paasmaker
127
+ end
128
+
129
+ # Get a named service from Paasmaker. Raises an InterfaceException
130
+ # if there is no such service.
131
+ def get_service(name)
132
+ if @services.has_key?(name)
133
+ return @services[name]
134
+ else
135
+ raise InterfaceException, "No such service #{name}."
136
+ end
137
+ end
138
+
139
+ # Return all the services assigned to this application.
140
+ def get_all_services()
141
+ return @services
142
+ end
143
+
144
+ # Get the application name.
145
+ def get_application_name()
146
+ return @variables['application']['name']
147
+ end
148
+
149
+ # Get the application version.
150
+ def get_application_version()
151
+ return @variables['application']['version']
152
+ end
153
+
154
+ # Get the workspace name.
155
+ def get_workspace_name()
156
+ return @variables['application']['workspace']
157
+ end
158
+
159
+ # Get the workspace stub.
160
+ def get_workspace_stub()
161
+ return @variables['application']['workspace_stub']
162
+ end
163
+
164
+ # Get the node tags. Returns a Hash of the tags.
165
+ def get_node_tags()
166
+ return @variables['node']
167
+ end
168
+
169
+ # Get the workspace tags. Returns a Hash of the tags.
170
+ def get_workspace_tags()
171
+ return @variables['workspace']
172
+ end
173
+
174
+ # Get the port that you should be listening on.
175
+ def get_port()
176
+ return @port
177
+ end
178
+
179
+ # Helper to fetch the rails environment. The way this works is
180
+ # that it looks for the <tt>RAILS_ENV</tt> key on the workspace
181
+ # metadata, and uses that if found. If not, it uses the default
182
+ # supplied.
183
+ def get_rails_env(default)
184
+ workspace_tags = get_workspace_tags()
185
+ if workspace_tags.has_key?('RAILS_ENV')
186
+ return workspace_tags['RAILS_ENV']
187
+ else
188
+ return default
189
+ end
190
+ end
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paasmaker-interface
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Foote
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A helper class that makes it easier to interface with the Paasmaker platform-as-a-service.
31
+ email:
32
+ - freefoote@paasmaker.org
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/paasmaker.rb
38
+ - README.md
39
+ - LICENSE
40
+ homepage: http://paasmaker.org
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: An application interface to Paasmaker.
65
+ test_files: []
66
+ has_rdoc: