Check-in [33f6d5eeca]
Not logged in
Overview
Comment:Implemented list --state. Fixed first start of vm, when no monitor socket exists yet
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 33f6d5eeca95e3d854085f5aeed18aaba23f3464
User & Date: vitus on 2015-11-18 14:44:29
Other Links: manifest | tags
Context
2015-12-11
15:27
More or less written command line interface to create vms. Fixed monitor path in the start.template check-in: e43aa4d9b2 user: vitus tags: trunk
2015-11-18
14:44
Implemented list --state. Fixed first start of vm, when no monitor socket exists yet check-in: 33f6d5eeca user: vitus tags: trunk
2015-11-17
15:36
Skip unexisting dirs in find_vm check-in: 2d6b5b3575 user: vitus tags: trunk
Changes

Modified vws from [14e25b598f] to [a01d53b13c].

10
11
12
13
14
15
16
17


18
19
20
21
22
23



24
25










26
27
28
29
30
31
32
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







-
+
+






+
+
+
-
-
+
+
+
+
+
+
+
+
+
+







def find_vm(name):
    search_path=[os.environ['HOME']+"/VWs",
        config.get("directories","SharedVMs"),
        config.get("directories","AutostartVMs")]
    for dirname in search_path:
        if not os.access(dirname,os.X_OK):
            continue
        if name in os.listdir(dirname):
        if ( name in os.listdir(dirname) and
             os.access(dirname+"/"+name+"/start",os.X_OK)):
            return dirname+"/"+name
    raise ValueError("Machine "+name+" not found.")


def connect_vm(vm_dir):
    sock=socket.socket(socket.AF_UNIX)
    if not os.access(vm_dir+"/monitor",os.W_OK):
        return None
    try:
    sock.connect(vm_dir+"/monitor")
    greeting=sock.recv(1024)
        sock.connect(vm_dir+"/monitor")
    except IOError as e:
        if e.errno == errno.ECONNREFUSED:
            # virtal machine is not running
            return None
        else:
            raise e
    r,w,x=select.select([sock],[],[],0.001)
    if sock in r:
        greeting=sock.recv(1024)
    return sock

def send_command(sock,command):
    fcntl.flock(sock,fcntl.LOCK_EX)
    try:
        sock.send(command+"\n")
        answer=""
146
147
148
149
150
151
152


153
154
155
















156

157
158
159
160
161
162
163
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







+
+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+







    count = 0
    search_path=[os.environ['HOME']+"/VWs",
        config.get("directories","SharedVMs"),
        config.get("directories","AutostartVMs")]
    for dirname in search_path:
        if not os.access(dirname+"/.",os.X_OK):
            continue
        maxlen=0
        vms=[]
        for vmname in os.listdir(dirname):
            if os.access(dirname+"/"+vmname+"/start",os.X_OK):
                count += 1
                f=[vmname]
                if maxlen < len(vmname):
                    maxlen = len(vmname)
                if options.state:
                    sock = connect_vm(dirname+"/"+vmname)
                    if sock is None:
                        state = "stopped"
                    else:
                        sock.close()
                        state = "running"
                    f.append(state)
                vms.append(f)
        for f in sorted(vms):
            if len(f)==2:
                print "%*s %s" % (-maxlen,f[0],f[1])
            else:
                print vmname
                print f[0]
    if not count:
        sys.exit(1)
def cmd_version(options):
    print VERSION
#
# Utility functions for arg parsing
#
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
258
259
260
261
262
263
264


265


266







267
268
269
270
271
272

273
274
275
276
277
278
279
280
281
282







-
-
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-










p=new_command(cmds,'spiceuri',help='Output spice URI of machine')
parsed_args=args.parse_args(sys.argv[1:])

parsed_args.stopped = False
stopped_vm_commands = ['start','snapshot','revert','commit','snapshots']
if hasattr(parsed_args,'machine'):
    parsed_args.dir=find_vm(parsed_args.machine)
    try:
        parsed_args.sock=connect_vm(parsed_args.dir)
    parsed_args.sock=connect_vm(parsed_args.dir)
    except IOError as e:
        if e.errno == errno.ECONNREFUSED:
    if parsed_args.sock  is None:
            # virtal machine is not running
            if not parsed_args.command in stopped_vm_commands:
                print >>sys.stderr, "Virtual machine %s is not running."%parsed_args.machine
                sys.exit(1)
            else:
                parsed_args.stopped = True
        else:
        if not parsed_args.command in stopped_vm_commands:
            print >>sys.stderr, "Virtual machine %s is not running."%parsed_args.machine
            sys.exit(1)
        else:
            parsed_args.stopped = True

            raise e
funcname="cmd_"+parsed_args.command

if hasattr(parsed_args,"subcommand"):
    funcname+="_"+parsed_args.subcommand
try:
   func=globals()[funcname]
except KeyError:
    print >>sys.stderr,"Operation %s is not implemented"%funcname
    sys.exit(3)
func(parsed_args)