Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mock-cheops
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
chephren
mock-cheops
Commits
2f560129
Unverified
Commit
2f560129
authored
1 year ago
by
REIG Julien
Browse files
Options
Downloads
Patches
Plain Diff
add table precense
parent
e4806cf7
No related branches found
No related tags found
1 merge request
!16
Node knowledge
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/init_database.py
+78
-29
78 additions, 29 deletions
utils/init_database.py
with
78 additions
and
29 deletions
utils/init_database.py
+
78
−
29
View file @
2f560129
...
...
@@ -5,6 +5,11 @@ from consts.status import Status
from
.database_utils
import
db_file
,
db_queue_file
node1_address
=
'
http://localhost:3000/
'
node2_address
=
'
http://localhost:3001/
'
node3_address
=
'
http://localhost:3002/
'
node4_address
=
'
http://localhost:3003/
'
status
=
{
"
node1
"
:
{
"
state
"
:
Status
.
ONLINE
.
value
},
"
node2
"
:
{
"
state
"
:
Status
.
ONLINE
.
value
},
...
...
@@ -65,6 +70,23 @@ commands = {
],
}
# 746f746f2e747874a (resource 1):
# - n1
# e539b187571ec476a (resource 2):
# - n1
# - n2
# 3ad06e1ea9a9c2085 (resource 3):
# - n1
# - n2
# 0eaf0d20311e24abd (resource 4):
# - n1
# - n3
# 183ca5d3a2f6735bf (resource 5):
# - n1
# - n3
# 640b0507b542f5854 (resource 6):
# - n1
# - n4
def
init_database
():
with
sqlite3
.
connect
(
db_file
)
as
db_conn
:
...
...
@@ -90,6 +112,10 @@ def init_database():
print
(
"
Creating table resources
"
)
cursor
.
execute
(
"
CREATE TABLE resources (id TEXT PRIMARY KEY, name TEXT, last_update INTEGER)
"
)
# precense (resourceId, node_address)
print
(
"
Creating table presence
"
)
cursor
.
execute
(
"
CREATE TABLE presence (resource_id TEXT, node_address TEXT, PRIMARY KEY (resource_id, node_address), FOREIGN KEY (resource_id) REFERENCES resources(id))
"
)
# commands (resource_id, id, create_date, command)
print
(
"
Creating table commands
"
)
cursor
.
execute
(
"
CREATE TABLE commands (resource_id TEXT, id TEXT, create_date INTEGER, command TEXT, PRIMARY KEY (resource_id, id), FOREIGN KEY (resource_id) REFERENCES resources(id))
"
)
...
...
@@ -131,61 +157,84 @@ def init_queue_db():
"
CREATE TABLE command_queue (id INTEGER PRIMARY KEY AUTOINCREMENT, node_address TEXT, resource_id TEXT, command_object TEXT)
"
)
db_conn
.
commit
()
precense_insert
=
"
insert into presence (resource_id, node_address) values (?, ?)
"
state_insert
=
"
insert into state (state) values (?)
"
resources_insert
=
"
INSERT INTO resources (id, name, last_update) VALUES (?, ?, ?)
"
commands_insert
=
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
def
insert_node1
(
cursor
:
sqlite3
.
Cursor
,
db_conn
:
sqlite3
.
Connection
):
print
(
"
Inserting data for node1
"
)
cursor
.
execute
(
"
INSERT INTO state (state) VALUES (?)
"
,
(
status
[
"
node1
"
][
"
state
"
],))
cursor
.
executemany
(
"
INSERT INTO resources (id, name, last_update) VALUES (?, ?, ?)
"
,
resources
)
cursor
.
execute
(
state_insert
,
(
status
[
"
node1
"
][
"
state
"
],))
cursor
.
executemany
(
resources_insert
,
resources
)
cursor
.
executemany
(
precense_insert
,
[
(
"
746f746f2e747874a
"
,
node1_address
),
(
"
e539b187571ec476a
"
,
node1_address
),
(
"
e539b187571ec476a
"
,
node2_address
),
(
"
3ad06e1ea9a9c2085
"
,
node1_address
),
(
"
3ad06e1ea9a9c2085
"
,
node2_address
),
(
"
0eaf0d20311e24abd
"
,
node1_address
),
(
"
0eaf0d20311e24abd
"
,
node3_address
),
(
"
183ca5d3a2f6735bf
"
,
node1_address
),
(
"
183ca5d3a2f6735bf
"
,
node3_address
),
(
"
640b0507b542f5854
"
,
node1_address
),
(
"
640b0507b542f5854
"
,
node4_address
)
])
for
_
,
commands_list
in
commands
.
items
():
cursor
.
executemany
(
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
,
commands_list
)
cursor
.
executemany
(
commands_insert
,
commands_list
)
db_conn
.
commit
()
def
insert_node2
(
cursor
:
sqlite3
.
Cursor
,
db_conn
:
sqlite3
.
Connection
):
print
(
"
Inserting data for node2
"
)
cursor
.
execute
(
"
INSERT INTO state (state) VALUES (?)
"
,
(
status
[
"
node2
"
][
"
state
"
],))
cursor
.
execute
(
state_insert
,
(
status
[
"
node2
"
][
"
state
"
],))
rs
=
resources
[
1
:
3
]
cursor
.
executemany
(
"
INSERT INTO resources (id, name, last_update) VALUES (?, ?, ?)
"
,
rs
)
cursor
.
executemany
(
resources_insert
,
rs
)
cursor
.
executemany
(
precense_insert
,
[
(
"
e539b187571ec476a
"
,
node1_address
),
(
"
e539b187571ec476a
"
,
node2_address
),
(
"
3ad06e1ea9a9c2085
"
,
node1_address
),
(
"
3ad06e1ea9a9c2085
"
,
node2_address
)
])
for
r
in
rs
:
cursor
.
executemany
(
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
,
commands
[
r
[
0
]])
cursor
.
executemany
(
commands_insert
,
commands
[
r
[
0
]])
db_conn
.
commit
()
def
insert_node3
(
cursor
:
sqlite3
.
Cursor
,
db_conn
:
sqlite3
.
Connection
):
print
(
"
Inserting data for node3
"
)
cursor
.
execute
(
"
INSERT INTO state (state) VALUES (?)
"
,
(
status
[
"
node3
"
][
"
state
"
],))
cursor
.
execute
(
state_insert
,
(
status
[
"
node3
"
][
"
state
"
],))
rs
=
resources
[
3
:
5
]
cursor
.
executemany
(
"
INSERT INTO resources (id, name, last_update) VALUES (?, ?, ?)
"
,
rs
)
cursor
.
executemany
(
resources_insert
,
rs
)
cursor
.
executemany
(
precense_insert
,
[
(
"
0eaf0d20311e24abd
"
,
node1_address
),
(
"
0eaf0d20311e24abd
"
,
node3_address
),
(
"
183ca5d3a2f6735bf
"
,
node1_address
),
(
"
183ca5d3a2f6735bf
"
,
node3_address
)
])
cs
=
commands
[
rs
[
0
][
0
]]
cs
.
append
((
"
0eaf0d20311e24abd
"
,
"
7363616c616c8a
"
,
1699362580000
,
"
ls -l /tmp
"
))
cursor
.
executemany
(
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
,
cs
)
cursor
.
executemany
(
commands_insert
,
cs
)
cs
=
commands
[
rs
[
1
][
0
]]
cs
.
pop
()
cursor
.
executemany
(
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
,
cs
)
cursor
.
executemany
(
commands_insert
,
cs
)
db_conn
.
commit
()
def
insert_node4
(
cursor
:
sqlite3
.
Cursor
,
db_conn
:
sqlite3
.
Connection
):
print
(
"
Inserting data for node4
"
)
cursor
.
execute
(
"
INSERT INTO state (state) VALUES (?)
"
,
(
status
[
"
node4
"
][
"
state
"
],))
cursor
.
execute
(
state_insert
,
(
status
[
"
node4
"
][
"
state
"
],))
rs
=
resources
[
5
:]
cursor
.
executemany
(
"
INSERT INTO resources (id, name, last_update) VALUES (?, ?, ?)
"
,
rs
)
cursor
.
executemany
(
resources_insert
,
rs
)
cursor
.
executemany
(
precense_insert
,
[
(
"
640b0507b542f5854
"
,
node1_address
),
(
"
640b0507b542f5854
"
,
node4_address
)
])
for
r
in
rs
:
cursor
.
executemany
(
"
INSERT INTO commands (resource_id, id, create_date, command) VALUES (?, ?, ?, ?)
"
,
commands
[
r
[
0
]])
cursor
.
executemany
(
commands_insert
,
commands
[
r
[
0
]])
db_conn
.
commit
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment