From 609e684477ba9c5749c4e38a954e0a3349812e9b Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Tue, 20 May 2014 20:54:09 +0200 Subject: [PATCH] factored commands --- lib/islo.rb | 251 +--------------------------------- lib/islo/command.rb | 36 +++++ lib/islo/commands/mysql.rb | 70 ++++++++++ lib/islo/commands/postgres.rb | 50 +++++++ lib/islo/commands/redis.rb | 108 +++++++++++++++ 5 files changed, 268 insertions(+), 247 deletions(-) create mode 100644 lib/islo/command.rb create mode 100644 lib/islo/commands/mysql.rb create mode 100644 lib/islo/commands/postgres.rb create mode 100644 lib/islo/commands/redis.rb diff --git a/lib/islo.rb b/lib/islo.rb index cce8d28..d82b85e 100644 --- a/lib/islo.rb +++ b/lib/islo.rb @@ -1,40 +1,5 @@ # Islo - application isolator module Islo - # Generic command execution - class Command - class Error < StandardError; end - - attr_reader :title, :wd - attr_reader :command, :args - - def initialize(args, title: nil, wd: nil) - @command = args.shift - @args = args - @title = title unless title.nil? || title.empty? - @wd = Pathname.new(wd || Dir.pwd) - end - - def title? - !title.nil? - end - - def exec - Dir.chdir(wd.to_s) - Kernel.exec(*args_for_exec) - rescue SystemCallError => e - raise Command::Error, e.message - end - - private - - def args_for_exec - command = (title? ? [@command, title] : @command) - args = self.args - - [command] + args - end - end - class << self def commands @commands ||= {} @@ -49,216 +14,8 @@ module Islo (commands[name] || Command).new(args, options) end end - - # MySQL support - module Mysql - # MySQL client - class Client < Command - def self.name - :mysql - end - - def args - %W( - --socket=#{wd}/tmp/sockets/mysql.sock - -uroot - ) + super - end - - Islo.register(self) - end - - # MySQL server - class Server < Command - def self.name - :mysqld - end - - def args - %W( - --no-defaults - --datadir=#{wd}/db/mysql - --pid-file=#{wd}/tmp/pids/mysqld.pid - --socket=#{wd}/tmp/sockets/mysql.sock - --skip-networking - ) + super - end - - Islo.register(self) - end - - # MySQL initializer - class Init < Command - def self.name - :mysql_install_db - end - - def args - %W( - --no-defaults - --basedir=#{Mysql.basedir} - --datadir=#{wd}/db/mysql - --pid-file=#{wd}/tmp/pids/mysqld.pid - ) + super - end - - Islo.register(self) - end - - def self.basedir - '/usr/local' - end - end - - # Redis support - module Redis - # Redis client - class Client < Command - def self.name - :'redis-cli' - end - - def args - %w(-s redis.sock) - end - - # Change working directory (makes for a nicer prompt) - def wd - super + 'tmp/sockets' - end - - Islo.register(self) - end - - # Redis server - class Server < Command - def self.name - :'redis-server' - end - - def args - %W(#{wd}/db/redis/redis.conf) - end - - Islo.register(self) - end - - # Redis initializer - # - # Creates a minimal configuration because redis-server doesn't accept - # arguments allowing for paths to be set. - class Init < Command - def self.name - :'redis-init' - end - - def exec - FileUtils.mkdir_p(wd + 'db/redis') - - File.open(wd + 'db/redis/redis.conf', 'w') do |f| - f << template.gsub('${WORKING_DIR}', wd.to_s) - end - rescue SystemCallError => e - raise Command::Error, e.message - end - - private - - # rubocop:disable MethodLength,LineLength - def template - <<-EOT.gsub(/^ +/, '') - daemonize no - pidfile ${WORKING_DIR}/pids/redis.pid - port 0 - bind 127.0.0.1 - unixsocket ${WORKING_DIR}/tmp/sockets/redis.sock - unixsocketperm 700 - timeout 0 - tcp-keepalive 0 - loglevel notice - databases 1 - save 900 1 - save 300 10 - save 60 10000 - stop-writes-on-bgsave-error yes - rdbcompression yes - rdbchecksum yes - dbfilename dump.rdb - dir ${WORKING_DIR}/db/redis - slave-serve-stale-data yes - slave-read-only yes - repl-disable-tcp-nodelay no - slave-priority 100 - appendonly yes - appendfsync everysec - no-appendfsync-on-rewrite no - auto-aof-rewrite-percentage 100 - auto-aof-rewrite-min-size 64mb - lua-time-limit 5000 - slowlog-log-slower-than 10000 - slowlog-max-len 128 - hash-max-ziplist-entries 512 - hash-max-ziplist-value 64 - list-max-ziplist-entries 512 - list-max-ziplist-value 64 - set-max-intset-entries 512 - zset-max-ziplist-entries 128 - zset-max-ziplist-value 64 - activerehashing yes - client-output-buffer-limit normal 0 0 0 - client-output-buffer-limit slave 256mb 64mb 60 - client-output-buffer-limit pubsub 32mb 8mb 60 - EOT - end - - Islo.register(self) - end - end - - # PostgreSQL support - module Postgres - # PostgreSQL client - class Client < Command - def self.name - :psql - end - - def args - %W(--host=#{wd}/tmp/sockets) + super - end - - Islo.register(self) - end - - # PostgreSQL server - class Server < Command - def self.name - :postgres - end - - def args - %W( - -D #{wd}/db/postgres - -k #{wd}/tmp/sockets - ) + ['-h', ''] + super - end - - Islo.register(self) - end - - # PostgreSQL initializer - class Init < Command - def self.name - :initdb - end - - def args - %W( - -D #{wd}/db/postgres - ) + super - end - - Islo.register(self) - end - end +end + +[:mysql, :postgres, :redis].each do |c| + require "islo/commands/#{c}" end diff --git a/lib/islo/command.rb b/lib/islo/command.rb new file mode 100644 index 0000000..803296d --- /dev/null +++ b/lib/islo/command.rb @@ -0,0 +1,36 @@ +module Islo + # Generic command execution + class Command + class Error < StandardError; end + + attr_reader :title, :wd + attr_reader :command, :args + + def initialize(args, title: nil, wd: nil) + @command = args.shift + @args = args + @title = title unless title.nil? || title.empty? + @wd = Pathname.new(wd || Dir.pwd) + end + + def title? + !title.nil? + end + + def exec + Dir.chdir(wd.to_s) + Kernel.exec(*args_for_exec) + rescue SystemCallError => e + raise Command::Error, e.message + end + + private + + def args_for_exec + command = (title? ? [@command, title] : @command) + args = self.args + + [command] + args + end + end +end diff --git a/lib/islo/commands/mysql.rb b/lib/islo/commands/mysql.rb new file mode 100644 index 0000000..22a187b --- /dev/null +++ b/lib/islo/commands/mysql.rb @@ -0,0 +1,70 @@ +require 'islo/command' + +module Islo + # MySQL support + module Mysql + # MySQL client + class Client < Command + def self.name + :mysql + end + + def args + %W( + --socket=#{wd}/tmp/sockets/mysql.sock + -uroot + ) + super + end + + Islo.register(self) + end + + # MySQL server + class Server < Command + def self.name + :mysqld + end + + def args + %W( + --no-defaults + --datadir=#{wd}/db/mysql + --pid-file=#{wd}/tmp/pids/mysqld.pid + --socket=#{wd}/tmp/sockets/mysql.sock + --skip-networking + ) + super + end + + Islo.register(self) + end + + # MySQL initializer + class Init < Command + def self.name + :mysql_install_db + end + + def args + %W( + --no-defaults + --basedir=#{Mysql.basedir} + --datadir=#{wd}/db/mysql + --pid-file=#{wd}/tmp/pids/mysqld.pid + ) + super + end + + Islo.register(self) + end + + def self.basedir + path = %x(which mysql) + + fail Command::Error, 'could not find mysql' if path.empty? + + path = File.dirname(path) + path = File.dirname(path) + + path + end + end +end diff --git a/lib/islo/commands/postgres.rb b/lib/islo/commands/postgres.rb new file mode 100644 index 0000000..7211d75 --- /dev/null +++ b/lib/islo/commands/postgres.rb @@ -0,0 +1,50 @@ +require 'islo/command' + +module Islo + # PostgreSQL support + module Postgres + # PostgreSQL client + class Client < Command + def self.name + :psql + end + + def args + %W(--host=#{wd}/tmp/sockets) + super + end + + Islo.register(self) + end + + # PostgreSQL server + class Server < Command + def self.name + :postgres + end + + def args + %W( + -D #{wd}/db/postgres + -k #{wd}/tmp/sockets + ) + ['-h', ''] + super + end + + Islo.register(self) + end + + # PostgreSQL initializer + class Init < Command + def self.name + :initdb + end + + def args + %W( + -D #{wd}/db/postgres + ) + super + end + + Islo.register(self) + end + end +end diff --git a/lib/islo/commands/redis.rb b/lib/islo/commands/redis.rb new file mode 100644 index 0000000..499795f --- /dev/null +++ b/lib/islo/commands/redis.rb @@ -0,0 +1,108 @@ +require 'islo/command' + +module Islo + # Redis support + module Redis + # Redis client + class Client < Command + def self.name + :'redis-cli' + end + + def args + %w(-s redis.sock) + end + + # Change working directory (makes for a nicer prompt) + def wd + super + 'tmp/sockets' + end + + Islo.register(self) + end + + # Redis server + class Server < Command + def self.name + :'redis-server' + end + + def args + %W(#{wd}/db/redis/redis.conf) + end + + Islo.register(self) + end + + # Redis initializer + # + # Creates a minimal configuration because redis-server doesn't accept + # arguments allowing for paths to be set. + class Init < Command + def self.name + :'redis-init' + end + + def exec + FileUtils.mkdir_p(wd + 'db/redis') + + File.open(wd + 'db/redis/redis.conf', 'w') do |f| + f << template.gsub('${WORKING_DIR}', wd.to_s) + end + rescue SystemCallError => e + raise Command::Error, e.message + end + + private + + # rubocop:disable MethodLength,LineLength + def template + <<-EOT.gsub(/^ +/, '') + daemonize no + pidfile ${WORKING_DIR}/pids/redis.pid + port 0 + bind 127.0.0.1 + unixsocket ${WORKING_DIR}/tmp/sockets/redis.sock + unixsocketperm 700 + timeout 0 + tcp-keepalive 0 + loglevel notice + databases 1 + save 900 1 + save 300 10 + save 60 10000 + stop-writes-on-bgsave-error yes + rdbcompression yes + rdbchecksum yes + dbfilename dump.rdb + dir ${WORKING_DIR}/db/redis + slave-serve-stale-data yes + slave-read-only yes + repl-disable-tcp-nodelay no + slave-priority 100 + appendonly yes + appendfsync everysec + no-appendfsync-on-rewrite no + auto-aof-rewrite-percentage 100 + auto-aof-rewrite-min-size 64mb + lua-time-limit 5000 + slowlog-log-slower-than 10000 + slowlog-max-len 128 + hash-max-ziplist-entries 512 + hash-max-ziplist-value 64 + list-max-ziplist-entries 512 + list-max-ziplist-value 64 + set-max-intset-entries 512 + zset-max-ziplist-entries 128 + zset-max-ziplist-value 64 + activerehashing yes + client-output-buffer-limit normal 0 0 0 + client-output-buffer-limit slave 256mb 64mb 60 + client-output-buffer-limit pubsub 32mb 8mb 60 + EOT + end + + Islo.register(self) + end + end +end