Class: VCAP::Services::Base::Gateway

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

Instance Method Summary collapse

Instance Method Details

#additional_optionsObject



164
165
166
# File 'lib/base/gateway.rb', line 164

def additional_options
  {}
end

#async_gateway_classObject



133
134
135
# File 'lib/base/gateway.rb', line 133

def async_gateway_class
  VCAP::Services::AsynchronousServiceGateway
end

#parse_configObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/base/gateway.rb', line 32

def parse_config
  config_file = default_config_file

  OptionParser.new do |opts|
    opts.banner = "Usage: $0 [options]"
    opts.on("-c", "--config [ARG]", "Configuration File") do |opt|
      config_file = opt
    end
    opts.on("-h", "--help", "Help") do
      puts opts
      exit
    end
  end.parse!

  begin
    @config = parse_gateway_config(config_file)
  rescue => e
    puts "Couldn't read config file: #{e}"
    exit
  end
end

#parse_gateway_config(config_file) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/base/gateway.rb', line 137

def parse_gateway_config(config_file)
  config = YAML.load_file(config_file)
  config = VCAP.symbolize_keys(config)


  cc_api_version = config[:cc_api_version] || "v1"

  if cc_api_version == "v1"
    token = config[:token]
    raise "Token missing" unless token
    raise "Token must be a String or Int, #{token.class} given" unless (token.is_a?(Integer) || token.is_a?(String))
    config[:token] = token.to_s
  else
    service_auth_tokens = config[:service_auth_tokens]
    raise "Service auth token missing" unless service_auth_tokens
    raise "Token must be hash of the form: label_provider => token" unless service_auth_tokens.is_a?(Hash)

    # Each gateway only handles one service, so service_auth_tokens is expected to have just 1 entry
    raise "Unable to manage multiple services" unless service_auth_tokens.size == 1

    # Used by legacy services for validating incoming request (and temporarily for handle fetch/update v1 api)
    config[:token] = service_auth_tokens.values[0].to_s # For legacy services
  end

  config
end

#setup_async_job_configObject



63
64
65
66
67
68
69
70
# File 'lib/base/gateway.rb', line 63

def setup_async_job_config
  resque = @config[:resque]
  if resque
    resque = VCAP.symbolize_keys(resque)
    VCAP::Services::Base::AsyncJob::Config.redis_config = resque
    VCAP::Services::Base::AsyncJob::Config.logger = @config[:logger]
  end
end

#setup_pidObject



72
73
74
75
76
77
# File 'lib/base/gateway.rb', line 72

def setup_pid
  if @config[:pid]
    pf = VCAP::PidFile.new(@config[:pid])
    pf.unlink_at_exit
  end
end

#setup_vcap_loggingObject



54
55
56
57
58
59
60
61
# File 'lib/base/gateway.rb', line 54

def setup_vcap_logging
  steno_config = Steno::Config.to_config_hash(@config[:logging])
  steno_config[:context] = Steno::Context::FiberLocal.new
  Steno.init(Steno::Config.new(steno_config))
  # Use the current running binary name for logger identity name, since service gateway only has one instance now.
  logger = Steno.logger(File.basename($0))
  @config[:logger] = logger
end

#startObject



79
80
81
82
83
84
85
86
87
88
89
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
123
124
125
126
127
128
129
130
131
# File 'lib/base/gateway.rb', line 79

def start
  parse_config

  setup_vcap_logging

  setup_pid

  setup_async_job_config

  @config[:host] = VCAP.local_ip(@config[:ip_route])
  @config[:port] ||= VCAP.grab_ephemeral_port
  @config[:service][:label] = "#{@config[:service][:name]}-#{@config[:service][:version]}"
  @config[:service][:url]   = "http://#{@config[:host]}:#{@config[:port]}"
  node_timeout = @config[:node_timeout] || 5

  EM.error_handler do |ex|
    @config[:logger].fatal("#{ex} #{ex.backtrace.join("|")}")
    exit
  end

  # Go!
  EM.run do
    sp = provisioner_class.new(
           :logger   => @config[:logger],
           :index    => @config[:index],
           :ip_route => @config[:ip_route],
           :mbus => @config[:mbus],
           :node_timeout => node_timeout,
           :z_interval => @config[:z_interval],
           :max_nats_payload => @config[:max_nats_payload],
           :additional_options => additional_options,
           :status => @config[:status],
           :plan_management => @config[:plan_management],
           :service => @config[:service],
           :download_url_template => @config[:download_url_template],
           :cc_api_version => @config[:cc_api_version] || "v1" ,
           :snapshot_db => @config[:resque],
         )

    opts = @config.dup
    opts[:provisioner] = sp
    opts[:node_timeout] = node_timeout
    opts[:cloud_controller_uri] = @config[:cloud_controller_uri] || "api.vcap.me"

    sg = async_gateway_class.new(opts)

    server = Thin::Server.new(@config[:host], @config[:port], sg)
    if @config[:service][:timeout]
      server.timeout = [@config[:service][:timeout] + 1, Thin::Server::DEFAULT_TIMEOUT].max
    end
    server.start!
  end
end