-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.py
256 lines (194 loc) · 7.51 KB
/
command.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012-2014 Kouhei Maeda <mkouhei@palmtb.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os.path
import argparse
from __init__ import __version__
import control
def parse_options():
prs = argparse.ArgumentParser(description='usage')
prs.add_argument('-V', '--version', action='version', version=__version__)
setoption(prs, 'dirpath')
subprs = prs.add_subparsers(help='commands')
# Make repository
prs_make_repo(subprs)
# Add new node
prs_add_node(subprs)
# Create container
prs_create_container(subprs)
# Start container
prs_start_container(subprs)
# Shutdown container ; not yet supported
# prs_shutdown_container(subprs)
# destroy container
prs_destroy_container(subprs)
# Delete container
prs_delete_container(subprs)
# Listing nodes
prs_list_nodes(subprs)
# Listing defined containers
prs_list_containers(subprs)
args = prs.parse_args()
return args
# Make repository
def prs_make_repo(obj):
prs_make_repo = obj.add_parser('newrepo', help='make new repository')
setoption(prs_make_repo, 'dirpath')
prs_make_repo.set_defaults(func=make_repo)
# Add new node
def prs_add_node(obj):
prs_add_node = obj.add_parser('addnode', help='add new lxc host node')
setoption(prs_add_node, 'nodename')
prs_add_node.set_defaults(func=add_node)
# Create container
def prs_create_container(obj):
prs_create_container = obj.add_parser('deploy',
help='create lxc container')
setoption(prs_create_container, 'nodename')
setoption(prs_create_container, 'contname')
setoption(prs_create_container, 'vcpu')
setoption(prs_create_container, 'memory')
setoption(prs_create_container, 'clock')
setoption(prs_create_container, 'network')
setoption(prs_create_container, 'rootfs')
setoption(prs_create_container, 'debootstrap')
prs_create_container.set_defaults(func=create_container)
# Start container
def prs_start_container(obj):
prs_start_container = obj.add_parser('start',
help='start lxc container')
setoption(prs_start_container, 'nodename')
setoption(prs_start_container, 'contname')
prs_start_container.set_defaults(func=start_container)
'''
# Shutdown container; not yet supported by libvirt
def prs_shutdown_container(obj):
prs_shutdown_container = obj.add_parser('shutdown',
help='shutdown lxc container')
setoption(prs_shutdown_container, 'nodename')
setoption(prs_shutdown_container, 'contname')
prs_shutdown_container.set_defaults(func=shutdown_container)
'''
# Destroy container
def prs_destroy_container(obj):
prs_destroy_container = obj.add_parser('destroy',
help='destroy lxc container')
setoption(prs_destroy_container, 'nodename')
setoption(prs_destroy_container, 'contname')
prs_destroy_container.set_defaults(func=destroy_container)
# Delete(undefine) container
def prs_delete_container(obj):
prs_delete_container = obj.add_parser('delete',
help='delete lxc container')
setoption(prs_delete_container, 'nodename')
setoption(prs_delete_container, 'contname')
prs_delete_container.set_defaults(func=delete_container)
# Listing nodes
def prs_list_nodes(obj):
prs_list_nodes = obj.add_parser('listnode',
help='listing nodes')
prs_list_nodes.set_defaults(func=list_nodes)
# Listing defined containers
def prs_list_containers(obj):
prs_list_containers = obj.add_parser('listcont',
help='listing containers')
setoption(prs_list_containers, 'nodename')
prs_list_containers.set_defaults(func=list_containers)
def setoption(obj, kword):
if kword == 'dirpath':
obj.add_argument('-d', '--dirpath', action='store',
help='specify repository path')
elif kword == 'nodename':
obj.add_argument('-n', '--nodename', action='store', required=True,
help='hostname or IP address of lxc host node')
elif kword == 'contname':
obj.add_argument('-c', '--contname', action='store', required=True,
help='container name')
elif kword == 'vcpu':
obj.add_argument('--vcpu', action='store',
help='vcpu number / default: 1')
elif kword == 'memory':
obj.add_argument('--mem', action='store',
help='memory size / default: 64000(KB)')
elif kword == 'clock':
obj.add_argument('--clock', action='store',
help='clock / default: utc')
elif kword == 'network':
obj.add_argument('--network', action='store',
help='network type/ default: default')
elif kword == 'rootfs':
obj.add_argument(
'--rootfs', action='store_true',
help='create rootfs path to /var/lib/lxc/$(contname)')
elif kword == 'debootstrap':
obj.add_argument(
'--debian', action='store_true',
help='create rootfs image with debootstrap')
def repopath(args):
if args.__dict__.get('dirpath'):
dirpath = args.__dict__.get('dirpath')
else:
dirpath = os.path.abspath(os.environ['HOME'] + '/.iori') + '/'
return dirpath
def nodename(args):
if args.__dict__.get('nodename'):
nodename = args.__dict__.get('nodename')
else:
nodename = 'localhost'
return nodename
def make_repo(args):
ctl = control.Control(repopath(args))
ctl.make_repo()
def add_node(args):
if args.__dict__.get('nodename'):
nodename = args.__dict__.get('nodename')
ctl = control.Control(repopath(args))
ctl.add_node(nodename)
def create_container(args):
ctl = control.Control(repopath(args), nodename(args))
ctl.create_container(args)
def start_container(args):
ctl = control.Control(repopath(args), nodename(args))
ctl.start_container(args)
def list_nodes(args):
ctl = control.Control(repopath(args))
ctl.list_nodes()
def list_containers(args):
ctl = control.Control(repopath(args), nodename(args))
ctl.list_containers(args)
'''
def shutdown_container(args):
ctl = control.Control(repopath(args))
ctl.delete_container(args)
'''
def destroy_container(args):
ctl = control.Control(repopath(args), nodename(args))
ctl.destroy_container(args)
def delete_container(args):
ctl = control.Control(repopath(args), nodename(args))
ctl.delete_container(args)
def print_error(error):
print("ERROR: %s" % error)
def main():
try:
args = parse_options()
args.func(args)
except RuntimeError as error:
print_error(error)
sys.exit(1)
except UnboundLocalError as error:
print_error(e)
sys.exit(1)
if __name__ == '__main__':
main()