Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

Teaser_SDK.jpg

SDK - Empower your app with Data Persistence

MauroRiboniMX
Contributor
Disclaimer

The goal of this guide is to enable anyone who has already the basic skills to create an app to enhance his app with Data Persistence. Why?

With this feature it is possible to:

  • Create apps that can keep their own settings across ctrlX restores (obviously you need to backup them)
  • Create an app that is possible to be configured (you just modify the content an upload it again)
  • Have a better and safe integration with ctrlX AUTOMATION

This feature is well documented here: Data Persistence. We're going to implement it porting an open source snap of mongoDB to ctrlX with the data persistence integration.

Requirements

Needed devices:

  • ctrlX CORE or ctrlX COREVIRTUAL
  • An UBUNTU machine or build environment ready to work. Here is a ready startup: Setup SDK 
  • Basic snapcraft capability

STEP1: Clone the repo and modify the sandard snapcraft

The repo can be cloned easily inside a build environment or a common Ubuntu Machine:

git clone https://github.com/mauringo/mongodb36-configurable-snap

Open the folder inside VScode (or just modify on your own way). From the basic project I removed some description and the apps are not needed. I kept just the main mongo daemon and his run.sh script (content in shscripts).

The project anyway is really simple:

  • Conf contains the standard mongoDB configuration file.
  • shscripts contains conf and run.sh (the other files have been deleted because not needed here).
  • Snap contains the hooks folder: some scripts that are executed in some precise moment of the snap life.
  • Snapcraft.yaml.

snapcraft mongo 2

Edit snapcraft.yaml accordingly:

Here is the result we want to achieve. Basically we had (from top to bottom):

  • Added active-solution in the mongodb app plugs.
  • Added a part: configs. it is necessary to embed additional files.
  • Added a slot: we need the slot to let the previous files be available by outside the snap.
  • Added a plug: active-solution. It's the plug we need to have in order to read the memory area with the right permissions.
  • Added a hook: this script is automatically used when the snaps connect the active-solution plug.

mongo persistent storage

 

STEP 2: Edit accordingly the SH files 

Before the start it is important to highlight what the plug gives to us: a symlink to the solutions folder in his SNAP_COMMON folder:

$SNAP_COMMON/solutions/activeConfiguration/

Here we can create our folder. For the example we named it mongo36. In the end all the data are in the following path:

git $SNAP_COMMON/solutions/activeConfiguration/mongo36

I am going fast over this. This feature is well documented here: Data Persistence. As stated before.

Edit run.sh accordingly

Let's commend easily the starting file:

  • The export LC_ALL=C it's a locale setting, we keep it.
  • The app takes care that we have all the needed folders/files before to start, we need to adapt it.
  • The snap has been projected to run in the $SNAP_COMMON folder so we need to change it accordingly.

snapcraft mongo

Here is the new file:

  • We need to wait that the plug is correctly set, so we wait then some time.
  • The export LC_ALL=C it's a locale setting, we keep it as before.
  • We build our folder: basic directory + the foldername we want to use, in this case mongo36.
  • The app takes care that we have all the needed folders/files before to start, we go directly with CD in the right path.
  • The snap in the end runs in the same situation as before but, in a different directory and with a waiting delay in the start.

sript persistent data

Add the hook

We need to add in ./snap/hooks our hook file named "connect-plug-active-solution". This file will be called on the snap plug connect event and will prepare the memory folder for us. Here we have the content, the folder path is created just as before.

hook persistent data

STEP 3: The content file

How the content file works is well documented here: Package Assets. This file contains many setting for better integration in the environment. it is located in configs/package-assets in our folder and is named always: MYSNAPNAME.package-manifest.json. Here is our:

package asset persistent data

We have just the appDirectories settings. the name filed must be equal with our foldername, in this case mongo36. description is what the user sees. Icon is the name of the standard icon to be used. In the documentation there is any feature explained.

The result

Well, now our app is finished, we just need to test it. In the "APP DATA" folder we can now see the mongoDB configuration file and logs.

App resultApp result

Have Fun 🙂

1 Comment
chockalingam
Member

Excellent post! Able to recreate and apply for other projects as well by following the steps. Great work.

Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist