kaigara 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/sysops/operation.rb +3 -138
- data/app/models/sysops/package.rb +1 -1
- data/lib/kaigara/dsl.rb +151 -0
- data/lib/kaigara/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f61a93c89d4acea9db654743edc918fac14d447
|
4
|
+
data.tar.gz: 89247b9ac7f03dd1c0e4f12afc2b5cdb82e54a63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2b560e4a98687e8de3c03e3c237d77b262acef14f3028049801f01b65344a3c71d6368ba46f81b2285c43be4ec60bd5b84104d6debb7ac9f1d114379b8075cf
|
7
|
+
data.tar.gz: 43f09bacbe74743538c722e50cc7f3174a4933fdc9bdb4a865dc3dae4f0d8a017c6a8695afe6cc165dad6e214a33108c3d0113b2e062435d136fbec748084244
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'open3'
|
2
|
+
require 'kaigara/dsl'
|
2
3
|
|
3
4
|
module Kaigara
|
4
5
|
|
@@ -6,6 +7,7 @@ module Kaigara
|
|
6
7
|
# A script that +Sysops+ executes
|
7
8
|
#
|
8
9
|
class Operation
|
10
|
+
include Kaigara::DSL
|
9
11
|
|
10
12
|
#
|
11
13
|
# A proxy class for Thor
|
@@ -24,8 +26,7 @@ module Kaigara
|
|
24
26
|
@shell = ThorShell.new
|
25
27
|
@name = File.basename(path)
|
26
28
|
@content = File.read(path)
|
27
|
-
Environment.load_variables(self) # We loads variables to both shell and operation classes.
|
28
|
-
Environment.load_variables(@shell) # So it's available from your ruby code in +operations/+ and from our DSL.
|
29
|
+
Environment.load_variables(self) # We loads variables to both shell and operation classes. So it's available from your ruby code in +operations/+ and from our DSL.
|
29
30
|
end
|
30
31
|
|
31
32
|
# Executes operation content
|
@@ -33,142 +34,6 @@ module Kaigara
|
|
33
34
|
@shell.say "Applying #{@name}\n--------------------", :yellow
|
34
35
|
instance_eval @content
|
35
36
|
end
|
36
|
-
|
37
|
-
# One of the most important parts of DSL. You can use it directly in your operations.
|
38
|
-
def execute(cmd)
|
39
|
-
@shell.say "Running: #{cmd}", :yellow
|
40
|
-
stdin, stdout, stderr, wait_thr = Open3.popen3({}, "bash -e")
|
41
|
-
stdin.puts(cmd)
|
42
|
-
Thread.new do
|
43
|
-
stdout.each { |l| @shell.say(l, :green) }
|
44
|
-
end
|
45
|
-
Thread.new do
|
46
|
-
stderr.each { |l| @shell.say(l, :red) }
|
47
|
-
end
|
48
|
-
stdin.close
|
49
|
-
|
50
|
-
exit_status = wait_thr.value.exitstatus
|
51
|
-
if exit_status != 0
|
52
|
-
raise "Command `#{ cmd }` returned status code #{ exit_status }"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
#
|
57
|
-
# Return true if the `file` exists? and the content matches the `match`
|
58
|
-
# If a block is given it's executed if the statement is true
|
59
|
-
#
|
60
|
-
def file_matches?(file, match)
|
61
|
-
if File.exists?(file)
|
62
|
-
if File.read(file).match(match)
|
63
|
-
yield if block_given?
|
64
|
-
return true
|
65
|
-
end
|
66
|
-
end
|
67
|
-
false
|
68
|
-
end
|
69
|
-
|
70
|
-
#
|
71
|
-
# Return true if the OS is Ubuntu or Debian
|
72
|
-
# If a block is given it's executed if the statement is true
|
73
|
-
#
|
74
|
-
def debian_family?
|
75
|
-
file_matches?("/etc/issue", /Ubuntu|Debian/i) do
|
76
|
-
yield if block_given?
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
#
|
81
|
-
# Return true if the OS is CentOS or RedHat
|
82
|
-
# If a block is given it's executed if the statement is true
|
83
|
-
#
|
84
|
-
def redhat_family?
|
85
|
-
file_matches?("/etc/redhat-release", /CentOS|Red Hat/i) do
|
86
|
-
yield if block_given?
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
#
|
91
|
-
# Add common additional repositories
|
92
|
-
#
|
93
|
-
def repo_extended
|
94
|
-
redhat_family? do
|
95
|
-
package("epel-release")
|
96
|
-
package_update
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
#
|
101
|
-
# Update the local repository cache
|
102
|
-
#
|
103
|
-
def package_update
|
104
|
-
if debian_family?
|
105
|
-
execute("apt-get update")
|
106
|
-
elsif redhat_family?
|
107
|
-
execute("yum update")
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
#
|
112
|
-
# Install the package depending on OS
|
113
|
-
#
|
114
|
-
def package(name, version = nil)
|
115
|
-
if debian_family?
|
116
|
-
execute("apt-get install -y #{ name }")
|
117
|
-
elsif redhat_family?
|
118
|
-
execute("yum install -y #{ name }")
|
119
|
-
else
|
120
|
-
raise "OS not supported"
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
#
|
125
|
-
# Creates a file with the specified content
|
126
|
-
#
|
127
|
-
def file(filepath, content, opts = {overwrite: true})
|
128
|
-
if !opts[:overwrite] and File.exists?(filepath)
|
129
|
-
raise "File #{filepath} exists"
|
130
|
-
end
|
131
|
-
|
132
|
-
File.open(filepath, "w") do |fd|
|
133
|
-
fd.write(content)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
#
|
138
|
-
# Configure the hostname of the machine
|
139
|
-
#
|
140
|
-
def hostname(hostname)
|
141
|
-
file("/etc/hostname", hostname)
|
142
|
-
end
|
143
|
-
|
144
|
-
#
|
145
|
-
# Templates ERB template. You can use variables from metadata.rb in your templates.
|
146
|
-
# <tt>name</tt> - template name, without '.erb'
|
147
|
-
# <tt>-p</tt> - destination file. If you don't use it, the file renders to +/path-in-resources/+
|
148
|
-
#
|
149
|
-
def template(name, target = nil)
|
150
|
-
tpl_file = name + '.erb'
|
151
|
-
destination = target
|
152
|
-
destination = "/#{tpl_file}" if destination.nil?
|
153
|
-
destination.gsub!(/\.erb$/,'')
|
154
|
-
|
155
|
-
@shell.say "Rendering template #{tpl_file} to #{destination}", :yellow
|
156
|
-
ThorShell.source_root(File.join(@work_dir, 'resources'))
|
157
|
-
@shell.template(tpl_file, destination)
|
158
|
-
|
159
|
-
return destination
|
160
|
-
end
|
161
|
-
|
162
|
-
#
|
163
|
-
# Renders a template, then executes the script.
|
164
|
-
# You should add shebang to your script.
|
165
|
-
#
|
166
|
-
def script(name, path = nil)
|
167
|
-
target = template(name, File.join( (path.nil? ? "" : path), name))
|
168
|
-
@shell.chmod(target, 0755)
|
169
|
-
target.prepend('./') unless target.match(/^\//)
|
170
|
-
execute("#{target}")
|
171
|
-
end
|
172
37
|
end
|
173
38
|
end
|
174
39
|
|
@@ -50,7 +50,7 @@ module Kaigara
|
|
50
50
|
|
51
51
|
# Execute operations in the operations directory one by one
|
52
52
|
def run!(operations)
|
53
|
-
Dir[File.join(@operations_dir, '*.rb')].each do |x|
|
53
|
+
Dir[File.join(@operations_dir, '*.rb')].sort.each do |x|
|
54
54
|
if operations.empty? or operations.find { |op| x.include?(op) }
|
55
55
|
execute_operation!(x)
|
56
56
|
end
|
data/lib/kaigara/dsl.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
module Kaigara
|
2
|
+
module DSL
|
3
|
+
include Thor::Base
|
4
|
+
include Thor::Actions
|
5
|
+
include Thor::Shell
|
6
|
+
|
7
|
+
def self.included(receiver)
|
8
|
+
receiver.send :include, Thor::Actions
|
9
|
+
receiver.send :include, Thor::Base
|
10
|
+
receiver.send :include, Thor::Shell
|
11
|
+
end
|
12
|
+
|
13
|
+
# One of the most important parts of DSL. You can use it directly in your operations.
|
14
|
+
def execute(cmd)
|
15
|
+
Environment.load_variables(self)
|
16
|
+
say "Running: #{cmd}", :yellow
|
17
|
+
stdin, stdout, stderr, wait_thr = Open3.popen3({}, "bash -e")
|
18
|
+
stdin.puts(cmd)
|
19
|
+
Thread.new do
|
20
|
+
stdout.each { |l| say(l, :green) }
|
21
|
+
end
|
22
|
+
Thread.new do
|
23
|
+
stderr.each { |l| say(l, :red) }
|
24
|
+
end
|
25
|
+
stdin.close
|
26
|
+
|
27
|
+
exit_status = wait_thr.value.exitstatus
|
28
|
+
if exit_status != 0
|
29
|
+
raise "Command `#{ cmd }` returned status code #{ exit_status }"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Templates ERB template. You can use variables from metadata.rb in your templates.
|
35
|
+
# <tt>name</tt> - template name, without '.erb'
|
36
|
+
# <tt>-p</tt> - destination file. If you don't use it, the file renders to +/path-in-resources/+
|
37
|
+
#
|
38
|
+
def template(source, target)
|
39
|
+
Environment.load_variables(self)
|
40
|
+
tpl_file = 'resources/' + source + '.erb'
|
41
|
+
destination = target
|
42
|
+
destination = "/#{tpl_file}" if destination.nil?
|
43
|
+
destination.gsub!(/\.erb$/,'')
|
44
|
+
context = instance_eval("binding")
|
45
|
+
|
46
|
+
say "Rendering template #{tpl_file} to #{destination}", :yellow
|
47
|
+
File.write(destination, ERB.new(File.read(tpl_file), nil, "-", "@output_buffer").result(context))
|
48
|
+
|
49
|
+
return destination
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Renders a template, then executes the script.
|
54
|
+
# You should add shebang to your script.
|
55
|
+
#
|
56
|
+
def script(name, path = nil)
|
57
|
+
target = template(name, File.join( (path.nil? ? "" : path), name))
|
58
|
+
File.chmod(0755, target)
|
59
|
+
target.prepend('./') unless target.match(/^\//)
|
60
|
+
execute("#{target}")
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Return true if the `file` exists? and the content matches the `match`
|
65
|
+
# If a block is given it's executed if the statement is true
|
66
|
+
#
|
67
|
+
def file_matches?(file, match)
|
68
|
+
if File.exists?(file)
|
69
|
+
if File.read(file).match(match)
|
70
|
+
yield if block_given?
|
71
|
+
return true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
false
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Return true if the OS is Ubuntu or Debian
|
79
|
+
# If a block is given it's executed if the statement is true
|
80
|
+
#
|
81
|
+
def debian_family?
|
82
|
+
file_matches?("/etc/issue", /Ubuntu|Debian/i) do
|
83
|
+
yield if block_given?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Return true if the OS is CentOS or RedHat
|
89
|
+
# If a block is given it's executed if the statement is true
|
90
|
+
#
|
91
|
+
def redhat_family?
|
92
|
+
file_matches?("/etc/redhat-release", /CentOS|Red Hat/i) do
|
93
|
+
yield if block_given?
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# Add common additional repositories
|
99
|
+
#
|
100
|
+
def repo_extended
|
101
|
+
redhat_family? do
|
102
|
+
package("epel-release")
|
103
|
+
package_update
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# Update the local repository cache
|
109
|
+
#
|
110
|
+
def package_update
|
111
|
+
if debian_family?
|
112
|
+
execute("apt-get update")
|
113
|
+
elsif redhat_family?
|
114
|
+
execute("yum update -y")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Install the package depending on OS
|
120
|
+
#
|
121
|
+
def package(name, version = nil)
|
122
|
+
if debian_family?
|
123
|
+
execute("apt-get install -y #{ name }")
|
124
|
+
elsif redhat_family?
|
125
|
+
execute("yum install -y #{ name }")
|
126
|
+
else
|
127
|
+
raise "OS not supported"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
#
|
132
|
+
# Creates a file with the specified content
|
133
|
+
#
|
134
|
+
def file(filepath, content, opts = {overwrite: true})
|
135
|
+
if !opts[:overwrite] and File.exists?(filepath)
|
136
|
+
raise "File #{filepath} exists"
|
137
|
+
end
|
138
|
+
|
139
|
+
File.open(filepath, "w") do |fd|
|
140
|
+
fd.write(content)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
#
|
145
|
+
# Configure the hostname of the machine
|
146
|
+
#
|
147
|
+
def hostname(hostname)
|
148
|
+
file("/etc/hostname", hostname)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
data/lib/kaigara/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kaigara
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Helios Technologies
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/kaigara/base_controller.rb
|
117
117
|
- lib/kaigara/client.rb
|
118
118
|
- lib/kaigara/deephash.rb
|
119
|
+
- lib/kaigara/dsl.rb
|
119
120
|
- lib/kaigara/metadata.rb
|
120
121
|
- lib/kaigara/version.rb
|
121
122
|
homepage: https://github.com/helios-technologies/kaigara
|