Class: Paasmaker::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/paasmaker.rb

Overview

An interface class for Paasmaker. It can parse the Paasmaker environment variables and supply them to your application. Alternately, it can load override configuration files to fill in missing values if it is not running on Paasmaker.

Instance Method Summary collapse

Constructor Details

#initialize(override_paths) ⇒ Interface

Create a new instance of the interface class. Supply an array of override path names. For example, supply ['../project-name.json'] to load a configuration file one level above your project root.



28
29
30
31
32
33
34
35
36
37
# File 'lib/paasmaker.rb', line 28

def initialize(override_paths)
    @override_paths = override_paths
    @is_on_paasmaker = false
    @variables = {}
    @services = {}
    # It's over 9000.
    @port = 9001

    ()
end

Instance Method Details

#get_all_servicesObject

Return all the services assigned to this application.



140
141
142
# File 'lib/paasmaker.rb', line 140

def get_all_services()
    return @services
end

#get_application_nameObject

Get the application name.



145
146
147
# File 'lib/paasmaker.rb', line 145

def get_application_name()
    return @variables['application']['name']
end

#get_application_versionObject

Get the application version.



150
151
152
# File 'lib/paasmaker.rb', line 150

def get_application_version()
    return @variables['application']['version']
end

#get_node_tagsObject

Get the node tags. Returns a Hash of the tags.



165
166
167
# File 'lib/paasmaker.rb', line 165

def get_node_tags()
    return @variables['node']
end

#get_portObject

Get the port that you should be listening on.



175
176
177
# File 'lib/paasmaker.rb', line 175

def get_port()
    return @port
end

#get_rails_env(default) ⇒ Object

Helper to fetch the rails environment. The way this works is that it looks for the RAILS_ENV key on the workspace metadata, and uses that if found. If not, it uses the default supplied.



183
184
185
186
187
188
189
190
# File 'lib/paasmaker.rb', line 183

def get_rails_env(default)
    workspace_tags = get_workspace_tags()
    if workspace_tags.has_key?('RAILS_ENV')
        return workspace_tags['RAILS_ENV']
    else
        return default
    end
end

#get_service(name) ⇒ Object

Get a named service from Paasmaker. Raises an InterfaceException if there is no such service.



131
132
133
134
135
136
137
# File 'lib/paasmaker.rb', line 131

def get_service(name)
    if @services.has_key?(name)
        return @services[name]
    else
        raise InterfaceException, "No such service #{name}."
    end
end

#get_workspace_nameObject

Get the workspace name.



155
156
157
# File 'lib/paasmaker.rb', line 155

def get_workspace_name()
    return @variables['application']['workspace']
end

#get_workspace_stubObject

Get the workspace stub.



160
161
162
# File 'lib/paasmaker.rb', line 160

def get_workspace_stub()
    return @variables['application']['workspace_stub']
end

#get_workspace_tagsObject

Get the workspace tags. Returns a Hash of the tags.



170
171
172
# File 'lib/paasmaker.rb', line 170

def get_workspace_tags()
    return @variables['workspace']
end

#is_on_paasmaker?Boolean

Return true if the application is running on Paasmaker.

Returns:

  • (Boolean)


125
126
127
# File 'lib/paasmaker.rb', line 125

def is_on_paasmaker?()
    return @is_on_paasmaker
end

#load_configuration_fileObject

Helper function to find and load a configuration file. You should not need to call this externally.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/paasmaker.rb', line 66

def load_configuration_file() # :nodoc:
    @override_paths.each do |path|
        if File.exist?(path)

            if path.end_with?('.yml')
                # It's YAML, parse it as such.
                data = YAML.load_file(path)
            elsif path.end_with?('.json')
                # It's JSON.
                data = JSON.parse(IO.read(path))
            end

            store_configuration(path, data)
            return
        end

    end

    # If we got here, we were not able to load any files.
    raise InterfaceException, "Unable to find any configuration files to load."
end

#parse_metadataObject

Helper function to parse the Paasmaker metadata. You should not call this externally.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/paasmaker.rb', line 41

def () # :nodoc:
    raw_services = ENV['PM_SERVICES']
     = ENV['PM_METADATA']
    raw_port = ENV['PM_PORT']

    if raw_services and 
        # We're running on Paasmaker.
        @is_on_paasmaker = true

        # Load from environment variables.
        @variables = JSON.parse()
        @services = JSON.parse(raw_services)

        if raw_port
            @port = raw_port.to_i()
        end
    else
        # Not running on Paasmaker, load from
        # a configuration file.
        load_configuration_file()
    end
end

#store_configuration(path, data) ⇒ Object

Helper function to store a loaded configuration. You should not call this externally.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/paasmaker.rb', line 90

def store_configuration(path, data) # :nodoc:
    # Validate the data first, filling in some missing blanks.
    if not data.has_key?('services')
        data['services'] = {}
    end

    if not data.has_key?('workspace')
        data['workspace'] = {}
    end

    if not data.has_key?('node')
        data['node'] = {}
    end

    if not data.has_key?('application')
        raise InterfaceException, "You must have application data in your configuration file."
    end

    if data.has_key?('port')
        @port = data['port']
    end

    required_keys = ['name', 'version', 'workspace', 'workspace_stub']
    required_keys.each do |key|
        if not data['application'].has_key?(key)
            raise InterfaceException, "Missing required key #{key} in application section."
        end
    end

    # Store it all away.
    @services = data['services']
    @variables = data
end