Ejemplo n.º 1
0
def cmd_prepare(pod_id, config_path):
    """Prepare a pod directory, or no-op if pod exists."""
    oses.assert_root_privilege()
    # Make sure that it is safe to create a pod with this ID.
    ASSERT.not_equal(
        models.pod_id_to_machine_id(pod_id),
        models.read_host_machine_id(),
    )
    # Check before really preparing the pod.
    if g1.files.lexists(_get_pod_dir_path(pod_id)):
        LOG.info('skip duplicated pod: %s', pod_id)
        return
    config = jsons.load_dataobject(models.PodConfig, config_path)
    tmp_path = _create_tmp_pod_dir()
    try:
        _prepare_pod_dir(tmp_path, pod_id, config)
        with locks.acquiring_exclusive(_get_active_path()):
            if _maybe_move_pod_dir_to_active(tmp_path, pod_id):
                tmp_path = None
            else:
                LOG.info('skip duplicated pod: %s', pod_id)
    finally:
        if tmp_path:
            _remove_pod_dir(tmp_path)
Ejemplo n.º 2
0
    def test_install(self):
        bundle_dir = self.make_bundle_dir()
        ops_dir = self.make_ops_dir()

        # Test install.
        self.assertTrue(ops_dir.install(bundle_dir, ops_dir.path))
        # Check ops dir structure.
        self.assertEqual(
            self.list_dir(self.test_ops_dir_path),
            ['metadata', 'refs', 'volumes'],
        )
        self.assertEqual(
            self.list_dir(self.test_ops_dir_path / 'volumes'),
            ['some-volume'],
        )
        # Check metadata.
        metadata = jsons.load_dataobject(
            models.PodMetadata,
            self.test_ops_dir_path / 'metadata',
        )
        self.assertEqual(metadata.label, self.DEPLOY_INSTRUCTION.label)
        self.assertEqual(metadata.version, self.DEPLOY_INSTRUCTION.version)
        self.assertEqual(metadata.images, self.DEPLOY_INSTRUCTION.images)
        self.assertEqual(
            metadata.systemd_unit_configs,
            [self.CONFIG_1, self.CONFIG_2],
        )
        # Check volumes.
        self.scripts_mock.tar_extract.assert_called_once()
        # Check images.
        self.ctr_scripts_mock.ctr_import_image.assert_called_once_with(
            self.test_bundle_dir_path / self.BUNDLE_IMAGE_RELPATH,
        )
        # Check pods.
        self.ctr_scripts_mock.ctr_prepare_pod.assert_called_once_with(
            self.POD_ID, unittest.mock.ANY
        )
        self.ctr_scripts_mock.ctr_add_ref_to_pod.assert_called_once_with(
            self.POD_ID,
            self.test_ops_dir_path / 'refs' / self.POD_ID,
        )
        # Check systemd units.
        self.systemds_mock.install.assert_has_calls([
            unittest.mock.call(
                self.CONFIG_1,
                ops_dir.metadata,
                self.GROUP,
                self.UNIT_1,
                {
                    'ops_database_url': 'tcp://127.0.0.1:2390',
                },
                {
                    'port_1': '8001',
                    'port_2': '8002',
                },
            ),
            unittest.mock.call(
                self.CONFIG_2,
                ops_dir.metadata,
                self.GROUP,
                self.UNIT_2,
                {
                    'ops_database_url': 'tcp://127.0.0.1:2390',
                },
                {
                    'port_1': '8001',
                    'port_2': '8002',
                },
            ),
        ])
        self.systemds_mock.daemon_reload.assert_called_once()

        # Test uninstall.
        self.systemds_mock.daemon_reload.reset_mock()
        self.assertTrue(ops_dir.uninstall())
        self.systemds_mock.uninstall.assert_has_calls([
            unittest.mock.call(self.CONFIG_1),
            unittest.mock.call(self.CONFIG_2),
        ])
        self.systemds_mock.daemon_reload.assert_called_once()
        self.ctr_scripts_mock.ctr_remove_pod.assert_called_once_with(
            self.POD_ID
        )
        self.ctr_scripts_mock.ctr_remove_image.assert_called_once_with(
            self.DEPLOY_INSTRUCTION.images[0],
            skip_active=True,
        )
        self.assertTrue(g1.files.is_empty_dir(self.test_ops_dir_path))

        self.assertFalse(ops_dir.uninstall())
Ejemplo n.º 3
0
def _read_orig_config(pod_dir_path):
    return jsons.load_dataobject(
        models.PodConfig, _get_orig_config_path(pod_dir_path)
    )
Ejemplo n.º 4
0
 def __init__(self, path):
     self.path = path
     self.deploy_instruction = jsons.load_dataobject(
         self.deploy_instruction_type,
         ASSERT.predicate(self.deploy_instruction_path, Path.is_file))
     self.post_init()
Ejemplo n.º 5
0
 def metadata(self):  # pylint: disable=function-redefined
     return jsons.load_dataobject(
         self.metadata_type,
         ASSERT.predicate(self.metadata_path, Path.is_file),
     )
Ejemplo n.º 6
0
 def load(cls, path):
     return jsons.load_dataobject(cls, path)
Ejemplo n.º 7
0
def read_metadata(image_dir_path):
    """Read image metadata from an image directory."""
    return jsons.load_dataobject(
        ImageMetadata, _get_metadata_path(image_dir_path)
    )