cachivache-helpers 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ce5c412d212a96f20530c73d3cff9dc867b2da0
4
+ data.tar.gz: 24ef8bf696245a4d14fd31363d2e8f204f53c9d7
5
+ SHA512:
6
+ metadata.gz: e0515419f44a6c0b89dc4295d9351185dc67d6da8bde92a3fd535596a5b92037ba3f25cfee834778a9c90011fab0e4aa1a673dfbecdacdae0eb49394906f16cb
7
+ data.tar.gz: 248e9915873a495336a34b6fc25be909d0dc961867e01b17550d2e4cd8a622ef2462f1e01c82280abfbe69ce4316098008376379dafa5e64d70be3dca0426075
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require 'spec-helper'
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "1.9.3"
5
+ - "2.0"
6
+ - "2.1"
7
+ - "2.2"
8
+
9
+ before_install: gem install bundler
10
+
11
+ script: bundle exec rspec
12
+
13
+ git:
14
+ submodules: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cachivache-helpers.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec', require: false
7
+ gem 'simplecov', require: false
8
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Martin Rubi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Cachivache::Helpers
2
+
3
+ [Cachivache](https://github.com/cabeza-de-termo/cachivache) is a provisioner for Vagrant machines with Ubuntu images.
4
+
5
+ This gem provides helpers to use when defining Cachivache stuffs.
6
+
7
+ ## Status
8
+
9
+ [![Gem Version](https://badge.fury.io/rb/cachivache-helpers.svg)](https://badge.fury.io/rb/cachivache-helpers)
10
+ [![Build Status](https://travis-ci.org/cabeza-de-termo/cachivache-helpers.svg?branch=master)](https://travis-ci.org/cabeza-de-termo/cachivache-helpers)
11
+ [![Coverage Status](https://coveralls.io/repos/cabeza-de-termo/cachivache-helpers/badge.svg?branch=master&service=github)](https://coveralls.io/github/cabeza-de-termo/cachivache-helpers?branch=master)
12
+
13
+ ## Installation
14
+
15
+ In your [Cachivache](https://github.com/cabeza-de-termo/cachivache) folder, edit the `Gemfile` file add or update the line
16
+
17
+ ```ruby
18
+ gem 'cachivache-helpers', "~> 0.1"
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Please read the [Cachivache](https://github.com/cabeza-de-termo/cachivache) documentation.
24
+
25
+ ## Running the tests
26
+
27
+ - `bundle install`
28
+ - `bundle exec rspec`
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cabeza-de-termo/cachivache-helpers.
33
+
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
38
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cabeza-de-termo/cachivache-helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cachivache-helpers"
8
+ spec.version = CachivacheHelpers::VERSION
9
+ spec.authors = ["Martin Rubi"]
10
+ spec.email = ["martinrubi@gmail.com"]
11
+
12
+ spec.summary = %q{Helpers to use in Cachivache stuffs.}
13
+ spec.description = %q{Helpers to use in Cachivache stuffs.}
14
+ spec.homepage = "https://github.com/cabeza-de-termo/cachivache-helpers"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1 @@
1
+ require_relative 'version'
@@ -0,0 +1,32 @@
1
+ require 'colorize'
2
+ require 'pathname'
3
+
4
+ # Helper - Iterate all .rb files in a folder
5
+ def all_rb_files_in(folder, &block)
6
+ Dir[Pathname.new(folder) + '**/*.rb'].each do |file|
7
+ block.call(file)
8
+ end
9
+ end
10
+
11
+ def stuff_to_install(&block)
12
+ Cachivache.stuff_to_install.each do |stuff_name|
13
+ block.call stuff_name
14
+ end
15
+ end
16
+
17
+ public
18
+
19
+ ######################################
20
+ # require cachivache-helpers
21
+ ######################################
22
+
23
+ require 'cachivache-helpers'
24
+
25
+ ######################################
26
+ # Stuff Api behaviour
27
+ ######################################
28
+
29
+ include Stuff::ApiBehaviour
30
+ include Stuff::RemindersBehaviour
31
+
32
+ ShellContext.set_current( ShellExec.new(self) )
@@ -0,0 +1,17 @@
1
+ module RakeAdaptor
2
+ def stuff(task_definition, *args, &block)
3
+ task(task_definition, *args, &block)
4
+ end
5
+
6
+ def execute_shell_command(command)
7
+ sh command
8
+ end
9
+
10
+ def invoke_stuff(task_name, *args)
11
+ Rake::Task[task_name].invoke(*args)
12
+ end
13
+
14
+ def execute_stuff(task_name, *args)
15
+ Rake::Task[task_name].execute(*args)
16
+ end
17
+ end
@@ -0,0 +1,63 @@
1
+ class ShellContext
2
+ @current = nil
3
+ @reminders = []
4
+
5
+ def self.current()
6
+ @current
7
+ end
8
+
9
+ def self.set_current(shell_context)
10
+ @current = shell_context
11
+ end
12
+
13
+ def self.during_shell_context(new_shell_context, &block)
14
+ current_shell_context = @current
15
+
16
+ @current = new_shell_context
17
+
18
+ block.call
19
+
20
+ @current = current_shell_context
21
+ end
22
+
23
+ def self.reminders()
24
+ @reminders
25
+ end
26
+
27
+ def self.show_info(text)
28
+ puts '-----------------------------------------------'.red
29
+ puts text.green
30
+ puts '-----------------------------------------------'.red
31
+ end
32
+
33
+ # Instance methods
34
+
35
+ def shell(command)
36
+ raise 'Subclass responsibility'
37
+ end
38
+
39
+ def shell!(command)
40
+ raise 'Subclass responsibility'
41
+ end
42
+
43
+ def invoke(task_name, *args)
44
+ raise 'Subclass responsibility'
45
+ end
46
+
47
+ def execute(task_name, *args)
48
+ raise 'Subclass responsibility'
49
+ end
50
+
51
+ def remind_to(text)
52
+ ShellContext.reminders << text
53
+ end
54
+
55
+ def raise_validation_error(message)
56
+ show_info(message)
57
+ exit
58
+ end
59
+
60
+ def show_info(text)
61
+ ShellContext.show_info(text)
62
+ end
63
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'shell-context'
2
+
3
+ class ShellExec < ShellContext
4
+ def initialize(scope)
5
+ @scope = scope
6
+ end
7
+
8
+ def scope()
9
+ @scope
10
+ end
11
+
12
+ def shell(command)
13
+ shell! command
14
+ end
15
+
16
+ def shell!(command)
17
+ scope.execute_shell_command(command)
18
+ end
19
+
20
+ def invoke(task_name, *args)
21
+ scope.invoke_stuff(task_name, *args)
22
+ end
23
+
24
+ def execute(task_name, *args)
25
+ scope.execute_stuff(task_name, *args)
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'shell-context'
2
+
3
+ class ShellBuffer < ShellContext
4
+ def initialize(underlaying_shell_context)
5
+ @underlaying_shell_context = underlaying_shell_context
6
+ @contents = ''
7
+ end
8
+
9
+ def underlaying_shell_context()
10
+ @underlaying_shell_context
11
+ end
12
+
13
+ def contents
14
+ @contents
15
+ end
16
+
17
+ def shell(command)
18
+ contents << "\n" unless contents.empty?
19
+ contents << command
20
+ end
21
+
22
+ def shell!(command)
23
+ underlaying_shell_context.shell! command
24
+ end
25
+
26
+ def invoke(task_name, *args)
27
+ underlaying_shell_context.invoke(task_name, *args)
28
+ end
29
+
30
+ def execute(task_name, *args)
31
+ underlaying_shell_context.execute(task_name, *args)
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'shell-context'
2
+
3
+ class ShellDebugger < ShellContext
4
+ def initialize(scope)
5
+ @scope = scope
6
+ end
7
+
8
+ def scope()
9
+ @scope
10
+ end
11
+
12
+ def shell(command)
13
+ puts command.cyan
14
+ end
15
+
16
+ def shell!(command)
17
+ puts command.red
18
+ end
19
+
20
+ def invoke(task_name, *args)
21
+ scope.invoke_stuff(task_name, *args)
22
+ end
23
+
24
+ def execute(task_name, *args)
25
+ scope.execute_stuff(task_name, *args)
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'stuff-api-behaviour'
2
+
3
+ class ShellFileContext
4
+ include Stuff::ApiBehaviour
5
+
6
+ def self.with(args, &block)
7
+ new(args).evaluate_with block
8
+ end
9
+
10
+ def initialize(args)
11
+ @filename = args[:file]
12
+ end
13
+
14
+ def evaluate_with(block)
15
+ instance_eval(&block)
16
+ end
17
+
18
+ # replace pattern: "user: ''", with: "user: 'admin'"
19
+ def replace(params)
20
+ pattern = params[:pattern]
21
+ replacement = params[:with]
22
+ %Q{
23
+ sudo sed -i 's|#{pattern}|#{replacement}|g' #{@filename}
24
+ }
25
+ end
26
+
27
+ # append '[mongo]'
28
+ # append :new_line
29
+ def append(text)
30
+ text = "\n" if text == :new_line
31
+
32
+ %Q{
33
+ echo "#{text}" | sudo tee -a #{@filename}
34
+ }
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'stuff-api-behaviour'
2
+
3
+ class ShellIfContext
4
+ include Stuff::ApiBehaviour
5
+
6
+ def self.with(args, &block)
7
+ new(args).evaluate_with block
8
+ end
9
+
10
+ def initialize(args)
11
+ @guard = args[:guard]
12
+ end
13
+
14
+ def evaluate_with(block)
15
+ shell "if #{@guard}; then"
16
+
17
+ instance_eval(&block)
18
+
19
+ shell "fi"
20
+ end
21
+ end
@@ -0,0 +1,145 @@
1
+ module Stuff
2
+ module ApiBehaviour
3
+ def shell_context()
4
+ ShellContext.current
5
+ end
6
+
7
+ def invoke(task_name, *args)
8
+ shell_context.invoke(task_name, *args)
9
+ end
10
+
11
+ def execute(task_name, *args)
12
+ shell_context.execute(task_name, *args)
13
+ end
14
+
15
+ def shell(command)
16
+ shell_context.shell(command)
17
+ end
18
+
19
+ def shell!(command)
20
+ shell_context.shell!(command)
21
+ end
22
+
23
+ # Examples:
24
+ #
25
+ # shell_unless file_exists: '/bla.conf' do
26
+ # shell %Q{
27
+ # ls -la
28
+ # rm -rf bla
29
+ # }
30
+ # end
31
+ #
32
+ # shell_unless folder_exists: '/bla' do
33
+ # shell %Q{
34
+ # ls -la
35
+ # rm -rf bla
36
+ # }
37
+ # end
38
+ #
39
+ # shell_unless file: '/bla', contains: 'some regex' do
40
+ # shell %Q{
41
+ # ls -la
42
+ # rm -rf bla
43
+ # }
44
+ # end
45
+ #
46
+ # shell_unless command: 'java -version', contains: 'java 1.8' do
47
+ # shell %Q{
48
+ # ls -la
49
+ # rm -rf bla
50
+ # }
51
+ # end
52
+ #
53
+ def shell_unless(params, &block)
54
+ if params.key?(:file_exists)
55
+ shell unless_file_exists(params[:file_exists], &block)
56
+ return
57
+ end
58
+ if params.key?(:folder_exists)
59
+ shell unless_folder_exists(params[:folder_exists], &block)
60
+ return
61
+ end
62
+ if params.key?(:file) && params.key?(:contains)
63
+ shell unless_regex_in_file(params[:contains], params[:file], &block)
64
+ return
65
+ end
66
+ if params.key?(:command) && params.key?(:contains)
67
+ shell unless_regex_in_command(params[:contains], params[:command], &block)
68
+ return
69
+ end
70
+ raise 'Invalid unless option'
71
+ end
72
+
73
+ # Examples:
74
+ #
75
+ # in_file "/bla.conf" do
76
+ # shell replace pattern: "%user%", with: "admin"
77
+ #
78
+ # shell append "[cachivache]"
79
+ # shell append "path='/bla'"
80
+ # end
81
+ def in_file(filename, &block)
82
+ script = during_shell_buffer do
83
+ ShellFileContext.with(file: filename, &block)
84
+ end
85
+
86
+ shell script
87
+ end
88
+
89
+ # Examples:
90
+ #
91
+ # remind_to 'This is shown when the provision ends'
92
+ def remind_to(text)
93
+ shell_context.remind_to(text)
94
+ end
95
+
96
+ def raise_validation_error(message)
97
+ shell_context.raise_validation_error(message)
98
+ end
99
+
100
+ # Example:
101
+ #
102
+ # configure :Something do
103
+ # let(:bla) { '123' }
104
+ # let(:ble) { 'abc' }
105
+ # end
106
+ def configure(class_name, &block)
107
+ Stuff::Configuration.ensure_subclass_exists(class_name)
108
+ Stuff::Configuration.class_named(class_name).instance_eval(&block)
109
+ end
110
+
111
+ protected
112
+
113
+ def during_shell_buffer(&block)
114
+ temporary_shell_buffer = ShellBuffer.new(shell_context)
115
+
116
+ ShellContext.during_shell_context(temporary_shell_buffer, &block)
117
+
118
+ temporary_shell_buffer.contents
119
+ end
120
+
121
+ def unless_file_exists(filename, &block)
122
+ during_shell_buffer do
123
+ ShellIfContext.with(guard: "[ ! -e #{filename} ]", &block)
124
+ end
125
+ end
126
+
127
+ def unless_folder_exists(folder, &block)
128
+ during_shell_buffer do
129
+ ShellIfContext.with(guard: "[ ! -d #{folder} ]", &block)
130
+ end
131
+ end
132
+
133
+ def unless_regex_in_file(regex, filename, &block)
134
+ during_shell_buffer do
135
+ ShellIfContext.with(guard: "! grep -q \"#{regex}\" \"#{filename}\"", &block)
136
+ end
137
+ end
138
+
139
+ def unless_regex_in_command(regex, command, &block)
140
+ during_shell_buffer do
141
+ ShellIfContext.with(guard: "! #{command} | grep -q \"#{regex}\"", &block)
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,11 @@
1
+ module Stuff
2
+ module RemindersBehaviour
3
+ def show_reminders()
4
+ ShellContext.reminders.each { |alert| show_info(alert) }
5
+ end
6
+
7
+ def show_info(text)
8
+ ShellContext.show_info(text)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module CachivacheHelpers
2
+ VERSION = "0.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cachivache-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Rubi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Helpers to use in Cachivache stuffs.
42
+ email:
43
+ - martinrubi@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - cachivache-helpers.gemspec
56
+ - lib/cabeza-de-termo/cachivache-helpers/helpers.rb
57
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/helpers.rb
58
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/rake-adaptor.rb
59
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-contexts/shell-context.rb
60
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-contexts/shell-exec.rb
61
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-contexts/shell_buffer.rb
62
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-contexts/shell_debugger.rb
63
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-file-context.rb
64
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/shell-if-context.rb
65
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/stuff-api-behaviour.rb
66
+ - lib/cabeza-de-termo/cachivache-helpers/utilities/stuff-reminders-behaviour.rb
67
+ - lib/cabeza-de-termo/cachivache-helpers/version.rb
68
+ homepage: https://github.com/cabeza-de-termo/cachivache-helpers
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.6
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Helpers to use in Cachivache stuffs.
92
+ test_files: []