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
Branches containing commit
No related tags found
Tags containing commit
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 @@
...
@@ -5,6 +5,7 @@
from
collections
import
OrderedDict
from
collections
import
OrderedDict
from
pathlib
import
Path
from
pathlib
import
Path
import
re
import
re
import
socket
import
subprocess
import
subprocess
import
sys
import
sys
from
typing
import
Any
from
typing
import
Any
...
@@ -277,9 +278,9 @@ def run_commands(cmds: list):
...
@@ -277,9 +278,9 @@ def run_commands(cmds: list):
if
not
cmd
.
get
(
"
target
"
):
if
not
cmd
.
get
(
"
target
"
):
raise
Exception
(
"
No target file to write in.
"
)
raise
Exception
(
"
No target file to write in.
"
)
if
(
if
(
cmd
.
get
(
"
backup
"
)
and
cmd
.
get
(
"
backup
"
)
Path
(
cmd
[
"
target
"
]).
exists
()
and
and
Path
(
cmd
[
"
target
"
]).
exists
()
not
Path
(
cmd
[
"
target
"
]
+
"
.back
"
).
exists
()
and
not
Path
(
cmd
[
"
target
"
]
+
"
.back
"
).
exists
()
):
):
Path
(
cmd
[
"
target
"
]).
rename
(
Path
(
cmd
[
"
target
"
]
+
"
.back
"
))
Path
(
cmd
[
"
target
"
]).
rename
(
Path
(
cmd
[
"
target
"
]
+
"
.back
"
))
log
(
"
A backup file has been created for:
\n
%s
"
%
cmd
[
"
target
"
])
log
(
"
A backup file has been created for:
\n
%s
"
%
cmd
[
"
target
"
])
...
@@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list):
...
@@ -325,30 +326,30 @@ def add_hosts_to_localhost(hosts: list):
:raises Exception: Houston we have a problem
: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
:
if
rc
==
0
and
hostname
not
in
hosts
:
hosts
.
insert
(
0
,
hostname
)
hosts
.
insert
(
0
,
hostname
)
with
open
(
'
/etc/hosts
'
,
'
r
'
)
as
fo
:
with
open
(
"
/etc/hosts
"
,
"
r
"
)
as
fo
:
content
=
fo
.
read
()
content
=
fo
.
read
()
new_content
=
list
()
new_content
=
list
()
found_127
=
False
found_127
=
False
for
line
in
content
.
split
(
'
\n
'
):
for
line
in
content
.
split
(
"
\n
"
):
if
not
found_127
and
line
.
startswith
(
'
127.0.0.1
'
):
if
not
found_127
and
line
.
startswith
(
"
127.0.0.1
"
):
found_127
=
True
found_127
=
True
for
host
in
hosts
:
for
host
in
hosts
:
if
'
'
+
host
not
in
line
:
if
"
"
+
host
not
in
line
:
line
+=
'
'
+
host
line
+=
"
"
+
host
log
(
'
Adding host %s to /etc/hosts 127.0.0.1 aliases.
'
%
host
)
log
(
"
Adding host %s to /etc/hosts 127.0.0.1 aliases.
"
%
host
)
new_content
.
append
(
line
)
new_content
.
append
(
line
)
if
not
found_127
:
if
not
found_127
:
new_content
.
append
(
'
127.0.0.1 %s
'
%
'
'
.
join
(
hosts
))
new_content
.
append
(
"
127.0.0.1 %s
"
%
"
"
.
join
(
hosts
))
new_content
=
'
\n
'
.
join
(
new_content
)
new_content
=
"
\n
"
.
join
(
new_content
)
if
new_content
!=
content
:
if
new_content
!=
content
:
with
open
(
'
/etc/hosts
'
,
'
w
'
)
as
fo
:
with
open
(
"
/etc/hosts
"
,
"
w
"
)
as
fo
:
fo
.
write
(
new_content
)
fo
.
write
(
new_content
)
log
(
'
/etc/hosts updated.
'
)
log
(
"
/etc/hosts updated.
"
)
else
:
else
:
log
(
'
/etc/hosts is already up to date.
'
)
log
(
"
/etc/hosts is already up to date.
"
)
OPENSSL_CONFIG_TEMPLATE
=
"""
OPENSSL_CONFIG_TEMPLATE
=
"""
...
@@ -392,7 +393,12 @@ subjectAltName = @alternate_names
...
@@ -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.
"""
Generate a self-signed certificate for the domains list.
:param domains: Domains for which the certificates will be self-signed
: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 =
...
@@ -416,24 +422,30 @@ def mkcert(domains: list, ecc: bool = True, days: int = 3650, config_tpl: str =
config_fh
.
write
(
config_tpl
)
config_fh
.
write
(
config_tpl
)
# key type: elliptic curve (default) or rsa
# key type: elliptic curve (default) or rsa
if
ecc
:
if
ecc
:
subprocess
.
check_call
([
subprocess
.
check_call
(
"
openssl
"
,
"
ecparam
"
,
[
"
openssl
"
,
"
ecparam
"
,
"
-name
"
,
"
secp384r1
"
,
"
-out
"
,
cert_dir
+
"
/ecparam
"
]
"
-name
"
,
"
secp384r1
"
,
)
"
-out
"
,
cert_dir
+
"
/ecparam
"
])
keytype
=
"
ec:
"
+
cert_dir
+
"
/ecparam
"
keytype
=
"
ec:
"
+
cert_dir
+
"
/ecparam
"
else
:
else
:
keytype
=
"
rsa
"
keytype
=
"
rsa
"
# execute openssl to generate keypair
# execute openssl to generate keypair
subprocess
.
check_call
([
subprocess
.
check_call
(
"
openssl
"
,
"
req
"
,
[
"
-config
"
,
cert_dir
+
"
/conf
"
,
"
openssl
"
,
"
-new
"
,
"
req
"
,
"
-x509
"
,
"
-config
"
,
"
-sha256
"
,
cert_dir
+
"
/conf
"
,
"
-nodes
"
,
"
-new
"
,
"
-newkey
"
,
keytype
,
"
-x509
"
,
"
-keyout
"
,
cert_dir
+
"
/key.pem
"
,
"
-sha256
"
,
"
-days
"
,
str
(
days
),
"
-nodes
"
,
"
-out
"
,
cert_dir
+
"
/cert.pem
"
"
-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