Exemple #1
0
def main(factory: str, sha: str, targets_json: str, machines: [],
         platforms: [], app_root_dir: str, publish_tool: str,
         apps_version: str, target_tag: str, target_version: str,
         new_targets_file: str):
    convert_docker_apps()
    status('Searching for Compose Apps in {}'.format(app_root_dir))
    apps = ComposeApps(app_root_dir)
    status('Found Compose Apps: {}'.format(apps.str))

    status('Validating Compose Apps')
    apps.validate()
    status('Compose Apps has been validated: {}'.format(apps.str))

    apps_to_add_to_target = AppsPublisher(factory, publish_tool).publish(
        apps, apps_version)

    status(
        'Creating Targets that refer to the published Apps; tag: {}, version: {}, machines: {}, platforms: {} '
        .format(target_tag, target_version,
                ','.join(machines) if machines else '[]',
                ','.join(platforms) if platforms else '[]'))
    new_targets = create_target(targets_json, apps_to_add_to_target,
                                target_tag, sha, machines, platforms,
                                target_version, new_targets_file)
    if len(new_targets) == 0:
        logger.error('Failed to create Targets for the published Apps')
        return 1

    return 0
Exemple #2
0
 def test_compose_apps_app_images(self):
     app = ComposeApps(self.apps_root_dir)[0]
     expected_images = [
         'hub.foundries.io/test_factory/nginx', 'nginx:1.19.2-alpine',
         'hub.foundries.io/test_factory/app-07:latest'
     ]
     for image in app.images():
         self.assertIn(image, expected_images)
Exemple #3
0
    def setUp(self):
        self.apps_root_dir = tempfile.mkdtemp()
        self.app_name = 'app1'
        os.mkdir(os.path.join(self.apps_root_dir, self.app_name))
        with open(os.path.join(self.apps_root_dir, self.app_name, ComposeApps.App.ComposeFile), 'w') as compose_file:
            yaml_data = yaml.safe_load(self.ComposeAppDesc)
            yaml.dump(yaml_data, compose_file)

        self.apps = ComposeApps(self.apps_root_dir)
Exemple #4
0
class ComposeAppsTest(unittest.TestCase):
    ComposeAppDesc = '''
    services:
      nginx-01:
        image: hub.foundries.io/test_factory/nginx
        ports:
        - ${PORT-9999}:80
        restart: always
      nginx-02:
        image: nginx:1.19.2-alpine
        ports:
        - ${PORT-8888}:80
        restart: always
      python-www:
        image: hub.foundries.io/test_factory/app-07:latest
        ports:
        - 9987:80
        restart: always
        volumes:
        - .:/var/http
    version: '3.2'
    '''

    def setUp(self):
        self.apps_root_dir = tempfile.mkdtemp()
        self.app_name = 'app1'
        os.mkdir(os.path.join(self.apps_root_dir, self.app_name))
        with open(os.path.join(self.apps_root_dir, self.app_name, ComposeApps.App.ComposeFile), 'w') as compose_file:
            yaml_data = yaml.safe_load(self.ComposeAppDesc)
            yaml.dump(yaml_data, compose_file)

        self.apps = ComposeApps(self.apps_root_dir)

    def tearDown(self):
        shutil.rmtree(self.apps_root_dir)

    def test_compose_apps_init(self):
        self.assertEqual(len(self.apps), 1)
        self.apps.validate()
        self.assertEqual(self.apps.str, self.app_name)
        self.assertEqual(len(self.apps), 1)

    def test_compose_apps_app_init(self):
        app = self.apps[0]
        app.validate()
        self.assertEqual(len(app.services()), 3)

    def test_compose_apps_app_images(self):
        app = self.apps[0]
        expected_images = ['hub.foundries.io/test_factory/nginx',
                           'nginx:1.19.2-alpine',
                           'hub.foundries.io/test_factory/app-07:latest']
        for image in app.images():
            self.assertIn(image, expected_images)
Exemple #5
0
    def _fetch_apps(self, target, apps_shortlist=None, force=False):
        fetched_apps = []
        for app_name, app_uri in target.apps():
            if apps_shortlist and app_name not in apps_shortlist:
                logger.info('{} is not in the shortlist, skipping it'.format(app_name))
                continue

            uri = DockerRegistryClient.parse_image_uri(app_uri)
            app_dir = os.path.join(self.apps_dir(target.name), app_name, uri.hash)
            if os.path.exists(app_dir) and not force:
                logger.info('App has been already fetched; Target: {}, App: {}'.format(target.name, app_name))
                continue

            os.makedirs(app_dir, exist_ok=True)
            manifest_data = self._registry_client.pull_manifest(uri)
            with open(os.path.join(app_dir, self.ManifestFile), 'wb') as f:
                f.write(manifest_data)

            manifest = json.loads(manifest_data)
            app_blob_digest = manifest["layers"][0]["digest"]
            app_blob_hash = app_blob_digest[len('sha256:'):]
            app_blob = self._registry_client.pull_layer(uri, app_blob_digest)
            app_blob_file = os.path.join(app_dir, app_blob_hash + self.ArchiveFileExt)
            with open(app_blob_file, 'wb') as f:
                f.write(app_blob)

            with tarfile.open(fileobj=BIO(app_blob)) as t:
                t.extract('docker-compose.yml', app_dir)

            fetched_apps.append(ComposeApps.App(app_name, app_dir))
        return fetched_apps
Exemple #6
0
    def setUp(self):
        self.apps_root_dir = tempfile.mkdtemp()
        self.app_name = 'app1'
        self.factory = 'test_factory'
        self.publish_tool = 'some-fake-publish-tool'

        os.mkdir(os.path.join(self.apps_root_dir, self.app_name))
        with open(os.path.join(self.apps_root_dir, self.app_name, ComposeApps.App.ComposeFile), 'w') as compose_file:
            yaml_data = yaml.safe_load(self.ComposeAppDesc)
            yaml.dump(yaml_data, compose_file)

        self.apps = ComposeApps(self.apps_root_dir)
        self.publisher = AppsPublisher(self.factory, self.publish_tool)
Exemple #7
0
    def _fetch_apps(self, target, apps_shortlist=None, force=False):
        for app_name, app_uri in target.apps():
            if apps_shortlist and app_name not in apps_shortlist:
                logger.info('{} is not in the shortlist, skipping it'.format(app_name))
                continue

            app_dir = os.path.join(self.apps_dir(target.name), app_name)
            if not os.path.exists(app_dir) or force:
                os.makedirs(app_dir, exist_ok=True)
                logger.info('Downloading App; Target: {}, App: {}, Uri: {} '.format(target.name, app_name, app_uri))
                self._registry_client.download_compose_app(app_uri, app_dir)
            else:
                logger.info('App has been already fetched; Target: {}, App: {}'.format(target.name, app_name))
        return ComposeApps(self.apps_dir(target.name))
Exemple #8
0
 def test_compose_apps_app_init(self):
     app = ComposeApps(self.apps_root_dir)[0]
     self.assertEqual(len(app.services()), 3)
Exemple #9
0
 def test_compose_apps_init(self):
     apps = ComposeApps(self.apps_root_dir)
     self.assertEqual(len(apps), 1)
     self.assertEqual(apps.str, self.app_name)
     self.assertEqual(len(apps), 1)