Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3938355
migrate-ssh-keys.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
migrate-ssh-keys.py
View Options
#!/usr/bin/env python3
# -------------------------------------------------------------
# rOPS — migrate SSH keys from file to Salt state
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2017-11-09
# Description: Read a dictionary, and for each key, find in
# a specified folder a data file. Add data from
# this file to the dictionary. Output in YAML.
# License: BSD-2-Clause
# -------------------------------------------------------------
# -------------------------------------------------------------
# Table of contents
# -------------------------------------------------------------
#
# :: Configuration
# :: YAML style
# :: Update code
# :: Run task
#
# -------------------------------------------------------------
import
os
import
yaml
# -------------------------------------------------------------
# Configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Where is located the dictionary to update?
state_file
=
'pillar/core/users.sls'
state_key
=
'shellusers'
# Where are located the data fileS?
data_path
=
'roles/shellserver/users/files/ssh_keys/'
# What property should get the data and be added if missing in the dict?
state_data_property
=
'ssh_keys'
# -------------------------------------------------------------
# YAML style
#
# Allows to dump with indented lists
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class
SaltStyleDumper
(
yaml
.
Dumper
):
def
increase_indent
(
self
,
flow
=
False
,
indentless
=
False
):
return
super
(
SaltStyleDumper
,
self
)
.
increase_indent
(
flow
,
False
)
# -------------------------------------------------------------
# Update code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def
do_update
():
state
=
read_state
()
update_state
(
state
)
print
(
dump_state
(
state
))
def
read_state
():
fd
=
open
(
state_file
,
"r"
)
states
=
yaml
.
load
(
fd
.
read
())
fd
.
close
()
return
states
[
state_key
]
def
update_state
(
state
):
for
key
in
state
:
if
state_data_property
not
in
state
[
key
]:
state
[
key
][
state_data_property
]
=
read_data
(
key
)
def
read_data
(
key
):
path
=
data_path
+
key
if
not
os
.
path
.
exists
(
path
):
return
[]
return
[
line
.
strip
()
for
line
in
open
(
path
,
"r"
)
if
is_value_line
(
line
)]
def
is_value_line
(
line
):
if
line
.
startswith
(
"#"
):
return
False
if
line
.
strip
()
==
''
:
return
False
return
True
def
dump_state
(
state
):
return
yaml
.
dump
({
state_key
:
state
},
default_flow_style
=
False
,
Dumper
=
SaltStyleDumper
,
width
=
1000
)
# -------------------------------------------------------------
# Run task
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
do_update
()
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Thu, Dec 26, 01:27 (5 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2268770
Default Alt Text
migrate-ssh-keys.py (2 KB)
Attached To
Mode
rOPS Nasqueron Operations
Attached
Detach File
Event Timeline
Log In to Comment