vagabond 0.2.8 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/CHANGELOG.md +16 -0
  2. data/DEVELOP.md +38 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +33 -0
  5. data/README.md +55 -21
  6. data/USAGE.md +28 -3
  7. data/Vagabondfile.sample +47 -0
  8. data/Vagrantfile +17 -5
  9. data/bin/vagabond +5 -0
  10. data/lib/vagabond/Cheffile +7 -0
  11. data/lib/vagabond/Cheffile.dev +7 -0
  12. data/lib/vagabond/actions/cluster.rb +22 -8
  13. data/lib/vagabond/actions/create.rb +7 -4
  14. data/lib/vagabond/actions/destroy.rb +24 -14
  15. data/lib/vagabond/actions/freeze.rb +1 -0
  16. data/lib/vagabond/actions/init.rb +8 -11
  17. data/lib/vagabond/actions/provision.rb +16 -8
  18. data/lib/vagabond/actions/rebuild.rb +4 -9
  19. data/lib/vagabond/actions/ssh.rb +9 -1
  20. data/lib/vagabond/actions/start.rb +1 -0
  21. data/lib/vagabond/actions/status.rb +1 -0
  22. data/lib/vagabond/actions/thaw.rb +1 -0
  23. data/lib/vagabond/actions/up.rb +22 -7
  24. data/lib/vagabond/bootstraps/chef_10_compat_config.erb +62 -0
  25. data/lib/vagabond/bootstraps/no_lazy_load.erb +63 -0
  26. data/lib/vagabond/bootstraps/server-zero.erb +1 -8
  27. data/lib/vagabond/bootstraps/server.erb +9 -6
  28. data/lib/vagabond/constants.rb +1 -6
  29. data/lib/vagabond/core.rb +209 -0
  30. data/lib/vagabond/errors.rb +3 -0
  31. data/lib/vagabond/helpers.rb +11 -99
  32. data/lib/vagabond/helpers/base.rb +134 -0
  33. data/lib/vagabond/helpers/callbacks.rb +47 -0
  34. data/lib/vagabond/helpers/chains.rb +23 -0
  35. data/lib/vagabond/helpers/commands.rb +49 -0
  36. data/lib/vagabond/helpers/knife.rb +47 -0
  37. data/lib/vagabond/helpers/naming.rb +39 -0
  38. data/lib/vagabond/helpers/server.rb +30 -0
  39. data/lib/vagabond/internal_configuration.rb +186 -32
  40. data/lib/vagabond/kitchen.rb +110 -60
  41. data/lib/vagabond/knife.rb +5 -1
  42. data/lib/vagabond/layout.rb +1 -0
  43. data/lib/vagabond/monkey/kitchen_config.rb +1 -0
  44. data/lib/vagabond/notify_mash.rb +25 -0
  45. data/lib/vagabond/server.rb +81 -49
  46. data/lib/vagabond/settings.rb +17 -0
  47. data/lib/vagabond/spec.rb +125 -99
  48. data/lib/vagabond/uploader.rb +4 -1
  49. data/lib/vagabond/uploader/berkshelf.rb +2 -1
  50. data/lib/vagabond/uploader/knife.rb +3 -9
  51. data/lib/vagabond/uploader/librarian.rb +2 -5
  52. data/lib/vagabond/vagabond.rb +77 -93
  53. data/lib/vagabond/vagabondfile.rb +73 -9
  54. data/lib/vagabond/version.rb +2 -1
  55. data/vagabond.gemspec +7 -5
  56. metadata +58 -81
  57. data/lib/vagabond/cookbooks/apt/Berksfile +0 -8
  58. data/lib/vagabond/cookbooks/apt/CHANGELOG.md +0 -97
  59. data/lib/vagabond/cookbooks/apt/CONTRIBUTING +0 -29
  60. data/lib/vagabond/cookbooks/apt/LICENSE +0 -201
  61. data/lib/vagabond/cookbooks/apt/README.md +0 -243
  62. data/lib/vagabond/cookbooks/apt/TESTING.md +0 -25
  63. data/lib/vagabond/cookbooks/apt/attributes/default.rb +0 -4
  64. data/lib/vagabond/cookbooks/apt/files/default/apt-proxy-v2.conf +0 -50
  65. data/lib/vagabond/cookbooks/apt/metadata.rb +0 -30
  66. data/lib/vagabond/cookbooks/apt/providers/preference.rb +0 -61
  67. data/lib/vagabond/cookbooks/apt/providers/repository.rb +0 -132
  68. data/lib/vagabond/cookbooks/apt/recipes/cacher-client.rb +0 -59
  69. data/lib/vagabond/cookbooks/apt/recipes/cacher-ng.rb +0 -40
  70. data/lib/vagabond/cookbooks/apt/recipes/default.rb +0 -68
  71. data/lib/vagabond/cookbooks/apt/resources/preference.rb +0 -30
  72. data/lib/vagabond/cookbooks/apt/resources/repository.rb +0 -40
  73. data/lib/vagabond/cookbooks/apt/templates/debian-6.0/acng.conf.erb +0 -174
  74. data/lib/vagabond/cookbooks/apt/templates/default/01proxy.erb +0 -2
  75. data/lib/vagabond/cookbooks/apt/templates/default/acng.conf.erb +0 -276
  76. data/lib/vagabond/cookbooks/apt/templates/ubuntu-10.04/acng.conf.erb +0 -270
  77. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/README.md +0 -1
  78. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/cacher-ng_test.rb +0 -28
  79. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/default_test.rb +0 -28
  80. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/lwrps_test.rb +0 -48
  81. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/support/helpers.rb +0 -29
  82. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/metadata.rb +0 -6
  83. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/cacher-ng.rb +0 -20
  84. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/default.rb +0 -20
  85. data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/lwrps.rb +0 -66
  86. data/lib/vagabond/cookbooks/lxc/CHANGELOG.md +0 -37
  87. data/lib/vagabond/cookbooks/lxc/Gemfile +0 -4
  88. data/lib/vagabond/cookbooks/lxc/Gemfile.lock +0 -41
  89. data/lib/vagabond/cookbooks/lxc/README.md +0 -112
  90. data/lib/vagabond/cookbooks/lxc/attributes/default.rb +0 -26
  91. data/lib/vagabond/cookbooks/lxc/files/default/knife_lxc +0 -228
  92. data/lib/vagabond/cookbooks/lxc/files/default/lxc-awesome-ephemeral +0 -499
  93. data/lib/vagabond/cookbooks/lxc/libraries/lxc.rb +0 -477
  94. data/lib/vagabond/cookbooks/lxc/libraries/lxc_expanded_resources.rb +0 -40
  95. data/lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb +0 -84
  96. data/lib/vagabond/cookbooks/lxc/libraries/monkey.rb +0 -51
  97. data/lib/vagabond/cookbooks/lxc/metadata.rb +0 -12
  98. data/lib/vagabond/cookbooks/lxc/providers/config.rb +0 -75
  99. data/lib/vagabond/cookbooks/lxc/providers/container.rb +0 -354
  100. data/lib/vagabond/cookbooks/lxc/providers/default.rb +0 -57
  101. data/lib/vagabond/cookbooks/lxc/providers/ephemeral.rb +0 -40
  102. data/lib/vagabond/cookbooks/lxc/providers/fstab.rb +0 -30
  103. data/lib/vagabond/cookbooks/lxc/providers/interface.rb +0 -45
  104. data/lib/vagabond/cookbooks/lxc/providers/service.rb +0 -53
  105. data/lib/vagabond/cookbooks/lxc/recipes/containers.rb +0 -13
  106. data/lib/vagabond/cookbooks/lxc/recipes/default.rb +0 -58
  107. data/lib/vagabond/cookbooks/lxc/recipes/install_dependencies.rb +0 -15
  108. data/lib/vagabond/cookbooks/lxc/recipes/knife.rb +0 -37
  109. data/lib/vagabond/cookbooks/lxc/resources/config.rb +0 -19
  110. data/lib/vagabond/cookbooks/lxc/resources/container.rb +0 -54
  111. data/lib/vagabond/cookbooks/lxc/resources/default.rb +0 -12
  112. data/lib/vagabond/cookbooks/lxc/resources/ephemeral.rb +0 -13
  113. data/lib/vagabond/cookbooks/lxc/resources/fstab.rb +0 -12
  114. data/lib/vagabond/cookbooks/lxc/resources/interface.rb +0 -13
  115. data/lib/vagabond/cookbooks/lxc/resources/service.rb +0 -5
  116. data/lib/vagabond/cookbooks/lxc/templates/default/client.rb.erb +0 -13
  117. data/lib/vagabond/cookbooks/lxc/templates/default/default-lxc.erb +0 -3
  118. data/lib/vagabond/cookbooks/lxc/templates/default/file_content.erb +0 -2
  119. data/lib/vagabond/cookbooks/lxc/templates/default/fstab.erb +0 -5
  120. data/lib/vagabond/cookbooks/lxc/templates/default/interface.erb +0 -27
  121. data/lib/vagabond/cookbooks/vagabond/README.md +0 -10
  122. data/lib/vagabond/cookbooks/vagabond/attributes/default.rb +0 -18
  123. data/lib/vagabond/cookbooks/vagabond/files/default/lxc-centos +0 -460
  124. data/lib/vagabond/cookbooks/vagabond/libraries/vagabond.rb +0 -10
  125. data/lib/vagabond/cookbooks/vagabond/metadata.rb +0 -8
  126. data/lib/vagabond/cookbooks/vagabond/recipes/default.rb +0 -132
  127. data/lib/vagabond/cookbooks/vagabond/recipes/zero.rb +0 -9
  128. data/lib/vagabond/helpers/cheffile_loader.rb +0 -20
  129. data/vagabond-0.2.6.gem +0 -0
@@ -1,12 +1,5 @@
1
1
  bash -c '
2
2
 
3
- apt-get install -y -q wget
4
-
5
- mkdir -p /var/chef/cookbooks/vagabond
6
- mkdir -p /var/chef/cookbooks/lxc
7
- wget --no-check-certificate -qO- https://github.com/chrisroberts/chef-vagabond/tarball/master | tar xvzC /var/chef/cookbooks/vagabond --strip-components=1
8
- wget --no-check-certificate -qO- https://github.com/hw-cookbooks/lxc/tarball/master | tar xvzC /var/chef/cookbooks/lxc --strip-components=1
9
-
10
3
  (
11
4
  echo "
12
5
  {
@@ -15,6 +8,6 @@ echo "
15
8
  "
16
9
  ) > /tmp/chef-server.json
17
10
 
18
- chef-solo -j /tmp/chef-server.json
11
+ chef-solo -j /tmp/chef-server.json -c /etc/chef-solo-host.rb
19
12
 
20
13
  '
@@ -1,8 +1,4 @@
1
1
  bash -c '
2
- apt-get install -y -q wget
3
- mkdir -p /var/chef/cache /var/chef/cookbooks/chef-server /var/chef/cookbooks/chef-server-populator
4
- wget --no-check-certificate -qO- https://github.com/opscode-cookbooks/chef-server/archive/master.tar.gz | tar xvzC /var/chef/cookbooks/chef-server --strip-components=1
5
- wget --no-check-certificate -qO- https://github.com/hw-cookbooks/chef-server-populator/tarball/master | tar xvzC /var/chef/cookbooks/chef-server-populator --strip-components=1
6
2
 
7
3
  mkdir -p /tmp/chef-server-setup
8
4
  (
@@ -25,6 +21,13 @@ mkdir -p /etc/chef
25
21
  cp /tmp/chef-server-setup/validation.pem /etc/chef/validation.pem
26
22
  chmod 600 /etc/chef/validation.pem
27
23
 
24
+ chef-server-ctl stop
25
+ chef-server-ctl uninstall
26
+ rm -rf ~/*
27
+ rm -rf /opt/chef-server
28
+ rm -rf /var/opt/chef-server
29
+ dpkg -i /var/chef-host/chef-server*
30
+
28
31
  IPADDR=`ip addr show eth0 | grep "inet " | awk -F" " '\''{print $2}'\'' | sed -e "s/\/24//"`
29
32
  (
30
33
  echo "
@@ -44,7 +47,7 @@ echo "
44
47
  \"<%= Chef::Config[:node_name] %>\": \"client_key_pub.pem\",
45
48
  \"<%= Chef::Config[:validation_client_name] %>\": \"validation_pub.pem\"
46
49
  },
47
- \"servername_override\": \"${IPADDR}\",
50
+ \"endpoint\": \"${IPADDR}\",
48
51
  \"base_path\": \"/tmp/chef-server-setup\"
49
52
  },
50
53
  \"run_list\": [ \"recipe[chef-server-populator::solo]\", \"recipe[chef-server]\" ]
@@ -52,7 +55,7 @@ echo "
52
55
  "
53
56
  ) > /tmp/chef-server.json
54
57
 
55
- chef-solo -j /tmp/chef-server.json
58
+ chef-solo -j /tmp/chef-server.json -c /etc/chef-solo-host.rb
56
59
 
57
60
  rm -rf /tmp/chef-server-setup
58
61
 
@@ -1,12 +1,7 @@
1
+ #encoding: utf-8
1
2
  require 'chef/mash'
2
3
 
3
4
  module Vagabond
4
- BASE_TEMPLATES = File.readlines(
5
- File.join(File.dirname(__FILE__), 'cookbooks/vagabond/attributes/default.rb')
6
- ).map do |l|
7
- l.scan(%r{bases\]\[:([^\]]+)\]}).flatten.first
8
- end.compact.uniq
9
-
10
5
  COLORS = Mash.new(
11
6
  :success => :green,
12
7
  :create => :green,
@@ -0,0 +1,209 @@
1
+ #encoding: utf-8
2
+
3
+ require 'thor'
4
+ require 'chef/knife/core/ui'
5
+ require 'vagabond/uploader'
6
+ require 'elecksee/lxc'
7
+
8
+ %w(constants errors vagabondfile internal_configuration helpers).each do |dep|
9
+ require "vagabond/#{dep}"
10
+ end
11
+
12
+ module Vagabond
13
+ class Core
14
+
15
+ DISABLE_HOST_SOLO_ON = %w(status init)
16
+
17
+ include Thor::Actions
18
+ include Helpers
19
+
20
+ Actions.constants.each do |const|
21
+ klass = Actions.const_get(const)
22
+ include klass if klass.is_a?(Module)
23
+ end
24
+
25
+ attr_accessor :mappings_key
26
+ attr_accessor :lxc
27
+ attr_accessor :config
28
+ attr_accessor :action
29
+
30
+ CLI_OPTIONS = lambda do
31
+ class_option(:debug,
32
+ :type => :boolean,
33
+ :default => false
34
+ )
35
+
36
+ class_option(:force_solo,
37
+ :aliases => '--force-configure',
38
+ :type => :boolean,
39
+ :default => false,
40
+ :desc => 'Force configuration of system'
41
+ )
42
+
43
+ class_option(:color,
44
+ :type => :boolean,
45
+ :default => true,
46
+ :desc => 'Enable/disable colorized output'
47
+ )
48
+
49
+ class_option(:vagabond_file,
50
+ :aliases => '-f',
51
+ :type => :string,
52
+ :desc => 'Provide path to Vagabondfile'
53
+ )
54
+
55
+ class_option(:local_server,
56
+ :type => :boolean,
57
+ :default => true,
58
+ :desc => 'Enable/disable local Chef server usage if available'
59
+ )
60
+
61
+ class_option(:callbacks,
62
+ :type => :boolean,
63
+ :default => true,
64
+ :desc => 'Enable/disable action callbacks'
65
+ )
66
+ end
67
+
68
+ CLI_OPTIONS.call
69
+
70
+ # action:: Action to perform
71
+ # name:: Name of vagabond
72
+ # config:: Hash configuration
73
+ #
74
+ # Creates an instance
75
+ def initialize(*args)
76
+ super
77
+ @threads = Mash.new
78
+ @mappings_key = :mappings
79
+ end
80
+
81
+ ## COMMANDS
82
+
83
+ COMMANDS = lambda do |show_node=true|
84
+ Actions.constants.find_all do |const|
85
+ Actions.const_get(const).is_a?(Module)
86
+ end.map(&:to_s).map(&:downcase).each do |meth|
87
+ if(self.respond_to?("_#{meth}_desc"))
88
+ args = self.send("_#{meth}_desc")
89
+ else
90
+ args = ["#{meth}#{' NODE' if show_node}", "#{meth.capitalize} instance#{' of NODE' if show_node}"]
91
+ end
92
+ desc(*args)
93
+ if(self.respond_to?("_#{meth}_options"))
94
+ self.send("_#{meth}_options").each do |opts|
95
+ method_option(*opts)
96
+ end
97
+ end
98
+ define_method meth do |*args|
99
+ @original_args = args.dup
100
+ unless(args.include?(:no_setup))
101
+ setup(meth, *args)
102
+ end
103
+ result = execute
104
+ callbacks(meth)
105
+ chain!
106
+ result
107
+ end
108
+ end
109
+ end
110
+
111
+ COMMANDS.call
112
+
113
+ protected
114
+
115
+ def attributes
116
+ if(config[:attributes])
117
+ if(config[:attributes].is_a?(Hash))
118
+ JSON.dump(config[:attributes])
119
+ else
120
+ config[:attributes].to_s
121
+ end
122
+ end
123
+ end
124
+
125
+ def version
126
+ setup_ui
127
+ ui.info "#{ui.color('Vagabond:', :yellow, :bold)} - Advocating idleness and work-shyness"
128
+ ui.info " #{ui.color('Version:', :blue)} - #{VERSION.version} (#{VERSION.codename})"
129
+ exit
130
+ end
131
+
132
+ def execute
133
+ self.send("_#{action}")
134
+ end
135
+
136
+ def setup(action, name=nil, *args)
137
+ @action = action
138
+ @name = name
139
+ hash_args = args.detect{|x|x.is_a?(Hash)}
140
+ if(hash_args)
141
+ args.delete(hash_args)
142
+ _ui = hash_args.delete(:ui)
143
+ base_setup(_ui)
144
+ config.merge!(hash_args)
145
+ else
146
+ base_setup
147
+ end
148
+ @leftover_args = args
149
+ end
150
+
151
+ def name_required!
152
+ unless(name)
153
+ ui.fatal "Node name is required!"
154
+ raise VagabondError::MissingNodeName.new
155
+ end
156
+ end
157
+
158
+ def provision_solo(dir)
159
+ ui.info "#{ui.color('Vagabond:', :bold)} Provisioning node: #{ui.color(name, :magenta)}"
160
+ lxc.container_ip(20) # force wait for container to appear and do so quietly
161
+ cmd = direct_container_command(
162
+ "chef-solo -c #{File.join(dir, 'solo.rb')} -j #{File.join(dir, 'dna.json')}",
163
+ :live_stream => STDOUT
164
+ )
165
+ raise VagabondError::NodeProvisionFailed.new("Failed to provision: #{name}") unless cmd
166
+ end
167
+
168
+ def validate!
169
+ if(name.to_s == 'server')
170
+ ui.fatal "RESERVED node name supplied: #{ui.color(name, :red)}"
171
+ ui.info ui.color(" -> Try: vagabond server #{action}", :cyan)
172
+ raise VagabondError::ReservedName.new(name)
173
+ end
174
+ if(name && config.nil? && !options[:disable_name_validate])
175
+ ui.fatal "Invalid node name supplied: #{ui.color(name, :red)}"
176
+ ui.info ui.color(" -> Available: #{vagabondfile[:nodes].keys.sort.join(', ')}", :cyan)
177
+ raise VagabondError::InvalidName.new(name)
178
+ end
179
+ end
180
+
181
+ def check_existing!
182
+ if(lxc.exists?)
183
+ ui.error "LXC: #{name} already exists!"
184
+ true
185
+ end
186
+ end
187
+
188
+ def base_dir
189
+ File.dirname(vagabondfile.path)
190
+ end
191
+
192
+ def vagabond_dir
193
+ File.join(base_dir, '.vagabond')
194
+ end
195
+
196
+ def wait_for_completion(type=nil)
197
+ @threads ||= []
198
+ if(type)
199
+ Array(@threads[type]).collect{|hsh| hsh[:thread]}.map(&:join)
200
+ else
201
+ @threads.values.flatten.collect{|hsh| hsh[:thread]}.map(&:join)
202
+ end
203
+ end
204
+
205
+ def tasks(type=nil)
206
+ type ? @threads[type] : @threads
207
+ end
208
+ end
209
+ end
@@ -1,3 +1,4 @@
1
+ #encoding: utf-8
1
2
  module Vagabond
2
3
  class VagabondError < StandardError
3
4
  class << self
@@ -15,6 +16,8 @@ module Vagabond
15
16
  kitchen_no_cookbook_args kitchen_too_many_args
16
17
  kitchen_invalid_platform missing_node_name cluster_invalid
17
18
  kitchen_test_failed host_provision_failed spec_failed
19
+ node_provision_failed librarian_host_install_failed
20
+ echef_base_missing
18
21
  ).each_with_index do |klass_name, i|
19
22
  klass = klass_name.split('_').map(&:capitalize).join
20
23
  self.class_eval("class #{klass} < VagabondError; self.exit_code = #{i + 1}; end")
@@ -1,110 +1,22 @@
1
+ #encoding: utf-8
1
2
  require 'vagabond/constants'
2
- require 'tmpdir'
3
- require 'uuidtools'
3
+
4
+ Dir.glob(File.join(File.dirname(__FILE__), 'helpers/*.rb')).each do |path|
5
+ require "vagabond/helpers/#{File.basename(path).sub('.rb', '')}"
6
+ end
4
7
 
5
8
  module Vagabond
6
9
  module Helpers
7
-
8
- RAND_CHARS = ('a'..'z').map(&:to_s) + ('A'..'Z').map(&:to_s) + (0..9).map(&:to_s)
9
- GEN_NAME_LENGTH = 10
10
-
11
- private
12
-
13
- def base_setup
14
- @options = options.dup
15
- @vagabondfile = Vagabondfile.new(options[:vagabond_file], :allow_missing)
16
- Lxc.use_sudo = sudo
17
- options[:sudo] = sudo
18
- setup_ui
19
- @internal_config = InternalConfiguration.new(@vagabondfile, ui, options)
20
- end
21
-
22
- def sudo
23
- case vagabondfile[:sudo]
24
- when FalseClass
25
- ''
26
- when String
27
- "#{vagabondfile[:sudo]} "
28
- else
29
- 'sudo '
30
- end
31
- end
32
-
33
- def debug(s)
34
- ui.info "#{ui.color('DEBUG:', :red, :bold)} #{s}" if options[:debug] && ui
35
- end
36
-
37
- def random_name(n=nil)
38
- n = name unless n
39
- [n, SecureRandom.hex].compact.join('-')
40
- end
41
-
42
- def generated_name(n=nil)
43
- seed = vagabondfile.directory.chars.map(&:ord).inject(&:+)
44
- srand(seed)
45
- n = name unless n
46
- if(@_gn.nil? || @_gn[n].nil?)
47
- @_gn ||= Mash.new
48
- @_gn[n] = "#{n}-"
49
- GEN_NAME_LENGTH.times do
50
- @_gn[n] << RAND_CHARS[rand(RAND_CHARS.size)]
51
- end
52
- end
53
- @_gn[n]
54
- end
55
-
56
- def setup_ui(*args)
57
- unless(args.first.is_a?(Chef::Knife::UI))
58
- Chef::Config[:color] = options[:color].nil? ? true : options[:color]
59
- @ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
60
- else
61
- @ui = args.first
62
- end
63
- options[:debug] = STDOUT if options[:debug]
64
- self.class.ui = @ui unless args.include?(:no_class_set)
65
- @ui
66
- end
67
-
68
- def execute
69
- if(public_methods.include?(@action.to_sym))
70
- send(@action)
71
- else
72
- ui.error "Invalid action received: #{@action}"
73
- raise VagabondError::InvalidAction.new(@action)
74
- end
75
- end
76
-
77
- def generate_hash
78
- Digest::MD5.hexdigest(@vagabondfile.path)
79
- end
80
-
81
- def direct_container_command(command, args={})
82
- _lxc = args[:lxc] || lxc
83
- com = "#{sudo}ssh root@#{lxc.container_ip} -i /opt/hw-lxc-config/id_rsa -oStrictHostKeyChecking=no '#{command}'"
84
- debug(com)
85
- begin
86
- cmd = Mixlib::ShellOut.new(com,
87
- :live_stream => args[:live_stream] || options[:debug],
88
- :timeout => args[:timeout] || 1200
89
- )
90
- cmd.run_command
91
- cmd.error!
92
- true
93
- rescue
94
- raise if args[:raise_on_failure]
95
- false
96
- end
97
- end
98
-
99
10
  class << self
11
+
100
12
  def included(klass)
101
- klass.class_eval do
102
- class << self
103
- attr_accessor :ui
104
- end
105
- attr_accessor :vagabondfile, :internal_config, :name, :ui
13
+ ::Vagabond::Helpers.constants.each do |konst|
14
+ const = ::Vagabond::Helpers.const_get(konst)
15
+ next unless const.is_a?(Module)
16
+ klass.send(:include, const)
106
17
  end
107
18
  end
19
+
108
20
  end
109
21
  end
110
22
  end
@@ -0,0 +1,134 @@
1
+ #encoding: utf-8
2
+
3
+ require 'vagabond/constants'
4
+ require 'etc'
5
+
6
+ module Vagabond
7
+ module Helpers
8
+
9
+ module Base
10
+
11
+ SSH_KEY_BASE = '/opt/hw-lxc-config/id_rsa'
12
+
13
+ def base_setup(*args)
14
+ @options = Mash.new(@options.dup)
15
+ @vagabondfile = Vagabondfile.new(options[:vagabond_file], :allow_missing)
16
+ Lxc.use_sudo = sudo
17
+ options[:sudo] = sudo
18
+ setup_ui(*args)
19
+ config_args = args.detect{|i| i.is_a?(Hash) && i[:config]} || {}
20
+ @internal_config = InternalConfiguration.new(@vagabondfile, ui, options, config_args[:config] || {})
21
+ configure(:allow_missing) unless args.include?(:no_configure)
22
+ validate_if_required unless args.include?(:no_validate)
23
+ Chef::Log.init('/dev/null') unless options[:debug]
24
+ Settings[:ssh_key] = setup_key!
25
+ end
26
+
27
+ def setup_key!
28
+ path = "/tmp/.#{ENV['USER']}_id_rsa"
29
+ unless(File.exists?(path))
30
+ [
31
+ "cp #{SSH_KEY_BASE} #{path}",
32
+ "chown #{ENV['USER']} #{path}",
33
+ "chmod 600 #{path}"
34
+ ].each do |com|
35
+ cmd = build_command(com, :sudo => true)
36
+ cmd.run_command
37
+ cmd.error!
38
+ end
39
+ end
40
+ path
41
+ end
42
+
43
+ def configure(*args)
44
+ @config ||= Mash.new
45
+ @config.merge!(vagabondfile.for_node(name, *args))
46
+ @lxc = Lxc.new(internal_config[mappings_key][name] || '____nonreal____')
47
+ if(options[:local_server] && vagabondfile.local_chef_server? && lxc_installed?)
48
+ proto = vagabondfile[:local_chef_server][:zero] ? 'http' : 'https'
49
+ srv_name = internal_config[:mappings][:server] || '____nonreal____'
50
+ srv = Lxc.new(srv_name)
51
+ if(srv.running?)
52
+ knife_config :server_url => "#{proto}://#{srv.container_ip(10, true)}"
53
+ else
54
+ unless(action.to_s == 'status' || name.to_s =='server')
55
+ ui.warn 'Local chef server is not currently running!'
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def validate_if_required
62
+ if(respond_to?(check = "#{action}_validate?".to_sym))
63
+ validate! if send(check)
64
+ else
65
+ validate!
66
+ end
67
+ end
68
+
69
+ def sudo
70
+ sudo_val = vagabondfile[:sudo]
71
+ if(sudo_val.nil? || sudo_val.to_s == 'smart')
72
+ if(ENV['rvm_bin_path'] && RbConfig::CONFIG['bindir'].include?(File.dirname(ENV['rvm_bin_path'])))
73
+ sudo_val = 'rvmsudo'
74
+ elsif(Etc.getpwuid.uid == 0)
75
+ sudo_val = false
76
+ else
77
+ sudo_val = true
78
+ end
79
+ end
80
+ case sudo_val
81
+ when FalseClass
82
+ ''
83
+ when String
84
+ "#{sudo_val} "
85
+ else
86
+ 'sudo '
87
+ end
88
+ end
89
+
90
+ def debug(s)
91
+ ui.info "#{ui.color('DEBUG:', :red, :bold)} #{s}" if options[:debug] && ui
92
+ end
93
+
94
+ def setup_ui(*args)
95
+ unless(@ui)
96
+ unless(args.first.is_a?(Chef::Knife::UI))
97
+ Chef::Config[:color] = options[:color].nil? ? true : options[:color]
98
+ @ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
99
+ else
100
+ @ui = args.first
101
+ end
102
+ options[:debug] = STDOUT if options[:debug]
103
+ self.class.ui = @ui unless args.include?(:no_class_set)
104
+ end
105
+ @ui
106
+ end
107
+
108
+ def execute
109
+ if(public_methods.include?(@action.to_sym))
110
+ send(@action)
111
+ else
112
+ ui.error "Invalid action received: #{@action}"
113
+ raise VagabondError::InvalidAction.new(@action)
114
+ end
115
+ end
116
+
117
+ def lxc_installed?
118
+ system('which lxc-info > /dev/null')
119
+ end
120
+
121
+ class << self
122
+ def included(klass)
123
+ klass.class_eval do
124
+ class << self
125
+ attr_accessor :ui
126
+ end
127
+ attr_accessor :vagabondfile, :internal_config, :name, :ui, :options, :leftover_args
128
+ end
129
+ end
130
+ end
131
+
132
+ end
133
+ end
134
+ end