Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.package
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
Kirill Smelkov
slapos.package
Commits
9359cf97
Commit
9359cf97
authored
Jul 31, 2015
by
Alain Takoudjou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tasks to upload ansible logs files
parent
aa2e627c
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
1 deletion
+119
-1
playbook/roles/vm-bootstrap/tasks/main.yml
playbook/roles/vm-bootstrap/tasks/main.yml
+2
-0
playbook/roles/vm-bootstrap/tasks/rerun.yml
playbook/roles/vm-bootstrap/tasks/rerun.yml
+1
-1
playbook/roles/vm-bootstrap/tasks/uploadlog.yml
playbook/roles/vm-bootstrap/tasks/uploadlog.yml
+9
-0
playbook/roles/vm-bootstrap/templates/upload-script.j2
playbook/roles/vm-bootstrap/templates/upload-script.j2
+107
-0
No files found.
playbook/roles/vm-bootstrap/tasks/main.yml
View file @
9359cf97
-
include
:
uploadlog.yml
-
include
:
rerun.yml
-
include
:
rerun.yml
-
name
:
Create /etc/opt dir
-
name
:
Create /etc/opt dir
...
...
playbook/roles/vm-bootstrap/tasks/rerun.yml
View file @
9359cf97
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
-
name
:
Add a periodical update on the VM
-
name
:
Add a periodical update on the VM
cron
:
name="Update vm bootstrap"
cron
:
name="Update vm bootstrap"
minute="*/12"
minute="*/12"
job="bash -lc /usr/local/bin/vm-bootstrap-update >
/var/log/vm-bootstrap.cron
.log"
job="bash -lc /usr/local/bin/vm-bootstrap-update >
> /var/log/vm-bootstrap
.log"
-
name
:
Check if /opt/slapos.playbook already exists
-
name
:
Check if /opt/slapos.playbook already exists
file
:
path=/opt/slapos.playbook/ state=directory
file
:
path=/opt/slapos.playbook/ state=directory
...
...
playbook/roles/vm-bootstrap/tasks/uploadlog.yml
0 → 100644
View file @
9359cf97
---
-
name
:
Add upload script
template
:
src=upload-script.j2 dest=/usr/local/bin/ansible-upload-vm-logs mode=755
-
name
:
Add a periodical upload of logs and result
cron
:
name="Upload ansible files to http server"
minute="*/7"
job="bash -lc /usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log"
playbook/roles/vm-bootstrap/templates/upload-script.j2
0 → 100644
View file @
9359cf97
#!/usr/bin/env python
import
os
import
sys
import
requests
class
uploader
():
log_path
=
'/var/log/ansible/hosts'
url
=
''
def
__init__
(
self
,
url
=
'http://10.0.2.100/'
):
self
.
url
=
url
def
upload_file
(
self
,
file_name
,
content
,
override
=
False
):
values
=
{
'path'
:
file_name
,
'content'
:
content
}
if
override
:
values
[
'clear'
]
=
'1'
result
=
requests
.
post
(
self
.
url
,
data
=
values
)
return
result
def
upload_result
(
self
):
for
filename
in
os
.
listdir
(
self
.
log_path
):
filepath
=
os
.
path
.
join
(
self
.
log_path
,
filename
)
to_path
=
'ansible/%s'
%
filename
if
os
.
path
.
exists
(
filepath
)
and
os
.
path
.
isfile
(
filepath
):
content
=
""
with
open
(
filepath
,
'r'
)
as
fd
:
content
=
','
.
join
(
fd
.
readlines
())
r
=
self
.
upload_file
(
to_path
,
'[%s]'
%
content
,
True
)
print
'Content uploaded to %s'
%
to_path
,
r
def
readFileFrom
(
self
,
f
,
lastPosition
,
limit
=
20000
):
"""
Returns the last lines of file `f`, from position lastPosition.
and the last position
limit = max number of characters to read
"""
BUFSIZ
=
1024
f
.
seek
(
0
,
2
)
btes
=
f
.
tell
()
block
=
-
1
data
=
""
length
=
btes
truncated
=
False
# True if a part of log data has been truncated
#if (lastPosition <= 0 and length > limit) or (length - lastPosition > limit):
# lastPosition = length - limit
# truncated = True
if
lastPosition
>
btes
:
lastPosition
=
0
size
=
btes
-
lastPosition
while
btes
>
lastPosition
:
if
abs
(
block
*
BUFSIZ
)
<=
size
:
# Seek back one whole BUFSIZ
f
.
seek
(
block
*
BUFSIZ
,
2
)
data
=
f
.
read
(
BUFSIZ
)
+
data
else
:
margin
=
abs
(
block
*
BUFSIZ
)
-
size
if
length
<
BUFSIZ
:
f
.
seek
(
0
,
0
)
else
:
seek
=
block
*
BUFSIZ
+
margin
f
.
seek
(
seek
,
2
)
data
=
f
.
read
(
BUFSIZ
-
margin
)
+
data
btes
-=
BUFSIZ
block
-=
1
f
.
close
()
return
{
'data'
:
data
,
'position'
:
length
,
'truncated'
:
truncated
}
if
__name__
==
"__main__"
:
ansible_log
=
"/var/log/vm-bootstrap.log"
url
=
"http://10.0.2.100/"
if
len
(
sys
.
argv
)
<
3
:
print
"Use: %s upload_url file_to_upload"
%
sys
.
argv
[
0
]
print
"Default: %s %s %s"
%
(
sys
.
argv
[
0
],
url
,
ansible_log
)
if
len
(
sys
.
argv
)
>=
2
:
url
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
>=
3
:
ansible_log
=
sys
.
argv
[
2
]
uploader
=
uploader
()
state_file
=
"/opt/.ansible_log.state"
log_destination
=
"ansible/vm-bootstrap.log"
current_state
=
0
if
os
.
path
.
exists
(
state_file
):
with
open
(
state_file
,
'r'
)
as
f
:
current_state
=
int
(
f
.
read
())
if
os
.
path
.
exists
(
ansible_log
):
f
=
open
(
ansible_log
,
'r'
)
content
=
uploader
.
readFileFrom
(
f
,
current_state
)
f
.
close
()
if
content
[
'data'
]:
r
=
uploader
.
upload_file
(
log_destination
,
content
[
'data'
])
print
'Content uploaded to %s'
%
log_destination
,
r
with
open
(
state_file
,
'w'
)
as
f
:
f
.
write
(
str
(
content
[
'position'
]))
else
:
print
'No Ansible log file found in %s.'
%
ansible_log
# Post Ansible execution result
uploader
.
upload_result
()
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