2012-09-27 03:50:54 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2012-09-30 12:50:32 +00:00
DOCUMENTATION = '''
---
module: fireball
2012-10-01 12:29:46 +00:00
short_description: Enable fireball mode on remote node
2012-09-30 12:50:32 +00:00
description:
2012-10-02 02:41:00 +00:00
- This modules launches an ephemeral I(fireball) ZeroMQ message bus daemon on the remote node which
2012-11-21 17:49:30 +00:00
Ansible can use to communicate with nodes at high speed.
2012-10-02 02:41:00 +00:00
- The daemon listens on a configurable port for a configurable amount of time.
- Starting a new fireball as a given user terminates any existing user fireballs.
- Fireball mode is AES encrypted
2012-09-30 12:50:32 +00:00
version_added: "0.9"
options:
port:
description:
- TCP port for ZeroMQ
required: false
default: 5099
aliases: []
minutes:
description:
2012-10-01 12:29:46 +00:00
- The I(fireball) listener daemon is started on nodes and will stay around for
2012-10-02 02:41:00 +00:00
this number of minutes before turning itself off.
2012-09-30 12:50:32 +00:00
required: false
default: 30
2012-10-01 12:29:46 +00:00
# WARNING: very careful when moving space around, below
2012-09-30 12:50:32 +00:00
examples:
2012-10-01 12:29:46 +00:00
- code: |
- hosts: devservers
gather_facts: false
connection: ssh
sudo: yes
tasks:
2012-11-21 17:49:30 +00:00
- action: fireball
2012-10-18 11:49:19 +00:00
- hosts: devservers
2012-10-01 12:29:46 +00:00
connection: fireball
tasks:
2012-10-23 13:14:01 +00:00
- command: /usr/bin/anything
2012-10-02 02:41:00 +00:00
description: "This example playbook has two plays: the first launches I(fireball) mode on all hosts via SSH, and the second actually starts using I(fireball) node for subsequent management over the fireball interface"
2012-09-30 12:50:32 +00:00
notes:
2012-10-02 02:41:00 +00:00
- See the advanced playbooks chapter for more about using fireball mode.
2012-09-30 12:50:32 +00:00
requirements: [ "zmq", "keyczar" ]
author: Michael DeHaan
'''
2012-09-27 03:50:54 +00:00
import os
import sys
import shutil
import time
import base64
import syslog
import signal
2012-10-02 02:41:00 +00:00
import time
import signal
2013-03-01 23:24:17 +00:00
import traceback
2012-09-27 03:50:54 +00:00
syslog.openlog('ansible-%s' % os.path.basename(__file__))
PIDFILE = os.path.expanduser("~/.fireball.pid")
def log(msg):
2012-10-31 00:42:07 +00:00
syslog.syslog(syslog.LOG_NOTICE, msg)
2012-09-27 03:50:54 +00:00
if os.path.exists(PIDFILE):
try:
data = int(open(PIDFILE).read())
try:
os.kill(data, signal.SIGKILL)
except OSError:
pass
except ValueError:
pass
os.unlink(PIDFILE)
HAS_ZMQ = False
try:
2012-10-31 00:42:07 +00:00
import zmq
HAS_ZMQ = True
2012-09-27 03:50:54 +00:00
except ImportError:
2012-10-31 00:42:07 +00:00
pass
2012-09-27 03:50:54 +00:00
HAS_KEYCZAR = False
try:
2012-10-31 00:42:07 +00:00
from keyczar.keys import AesKey
HAS_KEYCZAR = True
2012-09-27 03:50:54 +00:00
except ImportError:
2012-10-31 00:42:07 +00:00
pass
2012-09-27 03:50:54 +00:00
# NOTE: this shares a fair amount of code in common with async_wrapper, if async_wrapper were a new module we could move
# this into utils.module_common and probably should anyway
def daemonize_self(module, password, port, minutes):
# daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
try:
pid = os.fork()
if pid > 0:
log("exiting pid %s" % pid)
# exit first parent
2013-04-04 12:31:26 +00:00
module.exit_json(msg="daemonized fireball on port %s for %s minutes" % (port, minutes))
2012-09-27 03:50:54 +00:00
except OSError, e:
log("fork #1 failed: %d (%s)" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(022)
# do second fork
try:
pid = os.fork()
if pid > 0:
log("daemon pid %s, writing %s" % (pid, PIDFILE))
pid_file = open(PIDFILE, "w")
pid_file.write("%s" % pid)
pid_file.close()
log("pidfile written")
sys.exit(0)
except OSError, e:
log("fork #2 failed: %d (%s)" % (e.errno, e.strerror))
sys.exit(1)
dev_null = file('/dev/null','rw')
os.dup2(dev_null.fileno(), sys.stdin.fileno())
os.dup2(dev_null.fileno(), sys.stdout.fileno())
os.dup2(dev_null.fileno(), sys.stderr.fileno())
log("daemonizing successful (%s,%s)" % (password, port))
2013-01-28 11:40:18 +00:00
def command(module, data):
2012-09-27 03:50:54 +00:00
if 'cmd' not in data:
return dict(failed=True, msg='internal error: cmd is required')
if 'tmp_path' not in data:
return dict(failed=True, msg='internal error: tmp_path is required')
2012-12-23 18:17:07 +00:00
if 'executable' not in data:
return dict(failed=True, msg='internal error: executable is required')
2012-09-27 03:50:54 +00:00
log("executing: %s" % data['cmd'])
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
rc, stdout, stderr = module.run_command(data['cmd'], executable=data['executable'], close_fds=True)
2012-09-27 03:50:54 +00:00
if stdout is None:
stdout = ''
if stderr is None:
stderr = ''
log("got stdout: %s" % stdout)
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
return dict(rc=rc, stdout=stdout, stderr=stderr)
2012-11-21 17:49:30 +00:00
2012-09-27 03:50:54 +00:00
def fetch(data):
if 'in_path' not in data:
2012-11-14 23:37:17 +00:00
return dict(failed=True, msg='internal error: in_path is required')
# FIXME: should probably support chunked file transfer for binary files
# at some point. For now, just base64 encodes the file
# so don't use it to move ISOs, use rsync.
2012-09-27 03:50:54 +00:00
fh = open(data['in_path'])
2013-02-18 00:48:02 +00:00
data = base64.b64encode(fh.read())
2012-09-27 03:50:54 +00:00
return dict(data=data)
def put(data):
if 'data' not in data:
return dict(failed=True, msg='internal error: data is required')
if 'out_path' not in data:
return dict(failed=True, msg='internal error: out_path is required')
2012-11-21 17:49:30 +00:00
2012-11-14 23:37:17 +00:00
# FIXME: should probably support chunked file transfer for binary files
# at some point. For now, just base64 encodes the file
# so don't use it to move ISOs, use rsync.
2012-09-27 03:50:54 +00:00
fh = open(data['out_path'], 'w')
2012-11-14 23:37:17 +00:00
fh.write(base64.b64decode(data['data']))
2012-09-27 03:50:54 +00:00
fh.close()
return dict()
def serve(module, password, port, minutes):
2012-10-02 02:41:00 +00:00
2012-09-27 03:50:54 +00:00
log("serving")
context = zmq.Context()
socket = context.socket(zmq.REP)
addr = "tcp://*:%s" % port
log("zmq serving on %s" % addr)
socket.bind(addr)
# password isn't so much a password but a serialized AesKey object that we xferred over SSH
# password as a variable in ansible is never logged though, so it serves well
key = AesKey.Read(password)
2012-11-21 17:49:30 +00:00
2012-09-27 03:50:54 +00:00
while True:
data = socket.recv()
2012-10-02 02:41:00 +00:00
try:
data = key.Decrypt(data)
except:
continue
2012-09-27 03:50:54 +00:00
data = json.loads(data)
mode = data['mode']
response = {}
if mode == 'command':
2013-01-28 11:40:18 +00:00
response = command(module, data)
2012-09-27 03:50:54 +00:00
elif mode == 'put':
response = put(data)
elif mode == 'fetch':
response = fetch(data)
data2 = json.dumps(response)
data2 = key.Encrypt(data2)
socket.send(data2)
def daemonize(module, password, port, minutes):
try:
daemonize_self(module, password, port, minutes)
2012-10-02 02:41:00 +00:00
def catcher(signum, _):
module.exit_json(msg='timer expired')
signal.signal(signal.SIGALRM, catcher)
signal.setitimer(signal.ITIMER_REAL, 60 * minutes)
2012-09-27 03:50:54 +00:00
serve(module, password, port, minutes)
except Exception, e:
2013-03-01 23:24:17 +00:00
tb = traceback.format_exc()
log("exception caught, exiting fireball mode: %s\n%s" % (e, tb))
2012-09-27 03:50:54 +00:00
sys.exit(0)
def main():
module = AnsibleModule(
argument_spec = dict(
port=dict(required=False, default=5099),
password=dict(required=True),
minutes=dict(required=False, default=30),
)
)
password = base64.b64decode(module.params['password'])
port = module.params['port']
2012-10-02 02:41:00 +00:00
minutes = int(module.params['minutes'])
2012-09-27 03:50:54 +00:00
if not HAS_ZMQ:
module.fail_json(msg="zmq is not installed")
if not HAS_KEYCZAR:
2012-11-21 17:49:30 +00:00
module.fail_json(msg="keyczar is not installed")
2012-09-27 03:50:54 +00:00
daemonize(module, password, port, minutes)
2012-11-21 17:49:30 +00:00
2012-09-27 03:50:54 +00:00
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()