Class: DeploYML::Configuration

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

Overview

The Configuration class loads in the settings from a deploy.yml file.

Direct Known Subclasses

Environment

Constant Summary collapse

DEFAULT_SCM =

Default SCM to use

:rsync
TASKS =

Valid task names

[
  :setup,
  :update,
  :install,
  :migrate,
  :config,
  :start,
  :stop,
  :restart
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Configuration

Creates a new DeploYML::Configuration using the given configuration.

Parameters:

  • config (Hash) (defaults to: {})

    The configuration for the project.

Options Hash (config):

  • :source (String)

    The source URI of the project Git repository.

  • :dest (Array<String, Hash>, String, Hash)

    The destination URI(s) to upload the project to.

  • :bundler (Boolean)

    Specifies whether the projects dependencies are controlled by Bundler.

  • :framework (Symbol)

    The framework used by the project.

  • :orm (Symbol)

    The ORM used by the project.

  • :environment (Symbol)

    The environment to run the project in.

  • :debug (Boolean) — default: false

    Specifies whether to enable debugging.

Raises:

  • (MissingOption)

    The server option Hash did not contain a name option.



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
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/deployml/configuration.rb', line 92

def initialize(config={})
  config = normalize_hash(config)

  @bundler = config.fetch(:bundler,false)

  @framework = case config[:framework]
               when 'rails', 'rails2', 'rails3'
                 :rails
               when String, Symbol
                 config[:framework].to_sym
               end

  @orm = if config[:orm]
           config[:orm].to_sym
         end

  @server_name, @server_options = parse_server(config[:server])

  @source = config[:source]
  @dest = if config[:dest]
            parse_dest(config[:dest])
          end

  @environment = if config[:environment]
                   config[:environment].to_sym
                 end

  @debug = config.fetch(:debug,false)

  @before = {}
  @after = {}

  TASKS.each do |task|
    if (config.has_key?(:before) && config[:before].has_key?(task))
      @before[task] = parse_commands(config[:before][task])
    end

    if (config.has_key?(:after) && config[:after].has_key?(task))
      @after[task] = parse_commands(config[:after][task])
    end
  end
end

Instance Attribute Details

#afterObject (readonly)

The arbitrary commands to run after various tasks



59
60
61
# File 'lib/deployml/configuration.rb', line 59

def after
  @after
end

#beforeObject (readonly)

The arbitrary commands to run before various tasks



56
57
58
# File 'lib/deployml/configuration.rb', line 56

def before
  @before
end

#bundlerObject (readonly)

Whether the project uses Bundler.



41
42
43
# File 'lib/deployml/configuration.rb', line 41

def bundler
  @bundler
end

#debugObject

Specifies whether to enable debugging.



53
54
55
# File 'lib/deployml/configuration.rb', line 53

def debug
  @debug
end

#destObject (readonly)

The destination URI to upload the project to.



38
39
40
# File 'lib/deployml/configuration.rb', line 38

def dest
  @dest
end

#environmentObject (readonly)

The environment to run the project in



50
51
52
# File 'lib/deployml/configuration.rb', line 50

def environment
  @environment
end

#frameworkObject (readonly)

The framework used by the project



44
45
46
# File 'lib/deployml/configuration.rb', line 44

def framework
  @framework
end

#ormObject (readonly)

The ORM used by the project



47
48
49
# File 'lib/deployml/configuration.rb', line 47

def orm
  @orm
end

#server_nameObject (readonly)

The server run the deployed project under



29
30
31
# File 'lib/deployml/configuration.rb', line 29

def server_name
  @server_name
end

#server_optionsObject (readonly)

Options for the server



32
33
34
# File 'lib/deployml/configuration.rb', line 32

def server_options
  @server_options
end

#sourceObject (readonly)

The source URI of the project Git repository.



35
36
37
# File 'lib/deployml/configuration.rb', line 35

def source
  @source
end

Instance Method Details

#each_dest {|dest| ... } ⇒ Enumerator

Iterates over each destination.

Yields:

  • (dest)

    The given block will be passed each destination URI.

Yield Parameters:

  • dest (Addressable::URI)

    A destination URI.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Since:

  • 0.5.0



149
150
151
152
153
154
155
156
157
# File 'lib/deployml/configuration.rb', line 149

def each_dest(&block)
  return enum_for(:each_dest) unless block_given?

  if @dest.kind_of?(Array)
    @dest.each(&block)
  elsif @dest
    yield @dest
  end
end

#normalize(value) ⇒ Hash, ... (protected)

Normalizes a value.

Parameters:

  • value (Hash, Array, Object)

    The value to normalize.

Returns:

  • (Hash, Array, Object)

    The normalized value.

Since:

  • 0.5.0



206
207
208
209
210
211
212
213
214
215
# File 'lib/deployml/configuration.rb', line 206

def normalize(value)
  case value
  when Hash
    normalize_hash(value)
  when Array
    normalize_array(value)
  else
    value
  end
end

#normalize_array(array) ⇒ Array (protected)

Normalizes an Array.

Parameters:

  • array (Array)

    The Array to normalize.

Returns:

  • (Array)

    The normalized Array.

Since:

  • 0.5.0



172
173
174
# File 'lib/deployml/configuration.rb', line 172

def normalize_array(array)
  array.map { |value| normalize(value) }
end

#normalize_hash(hash) ⇒ Hash{Symbol => Object} (protected)

Converts all the keys of a Hash to Symbols.

Parameters:

  • hash (Hash{Object => Object})

    The hash to be converted.

Returns:

  • (Hash{Symbol => Object})

    The normalized Hash.



185
186
187
188
189
190
191
192
193
# File 'lib/deployml/configuration.rb', line 185

def normalize_hash(hash)
  new_hash = {}

  hash.each do |key,value|
    new_hash[key.to_sym] = normalize(value)
  end

  return new_hash
end

#parse_address(address) ⇒ Addressable::URI (protected)

Parses an address.

Parameters:

  • address (Hash, String)

    The address to parse.

Returns:

  • (Addressable::URI)

    The parsed address.

Since:

  • 0.5.0



260
261
262
263
264
265
266
267
268
269
# File 'lib/deployml/configuration.rb', line 260

def parse_address(address)
  case address
  when Hash
    Addressable::URI.new(address)
  when String
    Addressable::URI.parse(address)
  else
    raise(InvalidConfig,"invalid address: #{address.inspect}",caller)
  end
end

#parse_commands(command) ⇒ Array<String> (protected)

Parses a command.

Parameters:

  • command (Array, String)

    The command or commands to parse.

Returns:

  • (Array<String>)

    The individual commands.

Raises:

  • (InvalidConfig)

    The command must be either an Array of a String.

Since:

  • 0.5.0



305
306
307
308
309
310
311
312
313
314
# File 'lib/deployml/configuration.rb', line 305

def parse_commands(command)
  case command
  when Array
    command.map { |line| line.to_s }
  when String
    command.enum_for(:each_line).map { |line| line.chomp }
  else
    raise(InvalidConfig,"commands must be an Array or a String")
  end
end

#parse_dest(dest) ⇒ Array<Addressable::URI>, Addressable::URI (protected)

Parses the value for the dest setting.

Parameters:

  • dest (Array, Hash, String)

    The value of the dest setting.

Returns:

  • (Array<Addressable::URI>, Addressable::URI)

    The parsed dest value.

Since:

  • 0.5.0



282
283
284
285
286
287
288
289
# File 'lib/deployml/configuration.rb', line 282

def parse_dest(dest)
  case dest
  when Array
    dest.map { |address| parse_address(address) }
  else
    parse_address(dest)
  end
end

#parse_server(server) ⇒ Array<Symbol, Hash> (protected)

Parses the value for the server setting.

Returns:

  • (Array<Symbol, Hash>)

    The name of the server and additional options.

Since:

  • 0.5.0



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/deployml/configuration.rb', line 225

def parse_server(server)
  name = nil
  options = {}

  case server
  when Symbol, String
    name = server.to_sym
  when Hash
    unless server.has_key?(:name)
      raise(MissingOption,"the 'server' option must contain a 'name' option for which server to use",caller)
    end

    if server.has_key?(:name)
      name = server[:name].to_sym
    end

    if server.has_key?(:options)
      options.merge!(server[:options])
    end
  end

  return [name, options]
end