def init_primary(host_ip, resource): """ when set primary for the first time, you must exec this function, other than set_primary if exec this function failed, it may because drbd wait the counterpart resource, you can exec function continue_resource to fix it, and then run init_primary again :param host_ip: :param resource: :return: """ _args = 'drbdadm -- --overwrite-data-of-peer primary ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result print json.dumps({hostname: inf}, indent=4) if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: %s, init the %s primary successfully" % (host_ip, resource)
def set_primary(host_ip, resource): """ 1.if set_primary for the first time, please use function init_primary 2.if the counterpart vm has been set primary before, exec this function would fail :param host_ip: :param resource: :return: """ _args = 'drbdadm primary ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: %s, set the %s primary successfully" % (host_ip, resource)
def mount(host_ip, src, path, partition=1): """ :param host_ip:string :param src: string, disk device name without partition number :param path: string :param partition: default partition 1 :return: """ fstype = get_filesystem_type(host_ip, src) # _src = src + str(partition) action = dict(module='mount', src=_src, path=path,fstype=fstype, state='mounted') res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: %s, %s is mounted on %s" % (host_ip, src, path)
def get_remote_port(host_ip): """ the remote port from 8000 to 8100 by default :param host_ip: :return: """ _args = 'python /etc/hty/get_port.py' action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: port = inf.get('stdout', None) try: _port = int(port) if _port > 0: return _port else: return "Failed: %s, no port available" except ValueError, e: return e
def create_partition(host_ip, device): """ create a partition default: one disk create one partition and partition number will be 1. please, do not create more than one partition :param host_ip:string :param device:string :return: """ action = dict(module='parted', device=device, number=1, state='present') res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr elif inf.get('changed', 0) == 'true': return "Success: %s, create %s1 successful" % (host_ip, device) else: return "Failed: %s, %s1 already exist " % (host_ip, device)
def send_file(host_ip, src, dest): action = dict(module='copy', src=src, dest=dest) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: %s, send %s to %s" % (host_ip, src, dest)
def get_remote_hostname(host_ip): _args = 'hostname' action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return str(inf.get('stdout', None))
def init_config(host_ip): _args = 'python /etc/hty/init_config.py' action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: %s, init the configuration successful" % host_ip
def unzip_image(host_ip, local_zip_file, local_unzip_file): _args ='python /etc/hty/unzip_image.py ' + str(local_zip_file) + str(local_unzip_file) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr = stderr + str(inf.get('stdout', None)).strip() return stderr else: return "Success"
def add_config(host_ip, resource): _args = 'python /etc/hty/add_config.py ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: %s, add %s to file: drbd.conf successful " % ( host_ip, resource)
def oss_download(host_ip, access_key_id, access_secret, end_point, bucket, remote_zip_file, local_zip_file): _args ='python /etc/hty/oss_download.py '+str(access_key_id) + ' ' + str(access_secret) + ' ' + \ str(end_point) + ' ' + str(bucket) + ' ' + str(remote_zip_file) + ' ' + str(local_zip_file) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr = stderr + str(inf.get('stdout', None)).strip() return stderr else: return "Success"
def wirte_resource(host_ip, resource, hty_name, hty_disk_device, hty_ip, hty_port, pub_name, pub_disk_device, pub_ip, pub_port, hty_partition=1, pub_partition=1): """ :param host_ip: :param resource: :param hty_name: :param hty_disk_device: :param hty_ip: :param hty_port: :param pub_name: :param pub_disk_device: :param pub_ip: :param pub_port: :return: """ res_args = str(resource) + ' ' + str(hty_name) + ' ' + str(hty_disk_device) +str(hty_partition) \ + ' ' + str(hty_ip) + ' ' + str(hty_port) + ' ' + str(pub_name) \ + ' ' + str(pub_disk_device) + str(pub_partition) + ' ' + str(pub_ip) + ' ' + str(pub_port) _args = 'python /etc/hty/write_res.py ' + res_args action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: %s, write the resource configuration successful" % host_ip
def send_file(host_ip, src, dest): action = dict(module='copy', src=src, dest=dest) res = order_run(host_ip, action) for hostname, result in res.host.items(): inf = result._result #delete print json.dumps({hostname: inf}, indent=4) #there is no specific return item like 'success', therefore I choose the item 'size' # to judge whether action is successful or not if inf.get('size', 0): #delete print "success" return True elif inf.get('unreachable', 0): print "failed, unreachable" return False else: #delete print "failed" return False
def get_filesystem_type(host_ip, device): """ get file system tpye of partition 1 :param host_ip: string :param device: string :return: """ action = dict(module='parted', device=device, state='info') res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return inf['partitions'][0]['fstype']
def stop_drbd(host_ip): """ :param host_ip: :return: """ action = dict(module='service', name='drbd', state='stopped') res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: print "Success: %s, service drbd stopped" % host_ip
def fetch_file(host_ip, src, dest): action = dict(module='fetch', src=src, dest=dest, fail_on_missing='yes', flat='yes') res = order_run(host_ip, action) local_host = socket.gethostname() if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: fetch %s:%s to %s:%s" % (host_ip, src, local_host, dest)
def get_resources_state(host_ip): """ 1. you can exec this function every ten second to get an real-time monitor :param host_ip: :return: """ _args = 'drbd-overview ' action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return inf.get('stdout_lines', None)
def set_secondary(host_ip, resource): """ :param host_ip: :param resource: :return: """ _args = 'drbdadm secondary ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: %s, set the %s secondary successfully" % (host_ip, resource)
def continue_resource(host_ip, resource): """ :param host_ip:string :param resource: string :return: """ _args = 'drbdadm up ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: the data sync of %s in %s is continuing now" % (resource, host_ip)
def init_metadata(host_ip, resource): """ init drbd metadata when the disk device has filesystem before :param host_ip: string :param resource: string :return: """ _args = '/etc/hty/init_metadata.sh ' + str(resource) action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s no found' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip for fail_inf in inf['stdout_lines']: stderr = stderr + fail_inf return stderr else: return "Success: %s, metadata of %s create successful" % (host_ip, resource)
def umount(host_ip, src, path, partition=1): """ :param host_ip:string :param src: string :param path: string :param partition: partition number, default 1 :return: """ _src = src + str(partition) action = dict(module='mount', path=path, state='unmounted') res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr += str(inf.get('msg', '')).strip() return stderr else: return "Success: %s, %s is unmounted from %s" % (host_ip, _src, path)
def extend_partition(host_ip, device, size, partition=1): """ extend a partition for writing drbd metadata :param host_ip:string :param device: string, like '/dev/vdb' :param size: int, :param partition:default 1 :return: string, judge the action whether success or not, by detecting 'Failed' or 'Success' in return string """ _args = 'parted ' + str(device) + ' resizepart ' + str(partition) + ' ' + str(size) + 'GB' action = dict(module='command', args=_args) res = order_run(host_ip, action) if not any(res.host): return 'Failed: %s is not founded' % host_ip for hostname, result in res.host.items(): inf = result._result if inf.get('unreachable', 0): return "Failed: %s is unreachable" % host_ip elif inf.get('failed', 0): stderr = "Failed: %s, " % host_ip stderr = stderr + str(inf.get('stdout', None)).strip() return stderr else: return "Success: %s, %s%s has been extended to %dGB" % (host_ip, device, partition, size)