Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 12 files changed
  • 0 comments
  • 1 contributor
1  Procfile
@@ -4,3 +4,4 @@ image-bot: cd socket_server/bots/image && bundle exec ruby image_bot.rb
4 4 youtube-bot: cd socket_server/bots/youtube && bundle exec ruby youtube_bot.rb
5 5 github-bot: cd socket_server/bots/github && bundle exec ruby github_bot.rb
6 6 pivotal-tracker-bot: cd socket_server/bots/pivotal_tracker && bundle exec ruby pivotal_tracker_bot.rb
  7 +deployment-bot: cd socket_server/bots/deployment && bundle exec ruby deployment_bot.rb
49 socket_server/bots/deployment/Rakefile
... ... @@ -0,0 +1,49 @@
  1 +require 'active_record'
  2 +require 'logger'
  3 +require 'fileutils'
  4 +
  5 +ActiveRecord::Base.establish_connection(
  6 + :adapter => "mysql2",
  7 + :host => "localhost",
  8 + :username => "root",
  9 + :password => "",
  10 + :database => "deployment_bot",
  11 + :encoding => 'utf8',
  12 + :reconnec => true
  13 +)
  14 +ActiveRecord::Base.logger = Logger.new(STDOUT)
  15 +
  16 +namespace :db do
  17 + desc "migrate your database"
  18 + task :migrate do
  19 + ActiveRecord::Migrator.migrate(
  20 + 'db/migrate',
  21 + ENV["VERSION"] ? ENV["VERSION"].to_i : nil
  22 + )
  23 + end
  24 +
  25 + desc "create an ActiveRecord migration in ./db/migrate"
  26 + task :create_migration do
  27 + name = ENV['NAME']
  28 + abort("no NAME specified. use `rake db:create_migration NAME=create_users`") if !name
  29 +
  30 + migrations_dir = File.join("db", "migrate")
  31 + version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
  32 + filename = "#{version}_#{name}.rb"
  33 + migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
  34 +
  35 + FileUtils.mkdir_p(migrations_dir)
  36 +
  37 + open(File.join(migrations_dir, filename), 'w') do |f|
  38 + f << (<<-EOS).gsub(" ", "")
  39 + class #{migration_name} < ActiveRecord::Migration
  40 + def self.up
  41 + end
  42 +
  43 + def self.down
  44 + end
  45 + end
  46 + EOS
  47 + end
  48 + end
  49 +end
13 socket_server/bots/deployment/db/migrate/20111009012858_create_projects.rb
... ... @@ -0,0 +1,13 @@
  1 +class CreateProjects < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :projects do |t|
  4 + t.string :clone_url
  5 + t.string :heroku_app_name
  6 + t.string :destination_tag_name
  7 + end
  8 + end
  9 +
  10 + def self.down
  11 + drop_table :projects
  12 + end
  13 +end
39 socket_server/bots/deployment/deployment_bot.rb
... ... @@ -0,0 +1,39 @@
  1 +require_relative '../../../lib/shared'
  2 +require File.join(PATHS::SOCKET_SERVER::BOT_LIB, 'bot_essentials')
  3 +
  4 +require_relative 'models/models'
  5 +require_relative 'lib/libs'
  6 +
  7 +class DeploymentBotRequest < BotRequestBase
  8 + handle_bot_event("fetch_projects") do
  9 + Project.all.as_json.to_json
  10 + end
  11 +
  12 + handle_bot_event("create_project") do
  13 + project_attributes = JSON.parse(message_hash)
  14 + project = Project.create(project_attributes)
  15 +
  16 + if project.persisted?
  17 + within_subprocess do
  18 + ProjectPreparer.prepare(project)
  19 + end
  20 + end
  21 +
  22 + project.as_json(methods: :errors).to_json
  23 + end
  24 +
  25 + def process_user_initiated_message
  26 + # TODO: Nothing to do here, should make this an optional method
  27 + end
  28 +end
  29 +
  30 +class DeploymentBot < BotBase
  31 + REPO_STORAGE_PATH = File.join(File.dirname(__FILE__), "repositories")
  32 +
  33 + def initialize
  34 + autoconnect_database
  35 + super
  36 + end
  37 +end
  38 +
  39 +DeploymentBot.new
1  socket_server/bots/deployment/lib/libs.rb
... ... @@ -0,0 +1 @@
  1 +require_relative 'project_preparer'
26 socket_server/bots/deployment/lib/project_preparer.rb
... ... @@ -0,0 +1,26 @@
  1 +module ProjectPreparer
  2 + def self.prepare(project)
  3 + html = "Preparing to onboard new project '#{project.heroku_app_name}'"
  4 +
  5 + message_object = MessageFromFactory.new(
  6 + project.destination_tag_name,
  7 + "DeploymentBot",
  8 + html
  9 + ).message
  10 +
  11 + BotInitiatedInterprocessMessage.new(
  12 + "deployment_bot",
  13 + "project_prepare",
  14 + message_hash: message_object
  15 + ).send_to_asynchronous_pipe
  16 +
  17 + output = `cd #{DeploymentBot::REPO_STORAGE_PATH} && git clone #{project.clone_url} #{project.id} --progress 2>&1`
  18 + output += `cd #{DeploymentBot::REPO_STORAGE_PATH}/#{project.id} && git remote add heroku git@heroku.com:#{project.heroku_app_name}.git`
  19 +
  20 + html = "Project '#{project.heroku_app_name}' imported:"
  21 + html << "<pre>#{output}</pre>"
  22 + message_object["data"] = html
  23 +
  24 + UserInitiatedInterprocessMessage.new(message_hash: message_object).send_to_asynchronous_pipe
  25 + end
  26 +end
5 socket_server/bots/deployment/models/models.rb
... ... @@ -0,0 +1,5 @@
  1 +require 'active_record'
  2 +
  3 +ActiveRecord::Base.include_root_in_json = false
  4 +
  5 +require_relative 'project'
4 socket_server/bots/deployment/models/project.rb
... ... @@ -0,0 +1,4 @@
  1 +class Project < ActiveRecord::Base
  2 + validates :destination_tag_name, presence: true
  3 + validates :clone_url, :heroku_app_name, presence: true, uniqueness: true
  4 +end
48 socket_server/bots/deployment/rack/preferences.rb
... ... @@ -0,0 +1,48 @@
  1 +require 'json'
  2 +require 'haml'
  3 +require 'sinatra'
  4 +
  5 +require File.join(PATHS::SOCKET_SERVER::LIB, 'interprocess_message')
  6 +
  7 +module DeploymentBot
  8 + module Rack
  9 + class Preferences < Sinatra::Base
  10 + set :views, File.dirname(__FILE__) + '/views'
  11 +
  12 + get "/" do
  13 + @projects = \
  14 + NamedPipe.with_anonymous_pipe_for_reading do |pipe|
  15 + BotInitiatedInterprocessMessage.new(
  16 + "deployment",
  17 + "fetch_projects",
  18 + response_pipe: pipe
  19 + ).send_to_bot
  20 +
  21 + pipe.read_json
  22 + end
  23 +
  24 + haml :preferences
  25 + end
  26 +
  27 + post "/projects" do
  28 + project = \
  29 + NamedPipe.with_anonymous_pipe_for_reading do |pipe|
  30 + BotInitiatedInterprocessMessage.new(
  31 + "deployment",
  32 + "create_project",
  33 + message_hash: params[:project].to_json,
  34 + response_pipe: pipe
  35 + ).send_to_bot
  36 +
  37 + pipe.read_json
  38 + end
  39 +
  40 + if project["id"].nil?
  41 + "Error saving project: #{project["errors"]}"
  42 + else
  43 + "Successfully saved project."
  44 + end
  45 + end
  46 + end
  47 + end
  48 +end
17 socket_server/bots/deployment/rack/views/preferences.haml
... ... @@ -0,0 +1,17 @@
  1 +%h3 Current projects:
  2 +%ul
  3 + - @projects.each do |project|
  4 + %li Clone URL: #{project["clone_url"]}, Heroku App Name: #{project["heroku_app_name"]}, Tag: #{project["destination_tag_name"]}
  5 +
  6 +%form{ action: "/bots/deployment/preferences/projects", method: "post" }
  7 +
  8 + %label Clone URL
  9 + %input.medium{ type: "text", name: "project[clone_url]" }
  10 +
  11 + %label Heroku App Name
  12 + %input.medium{ type: "text", name: "project[heroku_app_name]" }
  13 +
  14 + %label Destination Tag
  15 + %input.medium{ type: "text", name: "project[destination_tag_name]" }
  16 +
  17 + %input.btn.primary{ type: 'submit', value: "Save Project" }
1  socket_server/bots/deployment/repositories/.gitignore
... ... @@ -0,0 +1 @@
  1 +/*
2  socket_server/bots/pivotal_tracker/rack/views/preferences.haml
... ... @@ -1,7 +1,7 @@
1 1 %h3 Current projects:
2 2 %ul
3 3 - @projects.each do |project|
4   - %li Repository: #{project["project_id"]}, Tag: #{project["destination_tag_name"]}
  4 + %li Project ID: #{project["project_id"]}, Tag: #{project["destination_tag_name"]}
5 5
6 6 %form{ action: "/bots/pivotal_tracker/preferences/projects", method: "post" }
7 7

No commit comments for this range

Something went wrong with that request. Please try again.