vagabond 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/CHANGELOG.md +2 -0
  2. data/README.md +94 -0
  3. data/bin/vagabond +6 -0
  4. data/lib/vagabond.rb +1 -0
  5. data/lib/vagabond/actions/create.rb +29 -0
  6. data/lib/vagabond/actions/destroy.rb +31 -0
  7. data/lib/vagabond/actions/freeze.rb +14 -0
  8. data/lib/vagabond/actions/provision.rb +31 -0
  9. data/lib/vagabond/actions/ssh.rb +13 -0
  10. data/lib/vagabond/actions/status.rb +29 -0
  11. data/lib/vagabond/actions/thaw.rb +14 -0
  12. data/lib/vagabond/actions/up.rb +27 -0
  13. data/lib/vagabond/bootstraps/server.erb +62 -0
  14. data/lib/vagabond/commands.rb +58 -0
  15. data/lib/vagabond/config.rb +7 -0
  16. data/lib/vagabond/cookbooks/lxc/CHANGELOG.md +21 -0
  17. data/lib/vagabond/cookbooks/lxc/Gemfile +3 -0
  18. data/lib/vagabond/cookbooks/lxc/Gemfile.lock +132 -0
  19. data/lib/vagabond/cookbooks/lxc/README.md +83 -0
  20. data/lib/vagabond/cookbooks/lxc/attributes/default.rb +26 -0
  21. data/lib/vagabond/cookbooks/lxc/files/default/knife_lxc +228 -0
  22. data/lib/vagabond/cookbooks/lxc/libraries/lxc.rb +279 -0
  23. data/lib/vagabond/cookbooks/lxc/libraries/lxc_expanded_resources.rb +40 -0
  24. data/lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb +81 -0
  25. data/lib/vagabond/cookbooks/lxc/metadata.rb +11 -0
  26. data/lib/vagabond/cookbooks/lxc/providers/config.rb +82 -0
  27. data/lib/vagabond/cookbooks/lxc/providers/container.rb +342 -0
  28. data/lib/vagabond/cookbooks/lxc/providers/fstab.rb +71 -0
  29. data/lib/vagabond/cookbooks/lxc/providers/interface.rb +99 -0
  30. data/lib/vagabond/cookbooks/lxc/providers/service.rb +53 -0
  31. data/lib/vagabond/cookbooks/lxc/recipes/containers.rb +13 -0
  32. data/lib/vagabond/cookbooks/lxc/recipes/default.rb +45 -0
  33. data/lib/vagabond/cookbooks/lxc/recipes/install_dependencies.rb +15 -0
  34. data/lib/vagabond/cookbooks/lxc/recipes/knife.rb +37 -0
  35. data/lib/vagabond/cookbooks/lxc/resources/#container.rb# +28 -0
  36. data/lib/vagabond/cookbooks/lxc/resources/config.rb +19 -0
  37. data/lib/vagabond/cookbooks/lxc/resources/container.rb +28 -0
  38. data/lib/vagabond/cookbooks/lxc/resources/fstab.rb +11 -0
  39. data/lib/vagabond/cookbooks/lxc/resources/interface.rb +10 -0
  40. data/lib/vagabond/cookbooks/lxc/resources/service.rb +5 -0
  41. data/lib/vagabond/cookbooks/lxc/templates/default/client.rb.erb +13 -0
  42. data/lib/vagabond/cookbooks/lxc/templates/default/default-lxc.erb +3 -0
  43. data/lib/vagabond/cookbooks/lxc/templates/default/fstab.erb +5 -0
  44. data/lib/vagabond/cookbooks/lxc/templates/default/interface.erb +21 -0
  45. data/lib/vagabond/cookbooks/lxc/test/kitchen/Kitchenfile +7 -0
  46. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/metadata.rb +2 -0
  47. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/centos_lxc.rb +0 -0
  48. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/chef-bootstrap.rb +0 -0
  49. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_files.rb +0 -0
  50. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_templates.rb +0 -0
  51. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/ubuntu_lxc.rb +0 -0
  52. data/lib/vagabond/cookbooks/vagabond/attributes/default.rb +2 -0
  53. data/lib/vagabond/cookbooks/vagabond/libraries/vagabond.rb +10 -0
  54. data/lib/vagabond/cookbooks/vagabond/metadata.rb +6 -0
  55. data/lib/vagabond/cookbooks/vagabond/recipes/create.rb +3 -0
  56. data/lib/vagabond/cookbooks/vagabond/recipes/default.rb +30 -0
  57. data/lib/vagabond/internal_configuration.rb +147 -0
  58. data/lib/vagabond/server.rb +158 -0
  59. data/lib/vagabond/vagabond.rb +109 -0
  60. data/lib/vagabond/vagabondfile.rb +34 -0
  61. data/lib/vagabond/version.rb +10 -0
  62. data/vagabond.gemspec +18 -0
  63. metadata +125 -0
@@ -0,0 +1,158 @@
1
+ require 'vagabond/vagabond'
2
+ require 'mixlib/cli'
3
+ require 'digest/md5'
4
+
5
+ module Vagabond
6
+
7
+ class Server < Vagabond
8
+
9
+ def initialize(me, actions)
10
+ @name = me
11
+ @base_template = 'ubuntu_1204' # TODO: Make this dynamic
12
+ @action = actions.shift
13
+ setup_ui
14
+ load_configurations
15
+ end
16
+
17
+ def create
18
+ if(lxc.exists?)
19
+ ui.warn 'Server container already exists'
20
+ if(lxc.frozen?)
21
+ ui.fatal 'Server container is currently frozen!'
22
+ elsif(lxc.stopped?)
23
+ lxc.start
24
+ ui.info 'Server container has been started'
25
+ else
26
+ ui.info 'Server container is currently running'
27
+ end
28
+ else
29
+ ui.info 'Creating Chef server container...'
30
+ do_create
31
+ end
32
+ end
33
+
34
+ def destroy
35
+ if(lxc.exists?)
36
+ ui.info 'Destroying Chef server container...'
37
+ do_destroy
38
+ else
39
+ ui.fatal 'No Chef server exists within this environment'
40
+ end
41
+ end
42
+
43
+ def do_create
44
+ cmd = Mixlib::ShellOut.new("#{Config[:sudo]}lxc-clone -n #{generated_name} -o #{@base_template}")
45
+ cmd.run_command
46
+ cmd.error!
47
+ @lxc = Lxc.new(generated_name)
48
+ @internal_config[:mappings][name] = generated_name
49
+ @internal_config.save
50
+ ui.info "Chef Server container created!"
51
+ lxc.start
52
+ ui.info "Bootstrapping erchef..."
53
+ tem_file = File.expand_path(File.join(File.dirname(__FILE__), 'bootstraps/server.erb'))
54
+ com = "#{Config[:sudo]}knife bootstrap #{lxc.container_ip(10, true)} --template-file #{tem_file} -i /opt/hw-lxc-config/id_rsa"
55
+ cmd = Mixlib::ShellOut.new(com, :live_stream => STDOUT, :timeout => 1200)
56
+ cmd.run_command
57
+ cmd.error!
58
+ ui.info 'Chef Server has been created!'
59
+ auto_upload if vagabondfile[:local_chef_server][:auto_upload]
60
+ end
61
+
62
+ def auto_upload
63
+ ui.info 'Auto uploading all assets to local Chef server...'
64
+ upload_roles
65
+ upload_databags
66
+ upload_environments
67
+ upload_cookbooks
68
+ ui.info 'All assets uploaded to local Chef server!'
69
+ end
70
+
71
+ def upload_roles
72
+ ui.info "Uploading roles to local Chef server..."
73
+ com = "knife role from file #{File.join(base_dir, 'roles/*')} #{Config[:knife_opts]}"
74
+ cmd = Mixlib::ShellOut.new(com)
75
+ cmd.run_command
76
+ cmd.error!
77
+ ui.info "Roles uploaded to local Chef server!"
78
+ end
79
+
80
+ def upload_databags
81
+ ui.info "Uploading data bags to local Chef server..."
82
+ Dir.glob(File.join(base_dir, "data_bags/*")).each do |b|
83
+ next if %w(. ..).include?(b)
84
+ coms = [
85
+ "knife data bag create #{File.basename(b)} #{Config[:knife_opts]}",
86
+ "knife data bag from file #{File.basename(b)} #{Config[:knife_opts]} --all"
87
+ ].each do |com|
88
+ cmd = Mixlib::ShellOut.new(com)
89
+ cmd.run_command
90
+ cmd.error!
91
+ end
92
+ end
93
+ ui.info "Data bags uploaded to local Chef server!"
94
+ end
95
+
96
+ def upload_environments
97
+ ui.info "Uploading environments to local Chef server..."
98
+ com = "knife environment from file #{File.join(base_dir, 'environments/*')} #{Config[:knife_opts]}"
99
+ cmd = Mixlib::ShellOut.new(com)
100
+ cmd.run_command
101
+ cmd.error!
102
+ ui.info "Environments uploaded to local Chef server!"
103
+ end
104
+
105
+ def upload_cookbooks
106
+ ui.info "Uploading cookbooks to local Chef server..."
107
+ if(vagabondfile[:local_chef_server][:berkshelf])
108
+ berks_upload
109
+ else
110
+ raw_upload
111
+ end
112
+ end
113
+
114
+ def berks_upload
115
+ write_berks_config
116
+ com = "berks upload -c #{File.join(vagabond_dir, 'berks.json')}"
117
+ cmd = Mixlib::ShellOut.new(com)
118
+ cmd.run_command
119
+ cmd.error!
120
+ ui.info "Berks cookbook upload complete!"
121
+ end
122
+
123
+ def raw_upload
124
+ com = "knife cookbook upload#{Config[:knife_opts]} --all"
125
+ cmd = Mixlib::ShellOut.new(com)
126
+ cmd.run_command
127
+ cmd.error!
128
+ ui.info "Cookbook upload complete!"
129
+ end
130
+
131
+ def write_berks_config
132
+ path = File.join(vagabond_dir, 'berks.json')
133
+ if(File.exists?(path))
134
+ cur = Mash.new(JSON.load(File.read(path)))
135
+ else
136
+ cur = Mash.new
137
+ end
138
+ url = "https://#{lxc.container_ip(10, true)}"
139
+ if(cur[:chef].nil? || cur[:chef][:chef_server_url] != url)
140
+ cur[:chef] = Mash.new(:chef_server_url => url)
141
+ cur[:ssl] = Mash.new(:verify => false)
142
+ File.open(path, 'w') do |file|
143
+ file.write(JSON.dump(cur))
144
+ end
145
+ end
146
+ end
147
+
148
+ def generated_name
149
+ unless(@_gn)
150
+ s = Digest::MD5.new
151
+ s << vagabondfile.path
152
+ @_gn = "server-#{s.hexdigest}"
153
+ end
154
+ @_gn
155
+ end
156
+
157
+ end
158
+ end
@@ -0,0 +1,109 @@
1
+ Dir.glob(
2
+ File.join(
3
+ File.dirname(__FILE__), 'actions', '*.rb'
4
+ )
5
+ ).each do |action_module|
6
+ require action_module
7
+ end
8
+
9
+ require 'vagabond/vagabondfile'
10
+ require 'vagabond/internal_configuration'
11
+ require 'chef/knife/core/ui'
12
+ require File.join(File.dirname(__FILE__), 'cookbooks/lxc/libraries/lxc.rb')
13
+
14
+ module Vagabond
15
+ class Vagabond
16
+
17
+ class << self
18
+ attr_accessor :ui
19
+ end
20
+
21
+ # Load available actions
22
+ Actions.constants.each do |const_sym|
23
+ const = Actions.const_get(const_sym)
24
+ include const if const.is_a?(Module)
25
+ end
26
+
27
+ attr_reader :name
28
+ attr_reader :lxc
29
+ attr_reader :vagabondfile
30
+ attr_reader :config
31
+ attr_reader :internal_config
32
+ attr_reader :ui
33
+
34
+ # action:: Action to perform
35
+ # name:: Name of vagabond
36
+ # config:: Hash configuration
37
+ #
38
+ # Creates an instance
39
+ def initialize(action, name_args)
40
+ setup_ui
41
+ @action = action
42
+ @name = name_args.shift
43
+ load_configurations
44
+ validate!
45
+ end
46
+
47
+ def load_configurations
48
+ @vagabondfile = Vagabondfile.new(Config[:vagabond_file])
49
+ Config[:sudo] = sudo
50
+ Config[:disable_solo] = true if @action.to_sym == :status
51
+ Lxc.use_sudo = @vagabondfile[:sudo].nil? ? true : @vagabondfile[:sudo]
52
+ @internal_config = InternalConfiguration.new(@vagabondfile, ui)
53
+ @config = @vagabondfile[:boxes][name]
54
+ @lxc = Lxc.new(@internal_config[:mappings][name] || '____nonreal____')
55
+ unless(Config[:disable_local_server])
56
+ if(@vagabondfile[:local_chef_server] && @vagabondfile[:local_chef_server][:enabled])
57
+ srv = Lxc.new(@internal_config[:mappings][:server])
58
+ Config[:knife_opts] = " -s https://#{srv.container_ip(10, true)}"
59
+ end
60
+ end
61
+ end
62
+
63
+ def setup_ui
64
+ Chef::Config[:color] = true
65
+ @ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
66
+ self.class.ui = @ui
67
+ end
68
+
69
+ def validate!
70
+ if(name.to_s == 'server')
71
+ raise 'Box name `server` is reserved and not allowed!'
72
+ end
73
+ end
74
+
75
+ def execute
76
+ send(@action)
77
+ end
78
+
79
+ private
80
+
81
+ def generate_hash
82
+ Digest::MD5.hexdigest(@vagabondfile.path)
83
+ end
84
+
85
+ def check_existing!
86
+ if(@lxc.exists?)
87
+ ui.error "LXC: #{name} already exists!"
88
+ true
89
+ end
90
+ end
91
+
92
+ def sudo
93
+ case @vagabondfile[:sudo]
94
+ when TrueClass
95
+ 'sudo '
96
+ when String
97
+ "#{@vagabondfile[:sudo]} "
98
+ end
99
+ end
100
+
101
+ def base_dir
102
+ File.dirname(vagabondfile.path)
103
+ end
104
+
105
+ def vagabond_dir
106
+ File.join(base_dir, '.vagabond')
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,34 @@
1
+ require 'chef/mash'
2
+
3
+ module Vagabond
4
+ class Vagabondfile
5
+
6
+ attr_reader :path
7
+ attr_reader :config
8
+
9
+ def initialize(path=nil)
10
+ path = discover_path(Dir.pwd) unless path
11
+ @path = path
12
+ load_configuration!
13
+ end
14
+
15
+ def [](k)
16
+ @config[k]
17
+ end
18
+
19
+ def load_configuration!
20
+ raise 'No Vagabondfile file found!' unless @path && File.exists?(@path)
21
+ @config = Mash.new(self.instance_eval(IO.read(@path), @path, 1))
22
+ end
23
+
24
+ def discover_path(path)
25
+ d_path = Dir.glob(File.join(path, 'Vagabondfile')).first
26
+ unless(d_path)
27
+ cut_path = path.split(File::SEPARATOR)
28
+ cut_path.pop
29
+ d_path = discover_path(cut_path.join(File::SEPARATOR)) unless cut_path.empty?
30
+ end
31
+ d_path
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module Vagabond
2
+ class Version < Gem::Version
3
+ attr_reader :codename
4
+ def initialize(v, name)
5
+ @codename = name
6
+ super(v)
7
+ end
8
+ end
9
+ VERSION = Version.new('0.1.0', 'tramp')
10
+ end
data/vagabond.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
2
+ require 'vagabond/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'vagabond'
6
+ s.version = Vagabond::VERSION.version
7
+ s.summary = 'Vagabond creator'
8
+ s.author = 'Chris Roberts'
9
+ s.email = 'chrisroberts.code@gmail.com'
10
+ s.homepage = 'http://github.com/chrisroberts/vagabond'
11
+ s.description = 'Vagabonds, lazing about'
12
+ s.require_path = 'lib'
13
+ s.has_rdoc = true
14
+ s.extra_rdoc_files = ['README.md']
15
+ s.files = Dir['**/*']
16
+ s.executables = ['vagabond']
17
+ s.add_dependency 'chef'
18
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagabond
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Roberts
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Vagabonds, lazing about
31
+ email: chrisroberts.code@gmail.com
32
+ executables:
33
+ - vagabond
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - README.md
37
+ files:
38
+ - lib/vagabond/cookbooks/lxc/files/default/knife_lxc
39
+ - lib/vagabond/cookbooks/lxc/metadata.rb
40
+ - lib/vagabond/cookbooks/lxc/templates/default/interface.erb
41
+ - lib/vagabond/cookbooks/lxc/templates/default/fstab.erb
42
+ - lib/vagabond/cookbooks/lxc/templates/default/client.rb.erb
43
+ - lib/vagabond/cookbooks/lxc/templates/default/default-lxc.erb
44
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/metadata.rb
45
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_files.rb
46
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/centos_lxc.rb
47
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_templates.rb
48
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/ubuntu_lxc.rb
49
+ - lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/chef-bootstrap.rb
50
+ - lib/vagabond/cookbooks/lxc/test/kitchen/Kitchenfile
51
+ - lib/vagabond/cookbooks/lxc/Gemfile
52
+ - lib/vagabond/cookbooks/lxc/attributes/default.rb
53
+ - lib/vagabond/cookbooks/lxc/README.md
54
+ - lib/vagabond/cookbooks/lxc/providers/fstab.rb
55
+ - lib/vagabond/cookbooks/lxc/providers/interface.rb
56
+ - lib/vagabond/cookbooks/lxc/providers/service.rb
57
+ - lib/vagabond/cookbooks/lxc/providers/config.rb
58
+ - lib/vagabond/cookbooks/lxc/providers/container.rb
59
+ - lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb
60
+ - lib/vagabond/cookbooks/lxc/libraries/lxc_expanded_resources.rb
61
+ - lib/vagabond/cookbooks/lxc/libraries/lxc.rb
62
+ - lib/vagabond/cookbooks/lxc/resources/fstab.rb
63
+ - lib/vagabond/cookbooks/lxc/resources/interface.rb
64
+ - lib/vagabond/cookbooks/lxc/resources/service.rb
65
+ - lib/vagabond/cookbooks/lxc/resources/config.rb
66
+ - lib/vagabond/cookbooks/lxc/resources/#container.rb#
67
+ - lib/vagabond/cookbooks/lxc/resources/container.rb
68
+ - lib/vagabond/cookbooks/lxc/CHANGELOG.md
69
+ - lib/vagabond/cookbooks/lxc/Gemfile.lock
70
+ - lib/vagabond/cookbooks/lxc/recipes/default.rb
71
+ - lib/vagabond/cookbooks/lxc/recipes/install_dependencies.rb
72
+ - lib/vagabond/cookbooks/lxc/recipes/knife.rb
73
+ - lib/vagabond/cookbooks/lxc/recipes/containers.rb
74
+ - lib/vagabond/cookbooks/vagabond/metadata.rb
75
+ - lib/vagabond/cookbooks/vagabond/attributes/default.rb
76
+ - lib/vagabond/cookbooks/vagabond/libraries/vagabond.rb
77
+ - lib/vagabond/cookbooks/vagabond/recipes/create.rb
78
+ - lib/vagabond/cookbooks/vagabond/recipes/default.rb
79
+ - lib/vagabond/commands.rb
80
+ - lib/vagabond/server.rb
81
+ - lib/vagabond/vagabondfile.rb
82
+ - lib/vagabond/bootstraps/server.erb
83
+ - lib/vagabond/version.rb
84
+ - lib/vagabond/vagabond.rb
85
+ - lib/vagabond/config.rb
86
+ - lib/vagabond/internal_configuration.rb
87
+ - lib/vagabond/actions/up.rb
88
+ - lib/vagabond/actions/create.rb
89
+ - lib/vagabond/actions/provision.rb
90
+ - lib/vagabond/actions/status.rb
91
+ - lib/vagabond/actions/thaw.rb
92
+ - lib/vagabond/actions/destroy.rb
93
+ - lib/vagabond/actions/freeze.rb
94
+ - lib/vagabond/actions/ssh.rb
95
+ - lib/vagabond.rb
96
+ - README.md
97
+ - vagabond.gemspec
98
+ - bin/vagabond
99
+ - CHANGELOG.md
100
+ homepage: http://github.com/chrisroberts/vagabond
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Vagabond creator
124
+ test_files: []
125
+ has_rdoc: true