Running the Python Device Manager in a Production Environment

To run the device manager in a production environment, you may want to change the configuration to follow the conventions of the operating system on which you run the device manager.

For example, in Linux, you may want to use /etc for configuration, /var/lib for run-time files, and run the device manager from a system directory, such as /usr/bin or /usr/local/bin. If you configure all your applications to use the same configuration directory, it ensures that your applications use the same device_id file and you can easily identify all applications on the same device on the Management Portal. You must also create a user under which the device manager runs and add the user to the sudoers list with the required commands included.

In Windows, you may want to create a folder under C:\Program Files that contains the executable files required for all your device applications and create a folder under C:\ProgramData for configuration and run-time files.

If you include the Device Cloud agent components in your image for Yocto-based Linux distributions by adding the meta-device-cloud layer to your build, the device manager is already configured to run in a production environment. Configuration files are located in the /etc/python-device-cloud directory, run-time files are located in the /var/lib/python-device-cloud directory, it runs as the dc user, and it starts automatically as a service using systemd or sysvinit. Therefore, most of the steps in this section are not necessary. However, you still need to create the iot-connect.cfg file in the /etc/python-device-cloud, and you may want to customize the iot.cfg file and add custom attributes in the attributes.cfg file.

For other distributions, the device manager does not start automatically. You may want to run the application as a service to start it when the device starts and automatically restart it if it fails.

You need the device manager connection configuration file you generated. For more information, see the following:

You need a new thing definition and application for your device manager defined in Device Cloud.

  1. Open the device_manager.py file in a text editor and change the code as needed.
    1. If you want to store the device manager configuration files in a specific location, change the configuration directory.
      default_cfg_dir = "/etc/iot"
    2. (Optional) Assign a friendly name.

      While you can also assign a friendly name in the iot.cfg file, you may want to assign the value in the device manager code if you want to the value to contain information available at run time, such as the MAC address of the device.

      If you specify the value in the code, ensure that the value in the iot.cfg file is None.

      client.update_thing_details(name="My friendly name for thing 1")
  2. Change the iot.cfg file as needed.
    1. Change the run-time directory as needed.
      "runtime_dir":"/var/lib/iot",

      When you run the Retrieve Files method from the Management Portal, the device manager searches for the file to upload in the runtime_dir/upload directory if you do not specify a full path in the file_path box.

      When you run the Send Files method from the Management Portal, the device manager stores the file in the runtime_dir/download directory if you do not specify a full path in the file_path box.

      In both cases, if the full path is specified, this value is not used.

    2. Enable or disable the methods as needed.

      Disabled methods appear on the Management Portal but return an error if you run them.

    3. Change the file transfer configuration as needed.
    4. Change the secure tunnel access protocols, ports, and timeout values as needed.

      For more information about remote access and configuring secure tunnel access, see the Device Cloud Remote Access User's Guide.

  3. Copy the iot.cfg, iot-connect.cfg, and if applicable, the attributes.cfg files to the configuration directory you want to use.
  4. On Linux, create a user, for example device_cloud, under which to run the device manager program.
  5. On Linux, make a copy of the share/sudoers/device_cloud file, add or remove commands as required, and copy the file to the /etc/sudoers.d directory.

    Ensure that the file name matches the name of the user you created.

  6. To configure the device manager to start when the device starts, create the files for the appropriate service control method for your operating system.

    For examples of how to run the device manager in Linux with systemd or init.d, see the share/readme.md file, where the location of the share directory is based on how you obtained the source code.

    Ensure that any directories you specify, including the configuration directory specified in the device manager source code and the value of runtime_dir in the iot.cfg file, have the appropriate permissions for the user and group that runs the service.

  7. Copy the device_manager.py file to the location from which you want to run the application.

    If you run the device manager as a service, copy the file to the location you specified in the service configuration.

  8. Do one of the following to run the device manager:
    • To run the device manager as a service, start the service.

    • Run the device manager from the command line as the user you created.

When the device manager runs, the agent searches the specified configuration directory for the device_id, iot-connect.cfg, iot.cfg, and attributes.cfg files. The device manager appears as a thing with the thing key deviceId-device_manager_py on the Things page on the Management Portal. If you ran the device manager previously with the default configuration directory, the agent generates a new device_id file and the device manager uses a new thing key.

If you want all your applications on the device to share a common configuration directory, ensure that you specify the same directory when you initialize your other applications (see Initializing Your Python Application) and store the application-specific connection configuration file in the configuration directory.