本文整理汇总了Python中logger.MyLogger.writelog方法的典型用法代码示例。如果您正苦于以下问题:Python MyLogger.writelog方法的具体用法?Python MyLogger.writelog怎么用?Python MyLogger.writelog使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logger.MyLogger
的用法示例。
在下文中一共展示了MyLogger.writelog方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Container
# 需要导入模块: from logger import MyLogger [as 别名]
# 或者: from logger.MyLogger import writelog [as 别名]
class Container(object):
"""容器类,用于创建,销毁,停止,暂停,恢复容器等操作"""
def __init__(self):
#initial base config
self.cli = Client(base_url=DOCKER_BASE_URL, version='auto')
self.logger = MyLogger()
self.db = Database()
def generate_string(self, size=8):
"""生成指定长度的随机字符串"""
return ''.join(
random.choice(string.ascii_lowercase + string.ascii_uppercase +
string.digits) for _ in range(size))
def create_container(self, tid, mem_limit, hostname, image_name):
""" create a container and update status """
#create random mysql_password
mysql_password = self.generate_string(8)
self.logger.writelog('create mysql password %s' % mysql_password)
self.logger.writelog(
"create container with image name %s and hostname %s " %
(image_name, hostname))
try:
c = self.cli.create_container(image=image_name,environment={'MYSQL_PASSWORD':mysql_password},\
name=hostname,hostname=hostname, detach=False,tty=False,ports=[22,3306,80],\
host_config = self.cli.create_host_config(publish_all_ports=True,mem_limit = mem_limit))
docker_id = c.get('Id')
self.logger.writelog('container id: %s' % docker_id)
self.start_container(docker_id, tid)
#get IP address
ip = self.cli.inspect_container(
docker_id)['NetworkSettings']['IPAddress']
#get port mapping
ssh_port = self.cli.port(docker_id, '22')[0]['HostPort']
db_port = self.cli.port(docker_id, '3306')[0]['HostPort']
web_port = self.cli.port(docker_id, '80')[0]['HostPort']
sql = """insert into instances(tid,did,ip,hostname,dbport,webport,sshport,dbpwd) values('%s','%s','%s','%s','%s','%s','%s','%s');""" % (
tid, docker_id, ip, hostname, db_port, web_port, ssh_port,
mysql_password)
self.logger.writelog("insert record in instances table")
self.db.execute(sql)
self.logger.writelog("update task status:%d" % ST_RUNNING)
self.db.update_status(tid, ST_RUNNING)
return docker_id
except Exception, err:
#create failed
self.logger.writelog('create failed: %s ' % err, self.logger.error)
self.db.update_status(tid, ST_UNKNOWN)
return False