knife-lxc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in knife-lxc.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2012 Nokia Siemens Networks
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Knife lxc plugin
2
+
3
+ Plugin that extends knife cli with commands for managing lxc containers.
4
+
5
+ ## Installation
6
+
7
+ ### OS requirements
8
+ Tested on ubuntu 10.04 with ruby 1.8.7. You need to install lxc packages.
9
+
10
+ ### Gem installation
11
+ Install from command line
12
+
13
+ $ gem install knife-lxc
14
+
15
+ Or add dependency to your Gemfile
16
+
17
+ gem 'knife-lxc'
18
+
19
+ ## Usage
20
+
21
+ ### Create new container
22
+
23
+ $ knife lxc server create -N container_name
24
+
25
+ It will use default template (lucid-chef) for creating lxc container.
26
+
27
+ You can pass additional parameters
28
+
29
+ -d, --distro DISTRO Bootstrap a lxc container using a template; default is 'lucid-chef'
30
+ --ip IP Ip for new container
31
+ -E, --environment ENVIRONMENT Set the Chef environment
32
+
33
+ ### List containers
34
+
35
+ $ knife lxc server list
36
+
37
+ ### Delete container
38
+
39
+ $ knife lxc server delete -N container_name
40
+
41
+ ### Help
42
+
43
+ $ knife lxc
44
+
45
+ ## License
46
+
47
+ [MIT License](https://github.com/creatary/knife-lxc/blob/master/MIT-LICENSE)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "knife-lxc/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "knife-lxc"
7
+ s.version = KnifeLxc::VERSION
8
+ s.authors = ["Lukasz Kaniowski"]
9
+ s.email = ["developers@creatary.com"]
10
+ s.homepage = "https://github.com/creatary/knife-lxc"
11
+ s.summary = "Lxc plugin for knife."
12
+ s.description = "Handles creation and deletion of lxc containers."
13
+
14
+ s.rubyforge_project = "knife-lxc"
15
+
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # specify any dependencies here; for example:
23
+ # s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+
26
+ s.add_dependency 'chef', '~> 0.10.0'
27
+ s.add_dependency 'toft'
28
+ s.add_dependency 'cucumber-chef'
29
+ end
@@ -0,0 +1,98 @@
1
+ require 'chef/knife'
2
+ require 'toft'
3
+ require 'cucumber/chef/handy'
4
+
5
+ module KnifeLxc
6
+
7
+ class LxcServerCreate < Chef::Knife
8
+ include Toft
9
+ include Cucumber::Chef::Handy
10
+
11
+ banner "knife lxc server create -N NAME (options)"
12
+
13
+ option :node_name,
14
+ :short => "-N NAME",
15
+ :long => "--node-name NAME",
16
+ :description => "Container name and chef node name",
17
+ :required => true
18
+
19
+ option :node_ip,
20
+ :long => "--ip IP",
21
+ :description => "Ip for new container",
22
+ :default => "192.168.20.#{(rand(222 - 100) +100)}"
23
+
24
+ option :distro,
25
+ :short => "-d DISTRO",
26
+ :long => "--distro DISTRO",
27
+ :description => "Bootstrap a lxc container using a template; default is 'lucid-chef'",
28
+ :default => "lucid-chef"
29
+
30
+ option :run_list,
31
+ :short => "-r RUN_LIST",
32
+ :long => "--run-list RUN_LIST",
33
+ :description => "Comma separated list of roles/recipes to apply",
34
+ :proc => lambda { |o| o.split(/[\s,]+/) },
35
+ :default => []
36
+
37
+
38
+ # This method will be executed when you run this knife command.
39
+ def run
40
+ puts "Creating lxc container '#{config[:node_name]}' with ip '#{config[:node_ip]}' from template '#{config[:distro]}'"
41
+ node = create_node config[:node_name], {:ip => config[:node_ip], :type => config[:distro]}
42
+ start_node node
43
+ puts "Run chef client with run list: #{config[:run_list].join(' ')}"
44
+ run_chef node, config[:run_list], config[:environment]
45
+ puts "Node created! Details: ip => #{node.ip}, name => #{node.hostname} "
46
+ end
47
+
48
+ private
49
+
50
+
51
+ def start_node(node)
52
+ # TODO: use node.start
53
+ hostname = node.hostname
54
+ puts "Starting host node..."
55
+ system "lxc-start -n #{hostname} -d"
56
+ system "lxc-wait -n #{hostname} -s RUNNING"
57
+ puts "Waiting for sshd"
58
+ print (".") until tcp_test_ssh(node.ip) { sleep 10 }
59
+ puts " OK!"
60
+ end
61
+
62
+ def run_chef(node, run_list, environment)
63
+ set_run_list node.hostname, run_list
64
+ env_string = environment.nil? ? "" : "-E #{environment}"
65
+ run_ssh node, "chef-client -j /etc/chef/first-boot.json #{env_string}"
66
+ end
67
+
68
+ def run_ssh node, cmd
69
+ # TODO: use node.run_ssh
70
+ system "ssh #{node.ip} '#{cmd}'"
71
+ end
72
+
73
+
74
+ def tcp_test_ssh(hostname)
75
+ tcp_socket = TCPSocket.new(hostname, 22)
76
+ readable = IO.select([tcp_socket], nil, nil, 5)
77
+ if readable
78
+ yield
79
+ true
80
+ else
81
+ false
82
+ end
83
+ rescue Errno::ETIMEDOUT
84
+ false
85
+ rescue Errno::EPERM
86
+ false
87
+ rescue Errno::ECONNREFUSED
88
+ sleep 2
89
+ false
90
+ rescue Errno::EHOSTUNREACH
91
+ sleep 2
92
+ false
93
+ ensure
94
+ tcp_socket && tcp_socket.close
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,31 @@
1
+ require 'chef/knife'
2
+ require 'toft'
3
+
4
+ module KnifeLxc
5
+
6
+ class LxcServerDelete < Chef::Knife
7
+ include Toft
8
+
9
+ banner "knife lxc server delete -N NAME"
10
+
11
+ option :node_name,
12
+ :short => "-N NAME",
13
+ :long => "--node-name NAME",
14
+ :description => "Container name and chef node name",
15
+ :required => true
16
+
17
+ # This method will be executed when you run this knife command.
18
+ def run
19
+ puts "Delete container #{config[:node_name]}"
20
+ raise "Container not exists!! " unless container_exists? config[:node_name]
21
+ n = Node.new(config[:node_name])
22
+ n.destroy
23
+ end
24
+
25
+ def container_exists?(name)
26
+ `lxc-ls` =~ /#{name}/
27
+ end
28
+
29
+
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'chef/knife'
2
+
3
+ module KnifeLxc
4
+ LXC_CONFIG_PATHS = ['/tmp', '/etc/lxc']
5
+
6
+ class LxcServerList < Chef::Knife
7
+
8
+ banner "knife lxc server list"
9
+
10
+
11
+ # This method will be executed when you run this knife command.
12
+ def run
13
+ puts "Lxc containers list"
14
+ containers = `lxc-ls`.split.uniq
15
+ server_list = [
16
+ ui.color('Name', :bold),
17
+ ui.color('Ip', :bold)
18
+ ]
19
+ containers.each do |container|
20
+ server_list << container
21
+ server_list << get_ip(container)
22
+ end
23
+ puts ui.list(server_list, :uneven_columns_across, 2)
24
+ end
25
+
26
+ private
27
+ def get_ip(container)
28
+ config_path = find_config(container)
29
+ File.read(config_path).split("\n").each do |row|
30
+ if row =~ /ipv4/
31
+ ip = row.split("=").last
32
+ ip.strip!
33
+ return ip.split("/").first
34
+ end
35
+ end unless config_path.nil?
36
+ ""
37
+ end
38
+
39
+ def find_config(container)
40
+ LXC_CONFIG_PATHS.each do |path|
41
+ config_path = "#{path}/#{container}"
42
+ if File.exists? config_path
43
+ return config_path
44
+ end
45
+
46
+ # for toft
47
+ config_path = "#{path}/#{container}-conf"
48
+ if File.exists? config_path
49
+ return config_path
50
+ end
51
+ end
52
+ nil
53
+ end
54
+
55
+
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ require "knife-lxc/version"
2
+
3
+ module KnifeLxc
4
+ end
@@ -0,0 +1,3 @@
1
+ module KnifeLxc
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-lxc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lukasz Kaniowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-02 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: &2152996340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152996340
25
+ - !ruby/object:Gem::Dependency
26
+ name: toft
27
+ requirement: &2152995740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2152995740
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber-chef
38
+ requirement: &2152995080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2152995080
47
+ description: Handles creation and deletion of lxc containers.
48
+ email:
49
+ - developers@creatary.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - MIT-LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - knife-lxc.gemspec
60
+ - lib/chef/knife/lxc_server_create.rb
61
+ - lib/chef/knife/lxc_server_delete.rb
62
+ - lib/chef/knife/lxc_server_list.rb
63
+ - lib/knife-lxc.rb
64
+ - lib/knife-lxc/version.rb
65
+ homepage: https://github.com/creatary/knife-lxc
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project: knife-lxc
85
+ rubygems_version: 1.8.10
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Lxc plugin for knife.
89
+ test_files: []
90
+ has_rdoc: