diff options
-rw-r--r-- | app/assets/stylesheets/main.scss | 27 | ||||
-rw-r--r-- | app/controllers/projects_controller.rb | 1 | ||||
-rw-r--r-- | app/controllers/runner_projects_controller.rb | 40 | ||||
-rw-r--r-- | app/models/build.rb | 41 | ||||
-rw-r--r-- | app/models/network.rb | 15 | ||||
-rw-r--r-- | app/models/project.rb | 25 | ||||
-rw-r--r-- | app/models/runner.rb | 14 | ||||
-rw-r--r-- | app/models/runner_project.rb | 17 | ||||
-rw-r--r-- | app/views/projects/index.html.haml | 7 | ||||
-rw-r--r-- | app/views/projects/show.html.haml | 2 | ||||
-rw-r--r-- | app/views/runner_projects/edit.html.haml~ | 24 | ||||
-rw-r--r-- | app/views/runner_projects/index.html.haml | 64 | ||||
-rw-r--r-- | config/routes.rb | 2 | ||||
-rw-r--r-- | db/migrate/20130603144959_create_runner_projects.rb | 10 | ||||
-rw-r--r-- | db/migrate/20130603161449_add_project_gitlab_id_to_project.rb | 5 | ||||
-rw-r--r-- | db/schema.rb | 10 | ||||
-rw-r--r-- | lib/api/builds.rb | 8 | ||||
-rw-r--r-- | lib/api/helpers.rb | 6 | ||||
-rw-r--r-- | spec/factories/runner_projects.rb | 8 | ||||
-rw-r--r-- | spec/models/build_spec.rb | 39 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 38 | ||||
-rw-r--r-- | spec/models/runner_project_spec.rb | 16 | ||||
-rw-r--r-- | spec/models/runner_spec.rb | 11 |
23 files changed, 340 insertions, 90 deletions
diff --git a/app/assets/stylesheets/main.scss b/app/assets/stylesheets/main.scss index 7022cd2..fd6d747 100644 --- a/app/assets/stylesheets/main.scss +++ b/app/assets/stylesheets/main.scss @@ -46,29 +46,6 @@ a { color: #888; } -.project_box { - color:#777; - border: 1px solid #ddd; - margin-right:20px; - margin-bottom:20px; - float:left; - width:330px; - - .title { - font-size:16px; - line-height:28px; - padding: 10px; - } - - .alert { - border-radius: 0; - } - - .body { - padding:10px; - } -} - pre.trace { background: #000; color:#fff; @@ -227,3 +204,7 @@ h3 { td form { margin: 0; } + +.cred { + color: #900; +} diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 9d2a4f1..2875f75 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -106,6 +106,7 @@ class ProjectsController < ApplicationController params = { name: project.name_with_namespace, + gitlab_id: project.id, gitlab_url: project.web_url, scripts: 'ls -la', default_ref: project.default_branch, diff --git a/app/controllers/runner_projects_controller.rb b/app/controllers/runner_projects_controller.rb new file mode 100644 index 0000000..184a5a8 --- /dev/null +++ b/app/controllers/runner_projects_controller.rb @@ -0,0 +1,40 @@ +class RunnerProjectsController < ApplicationController + before_filter :authenticate_user! + + def index + @runner_projects = project.runner_projects.all + @runner_project = project.runner_projects.new + end + + def create + ActiveRecord::Base.transaction do + @runner_project = project.runner_projects.create!(params[:runner_project]) + + runner = @runner_project.runner + + opts = { + key: runner.public_key, + title: "gitlab-ci-runner-#{runner.id}", + private_token: current_user.private_token + } + + result = Network.new.add_deploy_key(current_user.url, project.gitlab_id, opts) + raise "Can't add deploy key" unless result + end + + ensure + redirect_to project_runner_projects_path + end + + def destroy + RunnerProject.find(params[:id]).destroy + + redirect_to project_runner_projects_path + end + + private + + def project + @project ||= Project.find(params[:project_id]) + end +end diff --git a/app/models/build.rb b/app/models/build.rb index 1301cd1..7e9d418 100644 --- a/app/models/build.rb +++ b/app/models/build.rb @@ -1,3 +1,23 @@ +# == Schema Information +# +# Table name: builds +# +# id :integer not null, primary key +# project_id :integer +# ref :string(255) +# status :string(255) +# finished_at :datetime +# trace :text(2147483647) +# created_at :datetime not null +# updated_at :datetime not null +# sha :string(255) +# started_at :datetime +# tmp_file :string(255) +# before_sha :string(255) +# push_data :text +# runner_id :integer +# + class Build < ActiveRecord::Base belongs_to :project belongs_to :runner @@ -117,27 +137,8 @@ class Build < ActiveRecord::Base end def repo_url - project.gitlab_url + '.git' + project.ssh_url_to_repo end end - -# == Schema Information -# -# Table name: builds -# -# id :integer(4) not null, primary key -# project_id :integer(4) -# ref :string(255) -# status :string(255) -# finished_at :datetime -# trace :text(2147483647 -# created_at :datetime not null -# updated_at :datetime not null -# sha :string(255) -# started_at :datetime -# tmp_file :string(255) -# before_sha :string(255) -# - diff --git a/app/models/network.rb b/app/models/network.rb index cf56d50..42cc030 100644 --- a/app/models/network.rb +++ b/app/models/network.rb @@ -31,6 +31,21 @@ class Network end end + def add_deploy_key(url, project_id, api_opts) + opts = { + body: api_opts.to_json, + headers: {"Content-Type" => "application/json"}, + } + + response = self.class.post(url + api_prefix + "projects/#{project_id}/keys.json", opts) + + if response.code == 201 + response.parsed_response + else + nil + end + end + private def api_prefix diff --git a/app/models/project.rb b/app/models/project.rb index 4f4bc1e..4a4729c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,15 +1,36 @@ +# == Schema Information +# +# Table name: projects +# +# id :integer not null, primary key +# name :string(255) not null +# timeout :integer default(1800), not null +# scripts :text default(""), not null +# created_at :datetime not null +# updated_at :datetime not null +# token :string(255) +# default_ref :string(255) +# gitlab_url :string(255) +# always_build :boolean default(FALSE), not null +# polling_interval :integer +# public :boolean default(FALSE), not null +# ssh_url_to_repo :string(255) +# + class Project < ActiveRecord::Base attr_accessible :name, :path, :scripts, :timeout, :token, :default_ref, :gitlab_url, :always_build, :polling_interval, - :public, :ssh_url_to_repo + :public, :ssh_url_to_repo, :gitlab_id has_many :builds, dependent: :destroy + has_many :runner_projects, dependent: :destroy + has_many :runners, through: :runner_projects # # Validations # - validates_presence_of :name, :scripts, :timeout, :token, :default_ref, :gitlab_url, :ssh_url_to_repo + validates_presence_of :name, :scripts, :timeout, :token, :default_ref, :gitlab_url, :ssh_url_to_repo, :gitlab_id validates_uniqueness_of :name diff --git a/app/models/runner.rb b/app/models/runner.rb index 2bf086f..f87838f 100644 --- a/app/models/runner.rb +++ b/app/models/runner.rb @@ -1,5 +1,19 @@ +# == Schema Information +# +# Table name: runners +# +# id :integer not null, primary key +# token :string(255) +# public_key :text +# created_at :datetime not null +# updated_at :datetime not null +# + class Runner < ActiveRecord::Base has_many :builds + has_many :runner_projects, dependent: :destroy + has_many :projects, through: :runner_projects + has_one :last_build, class_name: 'Build' attr_accessible :token, :public_key diff --git a/app/models/runner_project.rb b/app/models/runner_project.rb new file mode 100644 index 0000000..fe208ff --- /dev/null +++ b/app/models/runner_project.rb @@ -0,0 +1,17 @@ +# == Schema Information +# +# Table name: runner_projects +# +# id :integer not null, primary key +# runner_id :integer not null +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# + +class RunnerProject < ActiveRecord::Base + attr_accessible :project_id, :runner_id + + belongs_to :runner + belongs_to :project +end diff --git a/app/views/projects/index.html.haml b/app/views/projects/index.html.haml index 9d5ea0f..4591f96 100644 --- a/app/views/projects/index.html.haml +++ b/app/views/projects/index.html.haml @@ -7,6 +7,7 @@ %th Last build %th Access %th Builds + %th Runners %th - @projects.each do |project| @@ -34,6 +35,12 @@ %td = project.builds.count %td + - count = project.runners.count + - if count.zero? + %span.badge.badge-important= count + - else + %span.badge.badge-success= count + %td - if current_user %a{href: edit_project_path(project)} %i.icon-edit.icon-white diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml index 851d862..b68b03c 100644 --- a/app/views/projects/show.html.haml +++ b/app/views/projects/show.html.haml @@ -9,6 +9,8 @@ - if current_user .pull-right %span + = link_to 'Runners', project_runner_projects_path(@project), class: 'btn btn-small' + %span = link_to details_project_path(@project), class: 'btn btn-small' do Details %span diff --git a/app/views/runner_projects/edit.html.haml~ b/app/views/runner_projects/edit.html.haml~ new file mode 100644 index 0000000..f8f450e --- /dev/null +++ b/app/views/runner_projects/edit.html.haml~ @@ -0,0 +1,24 @@ +<h2>Edit <%= current_user.email %></h2> + +<%= form_for(current_user, :html => { :method => :put }) do |f| %> + + <div><%= f.label :email %><br /> + <%= f.email_field :email %></div> + + <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> + <%= f.password_field :password, :autocomplete => "off" %></div> + + <div><%= f.label :password_confirmation %><br /> + <%= f.password_field :password_confirmation %></div> + + <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> + <%= f.password_field :current_password %></div> + + <div class="form-actions"><%= f.submit "Update", class: 'btn btn-primary' %></div> +<% end %> + +<h3>Cancel my account</h3> + +<p class="alert alert-error"> <%= link_to "Cancel my account", current_user, :data => { :confirm => "Are you sure?" }, :method => :delete %>.</p> + +<%= link_to "Back", :back %> diff --git a/app/views/runner_projects/index.html.haml b/app/views/runner_projects/index.html.haml new file mode 100644 index 0000000..8c34de6 --- /dev/null +++ b/app/views/runner_projects/index.html.haml @@ -0,0 +1,64 @@ +%h3 + Project: #{link_to @project.name, @project} → Runners +%br + +%p + To register new runner visit #{link_to 'this page ', runners_path} + +%h5 Activated: + +%table.table + %tr + %th Runner ID + %th Runner Token + %th Last build + %th Builds Stats + %th Registered + %th + + - @runner_projects.each do |runner_project| + - runner = runner_project.runner + - builds = runner.builds.where(project_id: @project.id) + %tr + %td + %span.badge.badge-info= runner.id + %td + = runner.token + %td + - last_build = builds.last + - if last_build + = link_to last_build.short_sha, [last_build.project, last_build] + - else + unknown + %td + %span.badge.badge-success + #{builds.success.count} + %span / + %span.badge.badge-important + #{builds.failed.count} + %td + #{time_ago_in_words(runner_project.created_at)} ago + %td + = link_to 'Disable', [@project, runner_project], data: { confirm: "Are you sure?" }, method: :delete, class: 'btn btn-danger btn-small right' + +- if @runner_projects.blank? + .alert.alert-error + %h5 You should add at least one runner to process project builds + +%h5 Available +%table.table + %tr + %th ID + %th Token + %th + + - (Runner.all - @project.runners).each do |runner| + %tr + %td + = runner.id + %td + = runner.token + %td + = form_for [@project, @runner_project] do |f| + = f.hidden_field :runner_id, value: runner.id + = f.submit 'Add', class: 'btn btn-small' diff --git a/config/routes.rb b/config/routes.rb index 4bdde76..095b7fb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,8 @@ GitlabCi::Application.routes.draw do get :status end end + + resources :runner_projects end resource :user_sessions diff --git a/db/migrate/20130603144959_create_runner_projects.rb b/db/migrate/20130603144959_create_runner_projects.rb new file mode 100644 index 0000000..c65c8a5 --- /dev/null +++ b/db/migrate/20130603144959_create_runner_projects.rb @@ -0,0 +1,10 @@ +class CreateRunnerProjects < ActiveRecord::Migration + def change + create_table :runner_projects do |t| + t.integer :runner_id, null: false + t.integer :project_id, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20130603161449_add_project_gitlab_id_to_project.rb b/db/migrate/20130603161449_add_project_gitlab_id_to_project.rb new file mode 100644 index 0000000..3efdbb7 --- /dev/null +++ b/db/migrate/20130603161449_add_project_gitlab_id_to_project.rb @@ -0,0 +1,5 @@ +class AddProjectGitlabIdToProject < ActiveRecord::Migration + def change + add_column :projects, :gitlab_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 7cbb32a..adfb64c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130603144030) do +ActiveRecord::Schema.define(:version => 20130603161449) do create_table "builds", :force => true do |t| t.integer "project_id" @@ -42,6 +42,14 @@ ActiveRecord::Schema.define(:version => 20130603144030) do t.integer "polling_interval" t.boolean "public", :default => false, :null => false t.string "ssh_url_to_repo" + t.integer "gitlab_id" + end + + create_table "runner_projects", :force => true do |t| + t.integer "runner_id", :null => false + t.integer "project_id", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "runners", :force => true do |t| diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 094af15..75ae4e0 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -15,7 +15,7 @@ module API required_attributes! [:token] ActiveRecord::Base.transaction do - build = Build.pending.order('created_at ASC').first + build = Build.where(project_id: current_runner.projects).pending.order('created_at ASC').first not_found! and return unless build build.run! @@ -32,10 +32,8 @@ module API # Example Request: # PUT /builds/:id put ":id" do - build = Build.find(params[:id]) - runner = Runner.find_by_token(params[:token]) - - build.update_attributes trace: params[:trace], runner_id: runner.id + build = Build.where(project_id: current_runner.projects).find(params[:id]) + build.update_attributes(trace: params[:trace], runner_id: current_runner.id) case params[:state].to_s when 'success' diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 783a91a..f76b039 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -5,7 +5,11 @@ module API end def authenticate_runner! - forbidden! unless Runner.find_by_token(params[:token]) + forbidden! unless current_runner + end + + def current_runner + @runner ||= Runner.find_by_token(params[:token]) end # Checks the occurrences of required attributes, each attribute must be present in the params hash diff --git a/spec/factories/runner_projects.rb b/spec/factories/runner_projects.rb new file mode 100644 index 0000000..e43adcf --- /dev/null +++ b/spec/factories/runner_projects.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :runner_project do + runner_id 1 + project_id 1 + end +end diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index c0969f7..a1891c8 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -1,3 +1,23 @@ +# == Schema Information +# +# Table name: builds +# +# id :integer not null, primary key +# project_id :integer +# ref :string(255) +# status :string(255) +# finished_at :datetime +# trace :text(2147483647) +# created_at :datetime not null +# updated_at :datetime not null +# sha :string(255) +# started_at :datetime +# tmp_file :string(255) +# before_sha :string(255) +# push_data :text +# runner_id :integer +# + require 'spec_helper' describe Build do @@ -32,22 +52,3 @@ describe Build do end - -# == Schema Information -# -# Table name: builds -# -# id :integer(4) not null, primary key -# project_id :integer(4) -# ref :string(255) -# status :string(255) -# finished_at :datetime -# trace :text(2147483647 -# created_at :datetime not null -# updated_at :datetime not null -# sha :string(255) -# started_at :datetime -# tmp_file :string(255) -# before_sha :string(255) -# - diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 354a7a7..002a9f9 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: projects +# +# id :integer not null, primary key +# name :string(255) not null +# timeout :integer default(1800), not null +# scripts :text default(""), not null +# created_at :datetime not null +# updated_at :datetime not null +# token :string(255) +# default_ref :string(255) +# gitlab_url :string(255) +# always_build :boolean default(FALSE), not null +# polling_interval :integer +# public :boolean default(FALSE), not null +# ssh_url_to_repo :string(255) +# + require 'spec_helper' describe Project do @@ -61,25 +80,6 @@ end # gitlab_url :string(255) # always_build :boolean(1) default(FALSE), not null # polling_interval :integer(4) -# - - -# == Schema Information -# -# Table name: projects -# -# id :integer(4) not null, primary key -# name :string(255) not null -# path :string(255) not null -# timeout :integer(4) default(1800), not null -# scripts :text default(""), not null -# created_at :datetime not null -# updated_at :datetime not null -# token :string(255) -# default_ref :string(255) -# gitlab_url :string(255) -# always_build :boolean(1) default(FALSE), not null -# polling_interval :integer(4) # public :boolean(1) default(FALSE), not null # diff --git a/spec/models/runner_project_spec.rb b/spec/models/runner_project_spec.rb new file mode 100644 index 0000000..6428d8b --- /dev/null +++ b/spec/models/runner_project_spec.rb @@ -0,0 +1,16 @@ +# == Schema Information +# +# Table name: runner_projects +# +# id :integer not null, primary key +# runner_id :integer not null +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'spec_helper' + +describe RunnerProject do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/runner_spec.rb b/spec/models/runner_spec.rb index ef04222..97d5e54 100644 --- a/spec/models/runner_spec.rb +++ b/spec/models/runner_spec.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: runners +# +# id :integer not null, primary key +# token :string(255) +# public_key :text +# created_at :datetime not null +# updated_at :datetime not null +# + require 'spec_helper' describe Runner do |