|
6 | 6 |
|
7 | 7 | import os
|
8 | 8 | import sys
|
9 |
| -import yaml |
10 | 9 |
|
11 | 10 |
|
12 | 11 | BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
@@ -36,149 +35,15 @@ def call_ansible(inventory, playbook, *args):
|
36 | 35 | os.path.join(BASE, playbook),
|
37 | 36 | '-i', inventory,
|
38 | 37 | '-v',
|
39 |
| - '--ssh-common-args=-o StrictHostKeyChecking=no' |
| 38 | + '--ssh-common-args=-o StrictHostKeyChecking=no ' + |
| 39 | + '-o UserKnownHostsFile=/dev/null' |
40 | 40 | ]
|
41 | 41 | ansible_args.extend(args)
|
42 | 42 | ansible = local['ansible-playbook']
|
43 | 43 | exit_code = command_handler(ansible, ansible_args)
|
44 | 44 | return exit_code
|
45 | 45 |
|
46 | 46 |
|
47 |
| -def call_linchpin(work_dir, arg): |
48 |
| - """ |
49 |
| - Wraps a call out to the linchpin executable, and then kicks off a cinch |
50 |
| - Ansible playbook if necessary. |
51 |
| -
|
52 |
| - :param work_dir: The linch-pin working directory that contains a PinFile |
53 |
| - and associated configuration files |
54 |
| - :param arg: A single argument to pass to the linchpin command |
55 |
| - :return: The exit code returned from linchpin, or 255 if errors come from |
56 |
| - elsewhere |
57 |
| - """ |
58 |
| - # cinch will only support a subset of linchpin subcommands |
59 |
| - supported_cmds = ['up', 'destroy', 'init'] |
60 |
| - if arg not in supported_cmds: |
61 |
| - sys.exit('linchpin command "{0}" not ' |
62 |
| - 'supported by cinch'.format(arg)) |
63 |
| - |
64 |
| - # If we are to ask linch-pin to interact with infrastructure we will check |
65 |
| - # for some required configuration items and set up them for later use |
66 |
| - if arg != 'init': |
67 |
| - inventory_file = get_inventory(work_dir) |
68 |
| - inventory_path = os.path.join(work_dir, 'inventories', inventory_file) |
69 |
| - |
70 |
| - # For destroy/teardown, we must run our teardown playbook(s) *before* |
71 |
| - # linchpin terminates the instance(s) |
72 |
| - if arg == 'destroy': |
73 |
| - exit_code = call_ansible(inventory_path, 'teardown.yml') |
74 |
| - |
75 |
| - # Construct the arguments to pass to linch-pin by munging the arguments |
76 |
| - # provided to this method |
77 |
| - linchpin_args = [ |
78 |
| - '-v', |
79 |
| - '-w', work_dir, |
80 |
| - '--creds-path', os.path.join(work_dir, 'credentials') |
81 |
| - ] |
82 |
| - linchpin_args.append(arg) |
83 |
| - # Execute the 'linchpin' command |
84 |
| - linchpin = local['linchpin'] |
85 |
| - exit_code = command_handler(linchpin, linchpin_args) |
86 |
| - |
87 |
| - # Set up a linch-pin+cinch configuration skeleton for later use if the |
88 |
| - # 'init' subcommand was executed previously |
89 |
| - if arg == 'init': |
90 |
| - cinchpin_init(work_dir) |
91 |
| - |
92 |
| - # If linchpin is asked to provision resources, we will then run our |
93 |
| - # cinch provisioning playbook |
94 |
| - if arg == 'up' and exit_code == 0: |
95 |
| - exit_code = call_ansible(inventory_path, 'site.yml') |
96 |
| - return exit_code |
97 |
| - |
98 |
| - |
99 |
| -def cinchpin_init(work_dir): |
100 |
| - """ |
101 |
| - Set up a linch-pin+cinch configuration skeleton |
102 |
| -
|
103 |
| - :param work_dir: The linch-pin working directory that contains a PinFile |
104 |
| - and associated configuration files |
105 |
| - """ |
106 |
| - # Consistent filename to use for various linch-pin YAML configurations |
107 |
| - # for 'cinchpin' |
108 |
| - config_file = 'cinch.yml' |
109 |
| - # Cinch layout and topology paths to be added to linch-pin PinFile |
110 |
| - config_setup = { |
111 |
| - 'cinch': { |
112 |
| - 'topology': config_file, |
113 |
| - 'layout': config_file |
114 |
| - } |
115 |
| - } |
116 |
| - |
117 |
| - # Ansible group_vars directory that will be created for later use |
118 |
| - group_vars = os.path.join('inventories', 'group_vars') |
119 |
| - |
120 |
| - # Dictionary of workspace directories and target filenames where we will |
121 |
| - # put our skeletons |
122 |
| - local_paths = { |
123 |
| - 'layouts': config_file, |
124 |
| - 'topologies': config_file, |
125 |
| - 'credentials': config_file, |
126 |
| - group_vars: 'all' |
127 |
| - } |
128 |
| - |
129 |
| - # Overwrite the PinFile that linch-pin created with our configuration |
130 |
| - pin_file = os.path.join(work_dir, 'PinFile') |
131 |
| - with open(pin_file, 'w') as f: |
132 |
| - yaml.dump(config_setup, f, default_flow_style=False) |
133 |
| - |
134 |
| - # Create Ansible group_vars directory since linch-pin doesn't provide this |
135 |
| - os.mkdir(os.path.join(work_dir, group_vars)) |
136 |
| - |
137 |
| - # Write out the skeletons and inform the user that they exist |
138 |
| - for directory, filename in local_paths.items(): |
139 |
| - path = os.path.join(work_dir, directory, filename) |
140 |
| - with open(path, 'w') as f: |
141 |
| - f.write(SKEL_TEXT.format(directory, DOCS)) |
142 |
| - print('Please configure this file to use cinch: ' + path) |
143 |
| - print('Example configurations: ' + DOCS) |
144 |
| - |
145 |
| - |
146 |
| -def get_inventory(work_dir): |
147 |
| - """ |
148 |
| - Basic checks for cinch compatibility in the linch-pin working directory, |
149 |
| - and if successful, we produce a topology file for cinch to use. |
150 |
| -
|
151 |
| - :param work_dir: The linch-pin working directory as created by 'linchpin |
152 |
| - init' or 'cinchpin init' |
153 |
| - :return: The topology file to pass to the 'cinch' command |
154 |
| - """ |
155 |
| - # Attempt to open the linch-pin PinFile |
156 |
| - try: |
157 |
| - with open(os.path.join(work_dir, 'PinFile'), 'r') as f: |
158 |
| - pin_file_yaml = yaml.safe_load(f) |
159 |
| - except IOError: |
160 |
| - sys.exit('linch-pin PinFile not found in ' + work_dir) |
161 |
| - # We must find a topology section named 'cinch' to determine where our |
162 |
| - # inventory file will live |
163 |
| - try: |
164 |
| - cinch_topology = 'cinch' |
165 |
| - topology = pin_file_yaml[cinch_topology]['topology'] |
166 |
| - except KeyError: |
167 |
| - sys.exit('linch-pin PinFile must contain a topology ' |
168 |
| - 'section named "{0}"'.format(cinch_topology)) |
169 |
| - # The inventory file generated by linchpin that will be used by cinch for |
170 |
| - # configuration |
171 |
| - try: |
172 |
| - topology_path = os.path.join(work_dir, 'topologies', topology) |
173 |
| - with open(topology_path) as topology_file: |
174 |
| - topology_yaml = yaml.safe_load(topology_file) |
175 |
| - inventory_file = topology_yaml['topology_name'] + '.inventory' |
176 |
| - except (IOError, TypeError): |
177 |
| - sys.exit('linch-pin topology file not found or malformed: ' + |
178 |
| - topology_path) |
179 |
| - return inventory_file |
180 |
| - |
181 |
| - |
182 | 47 | def command_handler(command, args):
|
183 | 48 | """
|
184 | 49 | Generic function to run external programs.
|
|
0 commit comments