Skip to content

Commit

Permalink
centos network
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Apr 19, 2015
1 parent 8a16811 commit a6fe318
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions plugins/network/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api
import managers.centos_manager
import managers.debian_manager

# assert
Expand Down
101 changes: 101 additions & 0 deletions plugins/network/managers/centos_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import os
import subprocess

import aj
from aj.api import component
from aj.plugins.augeas.api import Augeas
from aj.plugins.network.api import NetworkManager

from .ifconfig import ifconfig_up, ifconfig_down, ifconfig_get_ip, ifconfig_get_up


@component(NetworkManager)
class CentOSNetworkManager(NetworkManager):
path = '/etc/sysconfig/network-scripts'
aug_path = '/files' + path

@classmethod
def __verify__(cls):
return aj.platform in ['centos']

def __init__(self, context):
NetworkManager.__init__(self, context)

def get_augeas(self, iface):
aug = Augeas(modules=[{
'name': 'Shellvars',
'lens': 'Shellvars.lns',
'incl': [
os.path.join(self.path, 'ifcfg-' + iface),
]
}])
aug.load()
return aug

def get_config(self):
ifaces = []
for file in os.listdir(self.path):
if file.startswith('ifcfg-'):
name = file.split('-')[1]
path = os.path.join(self.path, file)
aug_path = os.path.join(self.aug_path, file)
aug = self.get_augeas(name)
iface = {
'name': name,
'family': 'inet6' if bool(aug.get(aug_path + '/IPV6INIT')) else 'inet',
'addressing': aug.get(aug_path + '/BOOTPROTO') or 'static',
'address': aug.get(aug_path + '/IPADDR'),
'mask': aug.get(aug_path + '/NETMASK'),
'gateway': aug.get(aug_path + '/GATEWAY') if bool(aug.get(aug_path + '/IPV6INIT')) else aug.get(aug_path + '/IPV6_DEFAULTGW'),
'hwaddress': aug.get(aug_path + '/HWADDR'),
#'mtu': aug.get(aug_path + '/mtu'),
#'scope': aug.get(aug_path + '/scope'),
#'metric': aug.get(aug_path + '/metric'),
'dhcpClient': aug.get(aug_path + '/DHCP_HOSTNAME'),
}
ifaces.append(iface)
return ifaces

def set_config(self, config):
for index, iface in enumerate(config):
aug = self.get_augeas(iface['name'])
if iface['family'] == 'inet':
aug.remove(aug_path + '/IPV6INIT')
aug.remove(aug_path + '/IPV6ADDR')
aug.remove(aug_path + '/IPV6_DEFAULTGW')
aug.setd(aug_path + '/IPADDR', iface['address'])
aug.setd(aug_path + '/NETMASK', iface['mask'])
aug.setd(aug_path + '/GATEWAY', iface['gateway'])
else:
aug.remove(aug_path + '/IPADDR')
aug.remove(aug_path + '/NETMASK')
aug.remove(aug_path + '/GATEWAY')
aug.setd(aug_path + '/IPV6INIT', 'yes')
aug.setd(aug_path + '/IPV6ADDR', iface['address'])
aug.setd(aug_path + '/IPV6_DEFAULTGW', iface['gateway'])

aug.setd(path + '/BOOTPROTO', iface['method'])
aug.setd(path + '/HWADDR', iface['hwaddress'])
aug.setd(path + '/DHCP_HOSTNAME', iface['dhcpClient'])
aug.save()

def get_state(self, iface):
return {
'address': ifconfig_get_ip(iface),
'up': ifconfig_get_up(iface),
}

def up(self, iface):
ifconfig_up(iface)

def down(self, iface):
ifconfig_down(iface)

def get_hostname(self):
return subprocess.check_output('hostname')

def set_hostname(self, value):
with open('/etc/hostname', 'w') as f:
f.write(value)
subprocess.check_call(['hostname', value])

8 changes: 4 additions & 4 deletions plugins/network/resources/partial/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h4>{{configuringInterface.name}}</h4>
<label>Gateway</label>
<input ng:model="configuringInterface.gateway" type="text" class="form-control" />
</div>
<div class="form-group">
<div class="form-group" ng:if="configuringInterface.metric !== undefined">
<label>Metric</label>
<input ng:model="configuringInterface.metric" type="number" class="form-control" />
</div>
Expand All @@ -120,7 +120,7 @@ <h4>{{configuringInterface.name}}</h4>
<div ng:show="configuringInterface.addressing == 'dhcp'">
<div class="form-group">
<label>Client ID</label>
<input ng:model="configuringInterface.client" type="text" class="form-control" />
<input ng:model="configuringInterface.dhcpClient" type="text" class="form-control" />
</div>
</div>

Expand All @@ -140,14 +140,14 @@ <h4>{{configuringInterface.name}}</h4>
<label>MAC</label>
<input ng:model="configuringInterface.hwaddress" type="text" class="form-control" />
</div>
<div class="form-group">
<div class="form-group" ng:if="configuringInterface.mtu !== undefined">
<label>MTU</label>
<input ng:model="configuringInterface.mtu" type="number" class="form-control" />
</div>

<hr />

<div>
<div ng:if="configuringInterface.up_script !== undefined">
<div class="form-group">
<label>Pre-up script</label>
<input ng:model="configuringInterface.pre_up_script" type="text" class="form-control" />
Expand Down

0 comments on commit a6fe318

Please sign in to comment.