Exemplo n.º 1
0
class VagrantDriver(SandboxDriver):
    """Class to deal with Vagrant Boxes.
    """
    @staticmethod
    def status():
        """
        Return the status of this vm instance
        :return: the output of vagrant status in machine readable format
        """
        return Vagrant.status()

    def add_box(self, box_name):
        """Function to add a vagrant box if not exists.
        Args:
            box_name (str): the name of box, ex ubuntu/trusty64
        """
        Vagrant.box_add(box_name)

    def __init__(self, (box_name, box_id)):
        """ init the challenge based on box_name in the directory named box_id
        Args:
            box_name (str): the name of box, ex ubuntu/trusty64
            box_id (str): the id of box

        """
        # Add a base box if not exists
        self.vagrantAddBox(box_name)

        # Create a challenge directory
        if not os.path.exists("./data"):
            os.makedirs("./data")

            tmp_curr_dir = "./data"
            if not os.path.exists(tmp_curr_dir + "/boxes"):
                os.makedirs(tmp_curr_dir + "/boxes")
                tmp_curr_dir += "/boxes"

                challenge_id_base = box_name.replace('/', '_')
                i = 1
                challenge_id = challenge_id_base + str(i)
                while os.path.exists(tmp_curr_dir + "/" + challenge_id):
                    i += 1
                    challenge_id = challenge_id_base + str(i)

                self.boxId = challenge_id
                os.makedirs(tmp_curr_dir + "/" + challenge_id)
                tmp_curr_dir += "/" + challenge_id

        os.chdir(SandboxDriver.HACKADEMIC_TMP_CHAL_DIR + box_id)
        Vagrant.init(box_name)
        self.setup()