Enable logging

This commit is contained in:
Ilya Shakhat 2016-08-12 15:58:12 +03:00
parent 12c3b6201f
commit a84b555d85
4 changed files with 63 additions and 19 deletions

View File

@ -10,10 +10,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os_failures
def main():
# cloud config schema is an extension to os-client-config
cloud_config = {
'auth': {
'username': 'admin',
@ -25,7 +28,6 @@ def main():
'driver': 'fuel',
'address': '172.18.171.149',
'username': 'root',
'password': 'r00tme',
},
'power_management': {
'driver': 'kvm',
@ -33,28 +35,56 @@ def main():
'username': 'root',
}
}
client = os_failures.build_client(cloud_config)
client.verify()
service = client.get_service(name='keystone-api')
print(service)
# connect
distractor = os_failures.connect(cloud_config)
# verify connection to the cloud
distractor.verify()
# os_failures library operate with 2 types of instances:
# service - is software that runs in the cloud, e.g. keystone
# nodes - nodes that host the cloud, e.g. hardware server with hostname
# get a particular service in the cloud
service = distractor.get_service(name='keystone-api')
logging.info('Keystone API Service: %s', service)
# restart the service
service.restart()
# use case #2: get nodes where the service runs
nodes = service.get_nodes()
print(nodes)
logging.info('Nodes: %s', nodes)
# reboot these nodes
nodes.reboot()
# pick one one out of collection of nodes
one = nodes.pick()
# switch the node off
one.poweroff()
nodes = client.get_nodes()
print(nodes)
nodes.oom()
# get all nodes in the cloud
nodes = distractor.get_nodes()
logging.info('All cloud nodes: %s', nodes)
nodes = client.get_nodes(fqdns=['node-1.domain.tld'])
print(nodes)
nodes.reboot()
# reset all these nodes
nodes.reset()
# get nodes by their FQDNs
nodes = distractor.get_nodes(fqdns=['node-1.domain.tld'])
logging.info('Node with specific FQDN: %s', nodes)
# disable public network on these nodes
nodes.disable_network(network_name='public')
# enable public network on these nodes
nodes.enable_network(network_name='public')
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.INFO)
main()

View File

@ -19,7 +19,7 @@ __version__ = pbr.version.VersionInfo(
'os_failures').version_string()
def build_client(cloud_config):
def connect(cloud_config):
cloud_management = None
cloud_management_params = cloud_config.get('cloud_management') or {}

View File

@ -13,6 +13,7 @@
import abc
import json
import logging
import random
import six
@ -46,11 +47,20 @@ class FuelNodeCollection(node_collection.NodeCollection):
self.cloud_management.execute_on_cloud(ips, task)
def oom(self):
print('OOM!')
logging.info('Enforce nodes to run out of memory: %s', self)
def poweroff(self):
self.power_management.poweroff([n['mac'] for n in self.hosts])
def reset(self):
logging.info('Reset nodes: %s', self)
def enable_network(self, network_name):
logging.info('Enable network: %s on nodes: %s', network_name, self)
def disable_network(self, network_name):
logging.info('Disable network: %s on nodes: %s', network_name, self)
@six.add_metaclass(abc.ABCMeta)
class FuelService(service.Service):
@ -82,7 +92,8 @@ class KeystoneService(FuelService):
'command': 'service apache2 restart'
}
ips = self.get_cloud_nodes_ips(role='controller')
print(self.cloud_management.execute_on_cloud(ips, task))
exec_res = self.cloud_management.execute_on_cloud(ips, task)
logging.info('Restart the service, result: %s', exec_res)
SERVICE_NAME_TO_CLASS = {
@ -96,7 +107,6 @@ class FuelManagement(cloud_management.CloudManagement):
self.master_node_address = params['address']
self.username = params['username']
self.password = params['password']
self.master_node_executor = executor.AnsibleRunner(
remote_user=self.username)
@ -111,11 +121,13 @@ class FuelManagement(cloud_management.CloudManagement):
def verify(self):
hosts = self.get_cloud_hosts()
print(hosts)
logging.debug('Cloud hosts: %s', hosts)
task = {'command': 'hostname'}
host_addrs = [n['ip'] for n in hosts]
print(self.execute_on_cloud(host_addrs, task))
logging.debug('Cloud nodes hostnames: %s', self.execute_on_cloud(host_addrs, task))
logging.info('Connected to cloud successfully')
def get_cloud_hosts(self):
if not self.cached_cloud_hosts:

View File

@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from os_failures.ansible import executor
from os_failures.api import power_management
@ -24,4 +26,4 @@ class KVM(power_management.PowerManagement):
self.executor = executor.AnsibleRunner(remote_user=self.username)
def poweroff(self, hosts):
print('Power off hosts %s' % hosts)
logging.info('Power off hosts: %s', hosts)