Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENG-4443 Part 1. Create update scripts to update an app and the manifest #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Go stuff
go.work
go.work.sum

# VSCode stuff
.vscode/
23 changes: 0 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,6 @@ linters-settings:
gomodguard:
allowed:
modules:
- github.com/nextmv-io/sdk
- github.com/nextmv-io/sdk/route
- github.com/nextmv-io/sdk/route/osrm
- github.com/nextmv-io/sdk/route/here
- github.com/nextmv-io/sdk/route/google
- github.com/nextmv-io/sdk/route/routingkit
- github.com/nextmv-io/sdk/measure/here
- github.com/nextmv-io/sdk/measure/osrm
- github.com/nextmv-io/sdk/measure/google
- github.com/nextmv-io/sdk/measure/routingkit
- googlemaps.github.io/maps
- github.com/dgraph-io/ristretto
- go.uber.org/mock
- github.com/google/go-cmp
- github.com/hashicorp/golang-lru
- github.com/nextmv-io/go-routingkit
- github.com/twpayne/go-polyline
- googlemaps.github.io/maps
- github.com/itzg/go-flagsfiller
- github.com/gorilla/schema
- github.com/google/uuid
- github.com/xeipuuv/gojsonschema
- github.com/danielgtaylor/huma
# Allow increased cyclomatic complexity for examples.
gocyclo:
min-complexity: 45
Expand Down
23 changes: 23 additions & 0 deletions .nextmv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Nextmv Workflows

This directory contains functionality for managing community apps.

## Requirements

- For Python scripts please install the packages in `requirements.txt`:

```bash
pip install -r requirements.txt
```

## Use

- To update one or more apps. Standing in the `./nextmv` directory, run:

```bash
python update_apps.py \
-a app1=version1,app2=version2 \
-b BUCKET \
-f FOLDER \
-m MANIFEST_FILE
```
2 changes: 2 additions & 0 deletions .nextmv/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyyaml==6.0.1
ruff==0.1.7
195 changes: 195 additions & 0 deletions .nextmv/update_apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
"""
This script updates one or more applications by:

1. Updating the version.
2. Updating the SDK version (if the app is written in Go).
3. Updating the manifest file and uploading it.

Execute from the ./nextmv directory:

```bash
python update_apps.py \
-a app1=version1,app2=version2 \
-b BUCKET \
-f FOLDER \
-m MANIFEST_FILE
```
"""

import argparse
import copy
import os
import subprocess
from typing import Any

import yaml
from boto3 import client as s3Client

parser = argparse.ArgumentParser(description="Update community apps in the Marketplace.")
parser.add_argument(
"--apps",
"-a",
type=str,
help="Apps to release, with the version. Example: knapsack-gosdk=v1.0.0,knapsack-pyomo=v1.0.0",
required=True,
)
parser.add_argument(
"--bucket",
"-b",
type=str,
help="S3 bucket.",
required=True,
)
parser.add_argument(
"--folder",
"-f",
type=str,
help="S3 bucket folder.",
required=True,
)
parser.add_argument(
"--manifest",
"-m",
type=str,
help="Manifest file.",
required=True,
)
args = parser.parse_args()


def main():
"""
Entry point for the script.
"""

apps = [
{
"name": app.split("=")[0],
"version": app.split("=")[1],
}
for app in args.apps.split(",")
]
apps.sort(key=lambda x: x["name"])
client = s3Client("s3")

manifest = get_manifest(
client=client,
bucket=args.bucket,
folder=args.folder,
manifest_file=args.manifest,
)
workflow_configuration = read_yaml(
filepath=os.path.join(os.getcwd(), "workflow-configuration.yml"),
)
for app in apps:
if "v" not in app["version"]:
app["version"] = f"v{app['version']}"

update_app(
name=app["name"],
version=app["version"],
workflow_configuration=workflow_configuration,
)
manifest = update_manifest(manifest, app)

upload_manifest(
client=client,
bucket=args.bucket,
folder=args.folder,
manifest_file=args.manifest,
manifest=manifest,
)


def get_manifest(
client: s3Client,
bucket: str,
folder: str,
manifest_file: str,
) -> dict[str, Any]:
"""Returns the manifest from the S3 bucket."""

result = client.get_object(Bucket=bucket, Key=f"{folder}/{manifest_file}")
return yaml.safe_load(result["Body"].read())


def read_yaml(filepath: str) -> dict[str, Any]:
"""Returns the YAML file in the path."""

with open(filepath) as f:
return yaml.safe_load(f)


def update_app(name: str, version: str, workflow_configuration: dict[str, Any]):
"""Updates the app with the new version."""

workflow_info = {}
for app in workflow_configuration["apps"]:
if app["name"] == name:
workflow_info = app
break

with open(os.path.join(os.getcwd(), "..", name, "VERSION"), "w") as f:
f.write(version + "\n")

if workflow_info["type"] == "go":
sdk_version = workflow_info["sdk_version"]
if "v" not in sdk_version:
sdk_version = f"v{sdk_version}"

_ = subprocess.run(
["go", "get", f"github.com/nextmv-io/sdk@{sdk_version}"],
capture_output=True,
text=True,
check=True,
cwd=os.path.join("..", name),
)
_ = subprocess.run(
["go", "mod", "tidy"],
capture_output=True,
text=True,
check=True,
cwd=os.path.join("..", name),
)


def update_manifest(
old: dict[str, Any],
app: dict[str, Any],
) -> dict[str, Any]:
"""Updates the manifest with the new apps."""

new = copy.deepcopy(old)
for manifest_app in new["apps"]:
if manifest_app["name"] == app["name"]:
manifest_app["latest"] = app["version"]
manifest_app["versions"].append(app["version"])
break

return new


def upload_manifest(
client: s3Client,
bucket: str,
folder: str,
manifest_file: str,
manifest: dict[str, Any],
):
"""Uploads the manifest to the S3 bucket."""

class Dumper(yaml.Dumper):
"""Custom YAML dumper that does not use the default flow style."""

def increase_indent(self, flow=False, indentless=False):
return super().increase_indent(flow, False)

client.put_object(
Bucket=bucket,
Key=f"{folder}/{manifest_file}",
Body=yaml.dump(manifest, Dumper=Dumper, default_flow_style=False),
)


if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions .nextmv/workflow-configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apps:
- name: knapsack-gosdk
type: go
sdk_version: latest
- name: knapsack-java-ortools
type: java
- name: knapsack-ortools
type: python
- name: knapsack-pyomo
type: python
- name: nextroute
type: go
sdk_version: latest
- name: order-fulfillment-gosdk
type: go
sdk_version: latest
- name: routing-ortools
type: python
- name: shift-assignment-ortools
type: python
- name: shift-assignment-pyomo
type: python
- name: shift-planning-ortools
type: python
- name: shift-planning-pyomo
type: python
- name: shift-scheduling-gosdk
type: go
sdk_version: latest
- name: xpress
type: python
1 change: 1 addition & 0 deletions knapsack-gosdk/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions knapsack-java-ortools/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions knapsack-ortools/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions knapsack-pyomo/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions nextroute/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions order-fulfillment-gosdk/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions routing-ortools/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions shift-assignment-ortools/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions shift-assignment-pyomo/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions shift-planning-ortools/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions shift-planning-pyomo/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions shift-scheduling-gosdk/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
1 change: 1 addition & 0 deletions xpress/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.2.0
Loading