Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pim_dm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
pim_dm
Commits
dd3e14f8
Commit
dd3e14f8
authored
Jun 26, 2017
by
Pedro Oliveira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed argparser -> requires one argument
parent
f91a2e1a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
34 deletions
+41
-34
Daemon/Daemon.py
Daemon/Daemon.py
+18
-11
Run.py
Run.py
+23
-23
No files found.
Daemon/Daemon.py
View file @
dd3e14f8
...
@@ -63,14 +63,7 @@ class Daemon:
...
@@ -63,14 +63,7 @@ class Daemon:
"""Start the Daemon."""
"""Start the Daemon."""
# Check for a pidfile to see if the Daemon already runs
# Check for a pidfile to see if the Daemon already runs
try
:
if
self
.
is_running
():
with
open
(
self
.
pidfile
,
'r'
)
as
pf
:
pid
=
int
(
pf
.
read
().
strip
())
except
IOError
:
pid
=
None
if
pid
:
message
=
"pidfile {0} already exist. "
+
\
message
=
"pidfile {0} already exist. "
+
\
"Daemon already running?
\
n
"
"Daemon already running?
\
n
"
sys
.
stderr
.
write
(
message
.
format
(
self
.
pidfile
))
sys
.
stderr
.
write
(
message
.
format
(
self
.
pidfile
))
...
@@ -78,7 +71,6 @@ class Daemon:
...
@@ -78,7 +71,6 @@ class Daemon:
# Start the Daemon
# Start the Daemon
self
.
daemonize
()
self
.
daemonize
()
print
(
"daemonize"
)
self
.
run
()
self
.
run
()
def
stop
(
self
):
def
stop
(
self
):
...
@@ -86,7 +78,7 @@ class Daemon:
...
@@ -86,7 +78,7 @@ class Daemon:
# Get the pid from the pidfile
# Get the pid from the pidfile
try
:
try
:
with
open
(
self
.
pidfile
,
'r'
)
as
pf
:
with
open
(
self
.
pidfile
,
'r'
)
as
pf
:
pid
=
int
(
pf
.
read
().
strip
())
pid
=
int
(
pf
.
read
().
strip
())
except
IOError
:
except
IOError
:
pid
=
None
pid
=
None
...
@@ -120,4 +112,19 @@ class Daemon:
...
@@ -120,4 +112,19 @@ class Daemon:
"""You should override this method when you subclass Daemon.
"""You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by
It will be called after the process has been daemonized by
start() or restart()."""
start() or restart()."""
\ No newline at end of file
def
is_running
(
self
):
try
:
with
open
(
self
.
pidfile
,
'r'
)
as
pf
:
pid
=
int
(
pf
.
read
().
strip
())
except
IOError
:
return
False
""" Check For the existence of a unix pid. """
try
:
os
.
kill
(
pid
,
0
)
except
OSError
:
return
False
else
:
return
True
Run.py
View file @
dd3e14f8
...
@@ -10,11 +10,15 @@ import argparse
...
@@ -10,11 +10,15 @@ import argparse
class
MyDaemon
(
Daemon
):
class
MyDaemon
(
Daemon
):
def
stop
(
self
):
print
(
"stopping..."
)
# TODO: remove all interfaces
super
(
MyDaemon
,
self
).
stop
()
def
run
(
self
):
def
run
(
self
):
Main
.
main
()
Main
.
main
()
server_address
=
'./uds_socket'
server_address
=
'./uds_socket'
# Make sure the socket does not already exist
# Make sure the socket does not already exist
try
:
try
:
os
.
unlink
(
server_address
)
os
.
unlink
(
server_address
)
...
@@ -43,10 +47,10 @@ class MyDaemon(Daemon):
...
@@ -43,10 +47,10 @@ class MyDaemon(Daemon):
connection
.
sendall
(
pickle
.
dumps
(
Main
.
list_neighbors
()))
connection
.
sendall
(
pickle
.
dumps
(
Main
.
list_neighbors
()))
elif
args
.
add_interface
:
elif
args
.
add_interface
:
Main
.
add_interface
(
args
.
add_interface
[
0
])
Main
.
add_interface
(
args
.
add_interface
[
0
])
connection
.
s
endall
(
pickle
.
dumps
(
''
)
)
connection
.
s
hutdown
(
socket
.
SHUT_RDWR
)
elif
args
.
remove_interface
:
elif
args
.
remove_interface
:
Main
.
remove_interface
(
args
.
remove_interface
[
0
])
Main
.
remove_interface
(
args
.
remove_interface
[
0
])
connection
.
s
endall
(
pickle
.
dumps
(
''
)
)
connection
.
s
hutdown
(
socket
.
SHUT_RDWR
)
finally
:
finally
:
# Clean up the connection
# Clean up the connection
connection
.
close
()
connection
.
close
()
...
@@ -54,22 +58,15 @@ class MyDaemon(Daemon):
...
@@ -54,22 +58,15 @@ class MyDaemon(Daemon):
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
'PIM'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'PIM'
)
parser
.
add_argument
(
"-li"
,
"--list_interfaces"
,
action
=
"store_true"
,
default
=
False
,
group
=
parser
.
add_mutually_exclusive_group
(
required
=
True
)
help
=
"List All PIM Interfaces"
)
group
.
add_argument
(
"-start"
,
"--start"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Start PIM"
)
parser
.
add_argument
(
"-ln"
,
"--list_neighbors"
,
action
=
"store_true"
,
default
=
False
,
group
.
add_argument
(
"-stop"
,
"--stop"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Stop PIM"
)
help
=
"List All PIM Neighbors"
)
group
.
add_argument
(
"-restart"
,
"--restart"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Restart PIM"
)
parser
.
add_argument
(
"-ai"
,
"--add_interface"
,
nargs
=
1
,
group
.
add_argument
(
"-li"
,
"--list_interfaces"
,
action
=
"store_true"
,
default
=
False
,
help
=
"List All PIM Interfaces"
)
help
=
"Add PIM interface"
)
group
.
add_argument
(
"-ln"
,
"--list_neighbors"
,
action
=
"store_true"
,
default
=
False
,
help
=
"List All PIM Neighbors"
)
parser
.
add_argument
(
"-ri"
,
"--remove_interface"
,
nargs
=
1
,
group
.
add_argument
(
"-ai"
,
"--add_interface"
,
nargs
=
1
,
metavar
=
'IP_INTERFACE'
,
help
=
"Add PIM interface"
)
help
=
"Remove PIM interface"
)
group
.
add_argument
(
"-ri"
,
"--remove_interface"
,
nargs
=
1
,
metavar
=
'IP_INTERFACE'
,
help
=
"Remove PIM interface"
)
parser
.
add_argument
(
"-start"
,
"--start"
,
action
=
"store_true"
,
default
=
False
,
group
.
add_argument
(
"-v"
,
"--verbose"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Verbose (print all debug messages)"
)
help
=
"Start PIM"
)
parser
.
add_argument
(
"-stop"
,
"--stop"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Stop PIM"
)
parser
.
add_argument
(
"-restart"
,
"--restart"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Restart PIM"
)
parser
.
add_argument
(
"-v"
,
"--verbose"
,
action
=
"store_true"
,
default
=
False
,
help
=
"Verbose (print all debug messages)"
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
print
(
parser
.
parse_args
())
print
(
parser
.
parse_args
())
...
@@ -78,7 +75,6 @@ if __name__ == "__main__":
...
@@ -78,7 +75,6 @@ if __name__ == "__main__":
if
args
.
start
:
if
args
.
start
:
print
(
"start"
)
print
(
"start"
)
daemon
.
start
()
daemon
.
start
()
print
(
"start"
)
sys
.
exit
(
0
)
sys
.
exit
(
0
)
elif
args
.
stop
:
elif
args
.
stop
:
daemon
.
stop
()
daemon
.
stop
()
...
@@ -89,6 +85,10 @@ if __name__ == "__main__":
...
@@ -89,6 +85,10 @@ if __name__ == "__main__":
elif
args
.
verbose
:
elif
args
.
verbose
:
os
.
system
(
"tailf stdout"
)
os
.
system
(
"tailf stdout"
)
sys
.
exit
(
0
)
sys
.
exit
(
0
)
elif
not
daemon
.
is_running
():
print
(
"PIM is not running"
)
parser
.
print_usage
()
sys
.
exit
(
0
)
# Create a UDS socket
# Create a UDS socket
...
@@ -99,7 +99,7 @@ if __name__ == "__main__":
...
@@ -99,7 +99,7 @@ if __name__ == "__main__":
print
(
'connecting to %s'
%
server_address
)
print
(
'connecting to %s'
%
server_address
)
try
:
try
:
sock
.
connect
(
server_address
)
sock
.
connect
(
server_address
)
except
(
socket
.
error
)
:
except
socket
.
error
:
print
(
"erro socket"
)
print
(
"erro socket"
)
print
(
sys
.
stderr
)
print
(
sys
.
stderr
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
...
@@ -108,8 +108,8 @@ if __name__ == "__main__":
...
@@ -108,8 +108,8 @@ if __name__ == "__main__":
sock
.
sendall
(
pickle
.
dumps
(
args
))
sock
.
sendall
(
pickle
.
dumps
(
args
))
data
=
sock
.
recv
(
1024
*
256
)
data
=
sock
.
recv
(
1024
*
256
)
print
(
pickle
.
loads
(
data
))
if
data
:
print
(
pickle
.
loads
(
data
))
finally
:
finally
:
print
(
'closing socket'
)
print
(
'closing socket'
)
sock
.
close
()
sock
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment