Last active
January 6, 2016 19:10
-
-
Save goneri/60bc0c52565708090ad0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
from osp_deployer.config import Settings | |
from auto_common import Ssh | |
from auto_common import Scp | |
from jinja2 import Template | |
from novaclient import client as novaclient | |
def get_undercloud_info(settings): | |
install_admin_user = settings.director_install_account_user | |
install_admin_password = settings.director_install_account_pwd | |
hiera_output = Ssh.execute_command_tty( | |
settings.director_node.external_ip, | |
install_admin_user, | |
install_admin_password, | |
"sudo hiera admin_password") | |
password = hiera_output[0].rstrip() | |
nova = novaclient.Client(1.1, | |
auth_url='http://192.168.120.202:5000/v2.0', | |
username='admin', | |
api_key=password, | |
project_id='admin') | |
nodes = {} | |
profiles = {'controller': [], 'compute': [], 'storage': [], 'overcloud': []} | |
for server in nova.servers.list(): | |
ctlplane_ip = server.networks['ctlplane'] | |
nodes[server.name] = {'ip': ctlplane_ip[0]} | |
for profile in profiles: | |
if profile in server.name or profile is 'overcloud': | |
profiles[profile].append(server.name) | |
return nodes, profiles | |
def export_ssh_keys(settings): | |
install_admin_user = settings.director_install_account_user | |
install_admin_password = settings.director_install_account_pwd | |
if not os.path.exists(".ssh/id_rsa"): | |
subprocess.call(["ssh-keygen", "-t", "rsa", "-N", "", "-f", ".ssh/id_rsa"]) | |
Ssh.execute_command( | |
settings.director_node.external_ip, | |
install_admin_user, | |
install_admin_password, | |
"mkdir .ssh") | |
Ssh.execute_command( | |
settings.director_node.external_ip, | |
install_admin_user, | |
install_admin_password, | |
"chmod 700 .ssh") | |
Scp.put_file( | |
settings.director_node.external_ip, | |
install_admin_user, | |
install_admin_password, | |
".ssh/id_rsa.pub", | |
".ssh/authorized_keys"); | |
Ssh.execute_command( | |
settings.director_node.external_ip, | |
install_admin_user, | |
install_admin_password, | |
"chmod 600 .ssh/authorized_keys") | |
def write_host_file(settings, nodes, profiles): | |
template = Template(""" | |
localhost ansible_connection=local | |
sah ansible_ssh_host={{ settings.sah_node.external_ip }} ansible_ssh_user=stack ansible_ssh_private_key_file=~/.ssh/id_rsa | |
host0 ansible_ssh_host={{ settings.director_node.external_ip }} ansible_ssh_user=stack ansible_ssh_private_key_file=~/.ssh/id_rsa | |
undercloud ansible_ssh_host={{ settings.director_node.external_ip }} ansible_ssh_user=stack ansible_ssh_private_key_file=~/.ssh/id_rsa | |
{% for name, node in nodes.iteritems() -%} | |
name ansible_ssh_host={{ node.ip }} ansible_ssh_user=heat-admin ansible_ssh_private_key_file=~/.ssh/id_rsa | |
{% endfor -%} | |
{% for profile, nodes in profiles.iteritems() -%} | |
[{{ profile }}] | |
{% for node in nodes -%} | |
{{ node }} | |
{% endfor -%} | |
{% endfor -%} | |
[provisioned] | |
host0 | |
[undercloud] | |
undercloud | |
[local] | |
localhost | |
""") | |
fd = open("hosts", "w") | |
fd.write(template.render(settings=settings, nodes=nodes, profiles=profiles)) | |
fd.close() | |
def write_ansible_configuration_file(settings, profiles): | |
template = Template(""" | |
Host undercloud-root | |
Hostname {{ settings.director_node.external_ip }} | |
IdentityFile ~/.ssh/id_rsa | |
User root | |
StrictHostKeyChecking no | |
UserKnownHostsFile=/dev/null | |
Host undercloud | |
Hostname {{ settings.director_node.external_ip }} | |
IdentityFile ~/.ssh/id_rsa | |
User stack | |
StrictHostKeyChecking no | |
UserKnownHostsFile=/dev/null | |
""") | |
fd = open("ssh.config.ansible", "w") | |
fd.write(template.render(settings=settings, profiles=profiles)) | |
fd.close() | |
settings = Settings('/root/deploy-auto/osp_deployer/settings/goneri.ini') | |
export_ssh_keys(settings) | |
nodes, profiles = get_undercloud_info(settings) | |
write_host_file(settings, nodes, profiles) | |
write_ansible_configuration_file(settings, profiles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment