Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Creating the acm repo.

Added the database migration that uses sequel.
  • Loading branch information...
commit 5b995bc0f39a9d323179d8a27b7158ac89d7277c 0 parents
@joeldsa joeldsa authored
2  .gitignore
@@ -0,0 +1,2 @@
+.idea/*
+
33 Gemfile
@@ -0,0 +1,33 @@
+source :rubygems
+
+gem "bcrypt-ruby"
+gem "httpclient"
+gem "rack-test" # needed for console
+gem "rake"
+gem "sequel"
+gem "sinatra"
+gem "SystemTimer", :platforms => :ruby_18
+gem "thin"
+gem "uuidtools"
+gem "yajl-ruby"
+
+group :development do
+ gem "sqlite3"
+end
+
+group :production do
+ gem "pg"
+end
+
+group :test do
+ gem "autotest", :platforms => :ruby_19
+ gem "ci_reporter"
+ gem "machinist"
+ gem "minitar"
+ gem "rspec"
+ gem "ruby-debug", :platforms => :ruby_18
+ gem "ruby-debug19", :platforms => :ruby_19
+ gem "simplecov", :platforms => :ruby_19
+ gem "sqlite3"
+ gem "simplecov", :platforms => :ruby_19
+end
41 Rakefile
@@ -0,0 +1,41 @@
+require 'rake'
+
+desc "Run specs"
+task "spec" => ["bundler:install:test", "test:spec"]
+
+desc "Run functional tests"
+task "spec:unit" => ["bundler:install:test", "test:spec:unit"]
+
+desc "Run functional tests"
+task "spec:functional" => ["bundler:install:test", "test:spec:functional"]
+
+desc "Run specs using RCov"
+task "spec:cov" => ["bundler:install:test", "test:spec:rcov"]
+
+namespace "bundler" do
+
+ desc "Install gems"
+ task "install" do
+ sh("bundle install")
+ end
+
+ environments = %w(test development production)
+
+ environments.each do |env|
+ desc "Install gems for #{env}"
+ task "install:#{env}" do
+ sh("bundle install --local --without #{(environments - [env]).join(' ')}")
+ end
+ end
+
+end
+
+namespace "test" do
+
+ ["spec", "spec:unit", "spec:functional", "spec:rcov"].each do |task_name|
+ task task_name do
+ sh("cd spec && rake #{task_name}")
+ end
+ end
+
+end
1  autotest/discover.rb
@@ -0,0 +1 @@
+ Autotest.add_discovery { "rspec2" }
45 bin/acm
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+#
+ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
+
+require "rubygems"
+require "bundler/setup"
+
+$:.unshift(File.expand_path("../../lib", __FILE__))
+$:.unshift(File.expand_path("../../lib/acm", __FILE__))
+$:.unshift(File.expand_path("../../lib/acm/models", __FILE__))
+
+require "config"
+require "thin"
+
+config_file = nil
+
+opts = OptionParser.new do |opts|
+ opts.on("-c", "--config [ARG]", "Configuration File") do |opt|
+ config_file = opt
+ end
+end
+
+opts.parse!(ARGV.dup)
+
+config_file ||= ::File.expand_path("../../config/acm.yml", __FILE__)
+config = YAML.load_file(config_file)
+
+CollabSpaces::Config.configure(config)
+
+require "acm_controller"
+
+thin_server = Thin::Server.new("0.0.0.0", config["port"], :signals => false) do
+ use Rack::CommonLogger
+ map "/" do
+ run CollabSpaces::Controller::RackController.new
+ end
+end
+
+["TERM", "INT", "QUIT"].each do |signal|
+ trap(signal) do
+ thin_server.stop!
+ end
+end
+
+thin_server.start!
14 config/acm.yml
@@ -0,0 +1,14 @@
+---
+name: Collab Spaces
+port: 9090
+logging:
+ level: DEBUG
+ file: /tmp/acm.log
+dir: /tmp/acm_dir
+db:
+ database: "sqlite://acm.db"
+ max_connections: 32
+ pool_timeout: 10
+basic_auth:
+ user: acm_user
+ password: acm_password1234
78 db/migrations/001_initial.rb
@@ -0,0 +1,78 @@
+Sequel.migration do
+ up do
+ create_table :objects do
+ primary_key :id
+ string :immutable_id, :null => false, :unique => true
+ foreign_key :object_type_id, :object_types
+ string :name
+ text :metadata_json
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+
+ end
+
+ create_table :object_types do
+ primary_key :id
+ string :name, :null => false, :unique => true
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+
+ end
+
+ create_table :permissions do
+ primary_key :id
+ foreign_key :object_type_id, :object_types
+ string :name, :null => false
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+
+ unique ([:object_type_id, :name])
+
+ end
+
+ create_table :access_control_entities do
+ primary_key :id
+ foreign_key :object_id, :objects
+ foreign_key :permission_id, :permissions
+ foreign_key :group_id, :groups
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+
+ unique ([:object_id, :group_id, :permission_id])
+ end
+
+ create_table :groups do
+ primary_key :id
+ string :immutable_id, :null => false, :unique => true
+ foreign_key :object_id, :objects
+ string :name
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+ end
+
+ create_table :members do
+ primary_key :id
+ foreign_key :group_id, :groups
+ integer :user_id
+
+ time :created_at, :null => false
+ time :last_updated_at, :null => false
+ end
+
+ end
+
+ down do
+ drop_table :members
+ drop_table :groups
+ drop_table :access_control_entities
+ drop_table :permissions
+ drop_table :objects
+ drop_table :object_types
+
+ end
+end
BIN  lib/acm/acm.db
Binary file not shown
148 lib/acm/config.rb
@@ -0,0 +1,148 @@
+require "monitor"
+require "logger"
+require "securerandom"
+
+require "collab_spaces/thread_formatter"
+
+
+module CollabSpaces
+
+ class Config
+
+ class << self
+
+ CONFIG_OPTIONS = [
+ :base_dir,
+ :logger,
+ :db,
+ :name,
+ :revision,
+ :uaa,
+ :basic_auth
+ ]
+
+ CONFIG_OPTIONS.each do |option|
+ attr_accessor option
+ end
+
+ def clear
+ CONFIG_OPTIONS.each do |option|
+ self.instance_variable_set("@#{option}".to_sym, nil)
+ end
+ end
+
+ def configure(config)
+ @base_dir = config["dir"]
+ FileUtils.mkdir_p(@base_dir)
+
+ @logger = Logger.new(config["logging"]["file"] || STDOUT)
+ @logger.level = Logger.const_get(config["logging"]["level"].upcase)
+ @logger.formatter = ThreadFormatter.new
+
+ Dir.chdir(File.expand_path("..", __FILE__))
+ @revision = `(git show-ref --head --hash=8 2> /dev/null || echo 00000000) | head -n1`.strip
+
+ @name = config["name"] || ""
+
+ if config["db"]["database"].index("sqlite://") == 0
+ patch_sqlite
+ end
+
+ connection_options = {}
+ [:max_connections, :pool_timeout].each { |key| connection_options[key] = config["db"][key.to_s] }
+
+ @db = Sequel.connect(config["db"]["database"], connection_options)
+
+ puts("Database connection successful #{@db.inspect}")
+ @db.logger = @logger
+ @db.sql_log_level = :debug
+ Sequel::Model.plugin :validation_helpers
+
+ create_default_org_and_project()
+
+ @lock = Monitor.new
+
+ puts "Configuration complete"
+ @logger.debug("Collab Spaces running")
+
+ #@show_exceptions = config["sinatra"]["show_exceptions"]
+ #@raise_errors = config["sinatra"]["raise_errors"]
+ #@dump_errors = config["sinatra"]["dump_errors"]
+
+ @uaa = { :host => "localhost", :port => 8080, :context => "/cloudfoundry-identity-uaa", :user => "app", :password => "appclientsecret" }
+
+ @basic_auth = { :user => config["basic_auth"]["user"], :password => config["basic_auth"]["password"]}
+
+ end
+
+ def logger=(logger)
+ @logger = logger
+ end
+
+ def patch_sqlite
+ require "sequel"
+ require "sequel/adapters/sqlite"
+
+ Sequel::SQLite::Database.class_eval do
+ def connect(server)
+ opts = server_opts(server)
+ opts[:database] = ':memory:' if blank_object?(opts[:database])
+ db = ::SQLite3::Database.new(opts[:database])
+ db.busy_handler do |retries|
+ CollabSpaces::Config.logger.debug "SQLITE BUSY, retry ##{retries}"
+ sleep(0.1)
+ retries < 20
+ end
+
+ connection_pragmas.each { |s| log_yield(s) { db.execute_batch(s) } }
+
+ class << db
+ attr_reader :prepared_statements
+ end
+ db.instance_variable_set(:@prepared_statements, {})
+
+ db
+ end
+ end
+ end
+
+ def create_default_org_and_project()
+
+ @logger.debug("Is default org available?")
+ ds = @db[:resources]
+ all_org = ds.filter(:name => :all.to_s, :type => :organization.to_s).all()
+ if(all_org.nil? || all_org.size() == 0)
+ @logger.debug("Creating default org")
+ @db[:resources].insert(:id => -1,
+ :name => "all",
+ :owner_id => -1,
+ :type => "organization",
+ :description => "Root cloudfoundry org",
+ :immutable_id => SecureRandom.uuid,
+ :created_at => Time.now,
+ :last_updated_at => Time.now)
+ @db[:resources].insert(:id => -2,
+ :name => "all",
+ :owner_id => -1,
+ :type => "project",
+ :description => "Project for the root cloudfoundry org",
+ :immutable_id => SecureRandom.uuid,
+ :created_at => Time.now,
+ :last_updated_at => Time.now)
+ @db[:resources].insert(:id => -3,
+ :name => "organization",
+ :owner_id => -1,
+ :type => "resource_type",
+ :description => "Organization resource type for the root cloudfoundry org",
+ :immutable_id => SecureRandom.uuid,
+ :created_at => Time.now,
+ :last_updated_at => Time.now)
+ else
+ @logger.debug("Yes")
+ end
+
+ end
+
+ end
+ end
+end
0  lib/acm_controller.rb
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.