-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm2docker.py
More file actions
57 lines (45 loc) · 1.87 KB
/
Copy pathvm2docker.py
File metadata and controls
57 lines (45 loc) · 1.87 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
import os
import sys
import stat
from vm import *
from helper import *
root_dir = os.path.dirname(os.path.abspath(__file__))
docker_dir = os.path.join(root_dir, "dockerfiles")
scripts_dir = os.path.join(root_dir, "scripts")
class VM2Docker(object):
def __init__(self, vfile, vmuser, vmpasswd):
vmx = self._convertvftovmx(vfile)
self.vm = VMWare(vmx, vmuser, vmpasswd)
def __del__(self):
if isinstance(self.vm, VMWare):
logger.info("stop vmware guest: {}".format(self.vm.vmx))
self.vm.stop()
def run(self, imgname='vm2docker:raw'):
if imgname is None or imgname == '':
imgname = 'vm2docker:raw'
self.vm.start()
self._getimg()
cmd = "docker build -t {} {} ".format(imgname, "dockerfiles")
run(cmd)
def _getimg(self):
tar = os.path.join(scripts_dir, "tar.sh")
os.chmod(tar, stat.S_IXGRP)
self.vm.cpfiletoguest(tar, "/tar.sh")
status, res = self.vm.runscriptinguest("/bin/sh", "/tar.sh")
if status is False:
logger.error("tar / failed. please check your vm guest whether has enough disk space left.")
sys.exit(1)
self.vm.cpfilefromguest("/docker2vm_img.tar", os.path.join(docker_dir, "docker2vm_img.tar"))
if not os.path.exists("{}/docker2vm_img.tar".format(docker_dir)):
logger.error("get docker2vm_img.tar from vm guest failed.")
sys.exit(1)
def _convertvftovmx(self, vfile):
file, suffix = os.path.splitext(vfile)
if suffix not in ['.ova', '.ovf', '.vmx']:
raise TypeError("Unsupport VM type {}.".format(file))
sys.exit(1)
vmx = "{}_new.vmx".format(file)
if suffix != '.vmx':
cmd = "ovftool -tt=VMX {} {}".format(vfile, vmx)
run(cmd)
return vmx