2011-09-07 23:01:25 +00:00
|
|
|
#!/usr/bin/env python3
|
2011-09-08 12:04:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-09-07 22:41:37 +00:00
|
|
|
#
|
|
|
|
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist 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.
|
|
|
|
#
|
|
|
|
# cdist 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 cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2011-09-08 12:11:31 +00:00
|
|
|
import argparse
|
2011-09-11 19:15:58 +00:00
|
|
|
import datetime
|
2011-09-08 12:11:31 +00:00
|
|
|
import logging
|
2011-09-11 23:14:35 +00:00
|
|
|
import multiprocessing
|
2011-09-08 00:12:48 +00:00
|
|
|
import os
|
2011-09-16 12:47:50 +00:00
|
|
|
import re
|
2011-09-08 12:11:31 +00:00
|
|
|
import subprocess
|
2011-09-08 01:56:59 +00:00
|
|
|
import shutil
|
2011-09-11 18:17:20 +00:00
|
|
|
import stat
|
2011-09-08 12:11:31 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2011-09-08 01:56:59 +00:00
|
|
|
|
2011-09-26 09:05:18 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2011-09-26 09:45:19 +00:00
|
|
|
real_me = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
cdist_lib = os.path.abspath(os.path.join(real_me, '../lib'))
|
|
|
|
|
|
|
|
sys.path.insert(0, cdist_lib)
|
2011-09-16 14:08:25 +00:00
|
|
|
|
2011-09-26 09:07:45 +00:00
|
|
|
TYPE_PREFIX = "__"
|
2011-09-26 09:25:22 +00:00
|
|
|
CODE_HEADER = "#!/bin/sh -e\n"
|
2011-09-26 09:07:45 +00:00
|
|
|
|
2011-09-16 12:47:50 +00:00
|
|
|
def install(args):
|
2011-09-23 14:38:47 +00:00
|
|
|
"""Install remote system"""
|
|
|
|
process = {}
|
2011-09-16 12:47:50 +00:00
|
|
|
|
|
|
|
def commandline():
|
2011-09-23 14:38:47 +00:00
|
|
|
"""Parse command line"""
|
|
|
|
# Construct parser others can reuse
|
|
|
|
parser = {}
|
|
|
|
# Options _all_ parsers have in common
|
|
|
|
parser['most'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['most'].add_argument('-d', '--debug',
|
|
|
|
help='Set log level to debug', action='store_true')
|
|
|
|
|
|
|
|
# Main subcommand parser
|
2011-09-26 09:05:18 +00:00
|
|
|
parser['main'] = argparse.ArgumentParser(description='cdist ' + cdist.VERSION)
|
2011-09-23 14:38:47 +00:00
|
|
|
parser['main'].add_argument('-V', '--version',
|
|
|
|
help='Show version', action='version',
|
2011-09-26 09:05:18 +00:00
|
|
|
version='%(prog)s ' + cdist.VERSION)
|
2011-09-23 14:38:47 +00:00
|
|
|
parser['sub'] = parser['main'].add_subparsers(title="Commands")
|
|
|
|
|
|
|
|
# Banner
|
|
|
|
parser['banner'] = parser['sub'].add_parser('banner',
|
|
|
|
add_help=False)
|
2011-09-26 09:05:18 +00:00
|
|
|
parser['banner'].set_defaults(func=cdist.banner.banner)
|
2011-09-23 14:38:47 +00:00
|
|
|
|
|
|
|
# Config and install (common stuff)
|
|
|
|
parser['configinstall'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['configinstall'].add_argument('host', nargs='+',
|
|
|
|
help='one or more hosts to operate on')
|
|
|
|
parser['configinstall'].add_argument('-c', '--cdist-home',
|
|
|
|
help='Change cdist home (default: .. from bin directory)',
|
|
|
|
action='store')
|
|
|
|
parser['configinstall'].add_argument('-i', '--initial-manifest',
|
|
|
|
help='Path to a cdist manifest',
|
|
|
|
dest='manifest', required=False)
|
|
|
|
parser['configinstall'].add_argument('-p', '--parallel',
|
|
|
|
help='Operate on multiple hosts in parallel',
|
|
|
|
action='store_true', dest='parallel')
|
|
|
|
parser['configinstall'].add_argument('-s', '--sequential',
|
|
|
|
help='Operate on multiple hosts sequentially (default)',
|
|
|
|
action='store_false', dest='parallel')
|
|
|
|
|
|
|
|
# Config
|
|
|
|
parser['config'] = parser['sub'].add_parser('config',
|
|
|
|
parents=[parser['most'], parser['configinstall']])
|
2011-09-26 09:45:19 +00:00
|
|
|
parser['config'].set_defaults(func=cdist.config.config)
|
2011-09-23 14:38:47 +00:00
|
|
|
|
|
|
|
# Install
|
|
|
|
parser['install'] = parser['sub'].add_parser('install',
|
|
|
|
parents=[parser['most'], parser['configinstall']])
|
|
|
|
parser['install'].set_defaults(func=install)
|
|
|
|
|
|
|
|
for p in parser:
|
|
|
|
parser[p].epilog = "Get cdist at http://www.nico.schottelius.org/software/cdist/"
|
|
|
|
|
|
|
|
args = parser['main'].parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
# Most subcommands have --debug, so handle it here
|
|
|
|
if 'debug' in args:
|
|
|
|
if args.debug:
|
|
|
|
logging.root.setLevel(logging.DEBUG)
|
|
|
|
log.debug(args)
|
|
|
|
|
|
|
|
args.func(args)
|
2011-09-19 12:38:52 +00:00
|
|
|
|
2011-09-23 18:08:55 +00:00
|
|
|
|
2011-09-19 12:38:52 +00:00
|
|
|
if __name__ == "__main__":
|
2011-09-23 14:38:47 +00:00
|
|
|
try:
|
2011-09-26 09:05:18 +00:00
|
|
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
2011-09-26 09:18:36 +00:00
|
|
|
|
2011-09-26 09:07:45 +00:00
|
|
|
if re.match(TYPE_PREFIX, os.path.basename(sys.argv[0])):
|
2011-09-26 09:18:36 +00:00
|
|
|
import cdist.emulator
|
|
|
|
cdist.emulator.emulator(sys.argv)
|
2011-09-23 14:38:47 +00:00
|
|
|
else:
|
2011-09-26 09:09:58 +00:00
|
|
|
import cdist
|
2011-09-26 09:05:18 +00:00
|
|
|
import cdist.banner
|
2011-09-26 09:45:19 +00:00
|
|
|
import cdist.config
|
2011-09-26 09:05:18 +00:00
|
|
|
import cdist.exec
|
2011-09-23 18:08:55 +00:00
|
|
|
import cdist.path
|
|
|
|
|
2011-09-23 14:38:47 +00:00
|
|
|
commandline()
|
|
|
|
except KeyboardInterrupt:
|
2011-09-23 14:50:51 +00:00
|
|
|
sys.exit(0)
|
2011-09-26 09:05:18 +00:00
|
|
|
except cdist.Error as e:
|
2011-09-23 14:50:51 +00:00
|
|
|
log.error(e)
|
|
|
|
sys.exit(1)
|