File size: 10,432 Bytes
b115d50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import importlib.machinery as machinery
import os
import sys
import traceback
import zipfile
from abc import ABC, abstractmethod
from pathlib import Path

import click
from semver import VersionInfo

from steamship import Package, PackageVersion, PluginVersion, Steamship, SteamshipError
from steamship.cli.manifest_init_wizard import validate_handle, validate_version_handle
from steamship.cli.ship_spinner import ship_spinner
from steamship.data import Plugin
from steamship.data.manifest import Manifest
from steamship.data.user import User
from steamship.invocable.lambda_handler import get_class_from_module

DEFAULT_BUILD_IGNORE = [
    "build",
    ".git",
    ".venv",
    ".ipynb_checkpoints",
    ".DS_Store",
    "venv",
    "tests",
    "examples",
    ".idea",
    "__pycache__",
]


def update_config_template(manifest: Manifest):

    path = Path("src/api.py")
    if not path.exists():
        path = Path("api.py")
        if not path.exists():
            raise SteamshipError("Could not find api.py either in root directory or in src.")

    api_module = None
    try:
        sys.path.append(str(path.parent.absolute()))

        # load the API module to allow config inspection / generation
        api_module = machinery.SourceFileLoader("api", str(path)).load_module()
    except Exception:
        click.secho(
            "An error occurred while loading your api.py to check configuration parameters. Full stack trace below.",
            fg="red",
        )
        traceback.print_exc()
        click.get_current_context().abort()

    invocable_type = get_class_from_module(api_module)

    config_parameters = invocable_type.config_cls().get_config_parameters()

    if manifest.configTemplate != config_parameters:
        if len(config_parameters) > 0:
            click.secho("Config parameters changed; updating steamship.json.", fg="cyan")
            for param_name, param in config_parameters.items():
                click.echo(f"{param_name}:")
                click.echo(f"\tType: {param.type}")
                click.echo(f"\tDefault: {param.default}")
                click.echo(f"\tDescription: {param.description}")
        else:
            click.secho("Config parameters removed; updating steamship.json.", fg="cyan")

    manifest.configTemplate = config_parameters
    manifest.save()


def get_archive_path(manifest: Manifest) -> Path:
    return Path(".") / "build" / "archives" / f"{manifest.handle}_v{manifest.version}.zip"


def bundle_deployable(manifest: Manifest):
    archive_path = get_archive_path(manifest)
    archive_path.parent.mkdir(parents=True, exist_ok=True)
    excludes = DEFAULT_BUILD_IGNORE + manifest.build_config.get("ignore", [])

    archive_path.unlink(missing_ok=True)

    # This zipfile packaging is modeled after the typescript CLI.
    # Items in non-excluded root folders are moved to the top-level.

    with zipfile.ZipFile(
        file=archive_path, mode="a", compression=zipfile.ZIP_DEFLATED, allowZip64=False
    ) as zip_file:

        root = Path(".")
        for file_path in root.iterdir():
            if file_path.name not in excludes:
                if file_path.is_dir():
                    for directory, _, files in os.walk(file_path):
                        subdirectory_path = Path(directory)
                        if Path(directory).name not in excludes:
                            for file in files:
                                pypi_file = subdirectory_path / file
                                relative_to = pypi_file.relative_to(file_path)
                                zip_file.write(pypi_file, relative_to)

                else:
                    zip_file.write(file_path)


class DeployableDeployer(ABC):
    @abstractmethod
    def _create_version(self, client: Steamship, manifest: Manifest, thing_id: str):
        pass

    @abstractmethod
    def create_object(self, client: Steamship, manifest: Manifest):
        pass

    @abstractmethod
    def update_object(self, deployable, client: Steamship, manifest: Manifest):
        pass

    @abstractmethod
    def deployable_type(self):
        pass

    def create_or_fetch_deployable(self, client: Steamship, user: User, manifest: Manifest):
        if not manifest.handle or len(manifest.handle) == 0:
            self.ask_for_new_handle(manifest, was_missing=True)

        deployable = None
        while deployable is None:
            click.echo(
                f"Creating / fetching {self.deployable_type()} with handle [{manifest.handle}]... ",
                nl=False,
            )
            try:
                deployable = self.create_object(client, manifest)
                if deployable.user_id != user.id:
                    self.ask_for_new_handle(manifest)
                    deployable = None
            except SteamshipError as e:
                if e.message == "Something went wrong.":
                    self.ask_for_new_handle(manifest)
                else:
                    click.secho(
                        f"Unable to create / fetch {self.deployable_type()}. Server returned message: {e.message}"
                    )
                    click.get_current_context().abort()

        self.update_object(deployable, client, manifest)

        click.echo("Done.")
        return deployable

    def ask_for_new_handle(self, manifest: Manifest, was_missing: bool = False):
        if not was_missing:
            try_again = click.confirm(
                click.style(
                    f"\nIt looks like that handle [{manifest.handle}] is already in use. Would you like to change the handle and try again?",
                    fg="yellow",
                ),
                default=True,
            )
            if not try_again:
                click.get_current_context().abort()

        new_handle = click.prompt(
            f"What handle would you like to use for your {self.deployable_type()}? Valid characters are a-z and -",
            value_proc=validate_handle,
        )
        manifest.handle = new_handle
        manifest.save()

    def create_version(self, client: Steamship, manifest: Manifest, thing_id: str):
        version = None

        if not manifest.version or len(manifest.version) == 0:
            self.ask_for_new_version_handle(manifest, was_missing=True)

        while version is None:
            click.echo(f"Deploying version {manifest.version} of [{manifest.handle}]... ", nl=False)
            try:
                with ship_spinner():
                    version = self._create_version(client, manifest, thing_id)
            except SteamshipError as e:
                if "The object you are trying to create already exists." in e.message:
                    self.ask_for_new_version_handle(manifest)
                else:
                    click.secho(f"\nUnable to deploy {self.deployable_type()} version.", fg="red")
                    click.secho(f"Server returned message: {e.message}")
                    if "ModuleNotFoundError" in e.message:
                        click.secho(
                            "It looks like you may be missing a dependency in your requirements.txt.",
                            fg="yellow",
                        )
                    click.get_current_context().abort()
        click.echo("\nDone. 🚢")

    def ask_for_new_version_handle(self, manifest: Manifest, was_missing: bool = False):
        if not was_missing:
            try_again = click.confirm(
                click.style(
                    f"\nIt looks like that version [{manifest.version}] has already been deployed. Would you like to change the version handle and try again?",
                    fg="yellow",
                ),
                default=True,
            )
            if not try_again:
                click.get_current_context().abort()

        default_new = "1.0.0"
        try:
            default_new = str(VersionInfo.parse(manifest.version).bump_prerelease())
        except ValueError:
            pass
        old_archive_path = get_archive_path(manifest)
        new_version_handle = click.prompt(
            "What should the new version be? Valid characters are a-z, 0-9, . and -",
            value_proc=validate_version_handle,
            default=default_new,
        )
        manifest.version = new_version_handle
        manifest.save()
        new_archive_path = get_archive_path(manifest)
        old_archive_path.rename(new_archive_path)


class PackageDeployer(DeployableDeployer):
    def _create_version(self, client: Steamship, manifest: Manifest, thing_id: str):
        return PackageVersion.create(
            client=client,
            config_template=manifest.config_template_as_dict(),
            handle=manifest.version,
            filename=f"build/archives/{manifest.handle}_v{manifest.version}.zip",
            package_id=thing_id,
        )

    def create_object(self, client: Steamship, manifest: Manifest):
        return Package.create(
            client,
            handle=manifest.handle,
            profile=manifest,
            is_public=manifest.public,
            fetch_if_exists=True,
        )

    def update_object(self, deployable, client: Steamship, manifest: Manifest):
        deployable.profile = manifest

        package = deployable.update(client)
        return package

    def deployable_type(self):
        return "package"


class PluginDeployer(DeployableDeployer):
    def _create_version(self, client: Steamship, manifest: Manifest, thing_id: str):
        return PluginVersion.create(
            client=client,
            config_template=manifest.config_template_as_dict(),
            handle=manifest.version,
            filename=f"build/archives/{manifest.handle}_v{manifest.version}.zip",
            plugin_id=thing_id,
        )

    def create_object(self, client: Steamship, manifest: Manifest):
        return Plugin.create(
            client,
            description=manifest.description,
            is_public=manifest.public,
            transport=manifest.plugin.transport,
            type_=manifest.plugin.type,
            handle=manifest.handle,
            fetch_if_exists=True,
        )

    def update_object(self, deployable, client: Steamship, manifest: Manifest):
        deployable.profile = manifest

        plugin = deployable.update(client)
        return plugin

    def deployable_type(self):
        return "plugin"