#!/usr/bin/env python
# -*- coding: utf-8 -*-

from common import (
    set_resource_limit,
    link_essential_cmd,
    set_tcp_fast_recycling,
    add_easyops_user,
    make_basic_dir,
    write_hostname,
    exec_cmd,
    init_logger,
)

logger = init_logger("init_env")


def init(args):
    hosts = args.hosts

    hosts = [item.strip() for item in hosts.split(',')]
    ip2host = {}
    for host in hosts:
        ip, host = host.split(':')
        ip2host[ip.strip()] = host.strip()

    # write hosts
    write_hostname(ip2host)

    # open tcp reuse and fast recycle
    set_tcp_fast_recycling()

    # link_essential_cmd
    link_essential_cmd()

    # add easyops user
    add_easyops_user()

    # ulimit resource
    ok = set_resource_limit()
    if not ok:
        raise Exception('failed to set resource limit')

    # tmp 777
    exec_cmd(['chmod 777 /tmp'])
    make_basic_dir()
