Class: MarathonDeploy::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { :force => false, :deployfile => 'deploy.yml', :remove_elements => [], :environment => nil, }) ⇒ Application

Models an application to be added converted to json and send to the marathon-api

Parameters:

  • options (Hash) (defaults to: { :force => false, :deployfile => 'deploy.yml', :remove_elements => [], :environment => nil, })

    hash for the application object

Options Hash (options):

  • :force (Boolean)

    force a deployment by including an environment variable containing a random string value in the json marathon payload

  • :deployfile (String)

    file template and path. default deploy.yml in current directory

  • :environment (MarathonDeploy::Environment)

    environment specified with -e parameter



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/marathon_deploy/application.rb', line 19

def initialize(options={
    :force => false,
    :deployfile => 'deploy.yml',
    :remove_elements => [],
    :environment => nil,
})

  deployfile = options[:deployfile]
  @json = readFile(deployfile)

  if (!options[:environment].nil?)
    overrides = env_overrides(
      File.dirname(deployfile),
      options[:environment].name)

    @json = @json.deep_merge!(overrides)
  end

  missing_attributes = MarathonDefaults.missing_attributes(@json) 
  
  if(!missing_attributes.empty?)
    message = "#{deployfile} is missing required marathon API attributes: #{missing_attributes.join(',')}"
    raise Error::MissingMarathonAttributesError, message, caller
  end
  
  missing_envs = MarathonDefaults.missing_envs(@json)
  if(!missing_envs.empty?)
    message = "#{deployfile} is missing required environment variables: #{missing_envs.join(',')}"
    raise Error::MissingMarathonAttributesError, message, caller
  end
   
  @deployfile = deployfile                
  @json =  Utils.deep_symbolize(@json)  
    
  add_identifier if (options[:force])
  remove_elements(options[:remove_elements])
    
  inject_envs = ENV.select { |k,v| /^#{MarathonDeploy::MarathonDefaults::ENVIRONMENT_VARIABLE_PREFIX}/.match(k)  }
  cleaned_envs = Hash[inject_envs.map { |k,v| [k.gsub(/^#{MarathonDeploy::MarathonDefaults::ENVIRONMENT_VARIABLE_PREFIX}/,''), v ] }]   
  self.add_envs cleaned_envs.to_hash unless cleaned_envs.empty?
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/marathon_deploy/application.rb', line 12

def id
  @id
end

#jsonObject (readonly)

Returns the value of attribute json.



12
13
14
# File 'lib/marathon_deploy/application.rb', line 12

def json
  @json
end

Instance Method Details

#add_envs(envs) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/marathon_deploy/application.rb', line 97

def add_envs(envs)
  if (!@json[:env])
    @json[:env] = {}
  end
  if (envs.is_a?(Hash))
    envs.each do |key,value|
      if (value.is_a? Numeric)
        @json[:env][key] = value.to_json
      else
        @json[:env][key] = value
      end
    end 
  else
    raise Error::BadFormatError, "argument must be a hash", caller
  end
end

#add_identifierObject



70
71
72
73
74
# File 'lib/marathon_deploy/application.rb', line 70

def add_identifier
  random = Utils.random
  # Time.now.to_i
  @json[:env]['UNIQUE_ID'] = "#{id}_#{random}"
end

#envJSON

Returns list of ENV variables for this application.

Returns:

  • (JSON)

    list of ENV variables for this application



89
90
91
# File 'lib/marathon_deploy/application.rb', line 89

def env
  @json[:env]
end

#health_checksArray

Returns:

  • (Array)


66
67
68
# File 'lib/marathon_deploy/application.rb', line 66

def health_checks
  @json[:healthChecks]
end

#instancesObject



114
115
116
# File 'lib/marathon_deploy/application.rb', line 114

def instances
  @json[:instances]  
end

#overlay_preproduction_settingsObject



61
62
63
# File 'lib/marathon_deploy/application.rb', line 61

def overlay_preproduction_settings
  @json = MarathonDefaults.overlay_preproduction_settings(@json)
end

#remove_elements(remove_array) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/marathon_deploy/application.rb', line 76

def remove_elements(remove_array)
  if (remove_array.is_a?(Array))
    remove_array.each do |element|
      @json.delete(element)
    end
  end
end

#to_sObject



84
85
86
# File 'lib/marathon_deploy/application.rb', line 84

def to_s
  JSON.pretty_generate(@json)
end