vana 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73bd39d513e59438a74e818bc8f0104e162cad03
4
+ data.tar.gz: bc0eeb84d58fca4b421d3b006e74637d50852282
5
+ SHA512:
6
+ metadata.gz: f24f390aa8b0dbae0b5c82920d7b8a0e760ba91f23b0f23991294ff38b493d8bb62b7378f73b2b6416e69d4612455cdddea176fe576eaffc2665825f0d621170
7
+ data.tar.gz: bb4636d1da809c141504ed5c43112e658cabccec4616db30e8bf144f6c080ffd6778db2cd54574eaa351625e0000dc01d62fd24abb13b5628781afafe90451d1
data/lib/vana.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'vana/main.rb'
2
+
3
+ require 'vana/element.rb'
4
+ require 'vana/element/copy.rb'
5
+ require 'vana/element/shell.rb'
6
+
7
+ extend Vana
@@ -0,0 +1,41 @@
1
+ require 'colorize'
2
+ require 'json'
3
+
4
+ module Vana
5
+ module Element
6
+ # methods for every element
7
+ class BaseElement
8
+ def initialize(hosts, *args)
9
+ @hosts = hosts
10
+ @element_opts = {}
11
+ @opts = {}
12
+
13
+ yield self if block_given?
14
+ setup(*args)
15
+ end
16
+
17
+ def setup(*_args)
18
+ # nop
19
+ end
20
+
21
+ def action(_host, *_args)
22
+ # nop
23
+ end
24
+
25
+ def name=(name)
26
+ @element_opts[:name] = name
27
+ end
28
+
29
+ def execute(*args)
30
+ puts "-- #{@element_opts[:name]}"
31
+ @hosts.each do |host|
32
+ output = action(host, *args)
33
+ puts "#{host}: #{output.to_json}".colorize(output[:success] ? :light_green : :light_red)
34
+
35
+ # TODO: need a better place to handle exceptions and keep going with the script
36
+ # raise "Execution failed on '#{@element_opts[:name]}'" unless output[:success]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,71 @@
1
+ require 'fileutils'
2
+ require 'net/scp'
3
+
4
+ module Vana
5
+ # vana/main.rb
6
+ class Hosts
7
+ def copy(*args, &block)
8
+ Vana::Element::Copy.new(@hosts, *args, &block).execute
9
+ end
10
+ end
11
+
12
+ module Element
13
+ # copy element
14
+ class Copy < BaseElement
15
+ def setup(*args)
16
+ @opts[:src] ||= args[0]
17
+ @opts[:dest] ||= args[1]
18
+
19
+ @element_opts[:name] ||= "copy #{@opts[:src]} to #{@opts[:dest]}"
20
+ end
21
+
22
+ def src=(src)
23
+ @opts[:src] = src
24
+ end
25
+
26
+ def dest=(dest)
27
+ @opts[:dest] = dest
28
+ end
29
+
30
+ def remote_src=(remote_src)
31
+ @opts[:remote_src] = remote_src
32
+ end
33
+
34
+ def preserve_time=(preserve_time)
35
+ @opts[:preserve_time] = preserve_time
36
+ end
37
+
38
+ def action(host, *_args)
39
+ if host == 'localhost'
40
+ begin
41
+ FileUtils.cp_r(@opts[:src], @opts[:dest], preserve: @opts[:preserve_time])
42
+ success = true
43
+ rescue Errno::ENOENT
44
+ success = false
45
+ end
46
+ {
47
+ src: @opts[:src],
48
+ dest: @opts[:dest],
49
+ success: success
50
+ }
51
+ elsif @opts[:remote_src]
52
+ # TODO
53
+ else
54
+ begin
55
+ Net::SCP.start(host, nil) do |scp|
56
+ scp.upload!(@opts[:src], @opts[:dest], recursive: true, preserve: @opts[:preserve_time])
57
+ end
58
+ success = true
59
+ rescue Net::SCP::Error
60
+ success = false
61
+ end
62
+ {
63
+ src: @opts[:src],
64
+ dest: @opts[:dest],
65
+ success: success
66
+ }
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,67 @@
1
+ require 'net/ssh'
2
+ require 'open3'
3
+
4
+ module Vana
5
+ # vana/main.rb
6
+ class Hosts
7
+ def shell(*args, &block)
8
+ Vana::Element::Shell.new(@hosts, *args, &block).execute
9
+ end
10
+ end
11
+
12
+ module Element
13
+ # shell element
14
+ class Shell < BaseElement
15
+ def setup(*args)
16
+ @opts[:cmd] ||= args.first
17
+ @element_opts[:name] ||= @opts[:cmd]
18
+ end
19
+
20
+ def cmd=(cmd)
21
+ @opts[:cmd] = cmd
22
+ end
23
+
24
+ def action(host, *_args)
25
+ if host == 'localhost'
26
+ stdout, stderr, status = Open3.capture3(@opts[:cmd])
27
+ {
28
+ cmd: @opts[:cmd],
29
+ stdout: stdout,
30
+ stderr: stderr,
31
+ success: status.success?,
32
+ status: status.exitstatus,
33
+ pid: status.pid
34
+ }
35
+ else
36
+ Net::SSH.start(host) do |ssh|
37
+ output = {
38
+ cmd: @opts[:cmd],
39
+ stdout: '',
40
+ stderr: ''
41
+ }
42
+
43
+ ssh.open_channel do |channel|
44
+ channel.exec(@opts[:cmd]) do |_ch, _success|
45
+ # stdout
46
+ channel.on_data { |_ch, data| output[:stdout] << data }
47
+
48
+ # stderr
49
+ channel.on_extended_data { |_ch, data| output[:stderr] << data }
50
+
51
+ # exit status
52
+ channel.on_request('exit-status') do |_ch, data|
53
+ output[:status] = data.read_long
54
+ output[:success] = output[:status].zero?
55
+ end
56
+ end
57
+ end
58
+
59
+ ssh.loop
60
+
61
+ output
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
data/lib/vana/main.rb ADDED
@@ -0,0 +1,19 @@
1
+ # vana, approved by servers everywhere
2
+ module Vana
3
+ # hosts block
4
+ class Hosts
5
+ def initialize(*a, &b)
6
+ @hosts = *a
7
+ @b = b
8
+ end
9
+
10
+ def execute
11
+ instance_eval(&@b)
12
+ end
13
+ end
14
+
15
+ def hosts(*a, &b)
16
+ h = Hosts.new(*a, &b)
17
+ h.execute
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vana
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brik Royster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: dank emoji
14
+ email: brik@butthole.tv
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/vana.rb
20
+ - lib/vana/element.rb
21
+ - lib/vana/element/copy.rb
22
+ - lib/vana/element/shell.rb
23
+ - lib/vana/main.rb
24
+ homepage: http://butthole.tv
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: ansible but ruby
48
+ test_files: []