vycap 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 468dd254bbfa3f5b55211230db1b4993a8ae8ad9
4
+ data.tar.gz: 0fd71f11753e45272bc0825d5e245e7133a6be27
5
+ SHA512:
6
+ metadata.gz: 90f534638282176ea44da1ea2569ea407d5f3ca40d6394359f0c4faac449fa816b0455a302055a6a9221024c890ea4849329ed0339b622a6a71c354c40558b3e
7
+ data.tar.gz: 31cc09da24aa8e6461a9fbe2b324dcd563d55069afa01c577a42e9d4b83f6db9c174bf346f8c507c3f43401c097fd46458debec104bacc4b1eeb0bf99aabd61e
@@ -0,0 +1,5 @@
1
+ require 'supply_drop'
2
+ require File.expand_path(File.dirname(__FILE__) + "/vycap/tasks")
3
+ require File.expand_path(File.dirname(__FILE__) + "/vycap/plugin")
4
+
5
+ Capistrano.plugin :vycap_plugin, Vycap::Plugin
@@ -0,0 +1,90 @@
1
+ require 'erb'
2
+ require 'ostruct'
3
+
4
+ module Vycap
5
+ module Plugin
6
+ def generate_config(server, options)
7
+ template_filename = File.join(
8
+ options.fetch(:template_dir),
9
+ options.fetch(:template)
10
+ )
11
+ output_filename = File.join(
12
+ options.fetch(:output_dir),
13
+ "config.#{server}"
14
+ )
15
+ template_filename += ".erb" unless template_filename =~ /\.erb$/
16
+
17
+ context = if options[:context].nil?
18
+ OpenStruct.new
19
+ elsif options[:context].respond_to?(:call)
20
+ OpenStruct.new(options[:context].call(server))
21
+ else
22
+ OpenStruct.new(options[:context])
23
+ end
24
+
25
+ context.partials = evaluate_partials(options.fetch(:partials_dir), context)
26
+
27
+ erb = File.read(template_filename)
28
+ File.open(output_filename, "w") do |out|
29
+ out.write ERB.new(erb).result(context.instance_eval { binding })
30
+ end
31
+ end
32
+
33
+ def evaluate_partials(partials_dir, context)
34
+ return @vycap_partials unless @vycap_partials.nil?
35
+
36
+ @vycap_partials = {}
37
+
38
+ Dir["#{partials_dir}/*.erb"].sort.each do |file|
39
+ erb = File.read(file)
40
+
41
+ @vycap_partials[File.basename(file, ".erb")] = ERB.new(erb).result(context.instance_eval { binding })
42
+ end
43
+
44
+ @vycap_partials
45
+ end
46
+
47
+ def vcmd(cmd)
48
+ "/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper #{cmd}"
49
+ end
50
+
51
+ def vrun(*cmds)
52
+ cmds = cmds.flatten
53
+ if cmds.last.is_a?(Hash)
54
+ options = cmds.pop
55
+ else
56
+ options = {}
57
+ end
58
+
59
+ vcmds = cmds.map do |cmd|
60
+ vcmd(cmd)
61
+ end
62
+
63
+ sudo_prefix = options.fetch(:sudo, false) ? sudo : ""
64
+
65
+ run %Q{#{sudo_prefix} vbash -c '#{vcmds.join(" && ")}'; true}, :eof => true, :shell => false do |ch, stream, data|
66
+ case stream
67
+ when :out
68
+ if data =~ /^enter .* password:$/i
69
+ password = Capistrano::CLI.password_prompt(data + " ")
70
+ ch.send_data(password + "\n")
71
+ elsif data =~ /is not valid/ || data =~ /failed/
72
+ puts _colorize(data, "\033[31m")
73
+ else
74
+ puts data
75
+ end
76
+ when :err then warn "[err :: #{ch[:server]}] #{data}"
77
+ end
78
+ end
79
+ end
80
+
81
+ def vsudo(*cmds)
82
+ vrun cmds, :sudo => true
83
+ end
84
+
85
+ # borrowed from http://www.codedrop.ca/blog/archives/200
86
+ def _colorize(text, color_code)
87
+ "#{color_code}#{text}\033[0m"
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,116 @@
1
+ Capistrano::Configuration.instance.load do
2
+ set :vycap_template_dir, File.expand_path("./vycap/templates/")
3
+ set :vycap_partials_dir, vycap_template_dir + "/partials"
4
+ set :vycap_user, nil
5
+ set :vycap_remote_config, "/config/config.vycap"
6
+ set :vycap_tmp_dir, "/tmp"
7
+ set :vycap_template, "config"
8
+ set :vycap_template_context, {}
9
+ set :vycap_auth_dir, ""
10
+ set :vycap_remote_auth_dir, "/config/auth/vycap/"
11
+
12
+ namespace :vycap do
13
+ task :download_config_template do
14
+ failed_servers = roles[:vyatta].servers.map do |srv|
15
+ remote_file = SupplyDrop::Rsync.remote_address(vycap_user, srv.host, "/config/config.boot")
16
+ cmd = SupplyDrop::Rsync.command(
17
+ remote_file,
18
+ vycap_template_dir + "/config.#{srv}.erb",
19
+ :ssh => ssh_options
20
+ )
21
+ srv unless system cmd
22
+ end
23
+
24
+ raise "rsync failed on #{failed_servers.inspect}" if failed_servers.any?
25
+
26
+ roles[:vyatta].servers.map do |srv|
27
+ config_template = vycap_template_dir + "/config.#{srv}.erb"
28
+
29
+ original = File.read(config_template)
30
+ File.open(config_template, "w") do |file|
31
+ original.split("\n").each do |line|
32
+ file.puts line unless line =~ /\bhw-id\b/
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ namespace :upload do
39
+ task :default do
40
+ vycap.upload.config
41
+ vycap.upload.auth_files unless vycap_auth_dir == ""
42
+ end
43
+
44
+ task :config do
45
+ failed_servers = roles[:vyatta].servers.map do |srv|
46
+ vycap_plugin.generate_config(srv, {
47
+ :template => vycap_template,
48
+ :template_dir => vycap_template_dir,
49
+ :partials_dir => vycap_partials_dir,
50
+ :output_dir => vycap_tmp_dir,
51
+ :context => vycap_template_context
52
+ })
53
+
54
+ remote_file = SupplyDrop::Rsync.remote_address(vycap_user, srv.host, vycap_remote_config)
55
+
56
+ cmd = SupplyDrop::Rsync.command(
57
+ File.join(vycap_tmp_dir, "config.#{srv}"),
58
+ remote_file,
59
+ :ssh => ssh_options
60
+ )
61
+ srv unless system cmd
62
+ end
63
+
64
+ raise "rsync failed on #{failed_servers.inspect}" if failed_servers.any?
65
+
66
+ end
67
+
68
+ task :auth_files do
69
+ set :user, vycap_user unless vycap_user.nil?
70
+ failed_servers = roles[:vyatta].servers.map do |srv|
71
+ remote_file = SupplyDrop::Rsync.remote_address(vycap_user, srv.host, vycap_remote_auth_dir)
72
+ auth_dir = vycap_auth_dir
73
+
74
+ remote_file += "/" unless remote_file =~ /\/$/
75
+ auth_dir += "/" unless auth_dir =~ /\/$/
76
+
77
+
78
+ cmd = SupplyDrop::Rsync.command(
79
+ auth_dir,
80
+ remote_file,
81
+ :ssh => ssh_options
82
+ )
83
+ success = system cmd
84
+ run("chmod -R go-rw #{vycap_remote_auth_dir}")
85
+ srv unless success
86
+ end
87
+
88
+ raise "rsync failed on #{failed_servers.inspect}" if failed_servers.any?
89
+ end
90
+ end
91
+
92
+ namespace :diff do
93
+ task :show do
94
+ set :user, vycap_user unless vycap_user.nil?
95
+ vycap_plugin.vsudo(
96
+ "begin",
97
+ "load #{vycap_remote_config}",
98
+ 'show | grep -C 3 -E "^[+->]"',
99
+ "discard",
100
+ "end"
101
+ )
102
+ end
103
+
104
+ task :apply do
105
+ set :user, vycap_user unless vycap_user.nil?
106
+ vycap_plugin.vsudo(
107
+ "begin",
108
+ "load #{vycap_remote_config}",
109
+ "commit",
110
+ "save",
111
+ "end"
112
+ )
113
+ end
114
+ end
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vycap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Schless
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tested with vyatta 6.5
14
+ email: patrick@plainlystated.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ./lib/vycap.rb
20
+ - ./lib/vycap/plugin.rb
21
+ - ./lib/vycap/tasks.rb
22
+ homepage: https://github.com/plainlystated/vycap
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Capistrano plugin for managing vyatta servers
45
+ test_files: []