-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: add sync state and context to check previous run details #3989
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
72d2ca9
fix: add sync state and context to check previous run details
mndeveci ff401e4
Merge branch 'develop' into sync_context_and_state
mndeveci a003ecf
add unit tests
mndeveci 9732a5f
Merge branch 'develop' into sync_context_and_state
mndeveci e0a035d
fix unit tests & formatting
mndeveci 9a6c689
add integration tests
mndeveci c4d2da4
Merge branch 'develop' into sync_context_and_state
mndeveci c0c7156
Merge branch 'develop' into sync_context_and_state
qingchm 98bf1ca
remove redundant initialization
mndeveci 9848a9d
make black formatting
mndeveci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """ | ||
| Context object used by sync command | ||
| """ | ||
| import logging | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Optional, cast, Dict | ||
|
|
||
| import tomlkit | ||
| from tomlkit.api import _TOMLDocument as TOMLDocument | ||
| from tomlkit.items import Item | ||
|
|
||
| from samcli.lib.build.build_graph import DEFAULT_DEPENDENCIES_DIR | ||
| from samcli.lib.utils.osutils import rmtree_if_exists | ||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| DEFAULT_SYNC_STATE_FILE_NAME = "sync.toml" | ||
|
|
||
| SYNC_STATE = "sync_state" | ||
| DEPENDENCY_LAYER = "dependency_layer" | ||
|
|
||
|
|
||
| @dataclass | ||
| class SyncState: | ||
| dependency_layer: bool | ||
|
|
||
|
|
||
| def _sync_state_to_toml_document(sync_state: SyncState) -> TOMLDocument: | ||
| sync_state_toml_table = tomlkit.table() | ||
| sync_state_toml_table[DEPENDENCY_LAYER] = sync_state.dependency_layer | ||
|
|
||
| toml_document = tomlkit.document() | ||
| toml_document.add((tomlkit.comment("This file is auto generated by SAM CLI sync command"))) | ||
| toml_document.add(SYNC_STATE, cast(Item, sync_state_toml_table)) | ||
|
|
||
| return toml_document | ||
|
|
||
|
|
||
| def _toml_document_to_sync_state(toml_document: Dict) -> Optional[SyncState]: | ||
| if not toml_document: | ||
| return None | ||
|
|
||
| sync_state_toml_table = toml_document.get(SYNC_STATE) | ||
| if not sync_state_toml_table: | ||
| return None | ||
|
|
||
| return SyncState(sync_state_toml_table.get(DEPENDENCY_LAYER)) | ||
|
|
||
|
|
||
| class SyncContext: | ||
|
|
||
| _current_state: SyncState | ||
| _previous_state: Optional[SyncState] | ||
| _build_dir: Path | ||
| _cache_dir: Path | ||
| _file_path: Path | ||
|
|
||
| def __init__(self, dependency_layer: bool, build_dir: str, cache_dir: str): | ||
| self._current_state = SyncState(dependency_layer) | ||
| self._previous_state = None | ||
| self._build_dir = Path(build_dir) | ||
| self._cache_dir = Path(cache_dir) | ||
| self._file_path = Path(build_dir).parent.joinpath(DEFAULT_SYNC_STATE_FILE_NAME) | ||
|
|
||
| def __enter__(self) -> "SyncContext": | ||
| self._read() | ||
| LOG.debug( | ||
| "Entering sync context, previous state: %s, current state: %s", self._previous_state, self._current_state | ||
| ) | ||
|
|
||
| # if adl parameter is changed between sam sync runs, cleanup build, cache and dependencies folders | ||
| if self._previous_state and self._previous_state.dependency_layer != self._current_state.dependency_layer: | ||
| self._cleanup_build_folders() | ||
|
|
||
| return self | ||
|
|
||
| def __exit__(self, *args): | ||
| self._write() | ||
|
|
||
| def _write(self) -> None: | ||
| with open(self._file_path, "w+") as file: | ||
| file.write(tomlkit.dumps(_sync_state_to_toml_document(self._current_state))) | ||
|
|
||
| def _read(self) -> None: | ||
| try: | ||
| with open(self._file_path) as file: | ||
| toml_document = cast(Dict, tomlkit.loads(file.read())) | ||
| self._previous_state = _toml_document_to_sync_state(toml_document) | ||
| except OSError: | ||
| LOG.debug("Missing previous sync state, will create a new file at the end of this execution") | ||
|
|
||
| def _cleanup_build_folders(self): | ||
| """ | ||
| Cleans up build, cache and dependencies folders for clean start of the next session | ||
| """ | ||
| LOG.debug("Cleaning up build directory %s", self._build_dir) | ||
| rmtree_if_exists(self._build_dir) | ||
|
|
||
| LOG.debug("Cleaning up cache directory %s", self._cache_dir) | ||
| rmtree_if_exists(self._cache_dir) | ||
|
|
||
| dependencies_dir = Path(DEFAULT_DEPENDENCIES_DIR) | ||
| LOG.debug("Cleaning up dependencies directory: %s", dependencies_dir) | ||
| rmtree_if_exists(dependencies_dir) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
tests/integration/testdata/sync/infra/before/Java/HelloWorldFunction/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>helloworld</groupId> | ||
| <artifactId>HelloWorld</artifactId> | ||
| <version>1.0</version> | ||
| <packaging>jar</packaging> | ||
| <name>A sample Hello World created for SAM CLI.</name> | ||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>helloworld</groupId> | ||
| <artifactId>HelloWorldLayer</artifactId> | ||
| <version>1.0</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.amazonaws</groupId> | ||
| <artifactId>aws-lambda-java-core</artifactId> | ||
| <version>1.2.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.amazonaws</groupId> | ||
| <artifactId>aws-lambda-java-events</artifactId> | ||
| <version>3.11.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <version>3.2.4</version> | ||
| <configuration> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>shade</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
53 changes: 53 additions & 0 deletions
53
...tion/testdata/sync/infra/before/Java/HelloWorldFunction/src/main/java/helloworld/App.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package helloworld; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.net.URL; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.amazonaws.services.lambda.runtime.Context; | ||
| import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
| import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
| import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
|
|
||
| import helloworldlayer.SimpleMath; | ||
|
|
||
| /** | ||
| * Handler for requests to Lambda function. | ||
| */ | ||
| public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
|
||
| public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
| Map<String, String> headers = new HashMap<>(); | ||
| headers.put("Content-Type", "application/json"); | ||
| headers.put("X-Custom-Header", "application/json"); | ||
|
|
||
| APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() | ||
| .withHeaders(headers); | ||
|
|
||
| int sumResult = SimpleMath.sum(7, 5); | ||
|
|
||
| try { | ||
| final String pageContents = this.getPageContents("https://checkip.amazonaws.com"); | ||
| String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\", \"sum\": %d }", pageContents, sumResult); | ||
|
|
||
| return response | ||
| .withStatusCode(200) | ||
| .withBody(output); | ||
| } catch (IOException e) { | ||
| return response | ||
| .withBody("{}") | ||
| .withStatusCode(500); | ||
| } | ||
| } | ||
|
|
||
| private String getPageContents(String address) throws IOException{ | ||
| URL url = new URL(address); | ||
| try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { | ||
| return br.lines().collect(Collectors.joining(System.lineSeparator())); | ||
| } | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
tests/integration/testdata/sync/infra/before/Java/HelloWorldLayer/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>helloworld</groupId> | ||
| <artifactId>HelloWorldLayer</artifactId> | ||
| <version>1.0</version> | ||
| <packaging>jar</packaging> | ||
| <name>A sample Hello World created for SAM CLI.</name> | ||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <version>3.2.4</version> | ||
| <configuration> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>shade</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
8 changes: 8 additions & 0 deletions
8
...data/sync/infra/before/Java/HelloWorldLayer/src/main/java/helloworldlayer/SimpleMath.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package helloworldlayer; | ||
|
|
||
| public class SimpleMath { | ||
|
|
||
| public static int sum(int a, int b) { | ||
| return a + b; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| AWSTemplateFormatVersion: "2010-09-09" | ||
| Transform: AWS::Serverless-2016-10-31 | ||
|
|
||
| Globals: | ||
| Function: | ||
| Timeout: 30 | ||
|
|
||
| Resources: | ||
| HelloWorldFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| CodeUri: before/Java/HelloWorldFunction | ||
| Handler: helloworld.App::handleRequest | ||
| Runtime: java11 | ||
| MemorySize: 512 | ||
| Layers: | ||
| - !Ref HelloWorldLayer | ||
|
|
||
| HelloWorldLayer: | ||
| Type: AWS::Serverless::LayerVersion | ||
| Properties: | ||
| ContentUri: before/Java/HelloWorldLayer | ||
| CompatibleRuntimes: | ||
| - java11 | ||
| Metadata: | ||
| BuildMethod: java11 | ||
| BuildArchitecture: x86_64 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.