6 from vstf.common.utils import check_call
10 LOG = logging.getLogger(__name__)
13 class _ImageManager(object):
15 A qemu-img wrapper to create qcow2 child image from a parent image.
18 def __init__(self, parent_image_path, child_image_dir):
20 :param parent_image_path str: the parent image path.
21 :param child_image_dir str: the destination path to put child images.
23 self._create_child_str = 'qemu-img create -f %(image_type)s %(child_path)s -o backing_file=%(parent_path)s'
24 self._convert_str = "qemu-img convert -O %(image_type)s %(parent_path)s %(child_path)s"
25 self.child_image_dir = child_image_dir
26 self.parent_image_path = parent_image_path
27 assert os.path.isfile(self.parent_image_path)
28 assert os.path.isdir(self.child_image_dir)
30 def create_child_image(self, child_name, full_clone=False, image_type='qcow2'):
32 create a child image and put it in self.child_image_dir.
34 :param child_name: the image name to be created..
35 :return: return the path of child image.
38 image_path = os.path.join(self.child_image_dir, child_name) + '.' + image_type
40 cmd = self._convert_str % {'image_type': image_type, 'child_path': image_path, 'parent_path': self.parent_image_path}
42 cmd = self._create_child_str % {'child_path': image_path, 'parent_path': self.parent_image_path, 'image_type':image_type}
43 check_call(cmd.split())
47 class ImageManager(object):
48 def __init__(self, cfg):
50 ImageManager creates images from configuration context.
52 :param cfg: dict, example:
54 'parent_image': "/mnt/sdb/ubuntu_salt_master.img",
55 'dst_location': "/mnt/sdb",
58 'names': ['vm1','vm2','vm3','vm4']
62 super(ImageManager, self).__init__()
63 cfg = self._check_cfg(cfg)
64 self.parent_image = cfg['parent_image']
65 self.image_dir = cfg['dst_location']
66 self.full_clone = cfg['full_clone']
67 self.image_type = cfg['type']
68 self.names = cfg['names']
69 self.mgr = _ImageManager(self.parent_image, self.image_dir)
73 for key in ('parent_image', 'dst_location', 'full_clone', 'type', 'names'):
75 raise Exception("does't find %s config" % key)
76 if cfg['type'] not in ('raw', 'qcow2'):
77 raise Exception("type:%s not supported, only support 'raw' and 'qcow2'" % cfg['type'])
78 if not cfg['full_clone'] and cfg['type'] == 'raw':
79 raise Exception("only support 'qcow2' for not full_clone image creation" % cfg['type'])
84 create images by configuration context.
86 :return: True for success, False for failure.
88 for name in self.names:
89 image = self.mgr.create_child_image(name, self.full_clone, self.image_type)
90 LOG.info("image: %s created", image)
95 remove all the images created in one go.
97 :return: True for success. Raise exception otherwise.
99 for name in self.names:
100 image_path = os.path.join(self.image_dir, name + '.' + self.image_type)
102 os.unlink(image_path)
103 LOG.info("remove:%s successfully", image_path)
105 LOG.info("cann't find path:%s", image_path)
109 if __name__ == '__main__':
112 parser = argparse.ArgumentParser()
113 parser.add_argument('action', choices = ('create','clean'), help='action:create|clean')
114 parser.add_argument('--config', help='config file to parse')
115 args = parser.parse_args()
116 logging.basicConfig(level=logging.INFO)
117 image_cfg = json.load(open(args.config))
118 mgr = ImageManager(image_cfg)
119 if args.action == 'create':
121 if args.action == 'clean':