watch.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:OperatingSystemLab 作者: wzc1995 项目源码 文件源码
def edit_hosts():
    f = os.popen('/usr/local/bin/etcdctl ls --sort --recursive /hosts')
    hosts_str = f.read()


    hosts_arr = hosts_str.strip('\n').split('\n')
    hosts_fd = open('/tmp/hosts', 'w')

    fcntl.flock(hosts_fd.fileno(), fcntl.LOCK_EX)

    hosts_fd.write('127.0.0.1 localhost cluster' + '\n')
    i = 0
    for host_ip in hosts_arr:
        host_ip = host_ip[host_ip.rfind('/') + 1:]
        if host_ip[0] == '0':
            hosts_fd.write(host_ip[1:] + ' cluster-' + str(i) + '\n')
        else:
            hosts_fd.write(host_ip + ' cluster-' + str(i) + '\n')
        i += 1

    hosts_fd.flush()
    os.system('/bin/cp /tmp/hosts /etc/hosts')
    hosts_fd.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号