forked from tranquilit/WAPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setuphelpers_linux.py
211 lines (174 loc) · 7.61 KB
/
setuphelpers_linux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------
# This file is part of WAPT
# Copyright (C) 2013 Tranquil IT Systems http://www.tranquil.it
# WAPT aims to help Windows systems administrators to deploy
# setup and update applications on users PC.
#
# WAPT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# WAPT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WAPT. If not, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------
from __future__ import absolute_import
import os
import socket
import struct
import getpass
import platform
import configparser
import platform
import psutil
import netifaces
import json
import cpuinfo
import sys
import subprocess
import logging
import glob
import datetime
import platform
import re
from setuphelpers_unix import *
try:
import apt
except:
apt=None
try:
import rpm
except:
rpm=None
logger = logging.getLogger('waptcore')
def isLinux64():
return platform.machine().endswith('64')
def get_distrib_version():
return platform.linux_distribution()[1]
def get_distrib_linux():
return platform.linux_distribution()[0]
def type_debian():
return platform.dist()[0].lower() in ('debian','linuxmint','ubuntu')
def type_redhat():
return platform.dist()[0].lower() in ('redhat','centos','fedora')
def host_info():
info = host_info_common_unix()
try:
dmi = dmi_info()
info['system_manufacturer'] = dmi['System_Information']['Manufacturer']
info['system_productname'] = dmi['System_Information']['Product_Name']
except:
logger.warning('Error while running dmidecode, dmidecode needs root privileges')
pass
info['platform'] = platform.system()
info['os_name'] = platform.linux_distribution()[0]
info['os_version'] = platform.linux_distribution()[1]
info['linux64'] = isLinux64()
info['distrib'] = get_distrib_linux()
info['distrib_version'] = get_distrib_version()
info['local_groups'] = {g.gr_name:g.gr_mem for g in grp.getgrall()}
info['local_users'] = []
for u in pwd.getpwall():
info['local_users'].append(u.pw_name)
gr_struct=grp.getgrgid(u.pw_gid)
if info['local_groups'].has_key(gr_struct.gr_name):
if u.pw_name not in info['local_groups'][gr_struct.gr_name]:
info['local_groups'][gr_struct.gr_name].append(u.pw_name)
else:
info['local_groups'][gr_struct.gr_name]=[u.pw_name]
return info
def dmi_info():
return dmi_info_common_unix()
def installed_softwares(keywords='',name=None):
""" Return list of installed software from apt or rpm
Args:
keywords (str or list): string to lookup in key, display_name or publisher fields
Returns:
list of dicts: [{'key', 'name', 'version', 'install_date', 'install_location'
'uninstall_string', 'publisher','system_component'}]
"""
name_re = re.compile(name) if name is not None else None
list_installed_softwares=[]
if isinstance(keywords,str) or isinstance(keywords,unicode):
keywords = keywords.lower().split()
else:
keywords = [ k.lower() for k in keywords ]
def check_words(target,words):
mywords = target.lower()
result = not words or mywords
for w in words:
result = result and w in mywords
return result
if apt:
for pkg in apt.Cache():
path_dpkg_info ="/var/lib/dpkg/info/"
if pkg.is_installed and ((name_re is None or name_re.match(pkg.name) or name_re.match(pkg.fullname)) or check_words(' '.join[pkg.name,pkg.fullname,pkg.versions[0].homepage],keywords)):
try:
if os.path.isfile(os.path.join(path_dpkg_info,(pkg.name+'.list'))):
install_date=os.path.getctime(os.path.join(path_dpkg_info,(pkg.name+'.list')))
else:
install_date=os.path.getctime(os.path.join(path_dpkg_info,(pkg.fullname+'.list')))
install_date=datetime.datetime.fromtimestamp(install_date).strftime('%Y-%m-%d %H:%M:%S')
except:
install_date=''
list_installed_softwares.append({'key':'','name':pkg.name,'version':str(pkg.installed).rsplit('=',1)[-1],'install_date':install_date,'install_location':'','uninstall_string':'','publisher':pkg.versions[0].homepage,'system_component':''})
elif rpm:
trans = rpm.TransactionSet()
for header in trans.dbMatch():
if (name_re is None or name_re.match(header['name'])) or check_words(' '.join[header['name'],header['url']],keywords):
list_installed_softwares.append({'key':'','name':header['name'],'version':header['version'],'install_date':datetime.datetime.strptime(header.sprintf("%{INSTALLTID:date}"),'%a %b %d %H:%M:%S %Y').strftime('%Y-%m-%d %H:%M:%S'),'install_location':'','uninstall_string':'','publisher':header['url'],'system_component':''})
else:
list_installed_softwares.append({'key':'Distribution not supported yet', 'name':'Distribution not supported yet', 'version':'Distribution not supported yet', 'install_date':'Distribution not supported yet', 'install_location':'Distribution not supported yet', 'uninstall_string':'Distribution not supported yet', 'publisher':'Distribution not supported yet','system_component':'Distribution not supported yet'})
return list_installed_softwares
def install_apt(package,allow_unauthenticated=False):
"""
Install .deb package from apt repositories
"""
update_apt()
if allow_unauthenticated:
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated %s' %package)
else:
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get install -y %s' %package)
def uninstall_apt(package):
"""
Remove a .deb package
"""
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get remove -y %s' %package)
def install_deb(path_to_deb):
"""
Install .deb package from file
"""
try:
return run('LANG=C DEBIAN_FRONTEND=noninteractive dpkg -i %s' % path_to_deb)
except:
return install_required_dependencies_apt()
def purge_deb(deb_name):
return run('LANG=C DEBIAN_FRONTEND=noninteractive dpkg --purge %s' % deb_name)
def install_required_dependencies_apt():
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get -f -y install')
def autoremove_apt():
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get -y autoremove')
def install_yum(package):
return run('LANG=C yum install -y %s' % (package))
def uninstall_yum(package):
return run('LANG=C yum remove -y %s' % package)
def autoremove_yum():
return run('LANG=C yum autoremove -y')
def update_apt():
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get -y update')
def upgrade_apt():
return run('LANG=C DEBIAN_FRONTEND=noninteractive apt-get -y upgrade')
def update_yum():
return run('LANG=C yum update -y')
def upgrade_yum():
return run('LANG=C yum upgrade -y')
def install_rpm(package):
return run('LANG=C yum localinstall -y %s' % (package))