Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tsm-orchestration
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
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
UFZ TSM
tsm-orchestration
Commits
169a62ac
Commit
169a62ac
authored
1 year ago
by
Bert Palm
Browse files
Options
Downloads
Patches
Plain Diff
first steps
parent
fc3fd822
No related branches found
No related tags found
1 merge request
!148
sftp sync script
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cron/scripts/sftp_sync/sftp_sync.py
+74
-0
74 additions, 0 deletions
cron/scripts/sftp_sync/sftp_sync.py
with
74 additions
and
0 deletions
cron/scripts/sftp_sync/sftp_sync.py
0 → 100644
+
74
−
0
View file @
169a62ac
#!/usr/bin/env python
from
__future__
import
annotations
import
os
import
sys
from
dataclasses
import
dataclass
import
psycopg
from
paramiko
import
SSHClient
@dataclass
class
FtpMeta
:
uri
:
str
path
:
str
username
:
str
password
:
str
=
None
def
remote_dir
(
self
)
->
str
:
return
f
"
{
self
.
uri
}
/
{
self
.
path
}
"
def
need_ssh_key
(
self
):
return
bool
(
self
.
password
)
def
connect_internal_ftp
():
pass
def
connect_external_ftp
():
pass
def
get_credentials
(
dsn
,
thing_id
):
with
psycopg
.
connect
(
dsn
)
as
conn
:
with
conn
.
cursor
()
as
cur
:
cur
.
execute
(
"
SELECT r.fileserver_uri, r.bucket, r.access_key, r.secret_key
"
# fixme
# ",t.ext_sftp_uri, t.ext_sftp_path, t.ext_sftp_username, t.ext_sftp_password "
"
FROM tsm_thing t
"
"
JOIN tsm_rawdatastorage r ON t.id = r.thing_id
"
"
WHERE t.thing_id = %s
"
,
[
thing_id
],
)
r
=
cur
.
fetchone
()
# fixme
if
len
(
r
)
==
4
:
r
=
r
+
r
internal
=
FtpMeta
(
*
r
[:
4
])
external
=
FtpMeta
(
*
r
[
4
:])
return
internal
,
external
def
connect
(
ftp
:
FtpMeta
):
client
=
SSHClient
()
client
.
load_system_host_keys
()
client
.
connect
(
"
ssh.example.com
"
)
stdin
,
stdout
,
stderr
=
client
.
exec_command
(
"
ls -l
"
)
if
__name__
==
"
__main__
"
:
if
len
(
sys
.
argv
)
!=
2
:
raise
ValueError
(
"
Expected a thing_id as first and only argument.
"
)
thing_id
=
sys
.
argv
[
1
]
for
k
in
[
"
SSH_PRIV_KEY_PATH
"
,
"
FTP_AUTH_DB_URI
"
]:
if
(
os
.
environ
.
get
(
k
)
or
None
)
is
None
:
raise
EnvironmentError
(
"
Environment variable {k} must be set
"
)
ssh_key
=
os
.
environ
[
"
SSH_PRIV_KEY_PATH
"
]
dsn
=
os
.
environ
[
"
FTP_AUTH_DB_URI
"
]
iftp
,
eftp
=
get_credentials
(
dsn
,
thing_id
)
print
(
iftp
)
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