Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F117520080
vyconf_session.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
vyconf_session.py
View Options
# Copyright 2025 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#
import
os
import
tempfile
import
shutil
from
functools
import
wraps
from
typing
import
Type
from
vyos.proto
import
vyconf_client
from
vyos.migrate
import
ConfigMigrate
from
vyos.migrate
import
ConfigMigrateError
from
vyos.component_version
import
append_system_version
from
vyos.utils.session
import
in_config_session
class
VyconfSessionError
(
Exception
):
pass
class
VyconfSession
:
def
__init__
(
self
,
token
:
str
=
None
,
pid
:
int
=
None
,
on_error
:
Type
[
Exception
]
=
None
):
self
.
pid
=
os
.
getpid
()
if
pid
is
None
else
pid
if
token
is
None
:
# CLI applications with arg pid=getppid() allow coordination
# with the ambient session; other uses (such as ConfigSession)
# may default to self pid
out
=
vyconf_client
.
send_request
(
'session_of_pid'
,
client_pid
=
self
.
pid
)
if
out
.
output
is
None
:
out
=
vyconf_client
.
send_request
(
'setup_session'
,
client_pid
=
self
.
pid
)
self
.
__token
=
out
.
output
else
:
out
=
vyconf_client
.
send_request
(
'session_update_pid'
,
token
=
token
,
client_pid
=
self
.
pid
)
if
out
.
status
:
raise
ValueError
(
f
'No existing session for token: {token}'
)
self
.
__token
=
token
self
.
in_config_session
=
in_config_session
()
if
self
.
in_config_session
:
out
=
vyconf_client
.
send_request
(
'enter_configuration_mode'
,
token
=
self
.
__token
)
if
out
.
status
:
raise
VyconfSessionError
(
self
.
output
(
out
))
self
.
on_error
=
on_error
def
__del__
(
self
):
if
not
self
.
in_config_session
:
self
.
teardown
()
def
teardown
(
self
):
vyconf_client
.
send_request
(
'teardown'
,
token
=
self
.
__token
)
def
exit_config_mode
(
self
):
if
self
.
session_changed
():
return
'Uncommited changes'
,
Errnum
.
UNCOMMITED_CHANGES
out
=
vyconf_client
.
send_request
(
'exit_configuration_mode'
,
token
=
self
.
__token
)
return
self
.
output
(
out
),
out
.
status
def
in_session
(
self
)
->
bool
:
return
self
.
in_config_session
def
session_changed
(
self
)
->
bool
:
out
=
vyconf_client
.
send_request
(
'session_changed'
,
token
=
self
.
__token
)
return
not
bool
(
out
.
status
)
@staticmethod
def
config_mode
(
f
):
@wraps
(
f
)
def
wrapped
(
self
,
*
args
,
**
kwargs
):
msg
=
'operation not available outside of config mode'
if
not
self
.
in_config_session
:
if
self
.
on_error
is
None
:
raise
VyconfSessionError
(
msg
)
raise
self
.
on_error
(
msg
)
return
f
(
self
,
*
args
,
**
kwargs
)
return
wrapped
@staticmethod
def
raise_exception
(
f
):
@wraps
(
f
)
def
wrapped
(
self
,
*
args
,
**
kwargs
):
if
self
.
on_error
is
None
:
return
f
(
self
,
*
args
,
**
kwargs
)
o
,
e
=
f
(
self
,
*
args
,
**
kwargs
)
if
e
:
raise
self
.
on_error
(
o
)
return
o
,
e
return
wrapped
@staticmethod
def
output
(
o
):
out
=
''
for
res
in
(
o
.
output
,
o
.
error
,
o
.
warning
):
if
res
is
not
None
:
out
=
out
+
res
return
out
@raise_exception
@config_mode
def
set
(
self
,
path
:
list
[
str
])
->
tuple
[
str
,
int
]:
out
=
vyconf_client
.
send_request
(
'set'
,
token
=
self
.
__token
,
path
=
path
)
return
self
.
output
(
out
),
out
.
status
@raise_exception
@config_mode
def
delete
(
self
,
path
:
list
[
str
])
->
tuple
[
str
,
int
]:
out
=
vyconf_client
.
send_request
(
'delete'
,
token
=
self
.
__token
,
path
=
path
)
return
self
.
output
(
out
),
out
.
status
@raise_exception
@config_mode
def
commit
(
self
)
->
tuple
[
str
,
int
]:
out
=
vyconf_client
.
send_request
(
'commit'
,
token
=
self
.
__token
)
return
output
(
out
),
out
.
status
@raise_exception
@config_mode
def
discard
(
self
)
->
tuple
[
str
,
int
]:
out
=
vyconf_client
.
send_request
(
'discard'
,
token
=
self
.
__token
)
return
self
.
output
(
out
),
out
.
status
@raise_exception
@config_mode
def
load_config
(
self
,
file
:
str
,
migrate
:
bool
=
False
)
->
tuple
[
str
,
int
]:
# pylint: disable=consider-using-with
if
migrate
:
tmp
=
tempfile
.
NamedTemporaryFile
()
shutil
.
copy2
(
file
,
tmp
.
name
)
config_migrate
=
ConfigMigrate
(
tmp
.
name
)
try
:
config_migrate
.
run
()
except
ConfigMigrateError
as
e
:
tmp
.
close
()
return
repr
(
e
),
1
file
=
tmp
.
name
else
:
tmp
=
''
out
=
vyconf_client
.
send_request
(
'load'
,
token
=
self
.
__token
,
location
=
file
)
if
tmp
:
tmp
.
close
()
return
self
.
output
(
out
),
out
.
status
@raise_exception
def
save_config
(
self
,
file
:
str
,
append_version
:
bool
=
False
)
->
tuple
[
str
,
int
]:
out
=
vyconf_client
.
send_request
(
'save'
,
token
=
self
.
__token
,
location
=
file
)
if
append_version
:
append_system_version
(
file
)
return
self
.
output
(
out
),
out
.
status
@raise_exception
def
show_config
(
self
,
path
:
list
[
str
]
=
None
)
->
tuple
[
str
,
int
]:
if
path
is
None
:
path
=
[]
out
=
vyconf_client
.
send_request
(
'show_config'
,
token
=
self
.
__token
,
path
=
path
)
return
self
.
output
(
out
),
out
.
status
File Metadata
Details
Attached
Mime Type
text/x-script.python
Expires
Sat, Sep 26, 8:55 AM (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4284701
Default Alt Text
vyconf_session.py (5 KB)
Attached To
Mode
rVYOSONEX vyos-1x
Attached
Detach File
Event Timeline
Log In to Comment