Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
envsetup
Manage
Activity
Members
Plan
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mediaserver
envsetup
Commits
96c18469
Verified
Commit
96c18469
authored
5 years ago
by
Nicolas KAROLAK
Browse files
Options
Downloads
Patches
Plain Diff
blacked
parent
cca16eba
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils.py
+45
-33
45 additions, 33 deletions
utils.py
with
45 additions
and
33 deletions
utils.py
+
45
−
33
View file @
96c18469
...
...
@@ -5,6 +5,7 @@
from
collections
import
OrderedDict
from
pathlib
import
Path
import
re
import
socket
import
subprocess
import
sys
from
typing
import
Any
...
...
@@ -277,9 +278,9 @@ def run_commands(cmds: list):
if
not
cmd
.
get
(
"
target
"
):
raise
Exception
(
"
No target file to write in.
"
)
if
(
cmd
.
get
(
"
backup
"
)
and
Path
(
cmd
[
"
target
"
]).
exists
()
and
not
Path
(
cmd
[
"
target
"
]
+
"
.back
"
).
exists
()
cmd
.
get
(
"
backup
"
)
and
Path
(
cmd
[
"
target
"
]).
exists
()
and
not
Path
(
cmd
[
"
target
"
]
+
"
.back
"
).
exists
()
):
Path
(
cmd
[
"
target
"
]).
rename
(
Path
(
cmd
[
"
target
"
]
+
"
.back
"
))
log
(
"
A backup file has been created for:
\n
%s
"
%
cmd
[
"
target
"
])
...
...
@@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list):
:raises Exception: Houston we have a problem
"""
rc
,
hostname
=
exec_cmd
(
'
hostname
'
)
rc
,
hostname
=
exec_cmd
(
"
hostname
"
)
if
rc
==
0
and
hostname
not
in
hosts
:
hosts
.
insert
(
0
,
hostname
)
with
open
(
'
/etc/hosts
'
,
'
r
'
)
as
fo
:
with
open
(
"
/etc/hosts
"
,
"
r
"
)
as
fo
:
content
=
fo
.
read
()
new_content
=
list
()
found_127
=
False
for
line
in
content
.
split
(
'
\n
'
):
if
not
found_127
and
line
.
startswith
(
'
127.0.0.1
'
):
for
line
in
content
.
split
(
"
\n
"
):
if
not
found_127
and
line
.
startswith
(
"
127.0.0.1
"
):
found_127
=
True
for
host
in
hosts
:
if
'
'
+
host
not
in
line
:
line
+=
'
'
+
host
log
(
'
Adding host %s to /etc/hosts 127.0.0.1 aliases.
'
%
host
)
if
"
"
+
host
not
in
line
:
line
+=
"
"
+
host
log
(
"
Adding host %s to /etc/hosts 127.0.0.1 aliases.
"
%
host
)
new_content
.
append
(
line
)
if
not
found_127
:
new_content
.
append
(
'
127.0.0.1 %s
'
%
'
'
.
join
(
hosts
))
new_content
=
'
\n
'
.
join
(
new_content
)
new_content
.
append
(
"
127.0.0.1 %s
"
%
"
"
.
join
(
hosts
))
new_content
=
"
\n
"
.
join
(
new_content
)
if
new_content
!=
content
:
with
open
(
'
/etc/hosts
'
,
'
w
'
)
as
fo
:
with
open
(
"
/etc/hosts
"
,
"
w
"
)
as
fo
:
fo
.
write
(
new_content
)
log
(
'
/etc/hosts updated.
'
)
log
(
"
/etc/hosts updated.
"
)
else
:
log
(
'
/etc/hosts is already up to date.
'
)
log
(
"
/etc/hosts is already up to date.
"
)
OPENSSL_CONFIG_TEMPLATE
=
"""
...
...
@@ -392,7 +393,12 @@ subjectAltName = @alternate_names
"""
def
mkcert
(
domains
:
list
,
ecc
:
bool
=
True
,
days
:
int
=
3650
,
config_tpl
:
str
=
OPENSSL_CONFIG_TEMPLATE
):
def
mkcert
(
domains
:
list
,
ecc
:
bool
=
True
,
days
:
int
=
3650
,
config_tpl
:
str
=
OPENSSL_CONFIG_TEMPLATE
,
):
"""
Generate a self-signed certificate for the domains list.
:param domains: Domains for which the certificates will be self-signed
...
...
@@ -416,24 +422,30 @@ def mkcert(domains: list, ecc: bool = True, days: int = 3650, config_tpl: str =
config_fh
.
write
(
config_tpl
)
# key type: elliptic curve (default) or rsa
if
ecc
:
subprocess
.
check_call
([
"
openssl
"
,
"
ecparam
"
,
"
-name
"
,
"
secp384r1
"
,
"
-out
"
,
cert_dir
+
"
/ecparam
"
])
subprocess
.
check_call
(
[
"
openssl
"
,
"
ecparam
"
,
"
-name
"
,
"
secp384r1
"
,
"
-out
"
,
cert_dir
+
"
/ecparam
"
]
)
keytype
=
"
ec:
"
+
cert_dir
+
"
/ecparam
"
else
:
keytype
=
"
rsa
"
# execute openssl to generate keypair
subprocess
.
check_call
([
"
openssl
"
,
"
req
"
,
"
-config
"
,
cert_dir
+
"
/conf
"
,
"
-new
"
,
"
-x509
"
,
"
-sha256
"
,
"
-nodes
"
,
"
-newkey
"
,
keytype
,
"
-keyout
"
,
cert_dir
+
"
/key.pem
"
,
"
-days
"
,
str
(
days
),
"
-out
"
,
cert_dir
+
"
/cert.pem
"
])
subprocess
.
check_call
(
[
"
openssl
"
,
"
req
"
,
"
-config
"
,
cert_dir
+
"
/conf
"
,
"
-new
"
,
"
-x509
"
,
"
-sha256
"
,
"
-nodes
"
,
"
-newkey
"
,
keytype
,
"
-keyout
"
,
cert_dir
+
"
/key.pem
"
,
"
-days
"
,
str
(
days
),
"
-out
"
,
cert_dir
+
"
/cert.pem
"
,
]
)
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