Device Registration & Deployment
Learn how to register a device and deploy an update at the same time
Registration and Update deployment is a common use-case that might be useful for several scenarios, for such cases we created a script by using our REST API to register the device and deploy an update at the same time.
For example, If you would like to make sure that all devices registered to your fleet are automatically updated with the latest software, or, some kind of configuration that initiates after a successful factory reset.
Instead of registering the device via the installation command or a pre-made image, we will use a Python script on the device that will register and deploy an update to itself.
Prerequisites:
  • REST API enabled
  • Be familiar with our API and tokens - Overview
  • An Update Flow is created and ready to deploy
  • Python3 is installed on the device.
  • requests library installed on the device
The script performs the following:
  1. 1.
    Registers the Device to Connect, according to the selected project
  2. 2.
    Deploying an update flow that was previously configured in the platform.
  3. 3.
    Even if the device is already registered, the update will still be deployed.
Instructions:
Take the code below and edit with the following configuration at the top:
  • USER_TOKEN - Your user token, can be taken from Settings > Account > Show User Token.
  • PROJECT_NAME - The name of the desired project that the device will be connected to.
  • UPDATE_FLOW_ID - ID of the desired flow to be deployed on the device, the flow must be a part of the project above.
1
import requests
2
import subprocess
3
import os
4
import json
5
import time
6
7
# ----------------------------#
8
9
10
# Needed to be edit per user before running
11
USER_TOKEN = ""
12
PROJECT_NAME = ""
13
UPDATE_FLOW_ID = ""
14
# ----------------------------#
15
16
17
# Don't touch - will be added automatically by the script
18
DEVICE_TOKEN = ""
19
DEVICE_UUID = ""
20
# Don't touch - static paths and URLs
21
INSTALLATION_URL = """su -c 'wget -O - https://connect.jfrog.io/v2/install_connect | sh -s {} {}'"""
22
CONNECT_AGENT_PATH = "/etc/connect/service/ConnectAgent"
23
AGENT_FLAG_TO_GET_DEVICE_TOKEN = "--print-token"
24
DEPLOY_UPDATE_API_URL = "https://api.connect.jfrog.io/v1/deploy_update"
25
GET_DEVICE_DETAILS_API_URL = "https://api.connect.jfrog.io/v1/devices_details"
26
27
28
def is_connect_agent_exist_on_device():
29
return os.path.exists(CONNECT_AGENT_PATH)
30
31
32
def install_connect_agent(user_token, project_name):
33
final_installation_url = INSTALLATION_URL.format(user_token, project_name)
34
35
subprocess.check_output(final_installation_url, shell=True)
36
37
print("waiting 2 minutes before continuing the process...")
38
39
time.sleep(120)
40
41
42
def get_device_token():
43
device_token = subprocess.check_output(CONNECT_AGENT_PATH + " " + AGENT_FLAG_TO_GET_DEVICE_TOKEN, shell=True)
44
45
return device_token.decode('ascii')
46
47
48
def get_device_uuid(user_token, project_name, device_token):
49
json_content = {'project_name': project_name,
50
51
'group_name': 'Production',
52
53
'user_token': user_token,
54
55
'device_token': device_token}
56
57
call_request = requests.get(GET_DEVICE_DETAILS_API_URL, json=json_content)
58
59
call_response = json.loads(call_request.text)
60
61
return call_response["message"][0]["device_uuid"]
62
63
64
def deploy_update_flow(update_flow_id, user_token, project_name, device_uuid):
65
print("deploying update flow {} on device {} ....".format(update_flow_id, device_uuid))
66
67
json_content = {'user_token': user_token,
68
69
'metadata': {'comment': 'deploy app v1',
70
71
'app': {'name': 'app', 'version': 'v1'}},
72
73
'flow_id': update_flow_id,
74
75
'devices_filter': {'project': {'name': project_name},
76
77
'groups': [{'name': 'production'}],
78
79
'filters': [{'type': 'specific_device',
80
81
'operand': 'is',
82
83
'value': device_uuid}]}
84
85
}
86
87
call_request = requests.post(DEPLOY_UPDATE_API_URL, json=json_content)
88
89
call_response = json.loads(call_request.text)
90
91
if call_request.status_code == 200:
92
93
print("process finished successfully")
94
95
else:
96
97
if call_request.status_code == 429:
98
99
error = "API limit reached"
100
101
else:
102
103
error = call_response["error_message"]
104
105
print(error)
106
107
108
if __name__ == '__main__':
109
110
if not is_connect_agent_exist_on_device():
111
112
if USER_TOKEN and PROJECT_NAME and UPDATE_FLOW_ID:
113
114
install_connect_agent(USER_TOKEN, PROJECT_NAME)
115
116
else:
117
118
print(
119
"one or all of the required parameters - user token, project name and update flow id were not provided, make sure to edit the script before running again")
120
121
exit(1)
122
123
else:
124
125
print("JFrog Connect is already installed on device")
126
127
if not DEVICE_TOKEN:
128
DEVICE_TOKEN = get_device_token()
129
130
if not DEVICE_UUID:
131
DEVICE_UUID = get_device_uuid(USER_TOKEN, PROJECT_NAME, DEVICE_TOKEN)
132
133
deploy_update_flow(UPDATE_FLOW_ID, USER_TOKEN, PROJECT_NAME, DEVICE_UUID)
Copied!
2. Copy the script to the device
3. Choose when the script would run, some prefer having it running after the next power-on, while others initiate it with their on-device application.
Copy link