/
fix
executable file
·134 lines (124 loc) · 4.45 KB
/
fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
from __future__ import print_function
from optparse import OptionParser
import sys
import os
# Messages
NO_ORDER = "No order given. Type 'fix -l' for a list of orders\n"
VERSION = 'LittleChef {0}'
INSTALL_ERROR = "LittleChef was not correctly installed: "\
"Couldn't import littlechef.py"
## Try to import package and set the fabfile path ##
fabfile = None
try:
import littlechef
# Get absolute directory for imported littlechef package
dirname = os.path.dirname(os.path.abspath(littlechef.__file__))
# Build path to the runner fabfile to pass to fabric
fabfile = os.path.join(dirname, 'runner.py')
except ImportError:
print(INSTALL_ERROR)
sys.exit(1)
class LittlechefOptionParser(OptionParser):
def print_help(self, file=None):
from fabric.main import list_commands, state, load_fabfile
OptionParser.print_help(self, file)
docstring, callables, default = load_fabfile(fabfile)
state.commands.update(callables)
for c in list_commands("\n", "normal"):
print(c)
sys.exit(0)
# commandline options
parser = LittlechefOptionParser()
parser.add_option(
"-v", "--version", dest="version", action="store_true", default=False,
help="Print littlechef version"
)
parser.add_option(
"-l", dest="list_commands", action="store_true", default=False,
help="List all available orders"
)
parser.add_option(
"--no-report", dest="no_report", action="store_true", default=False,
help="Don't save the chef-solo output as a report on the node"
)
parser.add_option(
"--why-run", dest="whyrun", action="store_true", default=False,
help="Do a configuration Whyrun, where no changes are performed to the node"
)
parser.add_option(
"--verbose", dest="verbose", action="store_true", default=False,
help="Output 'processing' statements"
)
parser.add_option(
"--debug", dest="debug", action="store_true", default=False,
help="Ask chef-solo for verbose and debugging output"
)
parser.add_option(
"--env", dest="environment", default=None,
help="Using a certain chef environment"
)
parser.add_option(
"--include-guests", dest="include_guests",
action="store_true", default=False,
help="when searching for nodes with tags also include guests"
)
(options, args) = parser.parse_args()
## Process args list and call fabric's main() ##
if not sys.argv:
print(NO_ORDER)
else:
if (os.path.basename(sys.argv[0]).startswith('fix')):
# In windows, the first argument may be just "fix"
fix_cmd = sys.argv[0]
else:
fix_cmd = None
if len(sys.argv) == 1 and fix_cmd:
# All that is in sys.argv is the fix command.
print(NO_ORDER)
else:
# Check for version, that overrides everything else.
if options.version:
print(VERSION.format(littlechef.__version__))
sys.exit(0)
if options.no_report:
littlechef.enable_logs = False
sys.argv.remove('--no-report')
if options.whyrun:
littlechef.whyrun = True
sys.argv.remove('--why-run')
if options.verbose:
littlechef.verbose = True
sys.argv.remove('--verbose')
if options.debug:
littlechef.loglevel = 'debug'
littlechef.verbose = True
sys.argv.remove('--debug')
if options.include_guests:
littlechef.include_guests = True
sys.argv.remove('--include-guests')
if options.environment is not None:
# Check for mistakes:
# * fix --env list_nodes
# * fix --env nodes_with_role:bla role:authorization
littlechef.chef_environment = options.environment
try:
# --env=ENV
sys.argv.remove("--env={0}".format(options.environment))
except ValueError:
# --env ENV
idx = sys.argv.index("--env")
sys.argv.pop(idx)
sys.argv.pop(idx)
if not len(args) or ":" in options.environment:
parser.error("No value given for --env")
# Otherwise, insert our fabfile at the correct place
if fix_cmd:
sys.argv[1:1] = ['-f', fabfile]
else:
sys.argv[0:0] = ['-f', fabfile]
# Pass control over to fabric
# Fabric should now import runner with COOKING = True
littlechef.__cooking__ = True
from fabric import main
main.main()