Skip to content

Commit 0e00fd6

Browse files
committed
Add dash-pipeline-utils in docker-sonic-mgmt
1 parent a1e2c1f commit 0e00fd6

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

dockers/docker-sonic-mgmt/Dockerfile.j2

+10
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ COPY debs/sonic-device-data_*.deb debs/libdashapi_*.deb debs/
227227

228228
RUN dpkg -i debs/sonic-device-data_*.deb debs/libdashapi_*.deb
229229

230+
# Install dash-pipeline-utils
231+
COPY ["files/github_get.py", "/usr/bin"]
232+
RUN cd /tmp \
233+
&& python3 /usr/bin/github_get.py https://api.github.com/repos/sonic-net/DASH/contents/dash-pipeline/utils \
234+
&& cd utils \
235+
&& python3 setup.py bdist_wheel \
236+
&& python3 -m pip install dist/dash_pipeline_utils*.whl \
237+
&& cd / \
238+
&& rm -rf /tmp/utils
239+
230240
RUN mkdir /var/run/sshd
231241
EXPOSE 22
232242

rules/docker-sonic-mgmt.mk

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
DOCKER_SONIC_MGMT = docker-sonic-mgmt.gz
33
$(DOCKER_SONIC_MGMT)_PATH = $(DOCKERS_PATH)/docker-sonic-mgmt
44
$(DOCKER_SONIC_MGMT)_DEPENDS += $(SONIC_DEVICE_DATA) $(PTF) $(LIB_SONIC_DASH_API)
5+
$(DOCKER_SONIC_MGMT)_FILES += $(GITHUB_GET)
56
SONIC_DOCKER_IMAGES += $(DOCKER_SONIC_MGMT)
67
SONIC_BULLSEYE_DOCKERS += $(DOCKER_SONIC_MGMT)

rules/scripts.mk

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ $(COPP_CONFIG_TEMPLATE)_PATH = files/image_config/copp
3535
RSYSLOG_PLUGIN_CONF_J2 = rsyslog_plugin.conf.j2
3636
$(RSYSLOG_PLUGIN_CONF_J2)_PATH = files/build_templates
3737

38+
GITHUB_GET = github_get.py
39+
$(GITHUB_GET)_PATH = scripts
40+
3841
SONIC_COPY_FILES += $(CONFIGDB_LOAD_SCRIPT) \
3942
$(ARP_UPDATE_SCRIPT) \
4043
$(ARP_UPDATE_VARS_TEMPLATE) \
@@ -46,4 +49,5 @@ SONIC_COPY_FILES += $(CONFIGDB_LOAD_SCRIPT) \
4649
$(UPDATE_CHASSISDB_CONFIG_SCRIPT) \
4750
$(SWSS_VARS_TEMPLATE) \
4851
$(RSYSLOG_PLUGIN_CONF_J2) \
52+
$(GITHUB_GET) \
4953
$(COPP_CONFIG_TEMPLATE)

scripts/github_get.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import os
4+
import sys
5+
import re
6+
import json
7+
import requests
8+
9+
def get(url, file_name=None, dir_name=None):
10+
'''
11+
Get the files in the given URL
12+
:param url: The URL is a github API URL that returns a JSON object
13+
:param file_name: if specified, url will be github download_url
14+
:return: None
15+
:raises: Exception if the download fails
16+
'''
17+
try:
18+
response = requests.get(url)
19+
response.raise_for_status()
20+
except requests.exceptions.RequestException as e:
21+
raise Exception(f"Request failed: {e}")
22+
23+
if file_name:
24+
with open(file_name, 'wb') as file:
25+
file.write(response.content)
26+
return
27+
28+
def get_file(file):
29+
if file['download_url']:
30+
get(file['download_url'], file_name=file['name'])
31+
else:
32+
if file['type'] == 'dir':
33+
get(file['url'], dir_name=file['name'])
34+
else:
35+
get(file['git_url'].replace('/git/trees/', '/contents?ref='))
36+
37+
try:
38+
data = response.json()
39+
except json.JSONDecodeError as e:
40+
raise Exception(f"JSON decode error: {e}")
41+
42+
if isinstance(data, list):
43+
if not dir_name:
44+
pattern = r"https://api\.github\.com/repos/[^/]+/([^/]+)/contents(.*)"
45+
repo_name, path = re.search(pattern, url).groups()
46+
path = path.split('/')[-1].split('?')[0]
47+
dir_name = path if path else repo_name
48+
49+
cur = os.getcwd()
50+
os.mkdir(dir_name)
51+
os.chdir(dir_name)
52+
for item in data:
53+
get_file(item)
54+
os.chdir(cur)
55+
else:
56+
get_file(data)
57+
58+
def help():
59+
print(f"Usage: {sys.argv[0]} <github REST API url>")
60+
print(" url format: https://api.github.com/repos/{owner}/{repo}/contents/{directory}?ref={branch}")
61+
print(f" url example: https://api.github.com/repos/sonic-net/DASH/contents/dash-pipeline/utils?ref=main")
62+
sys.exit(1)
63+
64+
if __name__ == '__main__':
65+
if len(sys.argv) < 2 or sys.argv[1] == '-h':
66+
help()
67+
68+
try:
69+
get(sys.argv[1])
70+
except Exception as e:
71+
print(e)
72+
sys.exit(1)

0 commit comments

Comments
 (0)