celery -A proj report
์ ์ถ๋ ฅ์ ํฌํจํ์ต๋๋ค.master
๋ถ๊ธฐ์ ๋ฌธ์ ๊ฐ ์์์ ํ์ธํ์ต๋๋ค.1 .deploy_tomcat2.py
`.AnsibleApi์์ CallApi ๊ฐ์ ธ์ค๊ธฐ
def django_process(jira_num):
์๋ฒ = '10.10.51.30'
์ด๋ฆ = 'abc'
ํฌํธ = 11011
์ฝ๋ = 'efs'
jdk = '1.12.13'
jvm = 'xxxx'
if str.isdigit(jira_num):
# import pdb
# pdb.set_trace()
call = CallApi(server,name,port,code,jdk,jvm)
return call.run_task()
`
๊ฐ์ ธ์ค๊ธฐ ๋ก๊น
.Logger์์ ๊ฐ์ ธ์ค๊ธฐ ๋ก๊ฑฐ
django.conf ๊ฐ์ ธ์ค๊ธฐ ์ค์ ์์
์ปฌ๋ ์
์์ ๋ช
๋ช
๋ ํํ ๊ฐ์ ธ์ค๊ธฐ
fromsible.parsing.dataloader ๊ฐ์ ธ์ค๊ธฐ DataLoader
VariableManager ๊ฐ์ ธ์ค๊ธฐ
fromsible.inventory ๊ฐ์ ธ์ค๊ธฐ ์ธ๋ฒคํ ๋ฆฌ
fromsible.playbook.play ๊ฐ์ ธ์ค๊ธฐ ์ฌ์
fromsible.executor.task_queue_manager import TaskQueueManager
fromsible.plugins.callback import CallbackBase
๋ก๊ทธ = ๋ก๊ฑฐ('/tmp/auto_deploy_tomcat.log',logging.INFO)
ํด๋์ค ๊ฒฐ๊ณผ์ฝ๋ฐฑ(์ฝ๋ฐฑ๋ฒ ์ด์ค):
def __init__(self, args, * kwargs):
super(ResultCallback, self).__init__( args, * kwargs)
self.host_ok = {}
self.host_unreachable = {}
self.host_failed = {}
def v2_runner_on_unreachable(self, result):
self.host_unreachable[result._host.get_name()] = result
def v2_runner_on_ok(self, result, *args, **kwargs):
self.host_ok[result._host.get_name()] = result
def v2_runner_on_failed(self, result, *args, **kwargs):
self.host_failed[result._host.get_name()] = result
ํด๋์ค CallApi(๊ฐ์ฒด):
์ฌ์ฉ์ = ์ค์ .SSH_USER
ssh_private_key_file = ์ค์ .SSH_PRIVATE_KEY_FILE
๊ฒฐ๊ณผ_์ฝ๋ฐฑ = ๊ฒฐ๊ณผ์ฝ๋ฐฑ()
์ต์
= namedtuple('์ต์
',
['์ฐ๊ฒฐ', '๋ชจ๋_๊ฒฝ๋ก', '๊ฐ์ธํค_ํ์ผ', 'ํฌํฌ', '๋๊ธฐ', '๋น์ปด_๋ฉ์๋',
'์ฌ์ฉ์ ๋๊ธฐ', 'ํ์ธ'])
def __init__(self,ip,name,port,code,jdk,jvm):
self.ip = ip
self.name = name
self.port = port
self.code = code
self.jdk = jdk
self.jvm = jvm
self.results_callback = ResultCallback()
self.results_raw = {}
def _gen_user_task(self):
tasks = []
deploy_script = 'autodeploy/abc.sh'
dst_script = '/tmp/abc.sh'
cargs = dict(src=deploy_script, dest=dst_script, owner=self.user, group=self.user, mode='0755')
args = "%s %s %d %s %s '%s'" % (dst_script, self.name, self.port, self.code, self.jdk, self.jvm)
tasks.append(dict(action=dict(module='copy', args=cargs),register='shell_out'))
tasks.append(dict(action=dict(module='debug', args=dict(msg='{{shell_out}}'))))
# tasks.append(dict(action=dict(module='command', args=args)))
# tasks.append(dict(action=dict(module='command', args=args), register='result'))
# tasks.append(dict(action=dict(module='debug', args=dict(msg='{{result.stdout}}'))))
self.tasks = tasks
def _set_option(self):
self._gen_user_task()
self.variable_manager = VariableManager()
self.loader = DataLoader()
self.options = self.Options(connection='smart', module_path=None, private_key_file=self.ssh_private_key_file, forks=None,
become=True, become_method='sudo', become_user='root', check=False)
self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[self.ip])
self.variable_manager.set_inventory(self.inventory)
play_source = dict(
name = "auto deploy tomcat",
hosts = self.ip,
remote_user = self.user,
gather_facts='no',
tasks = self.tasks
)
self.play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
def run_task(self):
self.results_raw = {'success':{}, 'failed':{}, 'unreachable':{}}
tqm = None
from celery.contrib import rdb
rdb.set_trace()
self._set_option()
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=None,
stdout_callback=self.results_callback,
)
result = tqm.run(self.play)
finally:
if tqm is not None:
tqm.cleanup()
for host, result in self.results_callback.host_ok.items():
self.results_raw['success'][host] = result._result
for host, result in self.results_callback.host_failed.items():
self.results_raw['failed'][host] = result._result
for host, result in self.results_callback.host_unreachable.items():
self.results_raw['unreachable'][host]= result._result
Log.info("result is :%s" % self.results_raw)
return self.results_raw
3.tasks.py
from __future__ import absolute_import, unicode_literals
์
๋ฌ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ shared_task์์
.deploy_tomcat2์์ django_process ๊ฐ์ ธ์ค๊ธฐ
@shared_task
def ๋ฐฐํฌ(์ซ์):
#return 'hello world {0}'.format(num)
#rdb.set_trace()
return django_process(num)`
django console :python manage.py shell ์์ deploy('123') , ์ฝ๋ ํธ์ถ ansibleApi๋ฅผ ์ฌ์ฉํ์ฌ abc.sh๋ฅผ dst ์๋ฒ์ ์ฑ๊ณต์ ์ผ๋ก ๋ณต์ฌํ ์ ์์ต๋๋ค.
ํ์ง๋ง 'deploy.apply_async(args=['1334'], queue='queue.ops.deploy', routing_key='ops.deploy')'๋ฅผ ์ฌ์ฉํ ๋ ์๋ํ์ง ์์์ต๋๋ค.
๋ฉฐ์น ๊ฒ์ํ๋๋ฐ ์์ง๋ ์๋๋ค์.
AnsibleApi๋ ๋น ์ฌ์ ์ ๋ฐํํฉ๋๋ค:{'์ฑ๊ณต': {}, '์คํจ': {}, '์ฐ๊ฒฐํ ์ ์์': {}}
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ฃผ์ฅ ๋นํ์ฑํ:
1.์ฌ๊ธฐ์ ์
๋ฌ๋ฆฌ๋ ๋ด๋ณด๋ด๊ธฐ PYTHONOPTIMIZE=1๋ก ์ค์ ํ๊ฑฐ๋ ์ด ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ฌ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค. -O OPTIMIZATION
2. python ํจํท ๋ค์ค ์ฒ๋ฆฌ process.py ๋ผ์ธ 102 ๋นํ์ฑํ:
assert not _current_process._config.get('daemon'), \
'daemonic processes are not allowed to have children'
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ฃผ์ฅ ๋นํ์ฑํ:
1.์ฌ๊ธฐ์ ์ ๋ฌ๋ฆฌ๋ ๋ด๋ณด๋ด๊ธฐ PYTHONOPTIMIZE=1๋ก ์ค์ ํ๊ฑฐ๋ ์ด ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฌ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค. -O OPTIMIZATION
2. python ํจํท ๋ค์ค ์ฒ๋ฆฌ process.py ๋ผ์ธ 102 ๋นํ์ฑํ:
assert not _current_process._config.get('daemon'), \ '๋ฐ๋ชฌ ํ๋ก์ธ์ค๋ ์์์ ๊ฐ์ง ์ ์์ต๋๋ค'
@Xuexiang825 ์ฃ์กํฉ๋๋ค. ์๋ํ์ง ์์ต๋๋ค. ์ ๋ Python 3.6.3๊ณผ ansible 2.4.2.0, celery 4.1.0์ ์ฌ์ฉํฉ๋๋ค. ๋ด ํ๋ ์ด๋ถ ์คํ๊ธฐ๋ ๋ฉ์์ง๋ฅผ ํ์ํ์ง๋ง ์๋ฌด ์์ ๋ ์ํํ์ง ์์ต๋๋ค.๋ํ -O OPTIMIZATION ๋งค๊ฐ๋ณ์์ ๋ํด ์๋ ค์ฃผ์ค ์ ์์ต๋๊น?
@Xuexiang825 ๋ฐฉ๊ธ ์ด ๊ธฐ์ฌ ์์ ๊ทํ์
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ฃผ์ฅ ๋นํ์ฑํ:
1.์ฌ๊ธฐ์ ์ ๋ฌ๋ฆฌ๋ ๋ด๋ณด๋ด๊ธฐ PYTHONOPTIMIZE=1๋ก ์ค์ ํ๊ฑฐ๋ ์ด ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฌ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค. -O OPTIMIZATION
2. python ํจํท ๋ค์ค ์ฒ๋ฆฌ process.py ๋ผ์ธ 102 ๋นํ์ฑํ:
assert not _current_process._config.get('daemon'), \ 'daemonic processes are not allowed to have children'