haconiwa 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Uchio KONDO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Haconiwa
2
+
3
+ [![Build Status](https://travis-ci.org/udzura/haconiwa.svg?branch=master)](https://travis-ci.org/udzura/haconiwa)
4
+
5
+ Ruby on Container / helper tools with DSL for your handmade linux containers
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'haconiwa'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install haconiwa
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require "haconiwa"
27
+
28
+ haconiwa = Haconiwa::Base.define do |config|
29
+ config.name = "new-haconiwa001" # to be hostname
30
+
31
+ config.cgroup["cpu.shares"] = 2048
32
+ config.cgroup["memory.limit_in_bytes"] = "256M"
33
+ config.cgroup["pid.max"] = 1024
34
+
35
+ config.chroot_to "/var/your_rootfs"
36
+ config.add_mount_point "/var/another/root/etc", to: "/etc", readonly: true
37
+ config.add_mount_point "/var/another/root/home", to: "/home"
38
+ config.add_mount_point "proc", to: "/proc", fs: "proc"
39
+
40
+ config.namespace.unshare "ipc"
41
+ config.namespace.unshare "mount"
42
+ config.namespace.unshare "pid"
43
+ config.namespace.use_netns "foobar"
44
+
45
+ config.capabilities.allow :all
46
+ config.capabilities.drop "CAP_SYS_TIME"
47
+ end
48
+
49
+ haconiwa.start
50
+
51
+ ## or to attach running container
52
+
53
+ Haconiwa.attach haconiwa.name
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/udzura/haconiwa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ desc "Run #{File.basename(File.dirname(__FILE__))}'s test suite"
5
+ Rake::TestTask.new do |t|
6
+ # To run test for only one file (or file path pattern)
7
+ # $ bundle exec rake test TEST=test/test_specified_path.rb
8
+ t.libs.concat ["test"]
9
+ t.test_files = Dir["test/**/test_*.rb"]
10
+ t.verbose = true
11
+ t.ruby_opts = ["-r config"]
12
+ end
13
+ task :default => :test
data/Vagrantfile ADDED
@@ -0,0 +1,55 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure(2) do |config|
5
+ config.vm.box = "puppetlabs/centos-7.0-64-puppet"
6
+
7
+ config.vm.network "forwarded_port", guest: 80, host: 38080
8
+ config.vm.network "private_network", ip: "192.168.98.10"
9
+ # config.vm.synced_folder "../data", "/vagrant_data"
10
+
11
+ config.vm.provider "virtualbox" do |vbox|
12
+ # Display the VirtualBox GUI when booting the machine
13
+ vbox.gui = true
14
+
15
+ # Customize the amount of memory on the VM:
16
+ vbox.memory = "2048"
17
+ vbox.cpus = 4
18
+
19
+ vbox.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
20
+ vbox.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
21
+ vbox.customize ["modifyvm", :id, "--nic2", "intnet"]
22
+ vbox.customize ["modifyvm", :id, "--intnet2", "internal_network"]
23
+ end
24
+
25
+ config.vm.provision "shell", inline: (<<-SHELL).gsub(/^ +/m, "")
26
+ set -x
27
+ if ! test -f /usr/local/rbenv/version; then
28
+ sudo bash -l << EOS
29
+ yum -y install epel-release
30
+ yum -y update
31
+ yum -y install libcgroup libcgroup-devel libcap-ng libcap-ng-devel
32
+ yum -y install gcc-c++ git glibc-headers libffi-devel libxml2 libxml2-devel \
33
+ libxslt libxslt-devel libyaml-devel make openssl-devel \
34
+ readline readline-devel sqlite-devel zlib zlib-devel
35
+ git clone https://github.com/rbenv/rbenv.git /usr/local/rbenv
36
+ ( cd /usr/local/rbenv && sudo src/configure && sudo make -C src )
37
+ echo 'export PATH="/usr/local/rbenv/bin:$PATH"' | tee -a /etc/profile.d/rbenv.sh
38
+ echo 'eval "$(rbenv init -)"' | tee -a /etc/profile.d/rbenv.sh
39
+ git clone https://github.com/rbenv/ruby-build.git /usr/local/rbenv/plugins/ruby-build
40
+ . /etc/profile.d/rbenv.sh
41
+ rbenv install 2.2.5
42
+ rbenv global 2.2.5
43
+ rbenv rehash
44
+
45
+ yum -y install lxc lxc-templates lxc-doc lxc-libs rsync debootstrap
46
+
47
+ mkdir /var/hakoniwa
48
+ mkdir /var/hakoniwa/root
49
+ mkdir /var/hakoniwa/rootfs
50
+ mkdir /var/hakoniwa/bundle
51
+ mkdir /var/hakoniwa/user_homes
52
+ EOS
53
+ fi
54
+ SHELL
55
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hakoniwa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require 'haconiwa'
2
+ require 'pathname'
3
+ haconiwa = Haconiwa::Base.define do |config|
4
+ config.name = "chroot001" # to be hostname
5
+
6
+ root = Pathname.new("/var/haconiwa/root")
7
+ config.add_mount_point "/var/haconiwa/rootfs", to: root, readonly: true
8
+ config.add_mount_point "/lib64", to: root.join("lib64"), readonly: true
9
+ config.add_mount_point "/usr/bin", to: root.join("usr/bin"), readonly: true
10
+ config.add_mount_point "/var/haconiwa/user_homes/haconiwa-test001/home/haconiwa", to: root.join("home/haconiwa")
11
+ config.add_mount_point "proc", to: root.join("proc"), fs: "proc"
12
+ config.chroot_to root
13
+
14
+ # config.namespace.unshare "mount"
15
+ end
16
+
17
+ haconiwa.start("/bin/bash")
data/haconiwa.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'haconiwa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "haconiwa"
8
+ spec.version = Haconiwa::VERSION
9
+ spec.authors = ["Uchio KONDO"]
10
+ spec.email = ["udzura@udzura.jp"]
11
+
12
+ spec.summary = %q{Ruby on Container / helper tools with DSL for your handmade linux containers}
13
+ spec.description = %q{Ruby on Container / helper tools with DSL for your handmade linux containers.}
14
+ spec.homepage = "https://github.com/udzura/haconiwa"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "ffi"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "test-unit", ">= 3"
27
+ spec.add_development_dependency "test-unit-rr"
28
+ spec.add_development_dependency "power_assert"
29
+ spec.add_development_dependency "pry"
30
+ end
data/lib/haconiwa.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "haconiwa/version"
2
+
3
+ module Haconiwa
4
+ end
5
+
6
+ require "haconiwa/base"
@@ -0,0 +1,41 @@
1
+ require "haconiwa/filesystem"
2
+ require "haconiwa/mount_point"
3
+ require "haconiwa/cgroup"
4
+ require "haconiwa/namespace"
5
+ require "haconiwa/capabilities"
6
+ require "haconiwa/runners"
7
+
8
+ module Haconiwa
9
+ class Base
10
+ attr_accessor :name,
11
+ :filesystem,
12
+ :cgroup,
13
+ :namespace,
14
+ :capabilities
15
+
16
+ def self.define(&b)
17
+ new.tap(&b)
18
+ end
19
+
20
+ def initialize
21
+ @filesystem = Filesystem.new
22
+ @cgroup = CGroup.new
23
+ @namespace = Namespace.new
24
+ @capabilities = Capabilities.new
25
+ end
26
+
27
+ # aliases
28
+ def chroot_to(dest)
29
+ self.filesystem.chroot = dest
30
+ end
31
+
32
+ def add_mount_point(point, options)
33
+ self.filesystem.mount_points << MountPoint.new(point, options)
34
+ end
35
+
36
+ def start(init_command='/sbin/init')
37
+ Runners::Linux.run(self, init_command)
38
+ end
39
+ alias run start
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module Haconiwa
2
+ class Capabilities
3
+ def initialize
4
+ @blacklist = []
5
+ @whitelist = []
6
+ end
7
+
8
+ def allow(*keys)
9
+ if keys.first == :all
10
+ @whitelist.clear
11
+ end
12
+ end
13
+
14
+ def drop(*keys)
15
+ @blacklist.concat(keys)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Haconiwa
2
+ class CGroup
3
+ def initialize
4
+ @groups = {}
5
+ end
6
+ attr_reader :groups
7
+
8
+ def [](key)
9
+ @groups[key]
10
+ end
11
+
12
+ def []=(key, value)
13
+ @groups[key] = value
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Haconiwa
2
+ class Filesystem
3
+ def initialize
4
+ @mount_points = []
5
+ end
6
+ attr_accessor :chroot, :mount_points
7
+
8
+ def mount_all!
9
+ Dir.chdir "/"
10
+ system "mount --make-private /"
11
+
12
+ mount_points.each do |mount|
13
+ mount.apply!
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module Haconiwa
2
+ class MountPoint
3
+ def initialize(point, options)
4
+ @src = point
5
+ @dest = options.delete(:to)
6
+ @readonly = options.delete(:readonly)
7
+ @fs = options.delete(:fs)
8
+ @options = options
9
+ end
10
+
11
+ def to_command
12
+ if @fs
13
+ "mount -t #{@fs} #{@src} #{@dest}"
14
+ else
15
+ "mount --bind #{@src} #{@dest}"
16
+ end
17
+ end
18
+
19
+ def apply!
20
+ STDERR.puts to_command
21
+ system to_command
22
+ if @readonly
23
+ STDERR.puts "mount --bind -o remount,ro #{@dest}"
24
+ system "mount --bind -o remount,ro #{@dest}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module Haconiwa
2
+ class Namespace
3
+ def initialize
4
+ @use_ns = []
5
+ @netns_name = nil
6
+ end
7
+
8
+ def unshare(ns_type)
9
+ @use_ns << ns_type
10
+ end
11
+
12
+ def use_netns(name)
13
+ @netns_name = name
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ module Haconiwa
2
+ module Runners
3
+ end
4
+ end
5
+
6
+ require 'haconiwa/runners/linux'
@@ -0,0 +1,29 @@
1
+ module Haconiwa::Runners
2
+ # see http://d.hatena.ne.jp/hiboma/20120518/1337337393
3
+
4
+ class Linux
5
+ UNSHARE = 272
6
+ CLONE_NEWNS = 0x00020000
7
+
8
+ def self.run(base, init_command)
9
+ fork {
10
+ unshare(CLONE_NEWNS)
11
+ system "readlink /proc/$$/ns/mnt"
12
+
13
+ base.filesystem.mount_all!
14
+
15
+ Dir.chroot base.filesystem.chroot
16
+ Dir.chdir "/"
17
+ exec init_command
18
+ }
19
+
20
+ puts "New container: is OK?"
21
+ system "readlink /proc/$$/ns/mnt"
22
+ loop {} # to be in front
23
+ end
24
+
25
+ def self.unshare(flag)
26
+ Kernel.syscall(UNSHARE, flag)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Haconiwa
2
+ VERSION = "0.0.1.pre"
3
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haconiwa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ platform: ruby
6
+ authors:
7
+ - Uchio KONDO
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit-rr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: power_assert
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby on Container / helper tools with DSL for your handmade linux containers.
112
+ email:
113
+ - udzura@udzura.jp
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - LICENSE
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - Vagrantfile
127
+ - bin/console
128
+ - bin/setup
129
+ - examples/chroot.rb
130
+ - haconiwa.gemspec
131
+ - lib/haconiwa.rb
132
+ - lib/haconiwa/base.rb
133
+ - lib/haconiwa/capabilities.rb
134
+ - lib/haconiwa/cgroup.rb
135
+ - lib/haconiwa/filesystem.rb
136
+ - lib/haconiwa/mount_point.rb
137
+ - lib/haconiwa/namespace.rb
138
+ - lib/haconiwa/runners.rb
139
+ - lib/haconiwa/runners/linux.rb
140
+ - lib/haconiwa/version.rb
141
+ homepage: https://github.com/udzura/haconiwa
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">"
157
+ - !ruby/object:Gem::Version
158
+ version: 1.3.1
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.4.5.1
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Ruby on Container / helper tools with DSL for your handmade linux containers
165
+ test_files: []