cachivache 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/rake-helper'
2
+
3
+ raise_validation_error "Please define the parameter Cachivache.git_user_email in './cachivache.rb' run 'vagrant provision' again" unless Cachivache.is_defined?(:git_user_email)
4
+ raise_validation_error "Please define the parameter Cachivache.git_user_name in './cachivache.rb' run 'vagrant provision' again" unless Cachivache.is_defined?(:git_user_name)
5
+ raise_validation_error "Please define some Cachivache.stuff_to_install in './cachivache.rb' run 'vagrant provision' again" if Cachivache.stuff_to_install.empty?
6
+
7
+ def install_cachivache_stuff_with(&block)
8
+ Cachivache.stuff_to_install.each do |stuff_name|
9
+ show_info "Provisioning #{stuff_name}"
10
+
11
+ block.call stuff_name
12
+ end
13
+
14
+ show_info "All done!!!"
15
+ end
16
+
17
+ stuff :provision do
18
+ install_cachivache_stuff_with do |stuff_name|
19
+ invoke stuff_name
20
+ end
21
+ end
22
+
23
+ stuff :explain do
24
+ install_cachivache_stuff_with do |stuff_name|
25
+ explained = during_shell_buffer { invoke stuff_name }
26
+ puts explained.green
27
+ end
28
+ end
29
+
30
+ stuff default: :provision
31
+
32
+ at_exit do
33
+ show_reminders
34
+ end
@@ -0,0 +1,70 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ require_relative 'cachivache'
5
+
6
+ Vagrant.configure(2) do |config|
7
+ # Every Vagrant development environment requires a box. You can search for
8
+ # boxes at https://atlas.hashicorp.com/search.
9
+ config.vm.box = "ubuntu/trusty32"
10
+
11
+ # Create a private network, which allows host-only access to the machine
12
+ # using a specific IP.
13
+ config.vm.network "private_network", ip: Cachivache.vagrant_ip_address
14
+
15
+ # Map the source folder
16
+ config.vm.synced_folder "../", Cachivache.src_folder
17
+
18
+ # Map the host ~/.ssh folder
19
+ config.vm.synced_folder "~/.ssh", Cachivache.ssh_keys_folder
20
+
21
+ # Provider-specific configuration so you can fine-tune various
22
+ # backing providers for Vagrant. These expose provider-specific options.
23
+
24
+ config.vm.provider "virtualbox" do |vb|
25
+ # Display the VirtualBox GUI when booting the machine
26
+ vb.gui = Cachivache.wants_gui?
27
+
28
+ # Customize the amount of memory on the VM:
29
+ vb.memory = Cachivache.wants_gui? ? "4096" : "1024"
30
+ end
31
+
32
+ # Enable provisioning with a shell script. Additional provisioners such as
33
+ # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
34
+ # documentation for more information about their specific syntax and use.
35
+
36
+ # Provisioning the VM with the custom provision-scripts
37
+
38
+ config.vm.provision "rbenv",
39
+ type: "shell",
40
+ privileged: false,
41
+ inline: %Q{
42
+ # From https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-14-04
43
+ if [ ! -d $HOME/.rbenv/ ]; then
44
+
45
+ sudo apt-get install git-core -y
46
+
47
+ git clone git://github.com/sstephenson/rbenv.git .rbenv
48
+ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
49
+ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
50
+
51
+ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
52
+ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
53
+ source ~/.bash_profile
54
+
55
+ git clone git://github.com/carsomyr/rbenv-bundler.git ~/.rbenv/plugins/bundler
56
+
57
+ sudo gem install bundler
58
+ fi
59
+ }
60
+
61
+ config.vm.provision "rake-provision",
62
+ type: "shell",
63
+ privileged: false,
64
+ inline: %Q{
65
+ cd #{Cachivache.cachivache_folder}
66
+
67
+ bundle install
68
+ rake provision
69
+ }
70
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'lib/stuff-configuration'
2
+
3
+ class Cachivache < Stuff::Configuration
4
+ # Vagrant params
5
+
6
+ let(:vagrant_ip_address) { '192.168.0.101' }
7
+
8
+ let(:home_folder) { '/home/vagrant' }
9
+
10
+ let(:src_folder) { home_folder + '/src' }
11
+ let(:ssh_keys_folder) { home_folder + '/ssh-keys' }
12
+ let(:bin_folder) { home_folder + '/bin-cachivache' }
13
+
14
+ # Cachivache folders
15
+
16
+ let(:cachivache_folder) { src_folder + '/cachivache' }
17
+ let(:stuff_library_folder) { cachivache_folder + '/stuff-library' }
18
+ let(:stuff_folder) { cachivache_folder + '/stuff' }
19
+
20
+ let(:wants_gui?) { false }
21
+
22
+ # Developer params
23
+
24
+ ####################################
25
+ # Define what stuff to install
26
+ ####################################
27
+
28
+ let(:stuff_to_install) {
29
+ [
30
+ # Put the stuff you want to install here
31
+ ]
32
+ }
33
+
34
+ ####################################
35
+ # Define your git user
36
+ ####################################
37
+
38
+ let(:git_user_email) { }
39
+ let(:git_user_name) { }
40
+
41
+ ####################################
42
+ # Define your git credentials
43
+ ####################################
44
+
45
+ let(:git_credentials) { %Q{
46
+ host github.com
47
+ HostName github.com
48
+ IdentityFile #{Cachivache.ssh_keys_folder}/id_rsa
49
+ User git
50
+
51
+ host bitbucket.org
52
+ HostName bitbucket.org
53
+ IdentityFile #{Cachivache.ssh_keys_folder}/id_rsa
54
+ User git
55
+ }
56
+ }
57
+ end
@@ -0,0 +1,50 @@
1
+ require 'rake'
2
+ require 'colorize'
3
+ require 'pathname'
4
+
5
+ require_relative '../cachivache'
6
+
7
+ # Helper - Iterate all .rb files in a folder
8
+ def all_rb_files_in(folder, &block)
9
+ Dir[Pathname.new(folder) + '**/*.rb'].each do |file|
10
+ block.call(file)
11
+ end
12
+ end
13
+
14
+ public
15
+
16
+ ######################################
17
+ # require all the files in /lib first
18
+ ######################################
19
+
20
+ all_rb_files_in Cachivache.cachivache_folder + '/lib' do |file|
21
+ require file
22
+ end
23
+
24
+ ######################################
25
+ # Stuff Api behaviour
26
+ ######################################
27
+
28
+ # Raname :task just for fun
29
+ def stuff(*args, &block)
30
+ task(*args, &block)
31
+ end
32
+
33
+ def execute_shell_command(command)
34
+ sh command
35
+ end
36
+
37
+ include Stuff::ApiBehaviour
38
+ include Stuff::RemindersBehaviour
39
+
40
+ ShellContext.set_current( ShellExec.new(self) )
41
+
42
+ ######################################
43
+ # Load the rest of the files
44
+ ######################################
45
+
46
+ all_rb_files_in Cachivache.cachivache_folder do |file|
47
+ Stuff::Configuration.creating_undefined_subclasses do
48
+ require file
49
+ end
50
+ end
@@ -0,0 +1,54 @@
1
+ class ShellContext
2
+ @current = nil
3
+
4
+ def self.current()
5
+ @current
6
+ end
7
+
8
+ def self.set_current(shell_context)
9
+ @current = shell_context
10
+ end
11
+
12
+ def self.during_shell_context(new_shell_context, &block)
13
+ current_shell_context = @current
14
+
15
+ @current = new_shell_context
16
+
17
+ block.call
18
+
19
+ @current = current_shell_context
20
+ end
21
+
22
+ def initialize(scope)
23
+ @scope = scope
24
+ end
25
+
26
+ def scope()
27
+ @scope
28
+ end
29
+
30
+ def shell(command)
31
+ raise 'Subclass responsibility'
32
+ end
33
+
34
+ def shell!(command)
35
+ scope.execute_shell_command(command)
36
+ end
37
+
38
+ def invoke(task_name, *args)
39
+ Rake::Task[task_name].invoke(*args)
40
+ end
41
+
42
+ def execute(task_name, *args)
43
+ Rake::Task[task_name].execute(*args)
44
+ end
45
+
46
+ def remind_to(text)
47
+ reminders << text
48
+ end
49
+
50
+ def raise_validation_error(message)
51
+ show_info(message)
52
+ exit
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'shell-context'
2
+
3
+ class ShellExec < ShellContext
4
+ def shell(command)
5
+ shell! command
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'shell-context'
2
+
3
+ class ShellBuffer < ShellContext
4
+ def initialize(*args)
5
+ super(*args)
6
+
7
+ @contents = ''
8
+ end
9
+
10
+ def contents
11
+ @contents
12
+ end
13
+
14
+ def shell(command)
15
+ contents << "\n" unless contents.empty?
16
+ contents << command
17
+ end
18
+ 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,134 @@
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
+ protected
101
+
102
+ def during_shell_buffer(&block)
103
+ temporary_shell_buffer = ShellBuffer.new(shell_context.scope)
104
+
105
+ ShellContext.during_shell_context(temporary_shell_buffer, &block)
106
+
107
+ temporary_shell_buffer.contents
108
+ end
109
+
110
+ def unless_file_exists(filename, &block)
111
+ during_shell_buffer do
112
+ ShellIfContext.with(guard: "[ ! -e #{filename} ]", &block)
113
+ end
114
+ end
115
+
116
+ def unless_folder_exists(folder, &block)
117
+ during_shell_buffer do
118
+ ShellIfContext.with(guard: "[ ! -d #{folder} ]", &block)
119
+ end
120
+ end
121
+
122
+ def unless_regex_in_file(regex, filename, &block)
123
+ during_shell_buffer do
124
+ ShellIfContext.with(guard: "! grep -q \"#{regex}\" \"#{filename}\"", &block)
125
+ end
126
+ end
127
+
128
+ def unless_regex_in_command(regex, command, &block)
129
+ during_shell_buffer do
130
+ ShellIfContext.with(guard: "! #{command} | grep -q \"#{regex}\"", &block)
131
+ end
132
+ end
133
+ end
134
+ end