mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 17:33:54 +00:00
Switch aws to use deploy_script, clean up ssh_username
This commit is contained in:
parent
d7a8f1f9b2
commit
b81e064386
@ -118,7 +118,15 @@ def ssh_username(vm_):
|
||||
'''
|
||||
Return the ssh_username. Defaults to 'ec2-user'.
|
||||
'''
|
||||
return vm_.get('ssh_username', __opts__.get('AWS.ssh_username', 'ec2-user'))
|
||||
usernames = vm_.get('ssh_username', __opts__.get('AWS.ssh_username', 'ec2-user'))
|
||||
if not isinstance(usernames, list):
|
||||
username = usernames
|
||||
usernames = [username]
|
||||
if not 'ec2-user' in usernames:
|
||||
usernames.append('ec2-user')
|
||||
if not 'root' in usernames:
|
||||
usernames.append('root')
|
||||
return usernames
|
||||
|
||||
|
||||
def ssh_interface(vm_):
|
||||
@ -161,8 +169,8 @@ def create(vm_):
|
||||
location = get_location(vm_)
|
||||
print('Creating Cloud VM {0} in {1}'.format(vm_['name'], location))
|
||||
conn = get_conn(location=location)
|
||||
kwargs = {'ssh_username': ssh_username(vm_),
|
||||
'ssh_key': __opts__['AWS.private_key']}
|
||||
usernames = ssh_username(vm_)
|
||||
kwargs = {'ssh_key': __opts__['AWS.private_key']}
|
||||
kwargs['name'] = vm_['name']
|
||||
deploy_script = script(vm_)
|
||||
kwargs['image'] = get_image(conn, vm_)
|
||||
@ -184,6 +192,7 @@ def create(vm_):
|
||||
)
|
||||
sys.stderr.write(err)
|
||||
return False
|
||||
print('Created node {0}'.format(vm_['name']))
|
||||
while not data.public_ips:
|
||||
time.sleep(0.5)
|
||||
data = get_node(conn, vm_['name'])
|
||||
@ -192,40 +201,24 @@ def create(vm_):
|
||||
else:
|
||||
ip_address = data.public_ips[0]
|
||||
if saltcloud.utils.wait_for_ssh(ip_address):
|
||||
fd_, path = tempfile.mkstemp()
|
||||
os.close(fd_)
|
||||
with open(path, 'w+') as fp_:
|
||||
fp_.write(deploy_script.script)
|
||||
cmd = ('scp -oStrictHostKeyChecking=no -i {0} {3} {1}@{2}:/tmp/deploy.sh ').format(
|
||||
__opts__['AWS.private_key'],
|
||||
kwargs['ssh_username'],
|
||||
ip_address,
|
||||
path,
|
||||
)
|
||||
if subprocess.call(cmd, shell=True) != 0:
|
||||
time.sleep(15)
|
||||
cmd = ('scp -oStrictHostKeyChecking=no -i {0} {3} {1}@{2}:/tmp/deploy.sh ').format(
|
||||
__opts__['AWS.private_key'],
|
||||
'root',
|
||||
ip_address,
|
||||
path,
|
||||
)
|
||||
subprocess.call(cmd, shell=True)
|
||||
cmd = ('ssh -oStrictHostKeyChecking=no -t -i {0} {1}@{2} '
|
||||
'"sudo bash /tmp/deploy.sh"').format(
|
||||
__opts__['AWS.private_key'],
|
||||
'root',
|
||||
ip_address,
|
||||
)
|
||||
else:
|
||||
cmd = ('ssh -oStrictHostKeyChecking=no -t -i {0} {1}@{2} '
|
||||
'"sudo bash /tmp/deploy.sh"').format(
|
||||
__opts__['AWS.private_key'],
|
||||
kwargs['ssh_username'],
|
||||
ip_address,
|
||||
)
|
||||
subprocess.call(cmd, shell=True)
|
||||
os.remove(path)
|
||||
username = 'ec2-user'
|
||||
for user in usernames:
|
||||
if saltcloud.utils.wait_for_passwd(host=ip_address, username=user, timeout=60, key_filename=__opts__['AWS.private_key']):
|
||||
username = user
|
||||
break
|
||||
kwargs['ssh_username'] = username
|
||||
deployed = saltcloud.utils.deploy_script(
|
||||
host=ip_address,
|
||||
username=username,
|
||||
key_filename=__opts__['AWS.private_key'],
|
||||
deploy_command='sudo bash /tmp/deploy.sh',
|
||||
tty=True,
|
||||
script=deploy_script.script)
|
||||
if deployed:
|
||||
print('Salt installed on {0}'.format(vm_['name']))
|
||||
else:
|
||||
print('Failed to start Salt on Cloud VM {0}'.format(vm_['name']))
|
||||
|
||||
print('Created Cloud VM {0} with the following values:'.format(
|
||||
vm_['name']
|
||||
))
|
||||
|
@ -10,6 +10,7 @@ import socket
|
||||
import tempfile
|
||||
import time
|
||||
import paramiko
|
||||
import subprocess
|
||||
|
||||
# Import salt libs
|
||||
import salt.crypt
|
||||
@ -136,31 +137,48 @@ def wait_for_ssh(host, port=22, timeout=900):
|
||||
return False
|
||||
|
||||
|
||||
def wait_for_passwd(host, port=22, timeout=900, username='root', password=None):
|
||||
def wait_for_passwd(host, port=22, timeout=900, username='root',
|
||||
password=None, key_filename=None):
|
||||
'''
|
||||
Wait until ssh connection can be accessed via password
|
||||
Wait until ssh connection can be accessed via password or ssh key
|
||||
'''
|
||||
start = time.time()
|
||||
while True:
|
||||
try:
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(hostname=host, port=22, username=username, password=password, timeout=15)
|
||||
kwargs = {'hostname': host,
|
||||
'port': 22,
|
||||
'username': username,
|
||||
'timeout': 15}
|
||||
if password and not key_filename:
|
||||
kwargs['password'] = password
|
||||
if key_filename:
|
||||
kwargs['key_filename'] = key_filename
|
||||
try:
|
||||
ssh.connect(**kwargs)
|
||||
except Exception as exc:
|
||||
print('There was an in wait_for_passwd: {0}'.format(exc))
|
||||
return True
|
||||
except Exception:
|
||||
time.sleep(1)
|
||||
if time.time() - start > timeout:
|
||||
return False
|
||||
|
||||
def deploy_script(host, port=22, timeout=900, username='root', password=None, script=None):
|
||||
def deploy_script(host, port=22, timeout=900, username='root',
|
||||
password=None, key_filename=None, script=None,
|
||||
deploy_command='/tmp/deploy.sh', tty=None):
|
||||
'''
|
||||
Copy a deploy script to a remote server, execute it, and remove it
|
||||
'''
|
||||
if wait_for_ssh(host=host, port=port, timeout=timeout):
|
||||
if wait_for_passwd(host, port=port, username=username, password=password, timeout=timeout):
|
||||
if wait_for_passwd(host, port=port, username=username, password=password, key_filename=key_filename, timeout=timeout):
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(host, port=port, username=username, password=password)
|
||||
if key_filename:
|
||||
ssh.connect(host, port=port, username=username, key_filename=key_filename)
|
||||
else:
|
||||
ssh.connect(host, port=port, username=username, password=password)
|
||||
tmpfh, tmppath = tempfile.mkstemp()
|
||||
tmpfile = open(tmppath, 'w')
|
||||
tmpfile.write(script)
|
||||
@ -171,7 +189,17 @@ def deploy_script(host, port=22, timeout=900, username='root', password=None, sc
|
||||
sftp.put(tmppath, '/tmp/deploy.sh')
|
||||
os.remove(tmppath)
|
||||
ssh.exec_command('chmod +x /tmp/deploy.sh')
|
||||
ssh.exec_command('/tmp/deploy.sh')
|
||||
if tty:
|
||||
# Tried this with paramiko's invoke_shell(), and got tired of
|
||||
# fighting with it
|
||||
cmd = ('ssh -oStrictHostKeyChecking=no -t -i {0} {1}@{2} "sudo bash /tmp/deploy.sh"').format(
|
||||
key_filename,
|
||||
username,
|
||||
host
|
||||
)
|
||||
subprocess.call(cmd, shell=True)
|
||||
else:
|
||||
stdin, stdout, stderr = ssh.exec_command(deploy_command)
|
||||
ssh.exec_command('rm /tmp/deploy.sh')
|
||||
return True
|
||||
return False
|
||||
|
Loading…
Reference in New Issue
Block a user