forked from evrone/lxc-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.rb
More file actions
152 lines (129 loc) · 4.08 KB
/
frontend.rb
File metadata and controls
152 lines (129 loc) · 4.08 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class LXCFrontend < Sinatra::Base
MEGABYTE = 1024.0 * 1024.0 # to calculate available memory
LXC_VM_PATH = "/var/lib/lxc" # default path to LXC data on Ubuntu
LXC_IP_REGEXP = /lxc.network.ipv4 = ([\d.]+)\/([\d]+)/ # regexp to find server IP
enable :sessions
use Rack::Flash
configure do
set :public_folder, Proc.new { File.join(root, "static") }
end
helpers do
def generate_confirm_code
rand(1999..9999).to_s
end
def bytes_in_megabytes(bytes)
(bytes / MEGABYTE).round
end
def lxc_ip_for(name)
config = lxc_config_for(name)
if config.match(LXC_IP_REGEXP)
$1
end
end
def lxc_config_for(name)
config_path = lxc_config_path_for(name)
File.open(config_path).read
end
def lxc_config_path_for(name)
File.join(LXC_VM_PATH, name.to_s, "config")
end
def lxc_interfaces_path_for(name)
File.join(LXC_VM_PATH, name.to_s, "rootfs", "etc", "network", "interfaces")
end
def lxc_interfaces_for(name)
config_path = lxc_interfaces_path_for(name)
File.open(config_path).read
end
def update_lxc_interfaces_for(name, content)
config_path = lxc_interfaces_path_for(name)
content.gsub!("\r\n", "\n") # otherwise it will be saved in DOS-format
File.open(config_path, 'w') do |file|
file.write(content)
end
end
def update_lxc_config_for(name, content)
config_path = lxc_config_path_for(name)
content.gsub!("\r\n", "\n") # otherwise it will be saved in DOS-format
File.open(config_path, 'w') do |file|
file.write(content)
end
end
end
get "/" do
LXC.use_sudo = true
@containers = LXC.containers
erb :index
end
get "/containers/:container/config" do
@container = LXC.container(params[:container])
if @container.exists?
@config_data = lxc_config_for(@container.name)
erb :config
else
halt 422
end
end
get "/containers/:container/interfaces" do
@container = LXC.container(params[:container])
if @container.exists?
@config_data = lxc_interfaces_for(@container.name)
erb :interfaces
else
halt 422
end
end
post "/containers/:container/update_config" do
container = LXC.container(params[:container])
if container.exists? && params[:config]
update_lxc_config_for(container.name, params[:config])
container.restart
flash[:notice] = "You have successfully saved #{container.name} config. The container was restarted."
end
redirect "/"
end
post "/containers/:container/update_interfaces" do
container = LXC.container(params[:container])
if container.exists? && params[:interfaces]
update_lxc_interfaces_for(container.name, params[:interfaces])
container.restart
flash[:notice] = "You have successfully saved #{container.name} interfaces. The container was restarted."
end
redirect "/"
end
get "/containers/:container/stop" do
container = LXC.container(params[:container])
if container.exists? && container.running?
container.stop
flash[:notice] = "You have successfully stopped #{container.name}."
sleep 0.5 # waiting when it will be stopped
end
redirect "/"
end
get "/containers/:container/start" do
container = LXC.container(params[:container])
if container.exists?
container.start
flash[:notice] = "You have successfully started #{container.name}."
sleep 0.5 # waiting when it will be started
end
redirect "/"
end
get "/containers/:container/secure_destroy" do
@container = LXC.container(params[:container])
if @container.exists?
session[:secure_destroy_key] = generate_confirm_code
erb :secure_destroy
else
halt 422
end
end
post "/containers/:container/destroy" do
container = LXC.container(params[:container])
if container.exists? && session[:secure_destroy_key] == params[:secure_destroy_key]
container.destroy
flash[:notice] = "You have successfully destroyed #{container.name}."
sleep 0.5 # waiting when it will be started
end
redirect "/"
end
end