Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 409588d

Browse files
committed
Merge pull request #196 from jamesrwhite/develop
v0.9.1
2 parents 286ab08 + 15d3e22 commit 409588d

23 files changed

+236
-946
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ start-gem-server:
22
(cd ../minicron-build/gem-server && rm -rf data && rackup)
33

44
build:
5+
rm -rf pkg
56
rm -f *.gem
67
rm -f Gemfile.lock
78
bundle

README.md

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,10 @@ instead.
105105

106106
1. First check you meet the [requirements](#requirements)
107107

108-
2. Either [grab the latest](https://github.com/jamesrwhite/minicron/releases/tag/v0.9.0) zip/tarball for your OS, use
109-
the handy [install script](install.sh) (where `OS` is one of:
110-
osx, linux-x86, linux-x86_64)
108+
2. Use the handy [install script](install.sh) or [grab the latest](https://github.com/jamesrwhite/minicron/releases/tag/v0.9.1) zip/tarball
109+
for your OS and install manually.
111110
```
112-
OS=linux-x86_64 bash -c "$(curl -sSL https://raw.githubusercontent.com/jamesrwhite/minicron/master/install.sh)"
111+
bash -c "$(curl -sSL https://raw.githubusercontent.com/jamesrwhite/minicron/master/install.sh)"
113112
```
114113
..or [build it yourself](README.md)
115114
if you're feeling adventurous!
@@ -118,27 +117,24 @@ instead.
118117
[minicron.toml](config/minicron.toml) as a guide on what options
119118
are configurable
120119

121-
4. Make sure you have created an empty database with the name you set in ````/etc/minicron.toml````
122-
123120
> **WARNING**
124121
>
125122
> the step below will drop any existing tables in the configured database and create new ones i.e:
126123
> ````sql
127124
> DROP TABLE IF EXISTS jobs; CREATE TABLE jobs ..
128125
> ````
129126
130-
6. You can then ````minicron db setup```` to create the database schema, alternatively you can use
131-
the [schema dump provided](lib/minicron/hub/db/schema.sql) (MySQL)
127+
4. You can then ````minicron db setup```` to create the database schema and run any pending migrations.
132128
133-
7. Done! See the usage section below for more details on how to use minicron now you have it installed
129+
5. Done! See the usage section below for more details on how to use minicron now you have it installed
134130
135131
#### Docker
136132
137133
You can also run minicron in a docker container, see below for instructions how:
138134
139135
````bash
140-
docker pull jamesrwhite/minicron:0.9.0
141-
minicron_container_id=$(docker run -d -p 127.0.0.1:9292:9292 -i -t jamesrwhite/minicron:0.9.0)
136+
docker pull jamesrwhite/minicron:0.9.1
137+
minicron_container_id=$(docker run -d -p 127.0.0.1:9292:9292 -i -t jamesrwhite/minicron:0.9.1)
142138
docker exec $minicron_container_id minicron db setup
143139
docker exec $minicron_container_id minicron server start
144140
````
@@ -197,14 +193,7 @@ how to run minicron behind a reverse proxy.
197193
To be able to perform CRUD operations on the crontab minicron needs to connect via SSH to the host.
198194
When you set up a host minicron automatically creates a public/private key pair for you and stores it
199195
in ````~/.ssh```` on the host the minicron server is being run on using the naming schema ````minicron_host_*HOST_ID*_rsa(.pub)````.
200-
To be able edit the crontab on a host minicron needs to have permission to edit ````/etc/crontab```` and write and
201-
execute permissions on the ````/etc```` directory so it can move files there (the crontab). This will most
202-
likely mean allowing minicron to connect to the host as the root user although you could alter your permissions
203-
to allow for this not to be the case.
204-
205-
As an example, to setup minicron SSH for the root user on a host copy the public key to the hosts
206-
authorized_keys file e.g ````/root/.ssh/authorized_keys```` on
207-
most linux distributions or ````/var/root/.ssh/authorized_keys```` on OSX.
196+
You will then to add the public key into the remote hosts authorized_keys file.
208197

209198
#### Version
210199

Rakefile

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
require 'minicron/hub/app'
2-
require 'sinatra/activerecord/rake'
1+
require 'minicron'
32
require 'bundler/gem_tasks'
4-
require 'rspec/core/rake_task'
53

6-
namespace :db do
7-
task :load_config do
8-
# Tell active record where the db dir is
9-
Sinatra::ActiveRecordTasks.db_dir = Minicron::HUB_PATH + '/db'
4+
# Task for local dev to dump schema
5+
task :dump_schema do
6+
# Parse our local config file
7+
Minicron.parse_file_config('./my.config.toml')
108

11-
# Parse the file config in /etc/minicron.toml
12-
Minicron.parse_file_config(nil)
9+
# Connect to the db
10+
Minicron.establish_db_connection(
11+
Minicron.config['server']['database'],
12+
Minicron.config['verbose']
13+
)
1314

14-
# Connect to the DB
15-
Minicron::Hub::App.setup_db
16-
end
17-
end
18-
19-
# rspec tests
20-
desc 'Run specs'
21-
RSpec::Core::RakeTask.new do |t|
22-
t.verbose = true
23-
t.rspec_opts = '--color --order random'
24-
end
25-
26-
# Show a list of tasks by default
27-
task :default do
28-
puts `rake --tasks`
15+
# Dump the schema file
16+
ActiveRecord::SchemaDumper.dump
2917
end

Vagrantfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ VAGRANTFILE_API_VERSION = "2"
55
$script = <<SCRIPT
66
apt-get update
77
apt-get install -y libsqlite3-dev wget unzip
8-
wget https://github.com/jamesrwhite/minicron/releases/download/v0.9.0/minicron-0.9.0-linux-x86_64.zip > minicron.zip
9-
unzip -o minicron-0.9.0-linux-x86_64.zip
10-
export PATH=~/minicron-0.9.0-linux-x86_64:$PATH
8+
wget https://github.com/jamesrwhite/minicron/releases/download/v0.9.1/minicron-0.9.1-linux-x86_64.zip > minicron.zip
9+
unzip -o minicron-0.9.1-linux-x86_64.zip
10+
export PATH=~/minicron-0.9.1-linux-x86_64:$PATH
1111
ufw allow 2222
1212
ufw allow 9292
1313
ufw enable
File renamed without changes.
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
ActiveRecord::Schema.define(version: 0) do
1414

15-
create_table "alerts", force: true do |t|
16-
t.integer "job_id", null: false
17-
t.integer "execution_id"
18-
t.integer "schedule_id"
15+
create_table "alerts", force: :cascade do |t|
16+
t.integer "job_id", limit: 4, null: false
17+
t.integer "execution_id", limit: 4
18+
t.integer "schedule_id", limit: 4
1919
t.string "kind", limit: 4, default: "", null: false
2020
t.datetime "expected_at"
2121
t.string "medium", limit: 9, default: "", null: false
@@ -29,13 +29,13 @@
2929
add_index "alerts", ["medium"], name: "medium", using: :btree
3030
add_index "alerts", ["schedule_id"], name: "schedule_id", using: :btree
3131

32-
create_table "executions", force: true do |t|
33-
t.integer "job_id", null: false
34-
t.integer "number", null: false
35-
t.datetime "created_at", null: false
32+
create_table "executions", force: :cascade do |t|
33+
t.integer "job_id", limit: 4, null: false
34+
t.integer "number", limit: 4, null: false
35+
t.datetime "created_at", null: false
3636
t.datetime "started_at"
3737
t.datetime "finished_at"
38-
t.integer "exit_status"
38+
t.integer "exit_status", limit: 4
3939
end
4040

4141
add_index "executions", ["created_at"], name: "executions_created_at", using: :btree
@@ -44,45 +44,45 @@
4444
add_index "executions", ["job_id"], name: "executions_job_id", using: :btree
4545
add_index "executions", ["started_at"], name: "started_at", using: :btree
4646

47-
create_table "hosts", force: true do |t|
48-
t.string "name"
49-
t.string "fqdn", default: "", null: false
50-
t.string "user", limit: 32, default: "", null: false
51-
t.string "host", default: "", null: false
52-
t.integer "port", null: false
53-
t.text "public_key"
54-
t.datetime "created_at", null: false
55-
t.datetime "updated_at", null: false
47+
create_table "hosts", force: :cascade do |t|
48+
t.string "name", limit: 255
49+
t.string "fqdn", limit: 255, default: "", null: false
50+
t.string "user", limit: 32, default: "", null: false
51+
t.string "host", limit: 255, default: "", null: false
52+
t.integer "port", limit: 4, null: false
53+
t.text "public_key", limit: 65535
54+
t.datetime "created_at", null: false
55+
t.datetime "updated_at", null: false
5656
end
5757

5858
add_index "hosts", ["fqdn"], name: "hostname", using: :btree
5959

60-
create_table "job_execution_outputs", force: true do |t|
61-
t.integer "execution_id", null: false
62-
t.integer "seq", null: false
63-
t.text "output", null: false
64-
t.datetime "timestamp", null: false
60+
create_table "job_execution_outputs", force: :cascade do |t|
61+
t.integer "execution_id", limit: 4, null: false
62+
t.integer "seq", limit: 4, null: false
63+
t.text "output", limit: 65535, null: false
64+
t.datetime "timestamp", null: false
6565
end
6666

6767
add_index "job_execution_outputs", ["execution_id"], name: "job_execution_outputs_execution_id", using: :btree
6868
add_index "job_execution_outputs", ["seq"], name: "seq", using: :btree
6969

70-
create_table "jobs", force: true do |t|
71-
t.integer "host_id", null: false
72-
t.string "job_hash", limit: 32, default: "", null: false
73-
t.string "name"
74-
t.string "user", limit: 32, null: false
75-
t.text "command", null: false
76-
t.datetime "created_at", null: false
77-
t.datetime "updated_at", null: false
70+
create_table "jobs", force: :cascade do |t|
71+
t.integer "host_id", limit: 4, null: false
72+
t.string "job_hash", limit: 32, default: "", null: false
73+
t.string "name", limit: 255
74+
t.string "user", limit: 32, null: false
75+
t.text "command", limit: 65535, null: false
76+
t.datetime "created_at", null: false
77+
t.datetime "updated_at", null: false
7878
end
7979

8080
add_index "jobs", ["created_at"], name: "jobs_created_at", using: :btree
8181
add_index "jobs", ["host_id"], name: "host_id", using: :btree
8282
add_index "jobs", ["job_hash"], name: "job_hash", unique: true, using: :btree
8383

84-
create_table "schedules", force: true do |t|
85-
t.integer "job_id", null: false
84+
create_table "schedules", force: :cascade do |t|
85+
t.integer "job_id", limit: 4, null: false
8686
t.string "minute", limit: 169
8787
t.string "hour", limit: 61
8888
t.string "day_of_the_month", limit: 83

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
VERSION="0.9.0"
4+
VERSION="0.9.1"
55

66
echo "Installing mincron v$VERSION"
77

lib/minicron.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'minicron/constants'
2+
require 'active_record'
23
require 'toml'
34
require 'sshkey'
45
require 'stringio'
@@ -224,4 +225,48 @@ def self.get_db_adapter(type)
224225
raise Minicron::DatabaseError, "The database #{type} is not supported"
225226
end
226227
end
228+
229+
# Get the activerecord config hash for the databaes
230+
#
231+
# @param type [Hash] database config
232+
# @return type [String] activerecord database config
233+
def self.get_activerecord_db_config(config)
234+
case config['type']
235+
when /mysql|postgresql/
236+
return {
237+
:adapter => Minicron.get_db_adapter(config['type']),
238+
:host => config['host'],
239+
:database => config['database'],
240+
:username => config['username'],
241+
:password => config['password']
242+
}
243+
when 'sqlite'
244+
# Calculate the realtive path to the db because sqlite or activerecord is
245+
# weird and doesn't seem to handle abs paths correctly
246+
root = Pathname.new(Dir.pwd)
247+
db = Pathname.new(Minicron::BASE_PATH + '/db')
248+
db_rel_path = db.relative_path_from(root)
249+
250+
return {
251+
:adapter => Minicron.get_db_adapter(config['type']),
252+
:database => "#{db_rel_path}/minicron.sqlite3" # TODO: Allow configuring this but default to this value
253+
}
254+
else
255+
raise Minicron::DatabaseError, "The database #{config['type']} is not supported"
256+
end
257+
end
258+
259+
# Get the activerecord config hash for the databaes
260+
#
261+
# @param type [Hash] database config
262+
def self.establish_db_connection(config, verbose = false)
263+
# Get the activerecord formatted config
264+
ar_config = self.get_activerecord_db_config(config)
265+
266+
# Connect to the database
267+
ActiveRecord::Base.establish_connection(ar_config)
268+
269+
# Enable ActiveRecord logging if in verbose mode
270+
ActiveRecord::Base.logger = verbose ? Logger.new(STDOUT) : nil
271+
end
227272
end

lib/minicron/cli/commands.rb

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
require 'insidious'
33
require 'rake'
44
require 'minicron/hub/app'
5-
require 'sinatra/activerecord'
6-
require 'sinatra/activerecord/rake'
5+
require 'active_record'
76
require 'minicron/transport'
87
require 'minicron/transport/client'
98
require 'minicron/transport/server'
@@ -14,7 +13,7 @@ class Commands
1413
# Add the `minicron db` command
1514
def self.add_db_cli_command(cli)
1615
cli.command :db do |c|
17-
c.syntax = 'minicron db [create|setup|migrate]'
16+
c.syntax = 'minicron db [setup|migrate|version]'
1817
c.description = 'Sets up the minicron database schema.'
1918

2019
c.action do |args, opts|
@@ -26,22 +25,63 @@ def self.add_db_cli_command(cli)
2625
# Parse the file and cli config options
2726
Minicron::CLI.parse_config(opts)
2827

29-
# Setup the db
30-
Minicron::Hub::App.setup_db
31-
32-
# Tell activerecord where the db folder is, it assumes it is in db/
33-
Sinatra::ActiveRecordTasks.db_dir = Minicron::HUB_PATH + '/db'
28+
# Display all the infos please
29+
ActiveRecord::Migration.verbose = true
3430

3531
# Adjust the task name for some more friendly tasks
3632
case args.first
3733
when 'setup'
38-
task = 'schema:load'
34+
# Remove the database name from the config in case it doesn't exist yet
35+
Minicron.establish_db_connection(
36+
Minicron.config['server']['database'].merge('database' => nil),
37+
Minicron.config['verbose']
38+
)
39+
40+
# Create the database
41+
ActiveRecord::Base.connection.create_database(
42+
Minicron.config['server']['database']['database'],
43+
{
44+
:charset => 'utf8',
45+
:collation => 'utf8_unicode_ci'
46+
}
47+
)
48+
49+
# Then create the initial schema based on schema.rb
50+
ActiveRecord::Tasks::DatabaseTasks.load_schema_for(
51+
Minicron.get_activerecord_db_config(Minicron.config['server']['database']),
52+
ActiveRecord::Base.schema_format,
53+
"#{Minicron::DB_PATH}/schema.rb"
54+
)
55+
56+
# Open a new connection to our shiny new database now we know it's there for sure
57+
Minicron.establish_db_connection(
58+
Minicron.config['server']['database'],
59+
Minicron.config['verbose']
60+
)
61+
62+
# Then run any migrations
63+
ActiveRecord::Migrator.migrate(Minicron::MIGRATIONS_PATH)
64+
when 'migrate'
65+
# Connect to the database
66+
Minicron.establish_db_connection(
67+
Minicron.config['server']['database'],
68+
Minicron.config['verbose']
69+
)
70+
71+
# Run any pending migrations
72+
ActiveRecord::Migrator.migrate(Minicron::MIGRATIONS_PATH)
73+
when 'version'
74+
# Connect to the database
75+
Minicron.establish_db_connection(
76+
Minicron.config['server']['database'],
77+
Minicron.config['verbose']
78+
)
79+
80+
# Print our the schema version
81+
puts ActiveRecord::Migrator.current_version
3982
else
40-
task = args.first
83+
raise Minicron::ArgumentError, "Unknown argument #{args.first}. See `minicron help db`"
4184
end
42-
43-
# Run the task
44-
Rake.application['db:' + task].invoke
4585
end
4686
end
4787
end

0 commit comments

Comments
 (0)