-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
132 lines (104 loc) · 2.76 KB
/
plugin.rb
File metadata and controls
132 lines (104 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'bundler/setup'
require_relative 'docker_plugin_middleware'
require 'json'
require 'fileutils'
class TestVolumePlugin
def initialize path="./images", mountpath = "./mounts"
FileUtils.mkdir_p(path)
FileUtils.mkdir_p(mountpath)
@path = path
@mountpath = mountpath
end
## Utility methods
def image_path(name)
File.expand_path(File.join(@path,name))
end
def volume_exists(name)
File.exists?(image_path(name))
end
def mount_path(name)
File.expand_path(File.join(@mountpath, name))
end
def mounted_path(name)
path = mount_path(name)
return File.exists?(path) ? path : nil
end
def create_volume(name)
path = image_path(name)
## FIXME: Error checking:
p system("dd","if=/dev/zero","of=#{path}","bs=1M","count=128")
p system("mkfs.ext4","-F",path)
end
def mount_volume(name, path)
img = image_path(name)
# FIXME: Error check
p system("mount","-o","loop", img,path).inspect
end
def unmount_volume(name,path)
# FIXME: Error checks
STDERR.puts system("umount",path).inspect
end
def remove_volume(name)
p File.unlink(image_path(name))
end
## Docker Volume plugin API
# We don't need any special prep, so hold off on creation until Mount
def create name
return {"Err" => "Volume #{name} already exists"} if volume_exists?(name)
create_volume(name)
{}
end
def remove name
return {"Err" => "Volume is mounted. Unmount first"} if mounted_path(name)
begin
remove_volume(name)
rescue
{"Err" => "Error while unlinking volume"}
end
{}
end
def mount name
return {"Err" => "Already mounted"} if mounted_path(name)
return {"Err" => "No such volume"} if !volume_exists(name)
mpath = mount_path(name)
return {"Err" => "Unable to create mount point #{mpath}"} if !FileUtils.mkdir_p(mpath)
mount_volume(name, mpath)
path(name)
rescue Exception => e
{"Err" => "Exception: #{e.message} / #{e.backtrace.inspect}"}
end
def path name
if path = mounted_path(name)
{"MountPoint" => path}
else
{"Err" => "Volume is not mounted"}
end
end
def unmount name
if path = mounted_path(name)
unmount_volume(name,path)
STDERR.puts FileUtils.rmdir(path).inspect
{}
else
{"Err" => "Volume does not exist or is not mounted"}
end
end
def get name
return {"Err" => "Volume does not exists"} if !volume_exists(name)
ret = {
"Volume" => {
"Name" => name,
}
}
ret["MountPoint"] ||= mounted_path(name)
ret
end
def list args
{"Err" => "List failed"}
end
end
app = DockerPlugins.new_app
app.set :volumedriver, TestVolumePlugin.new
app.set :networkdriver, nil
#app.set :networkdriver, TestNetworkPlugin.new
app.run!