mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 15:36:27 +00:00
64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
from abc import ABCMeta
|
|
from abc import abstractmethod
|
|
from common.tools import log_check_call
|
|
from fysom import Fysom
|
|
|
|
|
|
class AbstractPartition(object):
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
events = [{'name': 'create', 'src': 'nonexistent', 'dst': 'created'},
|
|
{'name': 'format', 'src': 'created', 'dst': 'formatted'},
|
|
{'name': 'mount', 'src': 'formatted', 'dst': 'mounted'},
|
|
{'name': 'unmount', 'src': 'mounted', 'dst': 'formatted'},
|
|
]
|
|
|
|
def __init__(self, size, filesystem, callbacks={}):
|
|
self.size = size
|
|
self.filesystem = filesystem
|
|
self.device_path = None
|
|
|
|
callbacks.update({'onbeforecreate': self._create,
|
|
'onbeforeformat': self._format,
|
|
'onbeforemount': self._mount,
|
|
'onbeforeunmount': self._unmount,
|
|
})
|
|
|
|
self.fsm = Fysom({'initial': 'nonexistent',
|
|
'events': self.events,
|
|
'callbacks': callbacks})
|
|
from common.fsm import attach_proxy_methods
|
|
attach_proxy_methods(self, self.events, self.fsm)
|
|
|
|
def state(self):
|
|
return self.fsm.current
|
|
|
|
def force_state(self, state):
|
|
self.fsm.current = state
|
|
|
|
def get_uuid(self):
|
|
[uuid] = log_check_call(['/sbin/blkid', '-s', 'UUID', '-o', 'value', self.device_path])
|
|
return uuid
|
|
|
|
def create(self, volume):
|
|
self.fsm.create(volume=volume)
|
|
|
|
@abstractmethod
|
|
def _create(self, e):
|
|
pass
|
|
|
|
def _format(self, e):
|
|
mkfs = '/sbin/mkfs.{fs}'.format(fs=self.filesystem)
|
|
log_check_call([mkfs, self.device_path])
|
|
|
|
def mount(self, destination):
|
|
self.fsm.mount(destination=destination)
|
|
|
|
def _mount(self, e):
|
|
log_check_call(['/bin/mount', '--types', self.filesystem, self.device_path, e.destination])
|
|
self.mount_dir = e.destination
|
|
|
|
def _unmount(self, e):
|
|
log_check_call(['/bin/umount', self.mount_dir])
|
|
del self.mount_dir
|