Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #55 from APItools/merge-on-premise
Browse files Browse the repository at this point in the history
Merge on premise
  • Loading branch information
mikz committed Oct 14, 2015
2 parents 0a0d55c + c2fbe40 commit b062cc5
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/controllers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ angular.module 'slug.controllers',
'slug.notifications'
'slug.controllers.services'
'slug.user_settings'
'slug.services.brain'
]
5 changes: 2 additions & 3 deletions app/assets/javascripts/controllers/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ angular.module('slug.controllers')
initialize: -> $http.post('/api/system/initialize')
reset: -> $http.post('/api/system/reset')

.controller 'AppController', ($scope, Service, System, $http) ->
.controller 'AppController', ($scope, Service, System, Brain) ->
System.initialize()

$http(method: 'GET', url: '/api/brain').success (data) ->
$scope.brain = data.host
$scope.brain = Brain
2 changes: 1 addition & 1 deletion app/assets/javascripts/controllers/root.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
angular.module('slug.root', ['ui.router'])
angular.module('slug.root', ['ui.router', 'slug.setup'])
.config ($urlRouterProvider) ->
$urlRouterProvider.when('/', '/dashboard')
57 changes: 57 additions & 0 deletions app/assets/javascripts/controllers/setup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
angular.module('slug.setup', ['ui.router', 'slug.user_settings'])

.config ($stateProvider) ->
Config =
config: (UserSettings) ->
UserSettings.$promise

$stateProvider
.state 'setup',
url: '/setup'
templateUrl: '/setup/show.html'
controller: 'SetupCtrl'
resolve: Config

.state 'pair',
url: '/pair'
templateUrl: '/setup/pair.html'
controller: 'PairCtrl'
resolve: Config

.controller 'SetupCtrl', ($scope, $state, config, flash, OnPremise) ->
setup = $scope.setup = report_usage: true

if config.get('set_up')
$state.go('dashboard')

$scope.saveSetup = ->
failure = ->
flash.error = 'There was an error when setting up this Traffic Monitor. Dou you have internet connection?'

update_config = ->
config.extend(report_usage: setup.report_usage, set_up: true)
config.$promise.then ->
flash.success = 'Successfully set up your Traffic Monitor'
$state.go('dashboard')

if setup.report_usage
uuid = config.get('uuid')
OnPremise.register(uuid).then(update_config, failure)
else
update_config()




.controller 'PairCtrl', ($scope, $state, Brain, OnPremise) ->
Brain.$promise.then ->
$scope.get_key = "#{Brain.host}/on_premise/link"

$scope.setup = {}

$scope.pair = () ->
key = $scope.setup.pairing_key
linking = OnPremise.link(key)

linking.success ->
$state.go('dashboard')
19 changes: 19 additions & 0 deletions app/assets/javascripts/services/brain.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
angular.module('slug.services.brain', [])

.factory 'Brain', ($http) ->
brain = { $promise: $http(method: 'GET', url: '/api/brain') }

brain.$promise.success (data) ->
brain.host = data.host

brain

.factory 'OnPremise', ($http) ->
register: (uuid) ->
$http.post('/api/brain/register', uuid: uuid)

link: (key) ->
$http.post('/api/brain/link', key: key)

unlink: ->
$http.post('/api/brain/unlink', {})
8 changes: 6 additions & 2 deletions app/views/navigation/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
</ul>

<ul class="nav pull-right">
<li>
<a ng-show='brain' ng-href="{{brain}}/slugs" >Go to global dashboard</a>
<li ng-show="config.get('paired')">
<a ng-show='brain.host' ng-href="{{brain}}/slugs" >Go to global dashboard</a>
</li>

<li ng-hide="config.get('paired')">
<a ng-href="pair" >Pair with global dashboard</a>
</li>
</ul>
</nav>
Expand Down
26 changes: 26 additions & 0 deletions app/views/setup/pair.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Pair your Traffic Monitor with the Global Dashboard</h1>

<form class="form-horizontal" name='pair_form' ng-submit="pair()">

<fieldset>
<div class="control-group" ng-class="{error: pair_form.pairing_key.$invalid}">
<label class="control-label" for="pairing_key">Your Pairing Key:</label>
<div class="controls">
<input id="pairing_key" class="input-xlarge" type="text"
name="pairing_key" ng-model="setup.pairing_key" required>
</div>
</div>
</fieldset>

<fieldset>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Pair!</button>
<span class="help-inline">
Or <a ng-href="{{get_key}}">Get new key</a>
</span>
</div>

</div>
</fieldset>
</form>
49 changes: 49 additions & 0 deletions app/views/setup/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div id="setup-modal">
<div id="setup-dialog" class="container">
<div class="hero-unit">

<h1>Congratulations! Your API traffic monitor is up and running.</h1>


<h2>What's next?</h2>

<ul>
<li>Pair your traffic monitor with your APItools account. <a
href="https://docs.apitools.com/docs/on-premise/">Learn more</a>
</li>
<li>Learn more about different ways of installation, DNS configuration <a
href="https://docs.apitools.com/docs/on-premise/">Learn more</a>
</li>
<li>Get familiar with APItools taking our tour. <a
href="https://docs.apitools.com/docs/tour/">Learn more</a>
</li>
</ul>
</div>

<form id="setup-form" name='setup_form' ng-submit="saveSetup()">
<fieldset ng-class="{error: form.name.$invalid}">
<legend>Anonymously report usage</legend>
<label class="checkbox" for="report_usage">

<input id="report_usage" type="checkbox" name="report_usage"
ng-model="setup.report_usage">
Send report</label>

<p class="help-block">
You can run your APItools monitor completely standalone if you don't choose to pair it with the cloud service.
However, if you're willing the you can help us out by reporting anonymized traffic stats.
This will allow us to do performance analysis of various APIs, predict performance issues
or just tell you if it is broken just for you or everyone else.</p>
<p class="help-block">
Read our <a href="https://docs.apitools.com/legal/terms-and-conditions/" target="_blank">Terms and Conditions</a>
and <a href="https://docs.apitools.com/legal/privacy-policy/" target="_blank">Privacy Policy</a>
to learn more about how we handle these data.<br/>
By running APItools Traffic Monitor you accept these Terms and Conditions and Privacy Policy.
</p>

<button type="submit" class="btn btn-primary btn-large">Save</button>
</fieldset>
</form>
</div>
</div>

7 changes: 0 additions & 7 deletions config/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ RUN ln -sf /var/www/brainslug/config/supervisor.conf /etc/supervisor/conf.d/open
&& ln -sf /var/www/brainslug/config/logrotate.conf /etc/logrotate.d/nginx \
&& rm /etc/supervisor/conf.d/cron.conf

# let nginx to write to redis backup folder
ENV SLUG_REDIS_DUMP_FOLDER_PATH /var/lib/redis/
ENV SLUG_REDIS_HOST 127.0.0.1
ENV SLUG_REDIS_PORT 6379

ENV SLUG_LOGFILE /var/log/supervisor/supervisord.log
ENV SLUG_CSRF_PROTECTION 1;

RUN chmod o+w ${SLUG_REDIS_DUMP_FOLDER_PATH}

CMD ["supervisord", "-n"]
EXPOSE 7071 10002

Expand Down
3 changes: 3 additions & 0 deletions config/env.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ env SLUG_CSRF_PROTECTION;
env SLUG_BRAIN_HOST;

env REDIS_PORT_6379_TCP_ADDR;
env REDIS_PORT_6379_TCP_PORT;
env SLUG_REDIS_DUMP_FOLDER_PATH;
env SLUG_REDIS_HOST;
env SLUG_REDIS_PORT;
env SLUG_REDIS_NAME_SERVER;
env SLUG_REDIS_NAME;
env DB_PORT_6379_TCP_ADDR;
env DB_PORT_6379_TCP_PORT;

env STATSD_PORT_8125_UDP_ADDR;
env SLUG_STATSD_SERVER;
Expand Down
6 changes: 6 additions & 0 deletions features/pages/home_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ class HomePage

def initialize
visit('/')
on_premise_setup
end

def on_premise_setup
find('#setup-dialog').click_on('Save')
rescue Capybara::ElementNotFound
end

def add_service
Expand Down
2 changes: 2 additions & 0 deletions lua/apps/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ router:delete( '/api/services/:service_id/dashboards/:id' , r('dashboards.del
router:get( '/api/brain' , r('brain.url'))
router:post( '/api/brain/report' , r('brain.report'))
router:post( '/api/brain/register' , r('brain.register'))
router:post( '/api/brain/link' , r('brain.link'))
router:post( '/api/brain/unlink' , r('brain.unlink'))

router:get( '/api/brain/middleware_specs/search/' , r('brain_middleware_specs.search'))
router:get( '/api/brain/middleware_specs/:id/' , r('brain_middleware_specs.show'))
Expand Down
2 changes: 2 additions & 0 deletions lua/brain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ end

Brain.make_report = function()
local slug_name = Config.get_slug_name()
local uuid = Config.get_uuid()

if not slug_name then
ngx.log(ngx.WARN, 'skipping brain report because there is no slug name')
return
end

local report = { version = 1,
uuid = uuid,
slug = slug_name,
services = {} }

Expand Down
4 changes: 2 additions & 2 deletions lua/concurredis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ local concurredis = {}

local REDIS_NAME_SERVER = os.getenv('SLUG_REDIS_NAME_SERVER')
local REDIS_NAME = os.getenv('SLUG_REDIS_NAME')
local REDIS_HOST = os.getenv('REDIS_PORT_6379_TCP_ADDR') or os.getenv("SLUG_REDIS_HOST")
local REDIS_PORT = tonumber(os.getenv("SLUG_REDIS_PORT") or 6379)
local REDIS_HOST = os.getenv('DB_PORT_6379_TCP_ADDR') or os.getenv('REDIS_PORT_6379_TCP_ADDR') or os.getenv("SLUG_REDIS_HOST")
local REDIS_PORT = tonumber( os.getenv('DB_PORT_6379_TCP_PORT') or os.getenv('REDIS_PORT_6379_TCP_PORT') or os.getenv("SLUG_REDIS_PORT") or 6379)

local POOL_SIZE = 30
local KEEPALIVE_TIMEOUT = 30 * 1000 -- 30 seconds in ms
Expand Down
15 changes: 12 additions & 3 deletions lua/controllers/brain_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ local brain = {
end,

register = function(params)
local registered = Brain.register()
Brain.use_keys(registered.keys)
h.send_json(registered)
local resp = Brain.register()
h.send_json(resp)
end,

link = function(params)
local json = h.request_json()
local key = json.key
h.send_json(Brain.link(key))
end,

unlink = function(params)
h.send_json(Brain.unlink())
end,

configure = function(params)
Expand Down
18 changes: 17 additions & 1 deletion lua/models/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local lock = require "lock"
local Model = require 'model'
local Config = Model:new()

local uuid = require 'uuid'

Config.collection = 'config'
Config.localhost = '127.0.0.1:7071' -- used by brain.lua and consumers/mail.lua

Expand Down Expand Up @@ -44,7 +46,8 @@ Config.update_missing = lock.wrapper('update_missing', Config.update_missing)

Config.default = function()
return {
csrf_secret = Config.csrf_secret()
csrf_secret = Config.csrf_secret(),
uuid = uuid.getUUID()
}
end

Expand Down Expand Up @@ -106,4 +109,17 @@ Config.set_slug_name = function(str)
return Config:update({_id = c._id}, {slug_name = str})
end

Config.get_uuid = function()
return Config.get().uuid
end

Config.get_link_key = function()
return Config.get().link_key
end

Config.set_link_key = function(key)
local c = Config.get()
return Config:update({_id = c._id}, {link_key = key})
end

return Config
2 changes: 1 addition & 1 deletion release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test?
def nginx(input)
config = configs.fetch('nginx')

config['logging'] = production?
config['logging'] = production? unless config.has_key?('logging')
config['release'] = release

if root = config['root']
Expand Down
1 change: 1 addition & 0 deletions release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ nginx:
lua_code_cache: 'on'
# this one is a function directly in release.rb
# logging:
logging: false
include:
- shared_dict.conf
- global.conf
Expand Down
26 changes: 26 additions & 0 deletions spec/api/brain_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe "Brain" do

let(:host) { "http://localhost:7071" }

describe "POST /api/brain/register" do
it "registers into the brain with the uuid" do
expect(post_json("#{host}/api/brain/register")).to be
end
end

describe "POST /api/brain/link" do
it "registers into the brain with the uuid" do
expect(post_json("#{host}/api/brain/link", body: {key: 'a key'}.to_json)).to be
expect(post_json("#{host}/api/config", body: "{}")).to include('link_key' => 'a key')
end
end

describe "POST /api/brain/unlink" do
it "registers into the brain with the uuid" do
expect(post_json("#{host}/api/brain/register")).to be
end
end

end
1 change: 1 addition & 0 deletions spec/fixtures/config/linked.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"oauth_access_token": "123", "name": "simba", "uuid": "uuid", "link_key": "link_key", "_id": 1}
2 changes: 1 addition & 1 deletion spec/fixtures/config/simple.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"oauth_access_token": "123", "name": "simba", "_id": 1}
{"oauth_access_token": "123", "name": "simba", "uuid": "uuid", "_id": 1}
Loading

0 comments on commit b062cc5

Please sign in to comment.