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

PostAction for modifying the deployment.template.json file in IoT Edge solutions #5831

Closed
fgheysels opened this issue Dec 25, 2022 · 12 comments
Labels
feature The issue defines new feature. gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you help-wanted good opportunity for contribution to the repo triaged The issue was evaluated by the triage team, placed on correct area, next action defined.
Milestone

Comments

@fgheysels
Copy link

Is your feature request related to a problem? Please describe.

I'm creating a project template for a custom IoT Edge module. When adding an iotedge module to an IoT Edge solution, you need to update the deployment.template.json file that is part of the solution to include that new module into the deployment.
It would be nice if there's a custom 'PostAction' which can do these modifications for you, as I currently don't see any way to do this myself.

As I read that the VS.NET extensions is in maintenance mode, and the CLI way will be the preferred way to create IoT Edge solutions / projects, this should be beneficial for Microsoft too ?

Describe the solution you'd like.

It would be nice if there would exist a PostAction that adds a new entry to the modules section of the deployment.template.json file that exists in an IoT Edge project.
It would be helpfull if the developer could also specify the filename (or filenames) of this json file, as it is very much possible that an IoT Edge solution has multiple different deployment templates.

That new entry should look like this:

"IotEdgeModule1": {
            "version": "1.0.0",
            "type": "docker",
            "status": "running",
            "restartPolicy": "always",
            "settings": {
              "image": "${MODULEDIR<../IotEdgeModule1>}",
              "createOptions": {}
            }
          }

Where IoTEdgeModule1 is the name of the new module. This could be the sourceName property of the template.

If this is too specific for a post-action, you could instead provide a post-action that is more generic. A post-action that allows the developer to add a JSON-object / subproperty to a specific property in a specific JSON file could maybe also do the job.

Additional context

No response

@vlada-shubina vlada-shubina added triaged The issue was evaluated by the triage team, placed on correct area, next action defined. help-wanted good opportunity for contribution to the repo labels Dec 27, 2022
@vlada-shubina vlada-shubina added this to the Backlog milestone Dec 27, 2022
@vlada-shubina vlada-shubina added gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you feature The issue defines new feature. labels Dec 27, 2022
@vlada-shubina
Copy link
Member

@fgheysels

Thank you for suggesting.
At the moment unfortunately it doesn't meet our priority bar, so this might be a good opportunity to contribute and create such a post action.

To everyone, if you feel that this feature (or modifying JSON files in post action) is useful for you, please let us know your use case in comments or simply react to the issue. This helps us prioritize the relevant issues. Thank you.

@fgheysels
Copy link
Author

@vlada-shubina
Is there any documentation available on how to contribute for creating new PostActions ?

@vlada-shubina
Copy link
Member

vlada-shubina commented Dec 29, 2022

Unfortunately, we don't have any specific guideline for post actions specifically, but I can share the high-level steps here:

Please feel free to reach out if you need more information.

Thank you

@fgheysels
Copy link
Author

fgheysels commented Jan 6, 2023

Thanks for the information @vlada-shubina
I've followed the info that is described here, but most of the projects cannot be loaded by VS.NET.
That's strange as I suspect that by running the build.ps1 and afterwards the sdk-build-env.ps1 script, I would assume that I have a 'self build' .NET 8 sdk ?

Next to that, if I succeed in creating such a post-action, will this only become available with .NET 8 ?

@vlada-shubina
Copy link
Member

Thanks for the information @vlada-shubina

I've followed the info that is described here, but most of the projects cannot be loaded by VS.NET.

After some investigation, this is because the SdkTargetFramework variable is set to .NET 8, but obviously, I do not have that SDK. :)

I could change the Directory.Build.props or targets file to target .NET 6 or 7 instead, but I don't think that this is the intention.

Next to that, if I succeed in creating such a post-action, will this only become available with .NET 8 ?

It is possible to download latest daily version of .NET from https://aka.ms/dotnet/8.0.1xx/daily/dotnet-sdk-win-x64.exe (list of all the packages available here: https://github.com/dotnet/installer). If you are using VS, you would probably need to use latest preview that supports .NET 8.

Alternatively, you can use release/7.0.2xx (or release/7.0.3xx once available) - they are built using .NET 7, and thats even more preferable.

Depending on the when the PR will be ready, it might go with .NET SDK 7.0.200 or 7.0.300. The code freeze for 7.0.200 is in two weeks.

@fgheysels
Copy link
Author

fgheysels commented Jan 10, 2023

I've been working on this for a while now, and I think I made some good progress.
However, I've noticed that the template engine is not replacing references to the sourceName in a postaction.

Suppose I have a template that looks like this (only relevant parts included):

{
  "name": "SomeTemplate",
  "identity": "SomeTemplate",
  "shortName": "SomeTemplate",
  "sourceName": "MyProject",
  "primaryOutputs": [
    {
      "path": "MyProject.csproj"
    }
  ],
  "postActions": [
    {
      "description": "Modify JSON file",
      "args": {
        "jsonFileName": "deployment.template.json",
        "parentPropertyPath": "modulesContent:$edgeAgent:properties.desired:modules",
        "newJsonPropertyName": "MyProject",
        "newJsonPropertyValue": "{\"image\":\"${MODULEDIR<../MyProject>}\"}"
      }
    }
  ]
}

As can be seen, the 'arguments' of the post-action contain some strings containing MyProject which is the sourceName that is configured in the template.
I would have expected that these strings would be replaced by the eventual project-name as well, but that is not the case. Of course, the template.json file is not part of the 'project template' itself, so I can understand that this is not happening.
However, inside the post-action, I also have no knowledge of the sourceName nor the string-value that is used to replace the sourceName. Maybe I'm missing something, but none of the parameters of the ProcessInternal method contain this information.

Is there any way to achieve this ?

edit: I've currently 'worked around' this by making use of the filechanges in the CreationEffects. It's a hacky solution imo, but I cannot come up with something better.

@fgheysels
Copy link
Author

I have created a (draft) Pull Request for this (dotnet/sdk#29880) .
The PR contains unit- and end-to-end tests. Documentation still needs to be provided, but I think it would be good to see if this change will be acceptable before doing some work on documentation.

@maridematte
Copy link
Contributor

@fgheysels, just to make sure that I understand the scenario that you are proposing. The post action will add properties to a json file, and that post action can / will be used multiple times depending on the number of properties that you want to add to that specific json file.

As for the file name, during post-actions we do not have ready access to the final name of the project files. You can read a bit more here on how we currently handle file names during that process. But, I think using CreationEffects to get the name should be ok, since we do not have an easier solution right now.

@fgheysels
Copy link
Author

Hi @maridematte
That's indeed correct. The post-action can add a property to a json file. If multiple properties need to be added, the post-action needs to be called multiple times.

@vlada-shubina
Copy link
Member

As can be seen, the 'arguments' of the post-action contain some strings containing MyProject which is the sourceName that is configured in the template. I would have expected that these strings would be replaced by the eventual project-name as well, but that is not the case. Of course, the template.json file is not part of the 'project template' itself, so I can understand that this is not happening. However, inside the post-action, I also have no knowledge of the sourceName nor the string-value that is used to replace the sourceName. Maybe I'm missing something, but none of the parameters of the ProcessInternal method contain this information.

Is there any way to achieve this ?

Unfortunately, he args in post actions won't be auto-replaced. Let me check the PR, thanks a lot for contributing.

@vlada-shubina
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature The issue defines new feature. gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you help-wanted good opportunity for contribution to the repo triaged The issue was evaluated by the triage team, placed on correct area, next action defined.
Projects
None yet
Development

No branches or pull requests

3 participants