Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
a
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Yusei Tahara
a
Commits
89b9952d
Commit
89b9952d
authored
Jan 12, 2024
by
Yusei Tahara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a resource agent.
parent
df3fa7d0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
172 additions
and
0 deletions
+172
-0
slapos-theia
slapos-theia
+172
-0
No files found.
slapos-theia
0 → 100644
View file @
89b9952d
#!/usr/bin/python3
import
os
import
sys
import
time
import
subprocess
OCF_SUCCESS
=
0
OCF_ERR_GENERIC
=
1
OCF_NOT_RUNNING
=
7
action
=
sys
.
argv
[
1
]
part_id
=
os
.
getenv
(
'OCF_RESKEY_part_id'
)
slappart
=
'slappart%s'
%
part_id
slappart_b
=
slappart
.
encode
(
'utf8'
)
slapuser
=
'slapuser%s'
%
part_id
service_list
=
os
.
getenv
(
'OCF_RESKEY_services'
,
''
).
split
(
','
)
#kumofs_gateway,kumofs_manager,kumofs_server,mariadb,zeo,zope,haproxy
def
splitLine
(
line
):
return
[
item
for
item
in
line
.
split
(
b' '
)
if
item
]
def
runCommand
(
*
args
):
return
subprocess
.
run
(
args
,
capture_output
=
True
)
def
runSlapOS
(
*
args
):
return
runCommand
(
'slapos'
,
*
args
)
def
checkTheiaStatus
(
target_status
):
completed_process
=
runSlapOS
(
'node'
)
for
line
in
completed_process
.
stdout
.
split
(
b'
\
n
'
):
if
line
.
startswith
(
slappart_b
):
split_line
=
splitLine
(
line
)
if
(
split_line
[
0
].
startswith
(
slappart_b
+
b':theia-instance'
)
and
split_line
[
1
].
decode
(
'utf8'
)
==
target_status
):
return
True
return
False
def
isTheiaRunning
():
return
checkTheiaStatus
(
'RUNNING'
)
def
isTheiaStopped
():
return
checkTheiaStatus
(
'STOPPED'
)
def
runSlapOSInTheia
(
*
args
):
return
runCommand
(
'sudo'
,
'-u'
,
slapuser
,
'/srv/slapgrid/%s/srv/runner/bin/slapos'
%
slappart
,
*
args
)
def
checkAppStatus
(
target_status
):
completed_process
=
runSlapOSInTheia
(
'node'
)
if
completed_process
.
stdout
==
b'unix:///srv/slapgrid/%s/srv/runner/instance/sv.sock no such file
\
n
'
%
slappart_b
:
if
target_status
==
'STOPPED'
:
return
True
else
:
return
False
error_list
=
[]
service_status_dict
=
{}
for
service
in
service_list
:
service_status_dict
[
service
]
=
[]
for
line
in
completed_process
.
stdout
.
split
(
b'
\
n
'
):
if
not
line
:
continue
split_line
=
splitLine
(
line
)
if
split_line
[
0
].
decode
(
'utf8'
)
==
'watchdog'
:
continue
service_name
=
split_line
[
0
].
split
(
b':'
)[
1
].
split
(
b'-'
)[
0
].
decode
(
'utf8'
)
current_status
=
split_line
[
1
].
decode
(
'utf8'
)
if
service_name
in
service_status_dict
:
service_status_dict
[
service_name
].
append
(
current_status
)
if
current_status
!=
target_status
:
error_list
.
append
(
line
)
if
error_list
:
return
False
for
service
in
service_status_dict
:
if
set
(
service_status_dict
[
service
])
!=
set
([
target_status
]):
return
False
return
True
def
isAppRunning
():
return
checkAppStatus
(
'RUNNING'
)
def
isAppStopped
():
return
checkAppStatus
(
'STOPPED'
)
def
monitor
():
result
=
OCF_ERR_GENERIC
try
:
if
isTheiaRunning
()
and
isAppRunning
():
result
=
OCF_SUCCESS
elif
isTheiaStopped
()
and
isAppStopped
():
result
=
OCF_NOT_RUNNING
except
:
pass
sys
.
exit
(
result
)
def
start
():
result
=
OCF_ERR_GENERIC
try
:
while
not
isTheiaRunning
():
runSlapOS
(
'node'
,
'start'
,
slappart
+
':*'
)
time
.
sleep
(
3
)
while
not
isAppRunning
():
runSlapOSInTheia
(
'node'
,
'start'
,
'all'
)
time
.
sleep
(
3
)
result
=
OCF_SUCCESS
except
:
pass
sys
.
exit
(
result
)
def
stop
():
result
=
OCF_ERR_GENERIC
try
:
while
not
isAppStopped
():
runSlapOSInTheia
(
'node'
,
'stop'
,
'all'
)
time
.
sleep
(
3
)
while
not
isTheiaStopped
():
runSlapOS
(
'node'
,
'stop'
,
slappart
+
':*'
)
time
.
sleep
(
3
)
result
=
OCF_SUCCESS
except
:
pass
sys
.
exit
(
result
)
def
metadata
():
print
(
'''
\
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="slapos-theia">
<version>1.0</version>
<longdesc lang="en">
This is the resource agent for SlapOS Theia.
</longdesc>
<shortdesc lang="en">Manages SlapOS Theia instance</shortdesc>
<parameters>
<parameter name="part_id" unique="1" required="1">
<longdesc>
Computer Partition ID, slappartX
</longdesc>
<shortdesc>Partition ID</shortdesc>
<content type="integer"/>
</parameter>
<parameter name="services" unique="0" required="1">
<longdesc>
Service names separated by comma
</longdesc>
<shortdesc>Service names</shortdesc>
<content type="string"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="60s" />
<action name="monitor" depth="0" timeout="20s" interval="60s" />
<action name="meta-data" timeout="5s" />
</actions>
</resource-agent>
'''
)
sys
.
exit
(
OCF_SUCCESS
)
if
action
==
'monitor'
:
monitor
()
elif
action
==
'start'
:
start
()
elif
action
==
'stop'
:
stop
()
elif
action
==
'meta-data'
:
metadata
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment