-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathkatalisma.py
executable file
·216 lines (161 loc) · 6.56 KB
/
katalisma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
# Katacoda to Instruqt converter
# v. 0.0.3
# Support for notes in first challenge, fix NodePort
#
# v. 0.0.2
# Support for bulk migrate with git submodule
#
# v. 0.0.1
# First draft
#
import os
import json
import yaml
import re
import shutil
# Instruqt will order the YAML and sanitize the YAML content (e.g. assignments)
# so there's no need to order the dict nor optimize the escaped block for non-YAML
track_d={}
visualEditor=False
#Reads the homepage pathway all in the intruqt-template dir
with open('instruqt-template/homepage-pathway-all.json', 'r') as hfile:
hdata=hfile.read()
courses = json.loads(hdata)
for course in courses['courses']:
print(course["external_link"])
pathway = course["external_link"].rsplit('/', 1)[-1]
print("Creating or updating Scenario : " + pathway)
if not os.path.exists("instruqt"):
os.mkdir("instruqt")
instruqtDir = "instruqt/" + pathway
if not os.path.exists(instruqtDir):
os.mkdir(instruqtDir)
print("Directory " , instruqtDir , " Created ")
else:
print("Directory " , instruqtDir , " already exists")
pathway_id=''
if "pathway_id" in course:
pathway_id=course['pathway_id']
else:
pathway_id=pathway
title=course['title']
track_d["title"] = title
track_d["slug"] = pathway
track_d["type"] = "track"
try:
with open(pathway + '/index.json', 'r') as mycourse:
course_data=mycourse.read()
except FileNotFoundError:
print("Path " + instruqtDir + '/index.json' + " not found, skipping")
continue
course_json = json.loads(course_data)
track_d["icon"] = "https://storage.googleapis.com/instruqt-frontend/img/tracks/default.png"
track_d["tags"] = ["rhel"]
track_d["owner"] = "rhel"
track_d["developers"] = [ "[email protected]"]
track_d["private"] = False
track_d["published"] = True
track_d["skipping_enabled"] = False
#handle time migrations
if "time" in course_json:
duration=re.match('(.*?)(-(.*?))? minutes', course_json["time"] )
if duration is not None:
time=duration.group(1)
if duration.group(3) is not None:
time=duration.group(3)
time = int(time) * 60
else:
print("Time not found " + course_json["time"])
time=300
else:
time=300
#handle level migrations
difficulty="intermediate"
level="beginner"
if "difficulty" in course_json:
difficulty = course_json["difficulty"].lower()
else:
difficulty = "basic"
if level == "advanced":
difficulty = "expert"
elif level == "easy" or level == "beginner" or level == "basic":
level = "beginner"
difficulty = "basic"
track_d["level"] = level
l_challenges=[]
d_challenges={}
src=r'instruqt-template/config.yml'
dst=instruqtDir + '/' + 'config.yml'
shutil.copyfile(src, dst)
#handle background and foreground scripts
if os.path.exists(instruqtDir + '/track_scripts'):
shutil.rmtree(instruqtDir + '/track_scripts')
shutil.copytree('instruqt-template/track_scripts', instruqtDir + '/track_scripts')
if os.path.isfile(pathway + "/background.sh"):
os.system('cp -fr ' + pathway + '/background.sh ' + instruqtDir + '/track_scripts/setup-rhel' )
if os.path.isfile(pathway + "/foreground.sh"):
os.system('tail --lines=+2 ' + pathway + '/foreground.sh >> ' + instruqtDir + '/track_scripts/setup-rhel' )
if not os.path.exists(instruqtDir + '/assets'):
os.mkdir(instruqtDir + '/assets')
if not os.path.exists(instruqtDir + '/scripts'):
os.mkdir(instruqtDir + '/scripts')
introText=course_json["details"]["intro"]["text"]
with open(pathway + '/' + introText, 'r') as myintro:
intro_data=myintro.read()
intro_md=re.sub(r'\(.\/assets',r'(https://katacoda.com/rhel-labs/assets',intro_data)
track_d["description"] = intro_md
#copy all the assets to the scripts folder
try:
os.system('cp -fr ' + pathway + '/assets/* ' + instruqtDir + '/scripts/' )
print('cp -fr ' + pathway + '/assets/* ' + instruqtDir + '/scripts/')
except KeyError:
pass
#Handle terminal
if course_json["environment"]["uilayout"] == "editor-terminal":
visualEditor=True
l_steps = course_json["details"]["steps"]
l_size = len(l_steps)
time = int(int(time) / l_size)
isFirstStep=True
for step in l_steps:
slug = step["text"]
slug = re.sub(r'\.md$', '', slug )
if not os.path.exists(instruqtDir + '/' + slug):
os.mkdir(instruqtDir + '/' + slug)
print("Directory " , instruqtDir + '/' + slug , " Created ")
else:
print("Directory " , instruqtDir + '/' + slug , " already exists")
d_challenges["slug"] = slug
d_challenges["title"] = step["title"]
d_challenges["type"] = "challenge"
with open(pathway + '/' + step["text"], 'r') as myassign:
assign_data=myassign.read()
md=re.sub(r'`{1,3}(.+?)`{1,3}\{\{execute\}\}', r'```\n\1\n```', assign_data )
md=re.sub(r'\{\{copy\}\}',r'', md)
md=re.sub(r'\{\{open\}\}',r'', md)
md=re.sub(r'\(\.\.\/\.\.\/assets',r'(https://katacoda.com/rhel-labs/assets',md)
md=re.sub(r'\(\/openshift\/assets',r'(https://katacoda.com/rhel-labs/assets',md)
d_challenges["assignment"] = md
if isFirstStep:
l_notes = [{"type": "text", "contents": intro_md}]
d_challenges["notes"] = l_notes
isFirstStep=False
else:
if "notes" in d_challenges:
del d_challenges["notes"]
#Enable terminal and web console tab
l_tabs = [{"title": "Terminal", "type": "terminal","hostname":"rhel"},
{"title": "RHEL Web Console", "type" : "service", "hostname" : "rhel", "path" : "/", "port" : 9090}]
#Enable code editor
if visualEditor:
l_tabs.append({"title": "Visual Editor", "type": "code","hostname":"rhel", "path":"/root"})
d_challenges["tabs"] = l_tabs
d_challenges["difficulty"]= difficulty
d_challenges["timelimit"]= time
dictionary_copy = d_challenges. copy()
l_challenges.append(dictionary_copy);
track_d["challenges"] = l_challenges
#write out yml
with open(instruqtDir + '/track.yml', 'w') as yaml_file:
yaml.dump(track_d, yaml_file, default_flow_style=False)