Skip to content

Commit

Permalink
Init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Rodriguez committed Sep 9, 2015
0 parents commit 9c08a88
Show file tree
Hide file tree
Showing 24 changed files with 366 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/pkg
/work
/.idea
/*.lock
3 changes: 3 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Tim Fischbach <[email protected]>
Nicolas Rodriguez <[email protected]>
Leandro Muniz de Lima <[email protected]>
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source 'https://rubygems.org'

gem 'jenkins-plugin-runtime', '~> 0.2.3'

gem 'jenkins-plugin-base', '~> 1.0.0', git: 'https://github.com/jbox-web/jenkins-plugin-base.git', tag: '1.0.0'
gem 'job-database-manager', '~> 1.0.0', git: 'https://github.com/jbox-web/job-database-manager.git', tag: '1.0.0'

group :development do
gem 'jpi', '~> 0.4.0'

## Uncomment this line if you want to develop with the last version of Jenkins.
## This line **MUST** stay commented when doing "jpi build"
## otherwhise jpi bundle the war file into the .hpi file (about 65mo)
gem 'jenkins-war', git: 'https://github.com/jbox-web/jenkins-war.git', branch: 'v1.628'
end
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Jenkins Job Database Manager Plugin for PostgreSQL

[![GitHub license](https://img.shields.io/github/license/jbox-web/job-database-manager-postgresql.svg)](https://github.com/jbox-web/job-database-manager-postgresql/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/jbox-web/job-database-manager-postgresql.svg)](https://github.com/jbox-web/job-database-manager-postgresql/releases/latest)
[![Code Climate](https://codeclimate.com/github/jbox-web/job-database-manager-postgresql/badges/gpa.svg)](https://codeclimate.com/github/jbox-web/job-database-manager-postgresql)
[![Download](https://img.shields.io/github/downloads/jbox-web/job-database-manager-postgresql/latest/job-database-manager-postgresql.hpi.svg)](https://github.com/jbox-web/job-database-manager-postgresql/releases/download/1.0.0/job-database-manager-postgresql.hpi)

### A Jenkins plugin that automatically set up PostgreSQL test databases for Jenkins jobs, easy ;)

## Installation

Download the [hpi file](https://github.com/jbox-web/job-database-manager-postgresql/releases/download/1.0.0/job-database-manager-postgresql.hpi) and install it within Jenkins interface.

## Configuration

In the global Jenkins configuration, set up host and port of your
PostgreSQL server and enter credentials of the PostgreSQL user that shall be
used to create databases and grant permissions. Note that this user
needs at least CREATEDB and CREATEROLE permissions.

## Usage

Configure a database name in a job. The plugin ensures the database
exists when the job is run. It grants all permissions for the database
to a job specific user and publishes its credentials in the
environment variables `$PGSQL_USER` and `$PGSQL_PASSWORD`.
It also ensures that the database is dropped at the end of the job.

These environment variables will be injected in the build :

* `$PGSQL_DATABASE`
* `$PGSQL_USER`
* `$PGSQL_PASSWORD`
* `$PGSQL_HOST`
* `$PGSQL_PORT`

## Contribute

You can contribute to this plugin in many ways such as :
* Helping with documentation
* Contributing code (features or bugfixes)
* Reporting a bug
* Submitting translations
15 changes: 15 additions & 0 deletions job-database-manager-postgresql.pluginspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Jenkins::Plugin::Specification.new do |plugin|
plugin.name = 'job-database-manager-postgresql'
plugin.display_name = 'Job Database Manager for PostgreSQL'
plugin.version = '1.0.0'
plugin.description = 'Automatically create and delete a PostgreSQL database for a job.'
plugin.url = 'http://jbox-web.github.io/job-database-manager-postgresql/'

plugin.developed_by 'Tim Fischbach', '[email protected]'
plugin.developed_by 'Nicolas Rodriguez', '[email protected]'
plugin.developed_by 'Leandro Muniz de Lima', '[email protected]'

plugin.uses_repository :git => 'https://github.com/jbox-web/job-database-manager-postgresql.git'

plugin.depends_on 'ruby-runtime', '0.12'
end
26 changes: 26 additions & 0 deletions models/builders/create_postgresql_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Builders
class CreatePostgresqlDatabase < Jenkins::Tasks::BuildWrapper
display_name 'Create a PostgreSQL database for the job'
include ::JobDatabaseManager::DbCreator

def get_setting_value_for(setting)
JobDatabaseManagerPostgresql.get_setting_value_for(setting)
end


def db_adapter_klass
'JobDatabaseManagerPostgresql::PostgresqlAdapter'
end


def db_adapter_name
'PostgreSQL'
end


def db_adapter_type
:pgsql
end

end
end
54 changes: 54 additions & 0 deletions models/job_database_manager_postgresql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'jenkins_plugin_base'
require 'job_database_manager'

require_relative 'job_database_manager_postgresql_descriptor'
require_relative 'postgresql_adapter'

#
# This is the main entry point for Jenkins plugins.
#

class JobDatabaseManagerPostgresql < Jenkins::Model::RootAction
#
# This should be available for all plugins.
# It adds the 2 methods below : *set_plugin_name* and *set_plugin_settings*
#
include ::JenkinsPluginBase::BasePlugin

#
# Declare your plugin name here. (Will be use in XML file)
#
set_plugin_name 'job-database-manager-postgresql'

#
# Declare your global plugin settings here :
# <param name> => <default_value>
#
set_plugin_settings({
:db_user => 'jenkins',
:db_password => 'jenkins',
:db_server_host => '127.0.0.1',
:db_server_port => '5432',
:db_bin_path => '/usr/bin/psql'
})

end


#
# Register our new class in Jenkins plugins
#
Jenkins::Plugin.instance.register_extension(JobDatabaseManagerPostgresql)


#
# Trigger accessors creation for our plugin.
# This must happen AFTER *register_extension* call otherwhise got :
# uninitialized constant #<Class:0x2d474d30>::JobDatabaseManagerPostgresql (NameError)
# Kind of chicken/egg issue.
#
# This trick allows plugin settings declaration to sit here instead of JobDatabaseManagerPostgresqlDescriptor.
# Settings are automatically retrieve from the Hash above and for each an accessor is created.
# They are are automatically saved and retrieved from XML file with default value handling.
#
JobDatabaseManagerPostgresqlDescriptor.create_settings_accessors!
31 changes: 31 additions & 0 deletions models/job_database_manager_postgresql_descriptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This is the main Descriptor for your plugin.
# It handles plugin's global configuration.
# It must respond to these methods :
# * load
# * save
# * configure

require 'jenkins_plugin_base'
require 'job_database_manager'

class JobDatabaseManagerPostgresqlDescriptor < Jenkins::Model::DefaultDescriptor

#
# Include PluginConfig::Base module to add the basic methods.
#
include ::JenkinsPluginBase::BaseConfig

class << self

def plugin_name
JobDatabaseManagerPostgresql.plugin_name
end


def plugin_settings
JobDatabaseManagerPostgresql.plugin_settings
end

end

end
62 changes: 62 additions & 0 deletions models/postgresql_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'job_database_manager'

class JobDatabaseManagerPostgresql < Jenkins::Model::RootAction
class PostgresqlAdapter < ::JobDatabaseManager::DbAdapter::AbstractAdapter

private


def create_database_query(database)
"CREATE DATABASE #{database};"
end


def create_user_query(database, user, password)
"CREATE USER #{user}" +
" WITH PASSWORD '#{password}';" +
"GRANT ALL PRIVILEGES ON DATABASE #{database}" +
" TO #{user};"
end


def drop_database_query(database)
"DROP DATABASE IF EXISTS #{database};"
end


def drop_privilege_query(user)
nil
end


def drop_user_query(user)
"DROP USER #{user};"
end


def db_user
['--username', user]
end


def env_vars
{ 'PGPASSWORD' => password }
end


def execute_query_cmd
'--command'
end


def db_name
['--dbname', 'postgres']
end


def query_cmd(sql)
[db_cmd, *db_host, *db_port, *db_user, *db_name, *db_query(sql)]
end

end
end
26 changes: 26 additions & 0 deletions models/publishers/destroy_postgresql_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Publishers
class DestroyPostgresqlDatabase < Jenkins::Tasks::Publisher
display_name 'Drop a PostgreSQL database after the job finished'
include ::JobDatabaseManager::DbDestroyer

def get_setting_value_for(setting)
JobDatabaseManagerPostgresql.get_setting_value_for(setting)
end


def db_adapter_klass
'JobDatabaseManagerPostgresql::PostgresqlAdapter'
end


def db_adapter_name
'PostgreSQL'
end


def db_adapter_type
:pgsql
end

end
end
19 changes: 19 additions & 0 deletions views/builders/create_postgresql_database/config.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%
f = taglib("/lib/form")

desc = 'Environment variables available in shell build steps: $PGSQL_DATABASE, $PGSQL_USER, $PGSQL_PASSWORD, $PGSQL_HOST, $PGSQL_PORT.'

f.entry :title => 'Database name', :field => 'job_db_name', :description => desc do
f.textbox
end

f.advanced do
f.entry :title => 'Database user name', :field => 'job_db_user' do
f.textbox
end

f.entry :title => 'Database user password', :field => 'job_db_pass' do
f.textbox
end
end
%>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL database name used for this job
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL database user password used for this job
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL database user name used for this job
26 changes: 26 additions & 0 deletions views/job_database_manager_postgresql/global.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%
f = taglib('/lib/form')

f.section :title => 'PostgreSQL Job Databases' do

f.entry :title => 'Jenkins Database User', :field => 'db_user' do
f.textbox :default => "#{descriptor.db_user}"
end

f.entry :title => 'Jenkins Database Password', :field => 'db_password' do
f.textbox :default => "#{descriptor.db_password}"
end

f.entry :title => 'Jenkins Database Server Host', :field => 'db_server_host' do
f.textbox :default => "#{descriptor.db_server_host}"
end

f.entry :title => 'Jenkins Database Server Port', :field => 'db_server_port', :description => 'These credentials are used to create databases for jobs.' do
f.textbox :default => "#{descriptor.db_server_port}"
end

f.entry :title => 'Jenkins PostgreSQL bin path', :field => 'db_bin_path' do
f.textbox :default => "#{descriptor.db_bin_path}"
end
end
%>
1 change: 1 addition & 0 deletions views/job_database_manager_postgresql/help-db_bin_path.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL Database root user
1 change: 1 addition & 0 deletions views/job_database_manager_postgresql/help-db_password.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL Database root password
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL Database server host
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL Database server port
1 change: 1 addition & 0 deletions views/job_database_manager_postgresql/help-db_user.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL Database root user
13 changes: 13 additions & 0 deletions views/publishers/destroy_postgresql_database/config.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%
f = taglib('/lib/form')

f.entry :title => 'Database name', :field => 'job_db_name' do
f.textbox
end

f.advanced do
f.entry :title => 'Database user name', :field => 'job_db_user' do
f.textbox
end
end
%>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL database name used for this job
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PostgreSQL database user name used for this job

0 comments on commit 9c08a88

Please sign in to comment.